Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix maxFeePerGas < maxPriorityFeePerGas transaction issue #1139

Merged
merged 2 commits into from
Feb 12, 2025
Merged
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
36 changes: 24 additions & 12 deletions src/lib/txn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ const estimateGasLimit = async (utx: TransactionConfig, web3: Web3) => {
.divn(100); // 20% cushion
};

const getMaxFeePerGas = async (web3: Web3) => {
const fee = await web3.eth.getGasPrice();
return (Number(fee) * 1.2).toFixed(0); // 20% cushion
};

const signTransaction = async ({
wallet,
walletType,
Expand All @@ -92,24 +87,41 @@ const signTransaction = async ({
}: signTransactionProps) => {
const from = wallet.address;
const estimate = await estimateGasLimit({ ...txn, from }, web3);
const maxFeePerGas = await getMaxFeePerGas(web3);

let block =
(await web3.eth.getBlock('pending')) || (await web3.eth.getBlock('latest'));

if (!block?.baseFeePerGas) {
throw new Error('Unable to fetch baseFeePerGas from the network');
}

const baseFeeWei = block.baseFeePerGas;

// from user dropdown
const maxPriorityFeePerGas = toWei(
String(Math.round(gasPriceData.maxPriorityFeePerGas)),
'gwei'
);

const maxFeePerGas = Math.floor(
(Number(baseFeeWei) + Number(maxPriorityFeePerGas)) * 1.2 // 20% cushion
);

const txParams: EIP1559TxData = {
data: toHex(txn.data),
to: toHex(txn.to),
gasLimit: toHex(estimate),
maxFeePerGas: toHex(maxFeePerGas),
maxPriorityFeePerGas: toHex(
toWei(Math.round(gasPriceData.maxPriorityFeePerGas).toFixed(0), 'gwei')
),
maxPriorityFeePerGas: toHex(maxPriorityFeePerGas),
nonce: toHex(nonce),
chainId: toHex(chainId),
type: toHex(EIP1559_TRANSACTION_TYPE),
};

const chain =
(networkType === NETWORK_TYPES.GOERLI) ||
(networkType === NETWORK_TYPES.LOCAL) ? Chain.Goerli : Chain.Mainnet;
networkType === NETWORK_TYPES.GOERLI || networkType === NETWORK_TYPES.LOCAL
? Chain.Goerli
: Chain.Mainnet;

const common = new Common({
chain,
Expand All @@ -118,7 +130,7 @@ const signTransaction = async ({

if (networkType === NETWORK_TYPES.LOCAL) {
common._chainParams.chainId = 1337;
common._chainParams.name = "ganache";
common._chainParams.name = 'ganache';
}

const txConfig: TxOptions = {
Expand Down