diff --git a/src/utils/transaction.ts b/src/utils/transaction.ts index 9f2fee7..0624c34 100644 --- a/src/utils/transaction.ts +++ b/src/utils/transaction.ts @@ -1,4 +1,7 @@ import { + Eip1559FeesNotSupportedError, + FeeValuesEIP1559, + FeeValuesLegacy, Hash, Hex, PublicClient, @@ -44,17 +47,13 @@ export async function populateTx( value: tx.value ?? 0n, data: tx.data ?? "0x", }; - const [estimatedGas, { maxFeePerGas, maxPriorityFeePerGas }] = - await Promise.all([ - // Only estimate gas if not provided. - tx.gas || provider.estimateGas(transactionData), - provider.estimateFeesPerGas(), - ]); + // Only estimate gas if not provided. + const estimatedGas = tx.gas || (await provider.estimateGas(transactionData)); + const gasValues = await getGasData(provider); return { ...transactionData, + ...gasValues, gas: estimatedGas, - maxFeePerGas, - maxPriorityFeePerGas, chainId, }; } @@ -71,6 +70,20 @@ export function addSignature({ return serializeTransaction(signedTx); } +export async function getGasData( + provider: PublicClient +): Promise { + try { + // EIP-1559 transaction + return await provider.estimateFeesPerGas(); + } catch (error: unknown) { + if (error instanceof Eip1559FeesNotSupportedError) { + console.warn(`${error.shortMessage} Using Legacy Gas Fees`); + } + return { gasPrice: await provider.getGasPrice() }; + } +} + /** * Relays signed transaction to Ethereum mem-pool for execution. * @param serializedTransaction - Signed Ethereum transaction. diff --git a/tests/unit/utils.transaction.test.ts b/tests/unit/utils.transaction.test.ts index 7964422..7495a79 100644 --- a/tests/unit/utils.transaction.test.ts +++ b/tests/unit/utils.transaction.test.ts @@ -1,4 +1,4 @@ -import { zeroAddress } from "viem"; +import { serializeTransaction, zeroAddress } from "viem"; import { Network, TransactionWithSignature } from "../../src"; import { buildTxPayload, @@ -54,4 +54,13 @@ describe("Transaction Builder Functions", () => { expect(tx.to).toEqual(zeroAddress); expect(tx.value).toEqual(0n); }); + + it.only("populate Non EIP-1559 Transaction", async () => { + const baseTx = { + chainId: 3776, + to: zeroAddress, + }; + const tx = await populateTx(baseTx, zeroAddress); + console.log("Build Transaction", serializeTransaction(tx)); + }); });