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
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.3.1';
17export const license = 'MIT';
18
19const SEC_IN_DAY = 86400;
20
21export function setup(sdk: Context) {
22 const OP_GAS_PREDEPLOY = '0x420000000000000000000000000000000000000F';
23 const OP_GAS_ABI = [
24 {
25 inputs: [
26 {
27 internalType: 'bytes',
28 name: '_data',
29 type: 'bytes',
30 },
31 ],
32 name: 'getL1Fee',
33 outputs: [
34 {
35 internalType: 'uint256',
36 name: '',
37 type: 'uint256',
38 },
39 ],
40 stateMutability: 'view',
41 type: 'function',
42 },
43 ];
44
45 const provider = sdk.ethers.getProvider('optimism');
46 const gasPredeployContract = sdk.ethers.getContract(OP_GAS_PREDEPLOY, OP_GAS_ABI, 'optimism');
47
48 const getTransferEthCost = async () => {
49 const l2GasPrice = await provider.getGasPrice();
50 const l2GasEstimate = await provider.estimateGas({
51 from: '0xbeefbeefbeefbeefbeefbeefbeefbeefbeefbeef', // Address has enough ETH that this won't fail
52 to: '0xcafebeefcafebeefcafebeefcafebeefcafebeef',
53 value: '0x38d7ea4c68000', // 0.001 ETH
54 });
55 const l1GasCost = await gasPredeployContract.getL1Fee(
56 sdk.ethers.utils.serializeTransaction({
57 nonce: 1234,
58 value: '0x38d7ea4c68000', // 0.001 ETH
59 gasPrice: l2GasPrice,
60 gasLimit: l2GasEstimate,
61 to: '0xcafebeefcafebeefcafebeefcafebeefcafebeef',
62 data: '0x',
63 })
64 );
65 const totalGasCostWei = l2GasPrice.mul(l2GasEstimate).add(l1GasCost).toNumber();
66 const ethPrice = await sdk.coinGecko.getCurrentPrice('ethereum');
67 return (totalGasCostWei * ethPrice) / 1e18;
68 };
69
70 const getTransferTokenCost = async () => {
71 const l2GasPrice = await provider.getGasPrice();
72 const l2GasEstimate = await provider.estimateGas({
73 from: '0x11e4857bb9993a50c685a79afad4e6f65d518dda', // Random account with USDT
74 to: '0x94b008aa00579c1307b0ef2c499ad98a8ce58e58', // USDT Contract
75 data:
76 '0xa9059cbb00000000000000000000000023e7039c34c7aa47a0d4e975fbc789e0f763fa640000000000000000000000000000000000000000000000000000000000000001',
77 });
78 const l1GasCost = await gasPredeployContract.getL1Fee(
79 sdk.ethers.utils.serializeTransaction({
80 nonce: 1234,
81 value: '0x',
82 gasPrice: l2GasPrice,
83 gasLimit: l2GasEstimate,
84 to: '0x94b008aa00579c1307b0ef2c499ad98a8ce58e58',
85 data:
86 '0xa9059cbb00000000000000000000000023e7039c34c7aa47a0d4e975fbc789e0f763fa640000000000000000000000000000000000000000000000000000000000000001',
87 })
88 );
89 const totalGasCostWei = l2GasPrice.mul(l2GasEstimate).add(l1GasCost).toNumber();
90 const ethPrice = await sdk.coinGecko.getCurrentPrice('ethereum');
91 return (totalGasCostWei * ethPrice) / 1e18;
92 };
93
94 const getSwapCost = async () => {
95 const l2GasPrice = await provider.getGasPrice();
96 const l2GasEstimate = await provider.estimateGas({
97 from: '0x11e4857bb9993a50c685a79afad4e6f65d518dda', // Random account with USDT
98 to: '0xe592427a0aece92de3edee1f18e0157c05861564', // Uniswap Router
99 data:
100 '0x414bf38900000000000000000000000094b008aa00579c1307b0ef2c499ad98a8ce58e58000000000000000000000000da10009cbd5d07dd0cecc66161fc93d7c9000da100000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000c9499bd14493f6d9006d6a13aca3098c786fe6b100000000000000000000000000000000000000000000000000000000912e6519000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
101 });
102 const l1GasCost = await gasPredeployContract.getL1Fee(
103 sdk.ethers.utils.serializeTransaction({
104 nonce: 1234,
105 value: '0x',
106 gasPrice: l2GasPrice,
107 gasLimit: l2GasEstimate,
108 to: '0xe592427a0aece92de3edee1f18e0157c05861564',
109 data:
110 '0x414bf38900000000000000000000000094b008aa00579c1307b0ef2c499ad98a8ce58e58000000000000000000000000da10009cbd5d07dd0cecc66161fc93d7c9000da100000000000000000000000000000000000000000000000000000000000001f4000000000000000000000000c9499bd14493f6d9006d6a13aca3098c786fe6b100000000000000000000000000000000000000000000000000000000912e6519000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
111 })
112 );
113 const totalGasCostWei = l2GasPrice.mul(l2GasEstimate).add(l1GasCost).toNumber();
114 const ethPrice = await sdk.coinGecko.getCurrentPrice('ethereum');
115 return (totalGasCostWei * ethPrice) / 1e18;
116 };
117
118 const getAverageSwapFee = async (date: string) => {
119 const dayId = Math.floor(sdk.date.dateToTimestamp(date) / SEC_IN_DAY);
120
121 const query = `{
122 dayStat(id: "${dayId}") {
123 averageCostUSD
124 }
125 nextDay: dayStat(id: "${dayId + 1}") {
126 averageCostUSD
127 }
128 }`;
129
130 const result = await sdk.graph.query('dmihal/optimism-average-fees', query);
131
132 if (!result.dayStat) {
133 throw new Error('Data unavailable');
134 }
135 if (!result.nextDay) {
136 throw new Error('Day incomplete');
137 }
138 return parseFloat(result.dayStat.averageCostUSD);
139 };
140
141 sdk.register({
142 id: 'optimistic-ethereum',
143 queries: {
144 feeTransferEth: getTransferEthCost,
145 feeTransferERC20: getTransferTokenCost,
146 feeSwap: getSwapCost,
147 oneDayAverageFeeSwap: getAverageSwapFee,
148 },
149 metadata: {
150 icon: sdk.ipfs.getDataURILoader(
151 'QmS1mBxRRDjuVPAPkjrmrnVgzYwyfchjvRZTH11vgjqabG',
152 'image/svg+xml'
153 ),
154 category: 'l2',
155 name: 'Optimism',
156 description:
157 'Optimism is an EVM-equivalent Optimistic Rollup chain. It aims to be simple, fast, and secure.',
158 l2BeatSlug: 'optimism',
159 website: 'https://optimism.io',
160 flags: {
161 throtle:
162 'Optimism is throttled while in beta. Fees will decrease as this throttle is lifted.',
163 },
164 },
165 });
166}
167
It's something off?
Report it to the discussion board on Discord, we will take care of it.