Skip to content

Commit e7d5629

Browse files
Merge pull request #1139 from urbit/st/txn-gas-fix
Fix maxFeePerGas < maxPriorityFeePerGas transaction issue
2 parents 8862a81 + 94ccbdb commit e7d5629

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/lib/txn.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
7974
const 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 = {

0 commit comments

Comments
 (0)