Skip to content

Commit

Permalink
fix: base max fee
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Nov 21, 2024
1 parent abf56be commit 4e7071a
Showing 1 changed file with 67 additions and 46 deletions.
113 changes: 67 additions & 46 deletions packages/extension/src/providers/ethereum/libs/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ class Transaction {
value: this.tx.value || '0x0',
});
}
async getOPfees(): Promise<BNType> {
async getOPfees(
fTx: LegacyTransaction | FeeMarketEIP1559Transaction,
): Promise<BNType> {
const OPContract = new this.web3.Contract(
OPTIMISM_PRICE_ORACLE_ABI as any,
OPTIMISM_PRICE_ORACLE,
);
const fTx = await this.getFinalizedTransaction({
gasPriceType: GasPriceTypes.ECONOMY,
});
const serializedTx = fTx.serialize();
return OPContract.methods
.getL1Fee(bufferToHex(serializedTx))
Expand Down Expand Up @@ -87,6 +86,7 @@ class Transaction {
maxFeePerGas?: string;
gasLimit: string;
formattedFeeHistory?: FormattedFeeHistory;
finalizedTransaction: LegacyTransaction | FeeMarketEIP1559Transaction;
}> {
const latestBlock = await this.web3.getBlock('latest', false);
const { isFeeMarketNetwork, feeHistory } = await this.web3
Expand Down Expand Up @@ -123,10 +123,20 @@ class Transaction {
nonce: this.tx.nonce || (numberToHex(nonce) as `0x${string}`),
value: this.tx.value || '0x0',
};
const common = Common.custom({
chainId: BigInt(this.tx.chainId),
});
const finalizedTransaction = LegacyTransaction.fromTxData(
legacyTx as FinalizedLegacyEthereumTransaction,
{
common,
},
);
return {
transaction: legacyTx,
gasPrice: gasPrice,
gasLimit: legacyTx.gasLimit,
finalizedTransaction,
};
} else {
// Fee market transaction (post EIP1559)
Expand All @@ -141,7 +151,7 @@ class Transaction {
const gasLimit =
this.tx.gasLimit ||
(numberToHex(await this.estimateGas()) as `0x${string}`);
const maxFeePerGas = !options.totalGasPrice
let maxFeePerGas = !options.totalGasPrice
? feeMarket.maxFeePerGas
: options.totalGasPrice.div(toBN(gasLimit));
const maxPriorityFeePerGas = feeMarket.maxPriorityFeePerGas;
Expand All @@ -162,13 +172,43 @@ class Transaction {
type: '0x02',
accessList: this.tx.accessList || [],
};
const common = Common.custom({
chainId: BigInt(this.tx.chainId),
defaultHardfork: Hardfork.London,
});
let finalizedTransaction = FeeMarketEIP1559Transaction.fromTxData(
feeMarketTx as FinalizedFeeMarketEthereumTransaction,
{
common,
},
);
if (options.totalGasPrice) {
const opFee = await this.getOPfees(finalizedTransaction);
if (opFee.gtn(0)) {
const gasFeeWithoutOPFee = options.totalGasPrice.sub(opFee);
maxFeePerGas = gasFeeWithoutOPFee.div(toBN(gasLimit));
feeMarketTx.maxFeePerGas = numberToHex(maxFeePerGas) as `0x${string}`;
feeMarketTx.maxPriorityFeePerGas = numberToHex(
maxPriorityFeePerGas.gt(maxFeePerGas)
? maxFeePerGas
: maxPriorityFeePerGas,
) as `0x${string}`;
finalizedTransaction = FeeMarketEIP1559Transaction.fromTxData(
feeMarketTx as FinalizedFeeMarketEthereumTransaction,
{
common,
},
);
}
}
return {
transaction: feeMarketTx,
gasLimit: feeMarketTx.gasLimit,
baseFeePerGas: numberToHex(baseFeePerGas!),
maxFeePerGas: numberToHex(feeMarket.maxFeePerGas),
maxPriorityFeePerGas: numberToHex(feeMarket.maxPriorityFeePerGas),
formattedFeeHistory,
finalizedTransaction,
};
}
}
Expand All @@ -182,30 +222,8 @@ class Transaction {
async getFinalizedTransaction(
options: TransactionOptions,
): Promise<LegacyTransaction | FeeMarketEIP1559Transaction> {
const { transaction } = await this.finalizeTransaction(options);

if (!transaction.maxFeePerGas) {
const common = Common.custom({
chainId: BigInt(transaction.chainId),
});
return LegacyTransaction.fromTxData(
transaction as FinalizedLegacyEthereumTransaction,
{
common,
},
);
} else {
const common = Common.custom({
chainId: BigInt(transaction.chainId),
defaultHardfork: Hardfork.London,
});
return FeeMarketEIP1559Transaction.fromTxData(
transaction as FinalizedFeeMarketEthereumTransaction,
{
common,
},
);
}
const { finalizedTransaction } = await this.finalizeTransaction(options);
return finalizedTransaction;
}

async getMessageToSign(options: TransactionOptions): Promise<Uint8Array> {
Expand All @@ -214,35 +232,38 @@ class Transaction {
}

async getGasCosts(): Promise<GasCosts> {
const { gasLimit, gasPrice, baseFeePerGas, formattedFeeHistory } =
await this.finalizeTransaction({
gasPriceType: GasPriceTypes.ECONOMY,
});
const opFee = await this.getOPfees();
const {
gasLimit,
gasPrice,
baseFeePerGas,
formattedFeeHistory,
finalizedTransaction,
} = await this.finalizeTransaction({
gasPriceType: GasPriceTypes.ECONOMY,
});
if (gasPrice) {
return {
[GasPriceTypes.ECONOMY]: numberToHex(
getGasBasedOnType(gasPrice, GasPriceTypes.ECONOMY)
.mul(toBN(gasLimit))
.add(opFee),
getGasBasedOnType(gasPrice, GasPriceTypes.ECONOMY).mul(
toBN(gasLimit),
),
),
[GasPriceTypes.REGULAR]: numberToHex(
getGasBasedOnType(gasPrice, GasPriceTypes.REGULAR)
.mul(toBN(gasLimit))
.add(opFee),
getGasBasedOnType(gasPrice, GasPriceTypes.REGULAR).mul(
toBN(gasLimit),
),
),
[GasPriceTypes.FAST]: numberToHex(
getGasBasedOnType(gasPrice, GasPriceTypes.FAST)
.mul(toBN(gasLimit))
.add(opFee),
getGasBasedOnType(gasPrice, GasPriceTypes.FAST).mul(toBN(gasLimit)),
),
[GasPriceTypes.FASTEST]: numberToHex(
getGasBasedOnType(gasPrice, GasPriceTypes.FASTEST)
.mul(toBN(gasLimit))
.add(opFee),
getGasBasedOnType(gasPrice, GasPriceTypes.FASTEST).mul(
toBN(gasLimit),
),
),
};
} else {
const opFee = await this.getOPfees(finalizedTransaction);
return {
[GasPriceTypes.ECONOMY]: numberToHex(
this.getFeeMarketGasInfo(
Expand Down

0 comments on commit 4e7071a

Please sign in to comment.