Adapter

Optimism Transaction Fees

Optimism is an EVM-equivalent Optimistic Rollup chain. It aims to be simple, fast, and secure.

Sub-Adapters 1

Preview and test each sub adapter.

Optimism (optimistic-ethereum)

Metadata

ID
optimistic-ethereum
icon
category

"l2"

name

"Optimism"

description

"Optimism is an EVM-equivalent Optimistic Rollup chain. It aims to be simple, fast, and secure."

l2BeatSlug

"optimism"

website

"https://optimism.io"

flags

{ "throtle": "Optimism is throttled while in beta. Fees will decrease as this throttle is lifted." }

Queries

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1/*
2Fees are calculated as follows (https://community.optimism.io/docs/developers/l2/new-fees.html):
3
4Total transaction fee is a combination of an "L2 execution fee" and an "L1 security fee":
5
6total_fee = (l2_gas_price * l2_gas_used) + (l1_gas_price * l1_gas_used)
7
8Where:
9- `l2_gas_price` corresponds to the cost of execution on L2
10- `l2_gas_used` corresponds to the amount of gas used on L2
11- `l1_gas_price` corresponds to the cost of publishing the transaction data on L1
12- `l1_gas_used` corresponds to the amount of transaction data published on L1
13*/
14
15export const name = 'Optimism Transaction Fees';
16export const version = '0.2.0';
17export const license = 'MIT';
18
19export function setup(sdk: Context) {
20  const OP_GAS_PREDEPLOY = '0x420000000000000000000000000000000000000F';
21  const OP_GAS_ABI = [
22    {
23      inputs: [
24        {
25          internalType: 'bytes',
26          name: '_data',
27          type: 'bytes',
28        },
29      ],
30      name: 'getL1Fee',
31      outputs: [
32        {
33          internalType: 'uint256',
34          name: '',
35          type: 'uint256',
36        },
37      ],
38      stateMutability: 'view',
39      type: 'function',
40    },
41  ];
42
43  const provider = sdk.ethers.getProvider('optimism');
44  const gasPredeployContract = sdk.ethers.getContract(OP_GAS_PREDEPLOY, OP_GAS_ABI, 'optimism');
45
46  const getTransferEthCost = async () => {
47    const l2GasPrice = await provider.getGasPrice();
48    const l2GasEstimate = await provider.estimateGas({
49      from: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef', // Address has enough ETH that this won't fail
50      to: '0xcafebeefcafebeefcafebeefcafebeefcafebeef',
51      value: '0x38d7ea4c68000', // 0.001 ETH
52    });
53    const l1GasCost = await gasPredeployContract.getL1Fee(
54      sdk.ethers.utils.serializeTransaction({
55        nonce: 1234,
56        value: '0x38d7ea4c68000', // 0.001 ETH
57        gasPrice: l2GasPrice,
58        gasLimit: l2GasEstimate,
59        to: '0xcafebeefcafebeefcafebeefcafebeefcafebeef',
60        data: '0x',
61      })
62    );
63    const totalGasCostWei = l2GasPrice.mul(l2GasEstimate).add(l1GasCost).toNumber();
64    const ethPrice = await sdk.coinGecko.getCurrentPrice('ethereum');
65    return (totalGasCostWei * ethPrice) / 1e18;
66  };
67
68  const getTransferTokenCost = async () => {
69    const l2GasPrice = await provider.getGasPrice();
70    const l2GasEstimate = await provider.estimateGas({
71      from: '0x11e4857bb9993a50c685a79afad4e6f65d518dda', // Random account with USDT
72      to: '0x94b008aa00579c1307b0ef2c499ad98a8ce58e58', // USDT Contract
73      data:
74        '0xa9059cbb00000000000000000000000023e7039c34c7aa47a0d4e975fbc789e0f763fa640000000000000000000000000000000000000000000000000000000000000001',
75    });
76    const l1GasCost = await gasPredeployContract.getL1Fee(
77      sdk.ethers.utils.serializeTransaction({
78        nonce: 1234,
79        value: '0x',
80        gasPrice: l2GasPrice,
81        gasLimit: l2GasEstimate,
82        to: '0x94b008aa00579c1307b0ef2c499ad98a8ce58e58',
83        data:
84          '0xa9059cbb00000000000000000000000023e7039c34c7aa47a0d4e975fbc789e0f763fa640000000000000000000000000000000000000000000000000000000000000001',
85      })
86    );
87    const totalGasCostWei = l2GasPrice.mul(l2GasEstimate).add(l1GasCost).toNumber();
88    const ethPrice = await sdk.coinGecko.getCurrentPrice('ethereum');
89    return (totalGasCostWei * ethPrice) / 1e18;
90  };
91
92  const getSwapCost = async () => {
93    const l2GasPrice = await provider.getGasPrice();
94    const l2GasEstimate = await provider.estimateGas({
95      from: '0x11e4857bb9993a50c685a79afad4e6f65d518dda', // Random account with USDT
96      to: '0xe592427a0aece92de3edee1f18e0157c05861564', // Uniswap Router
97      data:
98        '0x414bf38900000000000000000000000094b008aa00579c1307b0ef2c499ad98a8ce58e58000000000000000000000000da10009cbd5d07dd0cecc66161fc93d7c9000da100000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000c9499bd14493f6d9006d6a13aca3098c786fe6b100000000000000000000000000000000000000000000000000000000912e6519000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
99    });
100    const l1GasCost = await gasPredeployContract.getL1Fee(
101      sdk.ethers.utils.serializeTransaction({
102        nonce: 1234,
103        value: '0x',
104        gasPrice: l2GasPrice,
105        gasLimit: l2GasEstimate,
106        to: '0xe592427a0aece92de3edee1f18e0157c05861564',
107        data:
108          '0x414bf38900000000000000000000000094b008aa00579c1307b0ef2c499ad98a8ce58e58000000000000000000000000da10009cbd5d07dd0cecc66161fc93d7c9000da100000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000c9499bd14493f6d9006d6a13aca3098c786fe6b100000000000000000000000000000000000000000000000000000000912e6519000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
109      })
110    );
111    const totalGasCostWei = l2GasPrice.mul(l2GasEstimate).add(l1GasCost).toNumber();
112    const ethPrice = await sdk.coinGecko.getCurrentPrice('ethereum');
113    return (totalGasCostWei * ethPrice) / 1e18;
114  };
115
116  sdk.register({
117    id: 'optimistic-ethereum',
118    queries: {
119      feeTransferEth: getTransferEthCost,
120      feeTransferERC20: getTransferTokenCost,
121      feeSwap: getSwapCost,
122    },
123    metadata: {
124      icon: sdk.ipfs.getDataURILoader(
125        'QmS1mBxRRDjuVPAPkjrmrnVgzYwyfchjvRZTH11vgjqabG',
126        'image/svg+xml'
127      ),
128      category: 'l2',
129      name: 'Optimism',
130      description:
131        'Optimism is an EVM-equivalent Optimistic Rollup chain. It aims to be simple, fast, and secure.',
132      l2BeatSlug: 'optimism',
133      website: 'https://optimism.io',
134      flags: {
135        throtle:
136          'Optimism is throttled while in beta. Fees will decrease as this throttle is lifted.',
137      },
138    },
139  });
140}
141

It's something off?

Report it to the discussion board on Discord, we will take care of it.

Adapter Info

Version

0.2.0

License

MIT

IPFS CID

QmfBGU4FKdM1pgmpMjruwQHGinvm6Eh4zEcDbs1vqSjbK6

CID (source)

QmbG4wxA4LZEoW3BA9pDiZqrLA38tvS9qmyp4fZLPqiVic

Collections

l2-fees

Author

ethresearch.eth