viem 1.6.7 → 1.8.0

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 (218) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/actions/index.js +8 -2
  3. package/dist/cjs/actions/index.js.map +1 -1
  4. package/dist/cjs/actions/public/estimateFeesPerGas.js +60 -0
  5. package/dist/cjs/actions/public/estimateFeesPerGas.js.map +1 -0
  6. package/dist/cjs/actions/public/estimateGas.js +2 -2
  7. package/dist/cjs/actions/public/estimateGas.js.map +1 -1
  8. package/dist/cjs/actions/public/estimateMaxPriorityFeePerGas.js +44 -0
  9. package/dist/cjs/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -0
  10. package/dist/cjs/actions/public/verifyHash.js.map +1 -1
  11. package/dist/cjs/actions/public/verifyMessage.js.map +1 -1
  12. package/dist/cjs/actions/public/verifyTypedData.js.map +1 -1
  13. package/dist/cjs/actions/public/watchContractEvent.js +10 -7
  14. package/dist/cjs/actions/public/watchContractEvent.js.map +1 -1
  15. package/dist/cjs/actions/wallet/deployContract.js.map +1 -1
  16. package/dist/cjs/actions/wallet/prepareTransactionRequest.js +70 -0
  17. package/dist/cjs/actions/wallet/prepareTransactionRequest.js.map +1 -0
  18. package/dist/cjs/actions/wallet/sendRawTransaction.js +11 -0
  19. package/dist/cjs/actions/wallet/sendRawTransaction.js.map +1 -0
  20. package/dist/cjs/actions/wallet/sendTransaction.js +6 -6
  21. package/dist/cjs/actions/wallet/sendTransaction.js.map +1 -1
  22. package/dist/cjs/actions/wallet/signTransaction.js +47 -0
  23. package/dist/cjs/actions/wallet/signTransaction.js.map +1 -0
  24. package/dist/cjs/actions/wallet/writeContract.js.map +1 -1
  25. package/dist/cjs/chains/celo/formatters.js +4 -1
  26. package/dist/cjs/chains/celo/formatters.js.map +1 -1
  27. package/dist/cjs/clients/createTestClient.js.map +1 -1
  28. package/dist/cjs/clients/decorators/public.js +8 -0
  29. package/dist/cjs/clients/decorators/public.js.map +1 -1
  30. package/dist/cjs/clients/decorators/wallet.js +6 -0
  31. package/dist/cjs/clients/decorators/wallet.js.map +1 -1
  32. package/dist/cjs/errors/fee.js +42 -0
  33. package/dist/cjs/errors/fee.js.map +1 -0
  34. package/dist/cjs/errors/version.js +1 -1
  35. package/dist/cjs/index.js +9 -6
  36. package/dist/cjs/index.js.map +1 -1
  37. package/dist/cjs/public.js +5 -1
  38. package/dist/cjs/public.js.map +1 -1
  39. package/dist/cjs/utils/data/isBytesEqual.js +4 -3
  40. package/dist/cjs/utils/data/isBytesEqual.js.map +1 -1
  41. package/dist/cjs/utils/ens/encodedLabelToLabelhash.js +2 -2
  42. package/dist/cjs/utils/ens/encodedLabelToLabelhash.js.map +1 -1
  43. package/dist/cjs/utils/formatters/transactionRequest.js +9 -1
  44. package/dist/cjs/utils/formatters/transactionRequest.js.map +1 -1
  45. package/dist/cjs/utils/index.js +3 -3
  46. package/dist/cjs/utils/index.js.map +1 -1
  47. package/dist/esm/actions/index.js +3 -0
  48. package/dist/esm/actions/index.js.map +1 -1
  49. package/dist/esm/actions/public/estimateFeesPerGas.js +78 -0
  50. package/dist/esm/actions/public/estimateFeesPerGas.js.map +1 -0
  51. package/dist/esm/actions/public/estimateGas.js +2 -2
  52. package/dist/esm/actions/public/estimateGas.js.map +1 -1
  53. package/dist/esm/actions/public/estimateMaxPriorityFeePerGas.js +64 -0
  54. package/dist/esm/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -0
  55. package/dist/esm/actions/public/verifyHash.js.map +1 -1
  56. package/dist/esm/actions/public/verifyMessage.js.map +1 -1
  57. package/dist/esm/actions/public/verifyTypedData.js.map +1 -1
  58. package/dist/esm/actions/public/watchContractEvent.js +5 -2
  59. package/dist/esm/actions/public/watchContractEvent.js.map +1 -1
  60. package/dist/esm/actions/wallet/deployContract.js.map +1 -1
  61. package/dist/esm/actions/wallet/prepareTransactionRequest.js +109 -0
  62. package/dist/esm/actions/wallet/prepareTransactionRequest.js.map +1 -0
  63. package/dist/esm/actions/wallet/sendRawTransaction.js +31 -0
  64. package/dist/esm/actions/wallet/sendRawTransaction.js.map +1 -0
  65. package/dist/esm/actions/wallet/sendTransaction.js +6 -6
  66. package/dist/esm/actions/wallet/sendTransaction.js.map +1 -1
  67. package/dist/esm/actions/wallet/signTransaction.js +87 -0
  68. package/dist/esm/actions/wallet/signTransaction.js.map +1 -0
  69. package/dist/esm/actions/wallet/writeContract.js.map +1 -1
  70. package/dist/esm/chains/celo/formatters.js +4 -1
  71. package/dist/esm/chains/celo/formatters.js.map +1 -1
  72. package/dist/esm/clients/createTestClient.js.map +1 -1
  73. package/dist/esm/clients/decorators/public.js +8 -0
  74. package/dist/esm/clients/decorators/public.js.map +1 -1
  75. package/dist/esm/clients/decorators/wallet.js +6 -0
  76. package/dist/esm/clients/decorators/wallet.js.map +1 -1
  77. package/dist/esm/errors/fee.js +36 -0
  78. package/dist/esm/errors/fee.js.map +1 -0
  79. package/dist/esm/errors/version.js +1 -1
  80. package/dist/esm/index.js +2 -2
  81. package/dist/esm/index.js.map +1 -1
  82. package/dist/esm/public.js +2 -0
  83. package/dist/esm/public.js.map +1 -1
  84. package/dist/esm/utils/data/isBytesEqual.js +2 -1
  85. package/dist/esm/utils/data/isBytesEqual.js.map +1 -1
  86. package/dist/esm/utils/ens/encodedLabelToLabelhash.js +1 -1
  87. package/dist/esm/utils/ens/encodedLabelToLabelhash.js.map +1 -1
  88. package/dist/esm/utils/formatters/transactionRequest.js +8 -0
  89. package/dist/esm/utils/formatters/transactionRequest.js.map +1 -1
  90. package/dist/esm/utils/index.js +3 -1
  91. package/dist/esm/utils/index.js.map +1 -1
  92. package/dist/types/actions/index.d.ts +3 -0
  93. package/dist/types/actions/index.d.ts.map +1 -1
  94. package/dist/types/actions/public/estimateFeesPerGas.d.ts +48 -0
  95. package/dist/types/actions/public/estimateFeesPerGas.d.ts.map +1 -0
  96. package/dist/types/actions/public/estimateMaxPriorityFeePerGas.d.ts +37 -0
  97. package/dist/types/actions/public/estimateMaxPriorityFeePerGas.d.ts.map +1 -0
  98. package/dist/types/actions/public/verifyHash.d.ts +1 -1
  99. package/dist/types/actions/public/verifyHash.d.ts.map +1 -1
  100. package/dist/types/actions/public/verifyMessage.d.ts +2 -2
  101. package/dist/types/actions/public/verifyMessage.d.ts.map +1 -1
  102. package/dist/types/actions/public/verifyTypedData.d.ts +2 -2
  103. package/dist/types/actions/public/verifyTypedData.d.ts.map +1 -1
  104. package/dist/types/actions/public/watchContractEvent.d.ts.map +1 -1
  105. package/dist/types/actions/wallet/deployContract.d.ts +2 -1
  106. package/dist/types/actions/wallet/deployContract.d.ts.map +1 -1
  107. package/dist/types/actions/wallet/prepareTransactionRequest.d.ts +52 -0
  108. package/dist/types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -0
  109. package/dist/types/actions/wallet/sendRawTransaction.d.ts +38 -0
  110. package/dist/types/actions/wallet/sendRawTransaction.d.ts.map +1 -0
  111. package/dist/types/actions/wallet/sendTransaction.d.ts +2 -1
  112. package/dist/types/actions/wallet/sendTransaction.d.ts.map +1 -1
  113. package/dist/types/actions/wallet/signTransaction.d.ts +55 -0
  114. package/dist/types/actions/wallet/signTransaction.d.ts.map +1 -0
  115. package/dist/types/actions/wallet/writeContract.d.ts +2 -1
  116. package/dist/types/actions/wallet/writeContract.d.ts.map +1 -1
  117. package/dist/types/chains/celo/formatters.d.ts +303 -5
  118. package/dist/types/chains/celo/formatters.d.ts.map +1 -1
  119. package/dist/types/chains/celo/types.d.ts +43 -17
  120. package/dist/types/chains/celo/types.d.ts.map +1 -1
  121. package/dist/types/chains/index.d.ts +1084 -190
  122. package/dist/types/chains/index.d.ts.map +1 -1
  123. package/dist/types/chains/utils.d.ts +1 -1
  124. package/dist/types/chains/utils.d.ts.map +1 -1
  125. package/dist/types/clients/createTestClient.d.ts +1 -1
  126. package/dist/types/clients/createTestClient.d.ts.map +1 -1
  127. package/dist/types/clients/decorators/public.d.ts +112 -0
  128. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  129. package/dist/types/clients/decorators/wallet.d.ts +111 -0
  130. package/dist/types/clients/decorators/wallet.d.ts.map +1 -1
  131. package/dist/types/errors/fee.d.ts +16 -0
  132. package/dist/types/errors/fee.d.ts.map +1 -0
  133. package/dist/types/errors/version.d.ts +1 -1
  134. package/dist/types/index.d.ts +22 -16
  135. package/dist/types/index.d.ts.map +1 -1
  136. package/dist/types/public.d.ts +2 -0
  137. package/dist/types/public.d.ts.map +1 -1
  138. package/dist/types/types/chain.d.ts +64 -11
  139. package/dist/types/types/chain.d.ts.map +1 -1
  140. package/dist/types/types/eip1193.d.ts +13 -1
  141. package/dist/types/types/eip1193.d.ts.map +1 -1
  142. package/dist/types/types/fee.d.ts +1 -0
  143. package/dist/types/types/fee.d.ts.map +1 -1
  144. package/dist/types/utils/data/isBytesEqual.d.ts +1 -1
  145. package/dist/types/utils/data/isBytesEqual.d.ts.map +1 -1
  146. package/dist/types/utils/ens/encodeLabelhash.d.ts +1 -1
  147. package/dist/types/utils/ens/encodeLabelhash.d.ts.map +1 -1
  148. package/dist/types/utils/ens/encodedLabelToLabelhash.d.ts +1 -1
  149. package/dist/types/utils/ens/encodedLabelToLabelhash.d.ts.map +1 -1
  150. package/dist/types/utils/formatters/transactionRequest.d.ts +5 -0
  151. package/dist/types/utils/formatters/transactionRequest.d.ts.map +1 -1
  152. package/dist/types/utils/index.d.ts +3 -1
  153. package/dist/types/utils/index.d.ts.map +1 -1
  154. package/package.json +1 -1
  155. package/src/actions/index.ts +15 -0
  156. package/src/actions/public/estimateFeesPerGas.ts +154 -0
  157. package/src/actions/public/estimateGas.ts +5 -5
  158. package/src/actions/public/estimateMaxPriorityFeePerGas.ts +98 -0
  159. package/src/actions/public/verifyHash.ts +1 -1
  160. package/src/actions/public/verifyMessage.ts +3 -2
  161. package/src/actions/public/verifyTypedData.ts +3 -2
  162. package/src/actions/public/watchContractEvent.ts +9 -6
  163. package/src/actions/wallet/deployContract.ts +2 -1
  164. package/src/actions/wallet/prepareTransactionRequest.ts +179 -0
  165. package/src/actions/wallet/sendRawTransaction.ts +48 -0
  166. package/src/actions/wallet/sendTransaction.ts +8 -7
  167. package/src/actions/wallet/signTransaction.ts +137 -0
  168. package/src/actions/wallet/writeContract.ts +2 -1
  169. package/src/chains/celo/formatters.ts +11 -12
  170. package/src/chains/celo/types.ts +86 -30
  171. package/src/chains/utils.ts +4 -4
  172. package/src/clients/createTestClient.ts +2 -1
  173. package/src/clients/decorators/public.ts +145 -0
  174. package/src/clients/decorators/wallet.ts +133 -0
  175. package/src/errors/fee.ts +27 -0
  176. package/src/errors/rpc.ts +1 -1
  177. package/src/errors/version.ts +1 -1
  178. package/src/index.ts +45 -25
  179. package/src/public.ts +10 -0
  180. package/src/types/chain.ts +80 -12
  181. package/src/types/eip1193.ts +13 -1
  182. package/src/types/fee.ts +2 -0
  183. package/src/utils/data/isBytesEqual.ts +4 -1
  184. package/src/utils/ens/encodeLabelhash.ts +1 -1
  185. package/src/utils/ens/encodedLabelToLabelhash.ts +2 -2
  186. package/src/utils/formatters/transactionRequest.ts +10 -0
  187. package/src/utils/index.ts +4 -1
  188. package/chains/dist/cjs/celo/formatters.js +0 -58
  189. package/chains/dist/cjs/celo/formatters.js.map +0 -1
  190. package/chains/dist/cjs/celo/parsers.js +0 -72
  191. package/chains/dist/cjs/celo/parsers.js.map +0 -1
  192. package/chains/dist/cjs/celo/serializers.js +0 -75
  193. package/chains/dist/cjs/celo/serializers.js.map +0 -1
  194. package/chains/dist/cjs/celo/types.js +0 -3
  195. package/chains/dist/cjs/celo/types.js.map +0 -1
  196. package/chains/dist/cjs/index.js +0 -135
  197. package/chains/dist/cjs/index.js.map +0 -1
  198. package/chains/dist/cjs/optimism/fees.js +0 -7
  199. package/chains/dist/cjs/optimism/fees.js.map +0 -1
  200. package/chains/dist/cjs/optimism/formatters.js +0 -52
  201. package/chains/dist/cjs/optimism/formatters.js.map +0 -1
  202. package/chains/dist/cjs/optimism/types.js +0 -3
  203. package/chains/dist/cjs/optimism/types.js.map +0 -1
  204. package/chains/dist/cjs/types.js +0 -3
  205. package/chains/dist/cjs/types.js.map +0 -1
  206. package/chains/dist/cjs/utils.js +0 -49
  207. package/chains/dist/cjs/utils.js.map +0 -1
  208. package/chains/dist/tsconfig.build.tsbuildinfo +0 -1
  209. package/chains/node_modules/.bin/tsc +0 -17
  210. package/chains/node_modules/.bin/tsserver +0 -17
  211. package/chains/tsconfig.tsbuildinfo +0 -1
  212. package/dist/cjs/utils/transaction/prepareRequest.js +0 -64
  213. package/dist/cjs/utils/transaction/prepareRequest.js.map +0 -1
  214. package/dist/esm/utils/transaction/prepareRequest.js +0 -64
  215. package/dist/esm/utils/transaction/prepareRequest.js.map +0 -1
  216. package/dist/types/utils/transaction/prepareRequest.d.ts +0 -20
  217. package/dist/types/utils/transaction/prepareRequest.d.ts.map +0 -1
  218. package/src/utils/transaction/prepareRequest.ts +0 -143
@@ -0,0 +1,154 @@
1
+ import type { Client } from '../../clients/createClient.js'
2
+ import type { Transport } from '../../clients/transports/createTransport.js'
3
+ import {
4
+ BaseFeeScalarError,
5
+ Eip1559FeesNotSupportedError,
6
+ } from '../../errors/fee.js'
7
+ import type { Block } from '../../types/block.js'
8
+ import type {
9
+ Chain,
10
+ ChainEstimateFeesPerGasFnParameters,
11
+ ChainFeesFnParameters,
12
+ } from '../../types/chain.js'
13
+ import type { GetChain } from '../../types/chain.js'
14
+ import type {
15
+ FeeValuesEIP1559,
16
+ FeeValuesLegacy,
17
+ FeeValuesType,
18
+ } from '../../types/fee.js'
19
+ import type { PrepareTransactionRequestParameters } from '../wallet/prepareTransactionRequest.js'
20
+ import { internal_estimateMaxPriorityFeePerGas } from './estimateMaxPriorityFeePerGas.js'
21
+ import { getBlock } from './getBlock.js'
22
+ import { getGasPrice } from './getGasPrice.js'
23
+
24
+ export type EstimateFeesPerGasParameters<
25
+ chain extends Chain | undefined = Chain | undefined,
26
+ chainOverride extends Chain | undefined = Chain | undefined,
27
+ type extends FeeValuesType = FeeValuesType,
28
+ > = {
29
+ /**
30
+ * The type of fee values to return.
31
+ *
32
+ * - `legacy`: Returns the legacy gas price.
33
+ * - `eip1559`: Returns the max fee per gas and max priority fee per gas.
34
+ *
35
+ * @default 'eip1559'
36
+ */
37
+ type?: type | FeeValuesType
38
+ } & GetChain<chain, chainOverride>
39
+
40
+ export type EstimateFeesPerGasReturnType<
41
+ type extends FeeValuesType = FeeValuesType,
42
+ > =
43
+ | (type extends 'legacy' ? FeeValuesLegacy : never)
44
+ | (type extends 'eip1559' ? FeeValuesEIP1559 : never)
45
+
46
+ /**
47
+ * Returns an estimate for the fees per gas (in wei) for a
48
+ * transaction to be likely included in the next block.
49
+ * Defaults to [`chain.fees.estimateFeesPerGas`](/docs/clients/chains.html#fees-estimatefeespergas) if set.
50
+ *
51
+ * - Docs: https://viem.sh/docs/actions/public/estimateFeesPerGas.html
52
+ *
53
+ * @param client - Client to use
54
+ * @param parameters - {@link EstimateFeesPerGasParameters}
55
+ * @returns An estimate (in wei) for the fees per gas. {@link EstimateFeesPerGasReturnType}
56
+ *
57
+ * @example
58
+ * import { createPublicClient, http } from 'viem'
59
+ * import { mainnet } from 'viem/chains'
60
+ * import { estimateFeesPerGas } from 'viem/actions'
61
+ *
62
+ * const client = createPublicClient({
63
+ * chain: mainnet,
64
+ * transport: http(),
65
+ * })
66
+ * const maxPriorityFeePerGas = await estimateFeesPerGas(client)
67
+ * // { maxFeePerGas: ..., maxPriorityFeePerGas: ... }
68
+ */
69
+ export async function estimateFeesPerGas<
70
+ chain extends Chain | undefined,
71
+ chainOverride extends Chain | undefined,
72
+ type extends FeeValuesType = 'eip1559',
73
+ >(
74
+ client: Client<Transport, chain>,
75
+ args?: EstimateFeesPerGasParameters<chain, chainOverride, type>,
76
+ ): Promise<EstimateFeesPerGasReturnType<type>> {
77
+ return internal_estimateFeesPerGas(client, args as any)
78
+ }
79
+
80
+ export async function internal_estimateFeesPerGas<
81
+ chain extends Chain | undefined,
82
+ chainOverride extends Chain | undefined,
83
+ type extends FeeValuesType = 'eip1559',
84
+ >(
85
+ client: Client<Transport, chain>,
86
+ args: EstimateFeesPerGasParameters<chain, chainOverride, type> & {
87
+ block?: Block
88
+ request?: PrepareTransactionRequestParameters
89
+ },
90
+ ): Promise<EstimateFeesPerGasReturnType<type>> {
91
+ const {
92
+ block: block_,
93
+ chain = client.chain,
94
+ request,
95
+ type = 'eip1559',
96
+ } = args || {}
97
+
98
+ const baseFeeMultiplier = await (async () => {
99
+ if (typeof chain?.fees?.baseFeeMultiplier === 'function')
100
+ return chain.fees.baseFeeMultiplier({
101
+ block: block_ as Block,
102
+ client,
103
+ request,
104
+ } as ChainFeesFnParameters)
105
+ return chain?.fees?.baseFeeMultiplier ?? 1.2
106
+ })()
107
+ if (baseFeeMultiplier < 1) throw new BaseFeeScalarError()
108
+
109
+ const decimals = baseFeeMultiplier.toString().split('.')[1].length
110
+ const denominator = 10 ** decimals
111
+ const multiply = (base: bigint) =>
112
+ (base * BigInt(baseFeeMultiplier * denominator)) / BigInt(denominator)
113
+
114
+ const block = block_ ? block_ : await getBlock(client)
115
+
116
+ if (typeof chain?.fees?.estimateFeesPerGas === 'function')
117
+ return chain.fees.estimateFeesPerGas({
118
+ block: block_ as Block,
119
+ client,
120
+ multiply,
121
+ request,
122
+ type,
123
+ } as ChainEstimateFeesPerGasFnParameters) as unknown as EstimateFeesPerGasReturnType<type>
124
+
125
+ if (type === 'eip1559') {
126
+ if (typeof block.baseFeePerGas !== 'bigint')
127
+ throw new Eip1559FeesNotSupportedError()
128
+
129
+ const maxPriorityFeePerGas = request?.maxPriorityFeePerGas
130
+ ? request.maxPriorityFeePerGas
131
+ : await internal_estimateMaxPriorityFeePerGas(
132
+ client as Client<Transport, Chain>,
133
+ {
134
+ block,
135
+ chain,
136
+ request,
137
+ },
138
+ )
139
+
140
+ const baseFeePerGas = multiply(block.baseFeePerGas)
141
+ const maxFeePerGas =
142
+ request?.maxFeePerGas ?? baseFeePerGas + maxPriorityFeePerGas
143
+
144
+ return {
145
+ maxFeePerGas,
146
+ maxPriorityFeePerGas,
147
+ } as EstimateFeesPerGasReturnType<type>
148
+ }
149
+
150
+ const gasPrice = request?.gasPrice ?? multiply(await getGasPrice(client))
151
+ return {
152
+ gasPrice,
153
+ } as EstimateFeesPerGasReturnType<type>
154
+ }
@@ -21,9 +21,9 @@ import {
21
21
  assertRequest,
22
22
  } from '../../utils/transaction/assertRequest.js'
23
23
  import {
24
- type PrepareRequestParameters,
25
- prepareRequest,
26
- } from '../../utils/transaction/prepareRequest.js'
24
+ type PrepareTransactionRequestParameters,
25
+ prepareTransactionRequest,
26
+ } from '../wallet/prepareTransactionRequest.js'
27
27
 
28
28
  export type FormattedEstimateGas<
29
29
  TChain extends Chain | undefined = Chain | undefined,
@@ -107,9 +107,9 @@ export async function estimateGas<
107
107
  ...rest
108
108
  } =
109
109
  account.type === 'local'
110
- ? ((await prepareRequest(
110
+ ? ((await prepareTransactionRequest(
111
111
  client,
112
- args as PrepareRequestParameters,
112
+ args as PrepareTransactionRequestParameters,
113
113
  )) as EstimateGasParameters)
114
114
  : args
115
115
 
@@ -0,0 +1,98 @@
1
+ import type { Client } from '../../clients/createClient.js'
2
+ import type { Transport } from '../../clients/transports/createTransport.js'
3
+ import { Eip1559FeesNotSupportedError } from '../../errors/fee.js'
4
+ import type { Account } from '../../types/account.js'
5
+ import type { Block } from '../../types/block.js'
6
+ import type { Chain, ChainFeesFnParameters } from '../../types/chain.js'
7
+ import type { GetChain } from '../../types/chain.js'
8
+ import { hexToBigInt } from '../../utils/encoding/fromHex.js'
9
+ import type { PrepareTransactionRequestParameters } from '../wallet/prepareTransactionRequest.js'
10
+ import { getBlock } from './getBlock.js'
11
+ import { getGasPrice } from './getGasPrice.js'
12
+
13
+ export type EstimateMaxPriorityFeePerGasParameters<
14
+ chain extends Chain | undefined = Chain | undefined,
15
+ chainOverride extends Chain | undefined = Chain | undefined,
16
+ > = GetChain<chain, chainOverride>
17
+ export type EstimateMaxPriorityFeePerGasReturnType = bigint
18
+
19
+ /**
20
+ * Returns an estimate for the max priority fee per gas (in wei) for a
21
+ * transaction to be likely included in the next block.
22
+ * Defaults to [`chain.fees.defaultPriorityFee`](/docs/clients/chains.html#fees-defaultpriorityfee) if set.
23
+ *
24
+ * - Docs: https://viem.sh/docs/actions/public/estimateMaxPriorityFeePerGas.html
25
+ *
26
+ * @param client - Client to use
27
+ * @returns An estimate (in wei) for the max priority fee per gas. {@link EstimateMaxPriorityFeePerGasReturnType}
28
+ *
29
+ * @example
30
+ * import { createPublicClient, http } from 'viem'
31
+ * import { mainnet } from 'viem/chains'
32
+ * import { estimateMaxPriorityFeePerGas } from 'viem/actions'
33
+ *
34
+ * const client = createPublicClient({
35
+ * chain: mainnet,
36
+ * transport: http(),
37
+ * })
38
+ * const maxPriorityFeePerGas = await estimateMaxPriorityFeePerGas(client)
39
+ * // 10000000n
40
+ */
41
+ export async function estimateMaxPriorityFeePerGas<
42
+ chain extends Chain | undefined,
43
+ chainOverride extends Chain | undefined,
44
+ >(
45
+ client: Client<Transport, chain>,
46
+ args?: EstimateMaxPriorityFeePerGasParameters<chain, chainOverride>,
47
+ ): Promise<EstimateMaxPriorityFeePerGasReturnType> {
48
+ return internal_estimateMaxPriorityFeePerGas(client, args as any)
49
+ }
50
+
51
+ export async function internal_estimateMaxPriorityFeePerGas<
52
+ chain extends Chain | undefined,
53
+ chainOverride extends Chain | undefined,
54
+ >(
55
+ client: Client<Transport, chain>,
56
+ args: EstimateMaxPriorityFeePerGasParameters<chain, chainOverride> & {
57
+ block?: Block
58
+ request?: PrepareTransactionRequestParameters<
59
+ chain,
60
+ Account | undefined,
61
+ chainOverride
62
+ >
63
+ },
64
+ ): Promise<EstimateMaxPriorityFeePerGasReturnType> {
65
+ const { block: block_, chain = client.chain, request } = args || {}
66
+ if (typeof chain?.fees?.defaultPriorityFee === 'function') {
67
+ const block = block_ || (await getBlock(client))
68
+ return chain.fees.defaultPriorityFee({
69
+ block,
70
+ client,
71
+ request,
72
+ } as ChainFeesFnParameters)
73
+ } else if (chain?.fees?.defaultPriorityFee)
74
+ return chain?.fees?.defaultPriorityFee
75
+
76
+ try {
77
+ const maxPriorityFeePerGasHex = await client.request({
78
+ method: 'eth_maxPriorityFeePerGas',
79
+ })
80
+ return hexToBigInt(maxPriorityFeePerGasHex)
81
+ } catch {
82
+ // If the RPC Provider does not support `eth_maxPriorityFeePerGas`
83
+ // fall back to calculating it manually via `gasPrice - baseFeePerGas`.
84
+ // See: https://github.com/ethereum/pm/issues/328#:~:text=eth_maxPriorityFeePerGas%20after%20London%20will%20effectively%20return%20eth_gasPrice%20%2D%20baseFee
85
+ const [block, gasPrice] = await Promise.all([
86
+ block_ ? Promise.resolve(block_) : getBlock(client),
87
+ getGasPrice(client),
88
+ ])
89
+
90
+ if (typeof block.baseFeePerGas !== 'bigint')
91
+ throw new Eip1559FeesNotSupportedError()
92
+
93
+ const maxPriorityFeePerGas = gasPrice - block.baseFeePerGas
94
+
95
+ if (maxPriorityFeePerGas < 0n) return 0n
96
+ return maxPriorityFeePerGas
97
+ }
98
+ }
@@ -1,11 +1,11 @@
1
1
  import type { Address } from 'abitype'
2
2
 
3
- import type { Chain } from '../../chains/index.js'
4
3
  import type { Client } from '../../clients/createClient.js'
5
4
  import type { Transport } from '../../clients/transports/createTransport.js'
6
5
  import { universalSignatureValidatorAbi } from '../../constants/abis.js'
7
6
  import { universalSignatureValidatorByteCode } from '../../constants/contracts.js'
8
7
  import { CallExecutionError } from '../../errors/contract.js'
8
+ import type { Chain } from '../../types/chain.js'
9
9
  import type { ByteArray, Hex } from '../../types/misc.js'
10
10
  import { isBytesEqual } from '../../utils/data/isBytesEqual.js'
11
11
  import { encodeDeployData, isHex, toHex } from '../../utils/index.js'
@@ -1,10 +1,11 @@
1
- import type { Chain } from '../../chains/index.js'
1
+ import type { Address } from 'abitype'
2
+
2
3
  import type { Client } from '../../clients/createClient.js'
3
4
  import type { Transport } from '../../clients/transports/createTransport.js'
5
+ import type { Chain } from '../../types/chain.js'
4
6
  import type { ByteArray, Hex, SignableMessage } from '../../types/misc.js'
5
7
  import { hashMessage } from '../../utils/index.js'
6
8
  import { type VerifyHashParameters, verifyHash } from './verifyHash.js'
7
- import type { Address } from 'abitype'
8
9
 
9
10
  export type VerifyMessageParameters = Omit<VerifyHashParameters, 'hash'> & {
10
11
  /** The address that signed the original message. */
@@ -1,11 +1,12 @@
1
- import type { Chain } from '../../chains/index.js'
1
+ import type { Address, TypedData } from 'abitype'
2
+
2
3
  import type { Client } from '../../clients/createClient.js'
3
4
  import type { Transport } from '../../clients/transports/createTransport.js'
5
+ import type { Chain } from '../../types/chain.js'
4
6
  import type { ByteArray, Hex } from '../../types/misc.js'
5
7
  import type { TypedDataDefinition } from '../../types/typedData.js'
6
8
  import { hashTypedData } from '../../utils/signature/hashTypedData.js'
7
9
  import { type VerifyHashParameters, verifyHash } from './verifyHash.js'
8
- import type { Address, TypedData } from 'abitype'
9
10
 
10
11
  export type VerifyTypedDataParameters<
11
12
  TTypedData extends TypedData | { [key: string]: unknown } = TypedData,
@@ -2,7 +2,6 @@ import type { Abi, AbiEvent, Address, ExtractAbiEvent } from 'abitype'
2
2
 
3
3
  import type { Client } from '../../clients/createClient.js'
4
4
  import type { Transport } from '../../clients/transports/createTransport.js'
5
- import type { EncodeEventTopicsParameters, LogTopic } from '../../index.js'
6
5
  import type { Chain } from '../../types/chain.js'
7
6
  import type { GetEventArgs, InferEventName } from '../../types/contract.js'
8
7
  import type { Filter } from '../../types/filter.js'
@@ -17,14 +16,18 @@ import { observe } from '../../utils/observe.js'
17
16
  import { poll } from '../../utils/poll.js'
18
17
  import { stringify } from '../../utils/stringify.js'
19
18
 
20
- import { DecodeLogDataMismatch } from '../../errors/abi.js'
21
19
  import {
20
+ DecodeLogDataMismatch,
22
21
  DecodeLogTopicsMismatch,
23
- InvalidInputRpcError,
24
- decodeEventLog,
22
+ } from '../../errors/abi.js'
23
+ import { InvalidInputRpcError } from '../../errors/rpc.js'
24
+ import type { LogTopic } from '../../types/misc.js'
25
+ import { decodeEventLog } from '../../utils/abi/decodeEventLog.js'
26
+ import {
27
+ type EncodeEventTopicsParameters,
25
28
  encodeEventTopics,
26
- formatLog,
27
- } from '../../index.js'
29
+ } from '../../utils/abi/encodeEventTopics.js'
30
+ import { formatLog } from '../../utils/formatters/log.js'
28
31
  import {
29
32
  type CreateContractEventFilterParameters,
30
33
  createContractEventFilter,
@@ -3,7 +3,8 @@ import type { Abi } from 'abitype'
3
3
  import type { Account } from '../../accounts/types.js'
4
4
  import type { Client } from '../../clients/createClient.js'
5
5
  import type { Transport } from '../../clients/transports/createTransport.js'
6
- import type { Chain, GetChain } from '../../types/chain.js'
6
+ import type { Chain } from '../../types/chain.js'
7
+ import type { GetChain } from '../../types/chain.js'
7
8
  import type { GetConstructorArgs } from '../../types/contract.js'
8
9
  import type { Hex } from '../../types/misc.js'
9
10
  import type { UnionOmit } from '../../types/utils.js'
@@ -0,0 +1,179 @@
1
+ import type { Account } from '../../accounts/types.js'
2
+ import { parseAccount } from '../../accounts/utils/parseAccount.js'
3
+ import { internal_estimateFeesPerGas } from '../../actions/public/estimateFeesPerGas.js'
4
+ import {
5
+ type EstimateGasParameters,
6
+ estimateGas,
7
+ } from '../../actions/public/estimateGas.js'
8
+ import { getBlock } from '../../actions/public/getBlock.js'
9
+ import { getTransactionCount } from '../../actions/public/getTransactionCount.js'
10
+ import type { Client } from '../../clients/createClient.js'
11
+ import type { Transport } from '../../clients/transports/createTransport.js'
12
+ import { AccountNotFoundError } from '../../errors/account.js'
13
+ import {
14
+ Eip1559FeesNotSupportedError,
15
+ MaxFeePerGasTooLowError,
16
+ } from '../../errors/fee.js'
17
+ import type { GetAccountParameter } from '../../types/account.js'
18
+ import type { Chain } from '../../types/chain.js'
19
+ import type { GetChain } from '../../types/chain.js'
20
+ import type { TransactionSerializable } from '../../types/transaction.js'
21
+ import type { UnionOmit } from '../../types/utils.js'
22
+ import type { FormattedTransactionRequest } from '../../utils/formatters/transactionRequest.js'
23
+ import type { AssertRequestParameters } from '../../utils/transaction/assertRequest.js'
24
+ import { assertRequest } from '../../utils/transaction/assertRequest.js'
25
+ import { getTransactionType } from '../../utils/transaction/getTransactionType.js'
26
+
27
+ export type PrepareTransactionRequestParameters<
28
+ TChain extends Chain | undefined = Chain | undefined,
29
+ TAccount extends Account | undefined = Account | undefined,
30
+ TChainOverride extends Chain | undefined = Chain | undefined,
31
+ > = UnionOmit<
32
+ FormattedTransactionRequest<
33
+ TChainOverride extends Chain ? TChainOverride : TChain
34
+ >,
35
+ 'from'
36
+ > &
37
+ GetAccountParameter<TAccount> &
38
+ GetChain<TChain, TChainOverride>
39
+
40
+ export type PrepareTransactionRequestReturnType<
41
+ TChain extends Chain | undefined = Chain | undefined,
42
+ TAccount extends Account | undefined = Account | undefined,
43
+ TChainOverride extends Chain | undefined = Chain | undefined,
44
+ > = UnionOmit<
45
+ FormattedTransactionRequest<
46
+ TChainOverride extends Chain ? TChainOverride : TChain
47
+ >,
48
+ 'from'
49
+ > &
50
+ GetAccountParameter<TAccount> &
51
+ GetChain<TChain, TChainOverride>
52
+
53
+ /**
54
+ * Prepares a transaction request for signing.
55
+ *
56
+ * - Docs: https://viem.sh/docs/actions/wallet/prepareTransactionRequest.html
57
+ *
58
+ * @param args - {@link PrepareTransactionRequestParameters}
59
+ * @returns The transaction request. {@link PrepareTransactionRequestReturnType}
60
+ *
61
+ * @example
62
+ * import { createWalletClient, custom } from 'viem'
63
+ * import { mainnet } from 'viem/chains'
64
+ * import { prepareTransactionRequest } from 'viem/actions'
65
+ *
66
+ * const client = createWalletClient({
67
+ * chain: mainnet,
68
+ * transport: custom(window.ethereum),
69
+ * })
70
+ * const request = await prepareTransactionRequest(client, {
71
+ * account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
72
+ * to: '0x0000000000000000000000000000000000000000',
73
+ * value: 1n,
74
+ * })
75
+ *
76
+ * @example
77
+ * // Account Hoisting
78
+ * import { createWalletClient, http } from 'viem'
79
+ * import { privateKeyToAccount } from 'viem/accounts'
80
+ * import { mainnet } from 'viem/chains'
81
+ * import { prepareTransactionRequest } from 'viem/actions'
82
+ *
83
+ * const client = createWalletClient({
84
+ * account: privateKeyToAccount('0x…'),
85
+ * chain: mainnet,
86
+ * transport: custom(window.ethereum),
87
+ * })
88
+ * const request = await prepareTransactionRequest(client, {
89
+ * to: '0x0000000000000000000000000000000000000000',
90
+ * value: 1n,
91
+ * })
92
+ */
93
+ export async function prepareTransactionRequest<
94
+ TChain extends Chain | undefined,
95
+ TAccount extends Account | undefined,
96
+ TChainOverride extends Chain | undefined,
97
+ >(
98
+ client: Client<Transport, TChain, TAccount>,
99
+ args: PrepareTransactionRequestParameters<TChain, TAccount, TChainOverride>,
100
+ ): Promise<
101
+ PrepareTransactionRequestReturnType<TChain, TAccount, TChainOverride>
102
+ > {
103
+ const { account: account_ = client.account, chain, gas, nonce, type } = args
104
+ if (!account_) throw new AccountNotFoundError()
105
+ const account = parseAccount(account_)
106
+
107
+ const block = await getBlock(client, { blockTag: 'latest' })
108
+
109
+ const request = { ...args, from: account.address }
110
+
111
+ if (typeof nonce === 'undefined')
112
+ request.nonce = await getTransactionCount(client, {
113
+ address: account.address,
114
+ blockTag: 'pending',
115
+ })
116
+
117
+ if (typeof type === 'undefined') {
118
+ try {
119
+ request.type = getTransactionType(
120
+ request as TransactionSerializable,
121
+ ) as any
122
+ } catch {
123
+ // infer type from block
124
+ request.type =
125
+ typeof block.baseFeePerGas === 'bigint' ? 'eip1559' : 'legacy'
126
+ }
127
+ }
128
+
129
+ if (request.type === 'eip1559') {
130
+ // EIP-1559 fees
131
+ const { maxFeePerGas, maxPriorityFeePerGas } =
132
+ await internal_estimateFeesPerGas(client, {
133
+ block,
134
+ chain,
135
+ request: request as PrepareTransactionRequestParameters,
136
+ })
137
+
138
+ if (
139
+ typeof args.maxPriorityFeePerGas === 'undefined' &&
140
+ args.maxFeePerGas &&
141
+ args.maxFeePerGas < maxPriorityFeePerGas
142
+ )
143
+ throw new MaxFeePerGasTooLowError({
144
+ maxPriorityFeePerGas,
145
+ })
146
+
147
+ request.maxPriorityFeePerGas = maxPriorityFeePerGas
148
+ request.maxFeePerGas = maxFeePerGas
149
+ } else {
150
+ // Legacy fees
151
+ if (
152
+ typeof args.maxFeePerGas !== 'undefined' ||
153
+ typeof args.maxPriorityFeePerGas !== 'undefined'
154
+ )
155
+ throw new Eip1559FeesNotSupportedError()
156
+
157
+ const { gasPrice: gasPrice_ } = await internal_estimateFeesPerGas(client, {
158
+ block,
159
+ chain,
160
+ request: request as PrepareTransactionRequestParameters,
161
+ type: 'legacy',
162
+ })
163
+ request.gasPrice = gasPrice_
164
+ }
165
+
166
+ if (typeof gas === 'undefined')
167
+ request.gas = await estimateGas(client, {
168
+ ...request,
169
+ account: { address: account.address, type: 'json-rpc' },
170
+ } as EstimateGasParameters)
171
+
172
+ assertRequest(request as AssertRequestParameters)
173
+
174
+ return request as PrepareTransactionRequestReturnType<
175
+ TChain,
176
+ TAccount,
177
+ TChainOverride
178
+ >
179
+ }
@@ -0,0 +1,48 @@
1
+ import type { Client } from '../../clients/createClient.js'
2
+ import type { Transport } from '../../clients/transports/createTransport.js'
3
+ import type { Chain } from '../../types/chain.js'
4
+ import type { Hash } from '../../types/misc.js'
5
+ import type { TransactionSerialized } from '../../types/transaction.js'
6
+
7
+ export type SendRawTransactionParameters = {
8
+ /**
9
+ * The signed serialized tranasction.
10
+ */
11
+ serializedTransaction: TransactionSerialized
12
+ }
13
+
14
+ export type SendRawTransactionReturnType = Hash
15
+
16
+ /**
17
+ * Sends a **signed** transaction to the network
18
+ *
19
+ * - Docs: https://viem.sh/docs/actions/wallet/sendRawTransaction.html
20
+ * - JSON-RPC Method: [`eth_sendRawTransaction`](https://ethereum.github.io/execution-apis/api-documentation/)
21
+ *
22
+ * @param client - Client to use
23
+ * @param parameters - {@link SendRawTransactionParameters}
24
+ * @returns The transaction hash. {@link SendRawTransactionReturnType}
25
+ *
26
+ * @example
27
+ * import { createWalletClient, custom } from 'viem'
28
+ * import { mainnet } from 'viem/chains'
29
+ * import { sendRawTransaction } from 'viem/wallet'
30
+ *
31
+ * const client = createWalletClient({
32
+ * chain: mainnet,
33
+ * transport: custom(window.ethereum),
34
+ * })
35
+ *
36
+ * const hash = await sendRawTransaction(client, {
37
+ * serializedTransaction: '0x02f850018203118080825208808080c080a04012522854168b27e5dc3d5839bab5e6b39e1a0ffd343901ce1622e3d64b48f1a04e00902ae0502c4728cbf12156290df99c3ed7de85b1dbfe20b5c36931733a33'
38
+ * })
39
+ */
40
+ export async function sendRawTransaction<TChain extends Chain | undefined,>(
41
+ client: Client<Transport, TChain>,
42
+ { serializedTransaction }: SendRawTransactionParameters,
43
+ ): Promise<SendRawTransactionReturnType> {
44
+ return client.request({
45
+ method: 'eth_sendRawTransaction',
46
+ params: [serializedTransaction],
47
+ })
48
+ }
@@ -5,7 +5,8 @@ import type { Transport } from '../../clients/transports/createTransport.js'
5
5
  import { AccountNotFoundError } from '../../errors/account.js'
6
6
  import type { BaseError } from '../../errors/base.js'
7
7
  import type { GetAccountParameter } from '../../types/account.js'
8
- import type { Chain, GetChain } from '../../types/chain.js'
8
+ import type { Chain } from '../../types/chain.js'
9
+ import type { GetChain } from '../../types/chain.js'
9
10
  import type { Hash } from '../../types/misc.js'
10
11
  import type {
11
12
  TransactionRequest,
@@ -23,8 +24,9 @@ import {
23
24
  type AssertRequestParameters,
24
25
  assertRequest,
25
26
  } from '../../utils/transaction/assertRequest.js'
26
- import { prepareRequest } from '../../utils/transaction/prepareRequest.js'
27
27
  import { getChainId } from '../public/getChainId.js'
28
+ import { prepareTransactionRequest } from './prepareTransactionRequest.js'
29
+ import { sendRawTransaction } from './sendRawTransaction.js'
28
30
 
29
31
  export type SendTransactionParameters<
30
32
  TChain extends Chain | undefined = Chain | undefined,
@@ -129,7 +131,7 @@ export async function sendTransaction<
129
131
 
130
132
  if (account.type === 'local') {
131
133
  // Prepare the request for signing (assign appropriate fees, etc.)
132
- const request = await prepareRequest(client, {
134
+ const request = await prepareTransactionRequest(client, {
133
135
  account,
134
136
  accessList,
135
137
  chain,
@@ -147,16 +149,15 @@ export async function sendTransaction<
147
149
  if (!chainId) chainId = await getChainId(client)
148
150
 
149
151
  const serializer = chain?.serializers?.transaction
150
- const signedRequest = (await account.signTransaction(
152
+ const serializedTransaction = (await account.signTransaction(
151
153
  {
152
154
  ...request,
153
155
  chainId,
154
156
  } as TransactionSerializable,
155
157
  { serializer },
156
158
  )) as Hash
157
- return await client.request({
158
- method: 'eth_sendRawTransaction',
159
- params: [signedRequest],
159
+ return await sendRawTransaction(client, {
160
+ serializedTransaction,
160
161
  })
161
162
  }
162
163