Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/chain-adapters/EVM/EVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
concat,
pad,
isAddress,
SignedAuthorization,
type SignedAuthorization,
} from 'viem'
import { hashAuthorization } from 'viem/experimental'

Expand Down Expand Up @@ -69,9 +69,11 @@ export class EVM extends ChainAdapter<EVMTransactionRequest, EVMUnsignedTransact
transaction: EVMTransactionRequest
): Promise<EVMUnsignedTransaction> {
const fees = await fetchEVMFeeProperties(this.client, transaction)
const nonce = await this.client.getTransactionCount({
address: transaction.from,
})
const nonce =
transaction.nonce ??
(await this.client.getTransactionCount({
address: transaction.from,
}))

const { from, ...rest } = transaction

Expand All @@ -87,12 +89,14 @@ export class EVM extends ChainAdapter<EVMTransactionRequest, EVMUnsignedTransact
private async attachGasAndNonceLegacy(
transaction: EVMTransactionRequestLegacy
): Promise<EVMUnsignedLegacyTransaction> {
const gasPrice = await this.client.getGasPrice()
const nonce = await this.client.getTransactionCount({
address: transaction.from,
})
const gasPrice = transaction.gasPrice ?? (await this.client.getGasPrice())
const nonce =
transaction.nonce ??
(await this.client.getTransactionCount({
address: transaction.from,
}))

const { from, ...rest } = transaction;
const { from, ...rest } = transaction

return {
...rest,
Expand All @@ -105,8 +109,6 @@ export class EVM extends ChainAdapter<EVMTransactionRequest, EVMUnsignedTransact
}
}



private transformRSVSignature(signature: RSVSignature): Signature {
return {
r: `0x${signature.r}`,
Expand Down
5 changes: 4 additions & 1 deletion src/chain-adapters/EVM/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export type EVMUnsignedTransaction = TransactionRequest & {
}

export interface EVMTransactionRequest
extends Omit<EVMUnsignedTransaction, 'chainId' | 'type'> {
extends Omit<EVMUnsignedTransaction, 'chainId' | 'type' | 'nonce'> {
from: Address
nonce?: number
}

// Legacy transaction request coming from your dApp (includes 'from' address)
Expand All @@ -23,6 +24,8 @@ export interface EVMTransactionRequestLegacy {
to: `0x${string}`
value?: bigint
gas?: bigint
gasPrice?: bigint
nonce?: number
}

// Legacy unsigned transaction to be signed
Expand Down
16 changes: 12 additions & 4 deletions src/chain-adapters/EVM/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ export async function fetchEVMFeeProperties(
client: PublicClient,
transaction: TransactionRequest
): Promise<EVMFeeProperties> {
const [gas, feeData] = await Promise.all([
client.estimateGas(transaction),
client.estimateFeesPerGas(),
])
const gasPromise = transaction.gas
? Promise.resolve(transaction.gas)
: client.estimateGas(transaction)
const feeDataPromise =
transaction.maxFeePerGas && transaction.maxPriorityFeePerGas
? Promise.resolve({
maxFeePerGas: transaction.maxFeePerGas,
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas,
})
: client.estimateFeesPerGas()

const [gas, feeData] = await Promise.all([gasPromise, feeDataPromise])

const maxFeePerGas = feeData.maxFeePerGas ?? BigInt(10_000_000_000) // 10 gwei
const maxPriorityFeePerGas =
Expand Down