viem 2.40.3 → 2.40.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/_cjs/actions/wallet/prepareTransactionRequest.js +15 -8
  3. package/_cjs/actions/wallet/prepareTransactionRequest.js.map +1 -1
  4. package/_cjs/chains/definitions/reactiveTestnet.js +24 -0
  5. package/_cjs/chains/definitions/reactiveTestnet.js.map +1 -0
  6. package/_cjs/chains/index.js +7 -5
  7. package/_cjs/chains/index.js.map +1 -1
  8. package/_cjs/errors/version.js +1 -1
  9. package/_cjs/op-stack/abis.js +17 -1
  10. package/_cjs/op-stack/abis.js.map +1 -1
  11. package/_cjs/op-stack/actions/estimateOperatorFee.js +42 -0
  12. package/_cjs/op-stack/actions/estimateOperatorFee.js.map +1 -0
  13. package/_cjs/op-stack/actions/estimateTotalFee.js +4 -2
  14. package/_cjs/op-stack/actions/estimateTotalFee.js.map +1 -1
  15. package/_cjs/op-stack/decorators/publicL2.js +2 -0
  16. package/_cjs/op-stack/decorators/publicL2.js.map +1 -1
  17. package/_cjs/op-stack/index.js +3 -1
  18. package/_cjs/op-stack/index.js.map +1 -1
  19. package/_esm/actions/wallet/prepareTransactionRequest.js +15 -8
  20. package/_esm/actions/wallet/prepareTransactionRequest.js.map +1 -1
  21. package/_esm/chains/definitions/reactiveTestnet.js +21 -0
  22. package/_esm/chains/definitions/reactiveTestnet.js.map +1 -0
  23. package/_esm/chains/index.js +1 -0
  24. package/_esm/chains/index.js.map +1 -1
  25. package/_esm/errors/version.js +1 -1
  26. package/_esm/op-stack/abis.js +20 -0
  27. package/_esm/op-stack/abis.js.map +1 -1
  28. package/_esm/op-stack/actions/estimateOperatorFee.js +73 -0
  29. package/_esm/op-stack/actions/estimateOperatorFee.js.map +1 -0
  30. package/_esm/op-stack/actions/estimateTotalFee.js +5 -3
  31. package/_esm/op-stack/actions/estimateTotalFee.js.map +1 -1
  32. package/_esm/op-stack/decorators/publicL2.js +2 -0
  33. package/_esm/op-stack/decorators/publicL2.js.map +1 -1
  34. package/_esm/op-stack/index.js +1 -0
  35. package/_esm/op-stack/index.js.map +1 -1
  36. package/_types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -1
  37. package/_types/chains/definitions/reactiveTestnet.d.ts +39 -0
  38. package/_types/chains/definitions/reactiveTestnet.d.ts.map +1 -0
  39. package/_types/chains/index.d.ts +1 -0
  40. package/_types/chains/index.d.ts.map +1 -1
  41. package/_types/errors/version.d.ts +1 -1
  42. package/_types/op-stack/abis.d.ts +25 -0
  43. package/_types/op-stack/abis.d.ts.map +1 -1
  44. package/_types/op-stack/actions/estimateOperatorFee.d.ts +45 -0
  45. package/_types/op-stack/actions/estimateOperatorFee.d.ts.map +1 -0
  46. package/_types/op-stack/actions/estimateTotalFee.d.ts +4 -3
  47. package/_types/op-stack/actions/estimateTotalFee.d.ts.map +1 -1
  48. package/_types/op-stack/decorators/publicL2.d.ts +27 -0
  49. package/_types/op-stack/decorators/publicL2.d.ts.map +1 -1
  50. package/_types/op-stack/index.d.ts +1 -0
  51. package/_types/op-stack/index.d.ts.map +1 -1
  52. package/actions/wallet/prepareTransactionRequest.ts +15 -7
  53. package/chains/definitions/reactiveTestnet.ts +21 -0
  54. package/chains/index.ts +1 -0
  55. package/errors/version.ts +1 -1
  56. package/op-stack/abis.ts +21 -0
  57. package/op-stack/actions/estimateOperatorFee.ts +119 -0
  58. package/op-stack/actions/estimateTotalFee.ts +12 -4
  59. package/op-stack/decorators/publicL2.ts +34 -0
  60. package/op-stack/index.ts +6 -0
  61. package/package.json +1 -1
@@ -0,0 +1,21 @@
1
+ import { defineChain } from '../../utils/chain/defineChain.js'
2
+
3
+ export const reactiveTestnet = /*#__PURE__*/ defineChain({
4
+ id: 5_318_007,
5
+ name: 'Reactive Lasna Testnet',
6
+ nativeCurrency: {
7
+ decimals: 18,
8
+ name: 'Lasna React',
9
+ symbol: 'lREACT',
10
+ },
11
+ rpcUrls: {
12
+ default: { http: ['https://lasna-rpc.rnk.dev'] },
13
+ },
14
+ blockExplorers: {
15
+ default: {
16
+ name: 'Reactscan',
17
+ url: 'https://lasna.reactscan.net',
18
+ },
19
+ },
20
+ testnet: true,
21
+ })
package/chains/index.ts CHANGED
@@ -479,6 +479,7 @@ export { qMainnet } from './definitions/qMainnet.js'
479
479
  export { qTestnet } from './definitions/qTestnet.js'
480
480
  export { quai } from './definitions/quai.js'
481
481
  export { quaiTestnet } from './definitions/quaiTestnet.js'
482
+ export { reactiveTestnet } from './definitions/reactiveTestnet.js'
482
483
  export { real } from './definitions/real.js'
483
484
  export { redbellyMainnet } from './definitions/redbellyMainnet.js'
484
485
  export { redbellyTestnet } from './definitions/redbellyTestnet.js'
package/errors/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '2.40.3'
1
+ export const version = '2.40.4'
package/op-stack/abis.ts CHANGED
@@ -76,6 +76,27 @@ export const gasPriceOracleAbi = [
76
76
  },
77
77
  ] as const
78
78
 
79
+ /**
80
+ * ABI for the OP Stack [`L1Block` contract](https://github.com/ethereum-optimism/optimism/blob/develop/packages/contracts-bedrock/src/L2/L1Block.sol).
81
+ * @see https://optimistic.etherscan.io/address/0x4200000000000000000000000000000000000015
82
+ */
83
+ export const l1BlockAbi = [
84
+ {
85
+ inputs: [],
86
+ name: 'operatorFeeScalar',
87
+ outputs: [{ internalType: 'uint32', name: '', type: 'uint32' }],
88
+ stateMutability: 'view',
89
+ type: 'function',
90
+ },
91
+ {
92
+ inputs: [],
93
+ name: 'operatorFeeConstant',
94
+ outputs: [{ internalType: 'uint64', name: '', type: 'uint64' }],
95
+ stateMutability: 'view',
96
+ type: 'function',
97
+ },
98
+ ] as const
99
+
79
100
  export const l2OutputOracleAbi = [
80
101
  {
81
102
  inputs: [
@@ -0,0 +1,119 @@
1
+ import type { Address } from 'abitype'
2
+
3
+ import {
4
+ type EstimateGasErrorType,
5
+ type EstimateGasParameters,
6
+ estimateGas,
7
+ } from '../../actions/public/estimateGas.js'
8
+ import {
9
+ type ReadContractErrorType,
10
+ readContract,
11
+ } from '../../actions/public/readContract.js'
12
+ import type { Client } from '../../clients/createClient.js'
13
+ import type { Transport } from '../../clients/transports/createTransport.js'
14
+ import type { ErrorType } from '../../errors/utils.js'
15
+ import type { Account, GetAccountParameter } from '../../types/account.js'
16
+ import type { Chain, GetChainParameter } from '../../types/chain.js'
17
+ import type { TransactionRequestEIP1559 } from '../../types/transaction.js'
18
+ import type { RequestErrorType } from '../../utils/buildRequest.js'
19
+ import { getChainContractAddress } from '../../utils/chain/getChainContractAddress.js'
20
+ import type { HexToNumberErrorType } from '../../utils/encoding/fromHex.js'
21
+ import { l1BlockAbi } from '../abis.js'
22
+ import { contracts } from '../contracts.js'
23
+
24
+ export type EstimateOperatorFeeParameters<
25
+ TChain extends Chain | undefined = Chain | undefined,
26
+ TAccount extends Account | undefined = Account | undefined,
27
+ TChainOverride extends Chain | undefined = undefined,
28
+ > = Omit<TransactionRequestEIP1559, 'from'> &
29
+ GetAccountParameter<TAccount> &
30
+ GetChainParameter<TChain, TChainOverride> & {
31
+ /** L1 block attributes contract address. */
32
+ l1BlockAddress?: Address | undefined
33
+ }
34
+
35
+ export type EstimateOperatorFeeReturnType = bigint
36
+
37
+ export type EstimateOperatorFeeErrorType =
38
+ | RequestErrorType
39
+ | EstimateGasErrorType
40
+ | HexToNumberErrorType
41
+ | ReadContractErrorType
42
+ | ErrorType
43
+
44
+ /**
45
+ * Estimates the operator fee required to execute an L2 transaction.
46
+ *
47
+ * Operator fees are part of the Isthmus upgrade and allow OP Stack operators
48
+ * to recover costs related to Alt-DA, ZK proving, or custom gas tokens.
49
+ * Returns 0 for pre-Isthmus chains or when operator fee functions don't exist.
50
+ *
51
+ * @param client - Client to use
52
+ * @param parameters - {@link EstimateOperatorFeeParameters}
53
+ * @returns The operator fee (in wei). {@link EstimateOperatorFeeReturnType}
54
+ *
55
+ * @example
56
+ * import { createPublicClient, http, parseEther } from 'viem'
57
+ * import { optimism } from 'viem/chains'
58
+ * import { estimateOperatorFee } from 'viem/chains/optimism'
59
+ *
60
+ * const client = createPublicClient({
61
+ * chain: optimism,
62
+ * transport: http(),
63
+ * })
64
+ * const operatorFee = await estimateOperatorFee(client, {
65
+ * account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
66
+ * to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
67
+ * value: parseEther('1'),
68
+ * })
69
+ */
70
+ export async function estimateOperatorFee<
71
+ TChain extends Chain | undefined,
72
+ TAccount extends Account | undefined,
73
+ TChainOverride extends Chain | undefined = undefined,
74
+ >(
75
+ client: Client<Transport, TChain, TAccount>,
76
+ args: EstimateOperatorFeeParameters<TChain, TAccount, TChainOverride>,
77
+ ): Promise<EstimateOperatorFeeReturnType> {
78
+ const { chain = client.chain, l1BlockAddress: l1BlockAddress_ } = args
79
+
80
+ const l1BlockAddress = (() => {
81
+ if (l1BlockAddress_) return l1BlockAddress_
82
+ if (chain?.contracts?.l1Block)
83
+ return getChainContractAddress({
84
+ chain,
85
+ contract: 'l1Block',
86
+ })
87
+ return contracts.l1Block.address
88
+ })()
89
+
90
+ // Try to get operator fee parameters. If any of these calls fail,
91
+ // it means this is a pre-Isthmus chain and operator fees don't apply
92
+ try {
93
+ // Get operator fee parameters first to fail fast if not supported
94
+ const [operatorFeeScalar, operatorFeeConstant] = await Promise.all([
95
+ readContract(client, {
96
+ abi: l1BlockAbi,
97
+ address: l1BlockAddress,
98
+ functionName: 'operatorFeeScalar',
99
+ }),
100
+ readContract(client, {
101
+ abi: l1BlockAbi,
102
+ address: l1BlockAddress,
103
+ functionName: 'operatorFeeConstant',
104
+ }),
105
+ ])
106
+
107
+ // Estimate gas for the actual transaction
108
+ const gasUsed = await estimateGas(client, args as EstimateGasParameters)
109
+
110
+ // Calculate operator fee: saturatingAdd(saturatingMul(gasUsed, scalar) / 1e6, constant)
111
+ // Using saturating arithmetic to prevent overflow
112
+ const scaledFee = (gasUsed * BigInt(operatorFeeScalar)) / 1_000_000n
113
+ return scaledFee + BigInt(operatorFeeConstant)
114
+ } catch {
115
+ // If any call fails, this is likely a pre-Isthmus chain or the contract
116
+ // doesn't support these functions. Return 0 for operator fee.
117
+ return 0n
118
+ }
119
+ }
@@ -23,12 +23,18 @@ import {
23
23
  type EstimateL1FeeParameters,
24
24
  estimateL1Fee,
25
25
  } from './estimateL1Fee.js'
26
+ import {
27
+ type EstimateOperatorFeeErrorType,
28
+ type EstimateOperatorFeeParameters,
29
+ estimateOperatorFee,
30
+ } from './estimateOperatorFee.js'
26
31
 
27
32
  export type EstimateTotalFeeParameters<
28
33
  chain extends Chain | undefined = Chain | undefined,
29
34
  account extends Account | undefined = Account | undefined,
30
35
  chainOverride extends Chain | undefined = Chain | undefined,
31
- > = EstimateL1FeeParameters<chain, account, chainOverride>
36
+ > = EstimateL1FeeParameters<chain, account, chainOverride> &
37
+ EstimateOperatorFeeParameters<chain, account, chainOverride>
32
38
 
33
39
  export type EstimateTotalFeeReturnType = bigint
34
40
 
@@ -36,12 +42,13 @@ export type EstimateTotalFeeErrorType =
36
42
  | RequestErrorType
37
43
  | PrepareTransactionRequestErrorType
38
44
  | EstimateL1FeeErrorType
45
+ | EstimateOperatorFeeErrorType
39
46
  | EstimateGasErrorType
40
47
  | GetGasPriceErrorType
41
48
  | ErrorType
42
49
 
43
50
  /**
44
- * Estimates the L1 data fee + L2 fee to execute an L2 transaction.
51
+ * Estimates the L1 data fee + L2 fee + operator fee to execute an L2 transaction.
45
52
  *
46
53
  * @param client - Client to use
47
54
  * @param parameters - {@link EstimateTotalFeeParameters}
@@ -76,11 +83,12 @@ export async function estimateTotalFee<
76
83
  args as PrepareTransactionRequestParameters,
77
84
  )
78
85
 
79
- const [l1Fee, l2Gas, l2GasPrice] = await Promise.all([
86
+ const [l1Fee, operatorFee, l2Gas, l2GasPrice] = await Promise.all([
80
87
  estimateL1Fee(client, request as EstimateL1FeeParameters),
88
+ estimateOperatorFee(client, request as EstimateOperatorFeeParameters),
81
89
  estimateGas(client, request as EstimateGasParameters),
82
90
  getGasPrice(client),
83
91
  ])
84
92
 
85
- return l1Fee + l2Gas * l2GasPrice
93
+ return l1Fee + operatorFee + l2Gas * l2GasPrice
86
94
  }
@@ -52,6 +52,11 @@ import {
52
52
  type EstimateL1GasReturnType,
53
53
  estimateL1Gas,
54
54
  } from '../actions/estimateL1Gas.js'
55
+ import {
56
+ type EstimateOperatorFeeParameters,
57
+ type EstimateOperatorFeeReturnType,
58
+ estimateOperatorFee,
59
+ } from '../actions/estimateOperatorFee.js'
55
60
  import {
56
61
  type EstimateTotalFeeParameters,
57
62
  type EstimateTotalFeeReturnType,
@@ -418,6 +423,34 @@ export type PublicActionsL2<
418
423
  estimateL1Gas: <chainOverride extends Chain | undefined = undefined>(
419
424
  parameters: EstimateL1GasParameters<chain, account, chainOverride>,
420
425
  ) => Promise<EstimateL1GasReturnType>
426
+ /**
427
+ * Estimates the operator fee required to execute an L2 transaction.
428
+ *
429
+ * Returns 0 for pre-Isthmus chains or when operator fee functions don't exist.
430
+ *
431
+ * @param client - Client to use
432
+ * @param parameters - {@link EstimateOperatorFeeParameters}
433
+ * @returns The operator fee (in wei). {@link EstimateOperatorFeeReturnType}
434
+ *
435
+ * @example
436
+ * import { createPublicClient, http, parseEther } from 'viem'
437
+ * import { optimism } from 'viem/chains'
438
+ * import { publicActionsL2 } from 'viem/op-stack'
439
+ *
440
+ * const client = createPublicClient({
441
+ * chain: optimism,
442
+ * transport: http(),
443
+ * }).extend(publicActionsL2())
444
+ *
445
+ * const operatorFee = await client.estimateOperatorFee({
446
+ * account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
447
+ * to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
448
+ * value: parseEther('1'),
449
+ * })
450
+ */
451
+ estimateOperatorFee: <chainOverride extends Chain | undefined = undefined>(
452
+ parameters: EstimateOperatorFeeParameters<chain, account, chainOverride>,
453
+ ) => Promise<EstimateOperatorFeeReturnType>
421
454
  /**
422
455
  * Estimates the L1 data fee + L2 fee to execute an L2 transaction.
423
456
  *
@@ -509,6 +542,7 @@ export function publicActionsL2() {
509
542
  estimateL1Fee: (args) => estimateL1Fee(client, args),
510
543
  getL1BaseFee: (args) => getL1BaseFee(client, args),
511
544
  estimateL1Gas: (args) => estimateL1Gas(client, args),
545
+ estimateOperatorFee: (args) => estimateOperatorFee(client, args),
512
546
  estimateTotalFee: (args) => estimateTotalFee(client, args),
513
547
  estimateTotalGas: (args) => estimateTotalGas(client, args),
514
548
  }
package/op-stack/index.ts CHANGED
@@ -59,6 +59,12 @@ export {
59
59
  type EstimateL1GasReturnType,
60
60
  estimateL1Gas,
61
61
  } from './actions/estimateL1Gas.js'
62
+ export {
63
+ type EstimateOperatorFeeErrorType,
64
+ type EstimateOperatorFeeParameters,
65
+ type EstimateOperatorFeeReturnType,
66
+ estimateOperatorFee,
67
+ } from './actions/estimateOperatorFee.js'
62
68
  export {
63
69
  type EstimateTotalFeeErrorType,
64
70
  type EstimateTotalFeeParameters,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "viem",
3
3
  "description": "TypeScript Interface for Ethereum",
4
- "version": "2.40.3",
4
+ "version": "2.40.4",
5
5
  "main": "./_cjs/index.js",
6
6
  "module": "./_esm/index.js",
7
7
  "types": "./_types/index.d.ts",