on master help sign in
Merge pull request #1139 from urbit/st/txn-gas-fix

Fix maxFeePerGas < maxPriorityFeePerGas transaction issue

commite7d5629e16a5ed406d63a347af7bc95afd3cd42f authormaster-malwyl <93844417+master-malwyl@users.noreply.github.com> dateThu 13 Feb 2025, 03:53 (2 years ago) parent8862a8194ccbdb
1 file changed+24−12
src/lib/txn.ts+24−12
src/lib/txn.ts+24−12
@@ -71,11 +71,6 @@ const estimateGasLimit = async (utx: TransactionConfig, web3: Web3) => {
7171 .divn(100); // 20% cushion
7272};
7373
74−const getMaxFeePerGas = async (web3: Web3) => {
75− const fee = await web3.eth.getGasPrice();
76− return (Number(fee) * 1.2).toFixed(0); // 20% cushion
77−};
78−
7974const signTransaction = async ({
8075 wallet,
8176 walletType,
@@ -92,24 +87,41 @@ const signTransaction = async ({
9287}: signTransactionProps) => {
9388 const from = wallet.address;
9489 const estimate = await estimateGasLimit({ ...txn, from }, web3);
95− const maxFeePerGas = await getMaxFeePerGas(web3);
90+
91+ let block =
92+ (await web3.eth.getBlock('pending')) || (await web3.eth.getBlock('latest'));
93+
94+ if (!block?.baseFeePerGas) {
95+ throw new Error('Unable to fetch baseFeePerGas from the network');
96+ }
97+
98+ const baseFeeWei = block.baseFeePerGas;
99+
100+ // from user dropdown
101+ const maxPriorityFeePerGas = toWei(
102+ String(Math.round(gasPriceData.maxPriorityFeePerGas)),
103+ 'gwei'
104+ );
105+
106+ const maxFeePerGas = Math.floor(
107+ (Number(baseFeeWei) + Number(maxPriorityFeePerGas)) * 1.2 // 20% cushion
108+ );
96109
97110 const txParams: EIP1559TxData = {
98111 data: toHex(txn.data),
99112 to: toHex(txn.to),
100113 gasLimit: toHex(estimate),
101114 maxFeePerGas: toHex(maxFeePerGas),
102− maxPriorityFeePerGas: toHex(
103− toWei(Math.round(gasPriceData.maxPriorityFeePerGas).toFixed(0), 'gwei')
104− ),
115+ maxPriorityFeePerGas: toHex(maxPriorityFeePerGas),
105116 nonce: toHex(nonce),
106117 chainId: toHex(chainId),
107118 type: toHex(EIP1559_TRANSACTION_TYPE),
108119 };
109120
110121 const chain =
111− (networkType === NETWORK_TYPES.GOERLI) ||
112− (networkType === NETWORK_TYPES.LOCAL) ? Chain.Goerli : Chain.Mainnet;
122+ networkType === NETWORK_TYPES.GOERLI || networkType === NETWORK_TYPES.LOCAL
123+ ? Chain.Goerli
124+ : Chain.Mainnet;
113125
114126 const common = new Common({
115127 chain,
@@ -118,7 +130,7 @@ const signTransaction = async ({
118130
119131 if (networkType === NETWORK_TYPES.LOCAL) {
120132 common._chainParams.chainId = 1337;
121− common._chainParams.name = "ganache";
133+ common._chainParams.name = 'ganache';
122134 }
123135
124136 const txConfig: TxOptions = {