Skip to content

Commit

Permalink
fix: base&arb testnet max send error
Browse files Browse the repository at this point in the history
  • Loading branch information
weatherstar committed May 14, 2024
1 parent 059ad0d commit b18cc99
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions packages/engine/src/vaults/impl/evm/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,17 @@ import type {
import type { IRpcTxEvm } from './types';
import type { IJsonRpcRequest } from '@onekeyfe/cross-inpage-provider-types';

const BASE_SEPOLIA = 'evm--84532';
const ARB_SEPOLIA = 'evm--421614';

const EVM_L2_NETWORKS_REQUIRE_L1_FEE: string[] = [
OnekeyNetwork.optimism,
OnekeyNetwork.toptimism,
OnekeyNetwork.base,
OnekeyNetwork.arbitrum,
OnekeyNetwork.tarbitrum,
BASE_SEPOLIA,
ARB_SEPOLIA,
];

const ERC721 = ERC721MetadataArtifact.abi;
Expand Down Expand Up @@ -1273,37 +1280,49 @@ export default class Vault extends VaultBase {
// For L2 networks with L1 fee.
let baseFeeValue = '0';
if (EVM_L2_NETWORKS_REQUIRE_L1_FEE.includes(this.networkId)) {
// Optimism & Optimism Kovan
// call gasL1Fee(bytes) of GasPriceOracle at 0x420000000000000000000000000000000000000F
const txData = ethers.utils.serializeTransaction({
value: encodedTx.value,
data: encodedTx.data,
gasLimit: `0x${(unsignedTx?.feeLimit ?? new BigNumber('0')).toString(
16,
)}`,
to: encodedTx.to,
chainId: 10, // any number other than 0 will lead to fixed length of data
gasPrice: '0xf4240', // 0.001 Gwei
nonce: 1,
});

// keccak256(Buffer.from('getL1Fee(bytes)')) => '0x49948e0e...'
const data = `0x49948e0e${defaultAbiCoder
.encode(['bytes'], [txData])
.slice(2)}`;
const client = await this.getJsonRPCClient();
if (
this.networkId === OnekeyNetwork.arbitrum ||
this.networkId === ARB_SEPOLIA
) {
// keccak256(Buffer.from('getL1BaseFeeEstimate()')) => '0xf5d6ded7...'
const data = '0xf5d6ded7';
const l1FeeHex = await client.rpc.call('eth_call', [
{ to: '0x000000000000000000000000000000000000006C', data },
'latest',
]);

baseFeeValue = new BigNumber(l1FeeHex as string)
.shiftedBy(-network.feeDecimals)
.toFixed();
} else {
const txData = ethers.utils.serializeTransaction({
value: encodedTx.value,
data: encodedTx.data,
gasLimit: `0x${(unsignedTx?.feeLimit ?? new BigNumber('0')).toString(
16,
)}`,
to: encodedTx.to,
chainId: 10, // any number other than 0 will lead to fixed length of data
gasPrice: '0xf4240', // 0.001 Gwei
nonce: 1,
});

// RPC: eth_call
const l1FeeHex = await client.rpc.call('eth_call', [
{ to: '0x420000000000000000000000000000000000000F', data },
'latest',
]);
// RPC: eth_getBlockByNumber (rpc status check?)
// RPC: eth_getBalance useManageTokensOfAccount/useReloadAccountBalance
// may call multiple times
baseFeeValue = new BigNumber(l1FeeHex as string)
.shiftedBy(-network.feeDecimals)
.toFixed();
// keccak256(Buffer.from('getL1Fee(bytes)')) => '0x49948e0e...'
const data = `0x49948e0e${defaultAbiCoder
.encode(['bytes'], [txData])
.slice(2)}`;

// RPC: eth_call
const l1FeeHex = await client.rpc.call('eth_call', [
{ to: '0x420000000000000000000000000000000000000F', data },
'latest',
]);

baseFeeValue = new BigNumber(l1FeeHex as string)
.shiftedBy(-network.feeDecimals)
.toFixed();
}
}

const eip1559 = Boolean(
Expand Down

0 comments on commit b18cc99

Please sign in to comment.