Skip to content

Commit

Permalink
fix: gas fees
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov committed Dec 17, 2024
1 parent ee608ee commit 455f764
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 82 deletions.
2 changes: 1 addition & 1 deletion wallets/rn_cli_wallet/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ android {
applicationId "com.walletconnect.web3wallet.rnsample"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 51
versionCode 52
versionName "1.0"
resValue "string", "build_config_package", "com.walletconnect.web3wallet.rnsample"
}
Expand Down
12 changes: 4 additions & 8 deletions wallets/rn_cli_wallet/src/modals/SessionSendTransactionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {BridgeBadge} from '@/components/BridgeBadge';
import {
calculateEip155Gas,
getNonce,
getTransactionGas,
getTransferDetails,
} from '@/utils/EIP155WalletUtil';
import {isVerified} from '@/utils/HelperUtil';
import {VerifiedDomain} from '@/components/VerifiedDomain';
import {Loader} from '@/components/Loader';
import {ethers} from 'ethers';

export default function SessionSendTransactionModal() {
const {data} = useSnapshot(ModalStore.state);
Expand Down Expand Up @@ -111,7 +111,7 @@ export default function SessionSendTransactionModal() {
gasLimit: fees.gasLimit,
});
console.log('fees updated', fees);
setNetworkFee(`${fees.totalGas} ETH`);
setNetworkFee(`${ethers.utils.formatEther(fees.gasLimit)} ETH`);
setFetchingGas(false);
};

Expand Down Expand Up @@ -152,7 +152,7 @@ export default function SessionSendTransactionModal() {
const txData = {
...tx,
gasLimit: tx.gas,
...(await getTransactionGas(tx, tx.chainId)),
...(await calculateEip155Gas(tx, tx.chainId)),
};
delete txData.gas;
delete txData.gasPrice;
Expand All @@ -166,11 +166,7 @@ export default function SessionSendTransactionModal() {
);
const txData = {
...transaction,
gasLimit: routes.initialTransaction?.gas || {
hex: '0x05b6a8',
type: 'BigNumber',
},
...(await getTransactionGas(transaction, chainId)),
...(await calculateEip155Gas(transaction, chainId)),
};
delete txData.gas;
delete txData.gasPrice;
Expand Down
93 changes: 20 additions & 73 deletions wallets/rn_cli_wallet/src/utils/EIP155WalletUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,6 @@ export async function replaceMnemonic(mnemonicOrPrivateKey: string) {
export async function calculateEip155Gas(transaction: any, chainId: string) {
console.log('calculateEip155Gas:', chainId);
const chainData = PresetsUtil.getChainData(parseChainId(chainId));
console.log('chainData:');
// Define the sender (from) and receiver (to) addresses
const from = transaction.from;
const to = transaction.to; // Could be a contract address
const data = transaction.data; // Some contract interaction data

// Prepare the transaction object
const tx = {
from: from,
to: to,
data: data,
};

let provider = new ethers.providers.JsonRpcProvider(chainData.rpcUrl);

// Fetch the latest block to get the base fee
Expand All @@ -90,56 +77,26 @@ export async function calculateEip155Gas(transaction: any, chainId: string) {
// Calculate the max fee per gas (base fee + priority fee)
const maxFeePerGas = baseFee!.add(maxPriorityFeePerGas);

try {
// use this node to estimate gas as it doesn't reject when the amount is greater than the balance
// very useful for chain abstraction
provider = new ethers.providers.JsonRpcProvider(
'https://endpoints.omniatech.io/v1/arbitrum/one/public',
);

// Estimate the gas limit for this transaction based on its size and complexity
const gasLimit = await provider.estimateGas(tx);

// Log the details of the gas fees
console.log(
'Base Fee:',
ethers.utils.formatUnits(baseFee!, 'gwei'),
'Gwei',
);
console.log(
'Max Priority Fee:',
ethers.utils.formatUnits(maxPriorityFeePerGas, 'gwei'),
'Gwei',
);
console.log(
'Max Fee per Gas:',
ethers.utils.formatUnits(maxFeePerGas, 'gwei'),
'Gwei',
);
console.log('Estimated Gas Limit:', gasLimit.toString());

// The total gas cost (just as an example, no sign and send in this code)
const estimatedGasCost = gasLimit.mul(maxFeePerGas);
console.log('Estimated Gas Cost (Wei):', estimatedGasCost.toString());

return {
maxFeePerGas: ethers.utils.formatUnits(maxFeePerGas, 'wei'),
maxPriorityFeePerGas: ethers.utils.formatUnits(
maxPriorityFeePerGas,
'wei',
),
gasLimit,
totalGas: ethers.utils.formatUnits(estimatedGasCost, 'ether'),
};
} catch (error) {
console.error('Error fetching gas fees:', error);
return {
gasLimit: {hex: '0x05b6a8', type: 'BigNumber'},
maxFeePerGas: maxFeePerGas,
maxPriorityFeePerGas: '1100000',
totalGas: '0.00000020607740523',
};
}
const gasLimit = ethers.BigNumber.from(0x05b6a8);
// Log the details of the gas fees
console.log('Base Fee:', ethers.utils.formatUnits(baseFee!, 'gwei'), 'Gwei');
console.log(
'Max Priority Fee:',
ethers.utils.formatUnits(maxPriorityFeePerGas, 'gwei'),
'Gwei',
);
console.log(
'Max Fee per Gas:',
ethers.utils.formatUnits(maxFeePerGas, 'gwei'),
'Gwei',
);
console.log('Estimated Gas Limit:', gasLimit.toString());

return {
maxFeePerGas: ethers.utils.formatUnits(maxFeePerGas, 'wei'),
maxPriorityFeePerGas: ethers.utils.formatUnits(maxPriorityFeePerGas, 'wei'),
gasLimit: ethers.utils.formatUnits(gasLimit, 'wei'),
};
}

const fetchGasPrice = async (chainId: string) => {
Expand All @@ -153,16 +110,6 @@ const fetchGasPrice = async (chainId: string) => {
return data?.data;
};

export async function getTransactionGas(tx: any, chainId: string) {
const chainData = PresetsUtil.getChainData(parseChainId(chainId));
const provider = new ethers.providers.JsonRpcProvider(chainData.rpcUrl);
const feeData = await provider.getFeeData();
return {
maxFeePerGas: feeData.maxFeePerGas,
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
};
}

export async function calculateGasLimit(tx: any, chainId: string) {
const chainData = PresetsUtil.getChainData(parseChainId(chainId));
const provider = new ethers.providers.JsonRpcProvider(chainData.rpcUrl);
Expand Down

0 comments on commit 455f764

Please sign in to comment.