@@ -109,35 +109,22 @@ Let's set those up now.
109
109
110
110
{ <h3 >Import Viem and other necessary modules</h3 >}
111
111
112
- ``` js
113
-
114
- const { createPublicClient , createWalletClient , http , parseEther , parseGwei , formatEther } = require (' viem' );
115
- const { privateKeyToAccount } = require (' viem/accounts' );
116
- const { optimismSepolia } = require (' viem/chains' );
117
- const { publicActionsL2 , walletActionsL2 } = require (' viem/op-stack' );
112
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L3-L6 hash=32ecaac58846bfe7e785e2cc35562120
118
113
```
119
114
120
115
{ <h3 >Set up the account</h3 >}
121
116
122
- ``` js
123
- const account = privateKeyToAccount (process .env .TUTORIAL_PRIVATE_KEY );
117
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L8-L9 hash=165490e75b825c786a937fba7b8e159d
124
118
```
119
+
125
120
{ <h3 >Create the public client</h3 >}
126
121
127
- ``` js
128
- const publicClientL2 = createPublicClient ({
129
- chain: optimismSepolia,
130
- transport: http (" https://sepolia.optimism.io" ),
131
- }).extend (publicActionsL2 ());
122
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L11-L14 hash=42293ff382c932f806beb7252803a848
132
123
```
133
124
134
125
{ <h3 >Create the wallet client</h3 >}
135
126
136
- ``` js
137
- const walletClientL2 = createWalletClient ({
138
- chain: optimismSepolia,
139
- transport: http (" https://sepolia.optimism.io" ),
140
- }).extend (walletActionsL2 ());
127
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L16-L19 hash=e7b6423850765242512e71589382791b
141
128
```
142
129
</Steps >
143
130
@@ -151,43 +138,31 @@ You're now going to estimate the cost of a transaction on OP Mainnet.
151
138
Viem makes it easy to prepare a transactions so you can estimate the cost of a transaction before you sign it.
152
139
Here you'll define an object with the required transaction fields and send a small amount of ETH from your address to the address ` 0x1000000000000000000000000000000000000000 ` .
153
140
154
- ``` js
155
- const transaction = {
156
- account,
157
- to: ' 0x1000000000000000000000000000000000000000' ,
158
- value: parseEther (' 0.005' ),
159
- gasPrice: parseGwei (' 20' )
160
- };
161
- ```
141
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L21-L26 hash=583ee48142f33686188a7bf1cc312a1c
142
+ ```
162
143
This transaction will send ` 0.005 ` ETH to the specified address with a gas price of 20 Gwei.
163
144
164
145
{ <h3 >Estimate the execution gas fee</h3 >}
165
146
166
147
Now, let's estimate the gas limit for our transaction.
167
148
168
- ``` js
169
- const gasLimit = await publicClientL2 .estimateGas (transaction);
170
- console .log (` Estimated Gas Limit: ${ gasLimit} ` );
171
- ```
149
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L28-L29 hash=014939607bf2444d5f2e5a7babb02a46
150
+ ```
172
151
173
152
<Steps >
174
153
{ <h3 >Retrieve the current gas price</h3 >}
175
154
176
155
Retrieve the current gas price (effective gas price), Alternatively, given that you already set the gas price manually, you can use the ` getGasPrice ` method from viem.
177
156
178
- ``` js
179
- const effectiveGasPrice = await publicClientL2 .getGasPrice ();
180
- console .log (` effective Gas Price, ${ effectiveGasPrice} ` );
181
- ```
157
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L31-L32 hash=b43ae2cb1a7471de55858fbc7f7fe5a6
158
+ ```
182
159
183
160
{ <h3 >Calculate the execution gas fee</h3 >}
184
161
185
162
To calculate the execution gas fee simply multiply the gas limit by the effective gas price.
186
163
187
- ``` js
188
- const l2CostEstimate = gasLimit * effectiveGasPrice;
189
- console .log (` Estimated Execution Gas Fee: ${ formatEther (l2CostEstimate)} ` );
190
- ```
164
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L34-L35 hash=3e536e97b83458d3d3c7545b2c74ffd6
165
+ ```
191
166
</Steps >
192
167
193
168
{ <h3 >Estimate the L1 data fee</h3 >}
@@ -196,73 +171,55 @@ You're now going to estimate the cost of a transaction on OP Mainnet.
196
171
Under the hood, this function is estimating the amount of Ethereum gas required to publish this transaction on Ethereum and multiplying it by the current Ethereum gas price (as tracked by the L2).
197
172
This function returns the current cost estimate in wei.
198
173
199
- ``` js
200
- const l1CostEstimate = await publicClientL2 .estimateL1Fee (transaction)
201
- console .log (` Estimated L1 data Fee: ${ formatEther (l1CostEstimate)} ` );
202
- ```
174
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L37-L38 hash=b1ef73988e0876529a72b61d5822cbe1
175
+ ```
203
176
204
177
{ <h3 >Estimate the total cost</h3 >}
205
178
206
179
Once you've individually estimated the execution gas fee and the L1 data fee, you can sum these two values together to get the total cost of the transaction.
207
180
208
- ``` js
209
- const totalEstimate = l2CostEstimate + l1CostEstimate;
210
- console .log (` Estimated Total Cost: ${ formatEther (totalEstimate)} ` );
211
- ```
181
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L40-L41 hash=8685f586913b87a5881cc6f917d0de50
182
+ ```
212
183
213
184
{ <h3 >Send the transaction</h3 >}
214
185
215
186
Now that you've estimated the total cost of the transaction, go ahead and send it to the network.
216
187
This will make it possible to see the actual cost of the transaction to compare to your estimate.
217
188
218
- ``` js
219
- const txHash = await walletClientL2 .sendTransaction (transaction)
220
- console .log (` Transaction Hash: ${ txHash} ` );
221
- ```
189
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L43-L44 hash=a0a19fa7fc17165d934cc9f7af075464
190
+ ```
222
191
223
192
{ <h3 >Check the actual execution gas fee</h3 >}
224
193
225
194
Once you get back the transaction receipt, check the actual execution gas fee.
226
195
You can do so by accessing the ` gasUsed ` and ` effectiveGasPrice ` from the transaction receipt.
227
196
You can then multiply these values to get the actual L2 cost of the transaction
228
197
229
- ``` js
230
- const receipt = await publicClientL2 .getTransactionReceipt ({ hash: txHash });
231
- console .log (' Transaction receipt:' , receipt);
232
-
233
- const l2CostActual = receipt .gasUsed * receipt .effectiveGasPrice ;
234
- console .log (` L2 Actual Cost: ${ formatEther (l2CostActual)} ` );
235
- ```
198
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L46-L49 hash=e128f21d2c994f42bdc7f5fb51eb127d
199
+ ```
236
200
237
201
{ <h3 >Check the actual L1 data fee</h3 >}
238
202
239
203
You can also check the actual L1 data fee.
240
204
241
- ``` js
242
- const l1CostActual = await publicClientL2 .estimateL1Fee (txHash)
243
- console .log (` l1CostActual gas fee: ${ formatEther (l1CostActual)} ` );
244
-
245
- ```
205
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L51-L52 hash=ea1801a7618fa2a21f02a97717380f42
206
+ ```
246
207
247
208
{ <h3 >Check the actual total cost</h3 >}
248
209
249
210
Sum these two together to get the actual total cost of the transaction.
250
211
251
- ``` js
252
- const totalActual = l2CostActual + l1CostActual;
253
- console .log (` Total Actual Cost: ${ formatEther (totalActual)} ` );
254
- ```
212
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L54-L56 hash=6f7943da9d70654dc040b5561b3b360d
213
+ ```
255
214
256
215
{ <h3 >Check the difference</h3 >}
257
216
258
217
Finally, check the difference between the estimated total cost and the actual total cost.
259
218
This will give you a sense of how accurate your estimate was.
260
219
Estimates will never be entirely accurate, but they should be close!
261
220
262
- ``` js
263
- const difference = totalEstimate >= totalActual ? totalEstimate - totalActual : totalActual - totalEstimate
264
- console .log (` Estimation Difference: ${ formatEther (difference)} ETH` )
265
- ```
221
+ ``` js file=<rootDir>/public/tutorials/sdk-estimate-costs.js#L57-L58 hash=20c8c60af1cc39e842b207cfd2dfa2cb
222
+ ```
266
223
</Steps >
267
224
268
225
<Callout type = " info" >
0 commit comments