on master help sign in
fix maxFeePerGas < maxPriorityFeePerGas issue
35eb615Shawn Tobin
src/lib/txn.ts+24−12
@@ -71,11 +71,6 @@ const estimateGasLimit = async (utx: TransactionConfig, web3: Web3) => {
71 .divn(100); // 20% cushion
71 .divn(100); // 20% cushion
72};
72};
73
73
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−
79const signTransaction = async ({
74const signTransaction = async ({
80 wallet,
75 wallet,
81 walletType,
76 walletType,
@@ -92,24 +87,41 @@ const signTransaction = async ({
92}: signTransactionProps) => {
87}: signTransactionProps) => {
93 const from = wallet.address;
88 const from = wallet.address;
94 const estimate = await estimateGasLimit({ ...txn, from }, web3);
89 const estimate = await estimateGasLimit({ ...txn, from }, web3);
90+
91+ let block = await web3.eth.getBlock('pending');
92+
93+ // in case pending block doesn't return anything
94+ if (!block || !block.baseFeePerGas) {
95+ block = await web3.eth.getBlock('latest');
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+
95− const maxFeePerGas = await getMaxFeePerGas(web3);
106+ const maxFeePerGas = Math.floor(
107+ (Number(baseFeeWei) + Number(maxPriorityFeePerGas)) * 1.2 // 20% cushion
108+ );
96
109
97 const txParams: EIP1559TxData = {
110 const txParams: EIP1559TxData = {
98 data: toHex(txn.data),
111 data: toHex(txn.data),
99 to: toHex(txn.to),
112 to: toHex(txn.to),
100 gasLimit: toHex(estimate),
113 gasLimit: toHex(estimate),
101 maxFeePerGas: toHex(maxFeePerGas),
114 maxFeePerGas: toHex(maxFeePerGas),
102− maxPriorityFeePerGas: toHex(
115+ maxPriorityFeePerGas: toHex(maxPriorityFeePerGas),
103− toWei(Math.round(gasPriceData.maxPriorityFeePerGas).toFixed(0), 'gwei')
104− ),
105 nonce: toHex(nonce),
116 nonce: toHex(nonce),
106 chainId: toHex(chainId),
117 chainId: toHex(chainId),
107 type: toHex(EIP1559_TRANSACTION_TYPE),
118 type: toHex(EIP1559_TRANSACTION_TYPE),
108 };
119 };
109
120
110 const chain =
121 const chain =
111− (networkType === NETWORK_TYPES.GOERLI) ||
122+ networkType === NETWORK_TYPES.GOERLI || networkType === NETWORK_TYPES.LOCAL
112− (networkType === NETWORK_TYPES.LOCAL) ? Chain.Goerli : Chain.Mainnet;
123+ ? Chain.Goerli
124+ : Chain.Mainnet;
113
125
114 const common = new Common({
126 const common = new Common({
115 chain,
127 chain,
@@ -118,7 +130,7 @@ const signTransaction = async ({
118
130
119 if (networkType === NETWORK_TYPES.LOCAL) {
131 if (networkType === NETWORK_TYPES.LOCAL) {
120 common._chainParams.chainId = 1337;
132 common._chainParams.chainId = 1337;
121− common._chainParams.name = "ganache";
133+ common._chainParams.name = 'ganache';
122 }
134 }
123
135
124 const txConfig: TxOptions = {
136 const txConfig: TxOptions = {