Compute gas price for EIP-1559 transaction (#157)

This commit is contained in:
2022-08-09 10:50:25 +05:30
committed by GitHub
parent 1a903fccc6
commit 5077abc90f
5 changed files with 24 additions and 7 deletions
+16 -2
View File
@@ -33,8 +33,10 @@ export interface Transaction {
to: string;
value: string;
gasLimit: string;
gasPrice: string;
gasPrice?: string;
input: string;
maxPriorityFeePerGas?: string,
maxFeePerGas?: string,
}
export interface Block {
@@ -53,6 +55,7 @@ export interface Block {
gasUsed: string;
author: string;
size: string;
baseFee?: string;
}
export interface EventData {
@@ -369,7 +372,18 @@ export const createEvent = async (instanceExports: any, contractAddress: string,
const gasLimitStringPtr = await __newString(tx.gasLimit);
const txGasLimitPtr = await BigInt.fromString(gasLimitStringPtr);
const gasPriceStringPtr = await __newString(tx.gasPrice);
let gasPrice = tx.gasPrice;
if (!gasPrice) {
// Compute gasPrice for EIP-1559 transaction
// https://ethereum.stackexchange.com/questions/122090/what-does-tx-gasprice-represent-after-eip-1559
const feeDifference = BigNumber.from(tx.maxFeePerGas).sub(BigNumber.from(blockData.baseFee));
const maxPriorityFeePerGas = BigNumber.from(tx.maxPriorityFeePerGas);
const priorityFeePerGas = maxPriorityFeePerGas.lt(feeDifference) ? maxPriorityFeePerGas : feeDifference;
gasPrice = BigNumber.from(blockData.baseFee).add(priorityFeePerGas).toString();
}
const gasPriceStringPtr = await __newString(gasPrice);
const txGasPricePtr = await BigInt.fromString(gasPriceStringPtr);
const inputStringPtr = await __newString(tx.input);