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: base max fee + hw wallet shaded area #565

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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> {
kvhnuke marked this conversation as resolved.
Show resolved Hide resolved
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}`;
kvhnuke marked this conversation as resolved.
Show resolved Hide resolved
feeMarketTx.maxPriorityFeePerGas = numberToHex(
maxPriorityFeePerGas.gt(maxFeePerGas)
? maxFeePerGas
: maxPriorityFeePerGas,
kvhnuke marked this conversation as resolved.
Show resolved Hide resolved
) 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
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const supportedNets = getSupportedNetworks()

&__container {
width: 100%;
height: 600px;
height: 100%;
left: 0px;
top: 0px;
position: fixed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ withDefaults(defineProps<Props>(), {

&__container {
width: 100%;
height: 600px;
height: 100%;
left: 0px;
top: 0px;
position: fixed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
</a>
</div>
<div class="swap-best-offer__wrap">
<hardware-wallet-msg
v-if="account"
:wallet-type="account.walletType"
style="width: 90%"
/>
<hardware-wallet-msg v-if="account" :wallet-type="account.walletType" />
<custom-scrollbar
ref="bestOfferScrollRef"
class="swap-best-offer__scroll-area"
Expand Down
Loading