viem 0.0.0-w-20230821171049 → 0.0.0-w-20230828183856

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 (244) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/actions/ens/getEnsAddress.js +2 -2
  3. package/dist/cjs/actions/ens/getEnsAddress.js.map +1 -1
  4. package/dist/cjs/actions/index.js +8 -2
  5. package/dist/cjs/actions/index.js.map +1 -1
  6. package/dist/cjs/actions/public/estimateFeesPerGas.js +60 -0
  7. package/dist/cjs/actions/public/estimateFeesPerGas.js.map +1 -0
  8. package/dist/cjs/actions/public/estimateGas.js +2 -2
  9. package/dist/cjs/actions/public/estimateGas.js.map +1 -1
  10. package/dist/cjs/actions/public/estimateMaxPriorityFeePerGas.js +44 -0
  11. package/dist/cjs/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -0
  12. package/dist/cjs/actions/public/verifyHash.js.map +1 -1
  13. package/dist/cjs/actions/public/verifyMessage.js.map +1 -1
  14. package/dist/cjs/actions/public/verifyTypedData.js.map +1 -1
  15. package/dist/cjs/actions/public/waitForTransactionReceipt.js +35 -30
  16. package/dist/cjs/actions/public/waitForTransactionReceipt.js.map +1 -1
  17. package/dist/cjs/actions/public/watchContractEvent.js +10 -7
  18. package/dist/cjs/actions/public/watchContractEvent.js.map +1 -1
  19. package/dist/cjs/actions/wallet/deployContract.js.map +1 -1
  20. package/dist/cjs/actions/wallet/prepareTransactionRequest.js +70 -0
  21. package/dist/cjs/actions/wallet/prepareTransactionRequest.js.map +1 -0
  22. package/dist/cjs/actions/wallet/sendRawTransaction.js +11 -0
  23. package/dist/cjs/actions/wallet/sendRawTransaction.js.map +1 -0
  24. package/dist/cjs/actions/wallet/sendTransaction.js +6 -6
  25. package/dist/cjs/actions/wallet/sendTransaction.js.map +1 -1
  26. package/dist/cjs/actions/wallet/signTransaction.js +47 -0
  27. package/dist/cjs/actions/wallet/signTransaction.js.map +1 -0
  28. package/dist/cjs/chains/celo/formatters.js +4 -1
  29. package/dist/cjs/chains/celo/formatters.js.map +1 -1
  30. package/dist/cjs/chains/celo/parsers.js +76 -0
  31. package/dist/cjs/chains/celo/parsers.js.map +1 -0
  32. package/dist/cjs/chains/celo/serializers.js +2 -1
  33. package/dist/cjs/chains/celo/serializers.js.map +1 -1
  34. package/dist/cjs/chains/index.js +3 -2
  35. package/dist/cjs/chains/index.js.map +1 -1
  36. package/dist/cjs/chains/utils.js +3 -1
  37. package/dist/cjs/chains/utils.js.map +1 -1
  38. package/dist/cjs/clients/createTestClient.js.map +1 -1
  39. package/dist/cjs/clients/decorators/public.js +8 -0
  40. package/dist/cjs/clients/decorators/public.js.map +1 -1
  41. package/dist/cjs/clients/decorators/wallet.js +6 -0
  42. package/dist/cjs/clients/decorators/wallet.js.map +1 -1
  43. package/dist/cjs/errors/fee.js +42 -0
  44. package/dist/cjs/errors/fee.js.map +1 -0
  45. package/dist/cjs/index.js +9 -6
  46. package/dist/cjs/index.js.map +1 -1
  47. package/dist/cjs/public.js +5 -1
  48. package/dist/cjs/public.js.map +1 -1
  49. package/dist/cjs/utils/data/isBytesEqual.js +4 -3
  50. package/dist/cjs/utils/data/isBytesEqual.js.map +1 -1
  51. package/dist/cjs/utils/ens/encodedLabelToLabelhash.js +2 -2
  52. package/dist/cjs/utils/ens/encodedLabelToLabelhash.js.map +1 -1
  53. package/dist/cjs/utils/formatters/transactionRequest.js +9 -1
  54. package/dist/cjs/utils/formatters/transactionRequest.js.map +1 -1
  55. package/dist/cjs/utils/index.js +3 -3
  56. package/dist/cjs/utils/index.js.map +1 -1
  57. package/dist/cjs/utils/rpc.js +5 -7
  58. package/dist/cjs/utils/rpc.js.map +1 -1
  59. package/dist/cjs/utils/transaction/parseTransaction.js +8 -3
  60. package/dist/cjs/utils/transaction/parseTransaction.js.map +1 -1
  61. package/dist/cjs/version.js +1 -1
  62. package/dist/esm/actions/ens/getEnsAddress.js +2 -2
  63. package/dist/esm/actions/ens/getEnsAddress.js.map +1 -1
  64. package/dist/esm/actions/index.js +3 -0
  65. package/dist/esm/actions/index.js.map +1 -1
  66. package/dist/esm/actions/public/estimateFeesPerGas.js +78 -0
  67. package/dist/esm/actions/public/estimateFeesPerGas.js.map +1 -0
  68. package/dist/esm/actions/public/estimateGas.js +2 -2
  69. package/dist/esm/actions/public/estimateGas.js.map +1 -1
  70. package/dist/esm/actions/public/estimateMaxPriorityFeePerGas.js +64 -0
  71. package/dist/esm/actions/public/estimateMaxPriorityFeePerGas.js.map +1 -0
  72. package/dist/esm/actions/public/verifyHash.js.map +1 -1
  73. package/dist/esm/actions/public/verifyMessage.js.map +1 -1
  74. package/dist/esm/actions/public/verifyTypedData.js.map +1 -1
  75. package/dist/esm/actions/public/waitForTransactionReceipt.js +39 -34
  76. package/dist/esm/actions/public/waitForTransactionReceipt.js.map +1 -1
  77. package/dist/esm/actions/public/watchContractEvent.js +5 -2
  78. package/dist/esm/actions/public/watchContractEvent.js.map +1 -1
  79. package/dist/esm/actions/wallet/deployContract.js.map +1 -1
  80. package/dist/esm/actions/wallet/prepareTransactionRequest.js +109 -0
  81. package/dist/esm/actions/wallet/prepareTransactionRequest.js.map +1 -0
  82. package/dist/esm/actions/wallet/sendRawTransaction.js +31 -0
  83. package/dist/esm/actions/wallet/sendRawTransaction.js.map +1 -0
  84. package/dist/esm/actions/wallet/sendTransaction.js +6 -6
  85. package/dist/esm/actions/wallet/sendTransaction.js.map +1 -1
  86. package/dist/esm/actions/wallet/signTransaction.js +87 -0
  87. package/dist/esm/actions/wallet/signTransaction.js.map +1 -0
  88. package/dist/esm/chains/celo/formatters.js +4 -1
  89. package/dist/esm/chains/celo/formatters.js.map +1 -1
  90. package/dist/esm/chains/celo/parsers.js +72 -0
  91. package/dist/esm/chains/celo/parsers.js.map +1 -0
  92. package/dist/esm/chains/celo/serializers.js +1 -3
  93. package/dist/esm/chains/celo/serializers.js.map +1 -1
  94. package/dist/esm/chains/index.js +1 -0
  95. package/dist/esm/chains/index.js.map +1 -1
  96. package/dist/esm/chains/utils.js +1 -0
  97. package/dist/esm/chains/utils.js.map +1 -1
  98. package/dist/esm/clients/createTestClient.js.map +1 -1
  99. package/dist/esm/clients/decorators/public.js +8 -0
  100. package/dist/esm/clients/decorators/public.js.map +1 -1
  101. package/dist/esm/clients/decorators/wallet.js +6 -0
  102. package/dist/esm/clients/decorators/wallet.js.map +1 -1
  103. package/dist/esm/errors/fee.js +36 -0
  104. package/dist/esm/errors/fee.js.map +1 -0
  105. package/dist/esm/index.js +2 -2
  106. package/dist/esm/index.js.map +1 -1
  107. package/dist/esm/public.js +2 -0
  108. package/dist/esm/public.js.map +1 -1
  109. package/dist/esm/utils/data/isBytesEqual.js +2 -1
  110. package/dist/esm/utils/data/isBytesEqual.js.map +1 -1
  111. package/dist/esm/utils/ens/encodedLabelToLabelhash.js +1 -1
  112. package/dist/esm/utils/ens/encodedLabelToLabelhash.js.map +1 -1
  113. package/dist/esm/utils/formatters/transactionRequest.js +8 -0
  114. package/dist/esm/utils/formatters/transactionRequest.js.map +1 -1
  115. package/dist/esm/utils/index.js +3 -1
  116. package/dist/esm/utils/index.js.map +1 -1
  117. package/dist/esm/utils/rpc.js +5 -7
  118. package/dist/esm/utils/rpc.js.map +1 -1
  119. package/dist/esm/utils/transaction/parseTransaction.js +6 -3
  120. package/dist/esm/utils/transaction/parseTransaction.js.map +1 -1
  121. package/dist/esm/version.js +1 -1
  122. package/dist/types/actions/ens/getEnsAddress.d.ts.map +1 -1
  123. package/dist/types/actions/index.d.ts +3 -0
  124. package/dist/types/actions/index.d.ts.map +1 -1
  125. package/dist/types/actions/public/estimateFeesPerGas.d.ts +48 -0
  126. package/dist/types/actions/public/estimateFeesPerGas.d.ts.map +1 -0
  127. package/dist/types/actions/public/estimateMaxPriorityFeePerGas.d.ts +37 -0
  128. package/dist/types/actions/public/estimateMaxPriorityFeePerGas.d.ts.map +1 -0
  129. package/dist/types/actions/public/verifyHash.d.ts +1 -1
  130. package/dist/types/actions/public/verifyHash.d.ts.map +1 -1
  131. package/dist/types/actions/public/verifyMessage.d.ts +2 -2
  132. package/dist/types/actions/public/verifyMessage.d.ts.map +1 -1
  133. package/dist/types/actions/public/verifyTypedData.d.ts +2 -2
  134. package/dist/types/actions/public/verifyTypedData.d.ts.map +1 -1
  135. package/dist/types/actions/public/waitForTransactionReceipt.d.ts.map +1 -1
  136. package/dist/types/actions/public/watchContractEvent.d.ts.map +1 -1
  137. package/dist/types/actions/wallet/deployContract.d.ts +2 -1
  138. package/dist/types/actions/wallet/deployContract.d.ts.map +1 -1
  139. package/dist/types/actions/wallet/prepareTransactionRequest.d.ts +52 -0
  140. package/dist/types/actions/wallet/prepareTransactionRequest.d.ts.map +1 -0
  141. package/dist/types/actions/wallet/sendRawTransaction.d.ts +38 -0
  142. package/dist/types/actions/wallet/sendRawTransaction.d.ts.map +1 -0
  143. package/dist/types/actions/wallet/sendTransaction.d.ts +2 -1
  144. package/dist/types/actions/wallet/sendTransaction.d.ts.map +1 -1
  145. package/dist/types/actions/wallet/signTransaction.d.ts +55 -0
  146. package/dist/types/actions/wallet/signTransaction.d.ts.map +1 -0
  147. package/dist/types/chains/celo/formatters.d.ts +503 -5
  148. package/dist/types/chains/celo/formatters.d.ts.map +1 -1
  149. package/dist/types/chains/celo/parsers.d.ts +6 -0
  150. package/dist/types/chains/celo/parsers.d.ts.map +1 -0
  151. package/dist/types/chains/celo/serializers.d.ts +3 -14
  152. package/dist/types/chains/celo/serializers.d.ts.map +1 -1
  153. package/dist/types/chains/celo/types.d.ts +54 -14
  154. package/dist/types/chains/celo/types.d.ts.map +1 -1
  155. package/dist/types/chains/index.d.ts +15876 -3730
  156. package/dist/types/chains/index.d.ts.map +1 -1
  157. package/dist/types/chains/utils.d.ts +3 -2
  158. package/dist/types/chains/utils.d.ts.map +1 -1
  159. package/dist/types/clients/createTestClient.d.ts +1 -1
  160. package/dist/types/clients/createTestClient.d.ts.map +1 -1
  161. package/dist/types/clients/decorators/public.d.ts +112 -0
  162. package/dist/types/clients/decorators/public.d.ts.map +1 -1
  163. package/dist/types/clients/decorators/wallet.d.ts +111 -0
  164. package/dist/types/clients/decorators/wallet.d.ts.map +1 -1
  165. package/dist/types/errors/fee.d.ts +16 -0
  166. package/dist/types/errors/fee.d.ts.map +1 -0
  167. package/dist/types/index.d.ts +23 -17
  168. package/dist/types/index.d.ts.map +1 -1
  169. package/dist/types/public.d.ts +2 -0
  170. package/dist/types/public.d.ts.map +1 -1
  171. package/dist/types/types/chain.d.ts +64 -11
  172. package/dist/types/types/chain.d.ts.map +1 -1
  173. package/dist/types/types/contract.d.ts +2 -2
  174. package/dist/types/types/contract.d.ts.map +1 -1
  175. package/dist/types/types/eip1193.d.ts +14 -2
  176. package/dist/types/types/eip1193.d.ts.map +1 -1
  177. package/dist/types/types/fee.d.ts +1 -0
  178. package/dist/types/types/fee.d.ts.map +1 -1
  179. package/dist/types/utils/chain.d.ts +2 -2
  180. package/dist/types/utils/chain.d.ts.map +1 -1
  181. package/dist/types/utils/data/isBytesEqual.d.ts +1 -1
  182. package/dist/types/utils/data/isBytesEqual.d.ts.map +1 -1
  183. package/dist/types/utils/ens/encodeLabelhash.d.ts +1 -1
  184. package/dist/types/utils/ens/encodeLabelhash.d.ts.map +1 -1
  185. package/dist/types/utils/ens/encodedLabelToLabelhash.d.ts +1 -1
  186. package/dist/types/utils/ens/encodedLabelToLabelhash.d.ts.map +1 -1
  187. package/dist/types/utils/formatters/transactionRequest.d.ts +5 -0
  188. package/dist/types/utils/formatters/transactionRequest.d.ts.map +1 -1
  189. package/dist/types/utils/index.d.ts +3 -1
  190. package/dist/types/utils/index.d.ts.map +1 -1
  191. package/dist/types/utils/rpc.d.ts +1 -1
  192. package/dist/types/utils/rpc.d.ts.map +1 -1
  193. package/dist/types/utils/transaction/parseTransaction.d.ts +5 -1
  194. package/dist/types/utils/transaction/parseTransaction.d.ts.map +1 -1
  195. package/dist/types/version.d.ts +1 -1
  196. package/package.json +1 -1
  197. package/src/actions/ens/getEnsAddress.ts +6 -8
  198. package/src/actions/index.ts +15 -0
  199. package/src/actions/public/estimateFeesPerGas.ts +154 -0
  200. package/src/actions/public/estimateGas.ts +5 -5
  201. package/src/actions/public/estimateMaxPriorityFeePerGas.ts +98 -0
  202. package/src/actions/public/verifyHash.ts +1 -1
  203. package/src/actions/public/verifyMessage.ts +3 -2
  204. package/src/actions/public/verifyTypedData.ts +3 -2
  205. package/src/actions/public/waitForTransactionReceipt.ts +48 -44
  206. package/src/actions/public/watchContractEvent.ts +9 -6
  207. package/src/actions/wallet/deployContract.ts +2 -1
  208. package/src/actions/wallet/prepareTransactionRequest.ts +179 -0
  209. package/src/actions/wallet/sendRawTransaction.ts +48 -0
  210. package/src/actions/wallet/sendTransaction.ts +8 -7
  211. package/src/actions/wallet/signTransaction.ts +137 -0
  212. package/src/chains/celo/formatters.ts +11 -12
  213. package/src/chains/celo/parsers.ts +123 -0
  214. package/src/chains/celo/serializers.ts +13 -34
  215. package/src/chains/celo/types.ts +114 -26
  216. package/src/chains/index.ts +1 -0
  217. package/src/chains/utils.ts +10 -6
  218. package/src/clients/createTestClient.ts +2 -1
  219. package/src/clients/decorators/public.ts +145 -0
  220. package/src/clients/decorators/wallet.ts +133 -0
  221. package/src/errors/fee.ts +27 -0
  222. package/src/errors/rpc.ts +1 -1
  223. package/src/index.ts +46 -25
  224. package/src/public.ts +10 -0
  225. package/src/types/chain.ts +80 -12
  226. package/src/types/contract.ts +2 -2
  227. package/src/types/eip1193.ts +14 -2
  228. package/src/types/fee.ts +2 -0
  229. package/src/utils/chain.ts +2 -2
  230. package/src/utils/data/isBytesEqual.ts +4 -1
  231. package/src/utils/ens/encodeLabelhash.ts +1 -1
  232. package/src/utils/ens/encodedLabelToLabelhash.ts +2 -2
  233. package/src/utils/formatters/transactionRequest.ts +10 -0
  234. package/src/utils/index.ts +4 -1
  235. package/src/utils/rpc.ts +5 -8
  236. package/src/utils/transaction/parseTransaction.ts +7 -9
  237. package/src/version.ts +1 -1
  238. package/dist/cjs/utils/transaction/prepareRequest.js +0 -64
  239. package/dist/cjs/utils/transaction/prepareRequest.js.map +0 -1
  240. package/dist/esm/utils/transaction/prepareRequest.js +0 -64
  241. package/dist/esm/utils/transaction/prepareRequest.js.map +0 -1
  242. package/dist/types/utils/transaction/prepareRequest.d.ts +0 -20
  243. package/dist/types/utils/transaction/prepareRequest.d.ts.map +0 -1
  244. package/src/utils/transaction/prepareRequest.ts +0 -143
@@ -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
 
@@ -0,0 +1,137 @@
1
+ import type { Account } from '../../accounts/types.js'
2
+ import { parseAccount } from '../../accounts/utils/parseAccount.js'
3
+ import type { Client } from '../../clients/createClient.js'
4
+ import type { Transport } from '../../clients/transports/createTransport.js'
5
+ import { AccountNotFoundError } from '../../errors/account.js'
6
+ import type { GetAccountParameter } from '../../types/account.js'
7
+ import type { Chain, GetChain } from '../../types/chain.js'
8
+ import { type RpcTransactionRequest } from '../../types/rpc.js'
9
+ import type {
10
+ TransactionRequest,
11
+ TransactionSerializable,
12
+ TransactionSerialized,
13
+ } from '../../types/transaction.js'
14
+ import type { UnionOmit } from '../../types/utils.js'
15
+ import { assertCurrentChain } from '../../utils/chain.js'
16
+ import {
17
+ type FormattedTransactionRequest,
18
+ formatTransactionRequest,
19
+ } from '../../utils/formatters/transactionRequest.js'
20
+ import { numberToHex } from '../../utils/index.js'
21
+ import { assertRequest } from '../../utils/transaction/assertRequest.js'
22
+ import { getChainId } from '../public/getChainId.js'
23
+
24
+ export type SignTransactionParameters<
25
+ TChain extends Chain | undefined = Chain | undefined,
26
+ TAccount extends Account | undefined = Account | undefined,
27
+ TChainOverride extends Chain | undefined = Chain | undefined,
28
+ > = UnionOmit<
29
+ FormattedTransactionRequest<
30
+ TChainOverride extends Chain ? TChainOverride : TChain
31
+ >,
32
+ 'from'
33
+ > &
34
+ GetAccountParameter<TAccount> &
35
+ GetChain<TChain, TChainOverride>
36
+
37
+ export type SignTransactionReturnType = TransactionSerialized
38
+
39
+ /**
40
+ * Signs a transaction.
41
+ *
42
+ * - Docs: https://viem.sh/docs/actions/wallet/signTransaction.html
43
+ * - JSON-RPC Methods:
44
+ * - JSON-RPC Accounts: [`eth_signTransaction`](https://ethereum.github.io/execution-apis/api-documentation/)
45
+ * - Local Accounts: Signs locally. No JSON-RPC request.
46
+ *
47
+ * @param args - {@link SignTransactionParameters}
48
+ * @returns The signed serialized tranasction. {@link SignTransactionReturnType}
49
+ *
50
+ * @example
51
+ * import { createWalletClient, custom } from 'viem'
52
+ * import { mainnet } from 'viem/chains'
53
+ * import { signTransaction } from 'viem/actions'
54
+ *
55
+ * const client = createWalletClient({
56
+ * chain: mainnet,
57
+ * transport: custom(window.ethereum),
58
+ * })
59
+ * const signature = await signTransaction(client, {
60
+ * account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
61
+ * to: '0x0000000000000000000000000000000000000000',
62
+ * value: 1n,
63
+ * })
64
+ *
65
+ * @example
66
+ * // Account Hoisting
67
+ * import { createWalletClient, http } from 'viem'
68
+ * import { privateKeyToAccount } from 'viem/accounts'
69
+ * import { mainnet } from 'viem/chains'
70
+ * import { signTransaction } from 'viem/actions'
71
+ *
72
+ * const client = createWalletClient({
73
+ * account: privateKeyToAccount('0x…'),
74
+ * chain: mainnet,
75
+ * transport: custom(window.ethereum),
76
+ * })
77
+ * const signature = await signTransaction(client, {
78
+ * to: '0x0000000000000000000000000000000000000000',
79
+ * value: 1n,
80
+ * })
81
+ */
82
+ export async function signTransaction<
83
+ TChain extends Chain | undefined,
84
+ TAccount extends Account | undefined,
85
+ TChainOverride extends Chain | undefined,
86
+ >(
87
+ client: Client<Transport, TChain, TAccount>,
88
+ args: SignTransactionParameters<TChain, TAccount, TChainOverride>,
89
+ ): Promise<SignTransactionReturnType> {
90
+ const {
91
+ account: account_ = client.account,
92
+ chain = client.chain,
93
+ ...transaction
94
+ } = args
95
+
96
+ if (!account_)
97
+ throw new AccountNotFoundError({
98
+ docsPath: '/docs/actions/wallet/signTransaction',
99
+ })
100
+ const account = parseAccount(account_)
101
+
102
+ assertRequest({
103
+ account,
104
+ ...args,
105
+ })
106
+
107
+ const chainId = await getChainId(client)
108
+ if (chain !== null)
109
+ assertCurrentChain({
110
+ currentChainId: chainId,
111
+ chain,
112
+ })
113
+
114
+ const formatters = chain?.formatters || client.chain?.formatters
115
+ const format =
116
+ formatters?.transactionRequest?.format || formatTransactionRequest
117
+
118
+ if (account.type === 'local')
119
+ return account.signTransaction(
120
+ {
121
+ chainId,
122
+ ...transaction,
123
+ } as unknown as TransactionSerializable,
124
+ { serializer: client.chain?.serializers?.transaction },
125
+ ) as Promise<SignTransactionReturnType>
126
+
127
+ return await client.request({
128
+ method: 'eth_signTransaction',
129
+ params: [
130
+ {
131
+ ...format(transaction as unknown as TransactionRequest),
132
+ chainId: numberToHex(chainId),
133
+ from: account.address,
134
+ } as unknown as RpcTransactionRequest,
135
+ ],
136
+ })
137
+ }
@@ -1,5 +1,6 @@
1
1
  import { type ChainFormatters } from '../../types/chain.js'
2
2
  import type { Hash } from '../../types/misc.js'
3
+ import type { RpcTransaction } from '../../types/rpc.js'
3
4
  import { hexToBigInt } from '../../utils/encoding/fromHex.js'
4
5
  import { numberToHex } from '../../utils/encoding/toHex.js'
5
6
  import { defineBlock } from '../../utils/formatters/block.js'
@@ -12,13 +13,11 @@ import { defineTransactionRequest } from '../../utils/formatters/transactionRequ
12
13
  import type {
13
14
  CeloBlockOverrides,
14
15
  CeloRpcTransaction,
15
- CeloRpcTransactionOverrides,
16
16
  CeloRpcTransactionReceiptOverrides,
17
- CeloRpcTransactionRequestOverrides,
17
+ CeloRpcTransactionRequest,
18
18
  CeloTransaction,
19
- CeloTransactionOverrides,
20
19
  CeloTransactionReceiptOverrides,
21
- CeloTransactionRequestOverrides,
20
+ CeloTransactionRequest,
22
21
  } from './types.js'
23
22
 
24
23
  export const formattersCelo = {
@@ -34,7 +33,7 @@ export const formattersCelo = {
34
33
  const transactions = args.transactions?.map((transaction) => {
35
34
  if (typeof transaction === 'string') return transaction
36
35
  return {
37
- ...formatTransaction(transaction),
36
+ ...formatTransaction(transaction as RpcTransaction),
38
37
  feeCurrency: transaction.feeCurrency,
39
38
  gatewayFee: transaction.gatewayFee
40
39
  ? hexToBigInt(transaction.gatewayFee)
@@ -49,12 +48,12 @@ export const formattersCelo = {
49
48
  },
50
49
  }),
51
50
  transaction: /*#__PURE__*/ defineTransaction({
52
- format(args: CeloRpcTransactionOverrides): CeloTransactionOverrides {
51
+ format(args: CeloRpcTransaction): CeloTransaction {
53
52
  return {
54
53
  feeCurrency: args.feeCurrency,
55
54
  gatewayFee: args.gatewayFee ? hexToBigInt(args.gatewayFee) : null,
56
55
  gatewayFeeRecipient: args.gatewayFeeRecipient,
57
- }
56
+ } as CeloTransaction
58
57
  },
59
58
  }),
60
59
  transactionReceipt: /*#__PURE__*/ defineTransactionReceipt({
@@ -69,17 +68,17 @@ export const formattersCelo = {
69
68
  },
70
69
  }),
71
70
  transactionRequest: /*#__PURE__*/ defineTransactionRequest({
72
- format(
73
- args: CeloTransactionRequestOverrides,
74
- ): CeloRpcTransactionRequestOverrides {
75
- return {
71
+ format(args: CeloTransactionRequest): CeloRpcTransactionRequest {
72
+ const request = {
76
73
  feeCurrency: args.feeCurrency,
77
74
  gatewayFee:
78
75
  typeof args.gatewayFee !== 'undefined'
79
76
  ? numberToHex(args.gatewayFee)
80
77
  : undefined,
81
78
  gatewayFeeRecipient: args.gatewayFeeRecipient,
82
- }
79
+ } as CeloRpcTransactionRequest
80
+ if (args.type === 'cip42') request.type = '0x7c'
81
+ return request
83
82
  },
84
83
  }),
85
84
  } as const satisfies ChainFormatters
@@ -0,0 +1,123 @@
1
+ import { InvalidSerializedTransactionError } from '../../errors/transaction.js'
2
+ import type { Hex } from '../../types/misc.js'
3
+ import { isHex } from '../../utils/data/isHex.js'
4
+ import { sliceHex } from '../../utils/data/slice.js'
5
+ import { hexToBigInt, hexToNumber } from '../../utils/encoding/fromHex.js'
6
+ import type { RecursiveArray } from '../../utils/encoding/toRlp.js'
7
+ import type { GetSerializedTransactionType } from '../../utils/transaction/getSerializedTransactionType.js'
8
+ import {
9
+ type ParseTransactionReturnType,
10
+ parseAccessList,
11
+ parseTransaction,
12
+ toTransactionArray,
13
+ } from '../../utils/transaction/parseTransaction.js'
14
+ import { assertTransactionCIP42 } from './serializers.js'
15
+ import type {
16
+ CeloTransactionSerialized,
17
+ CeloTransactionType,
18
+ TransactionSerializableCIP42,
19
+ TransactionSerializedCIP42,
20
+ } from './types.js'
21
+
22
+ export type ParseTransactionCeloReturnType<
23
+ TSerialized extends CeloTransactionSerialized = CeloTransactionSerialized,
24
+ TType extends CeloTransactionType = GetSerializedTransactionType<TSerialized>,
25
+ > = TSerialized extends TransactionSerializedCIP42
26
+ ? TransactionSerializableCIP42
27
+ : ParseTransactionReturnType<TSerialized, TType>
28
+
29
+ export function parseTransactionCelo<
30
+ TSerialized extends CeloTransactionSerialized,
31
+ >(
32
+ serializedTransaction: TSerialized,
33
+ ): ParseTransactionCeloReturnType<TSerialized> {
34
+ const serializedType = sliceHex(serializedTransaction, 0, 1)
35
+
36
+ if (serializedType === '0x7c')
37
+ return parseTransactionCIP42(
38
+ serializedTransaction as TransactionSerializedCIP42,
39
+ ) as ParseTransactionCeloReturnType<TSerialized>
40
+
41
+ return parseTransaction(
42
+ serializedTransaction,
43
+ ) as ParseTransactionCeloReturnType<TSerialized>
44
+ }
45
+
46
+ function parseTransactionCIP42(
47
+ serializedTransaction: TransactionSerializedCIP42,
48
+ ): TransactionSerializableCIP42 {
49
+ const transactionArray = toTransactionArray(serializedTransaction)
50
+
51
+ const [
52
+ chainId,
53
+ nonce,
54
+ maxPriorityFeePerGas,
55
+ maxFeePerGas,
56
+ gas,
57
+ feeCurrency,
58
+ gatewayFeeRecipient,
59
+ gatewayFee,
60
+ to,
61
+ value,
62
+ data,
63
+ accessList,
64
+ v,
65
+ r,
66
+ s,
67
+ ] = transactionArray
68
+
69
+ if (transactionArray.length !== 15 && transactionArray.length !== 12) {
70
+ throw new InvalidSerializedTransactionError({
71
+ attributes: {
72
+ chainId,
73
+ nonce,
74
+ maxPriorityFeePerGas,
75
+ maxFeePerGas,
76
+ gas,
77
+ feeCurrency,
78
+ to,
79
+ gatewayFeeRecipient,
80
+ gatewayFee,
81
+ value,
82
+ data,
83
+ accessList,
84
+ ...(transactionArray.length > 12
85
+ ? {
86
+ v,
87
+ r,
88
+ s,
89
+ }
90
+ : {}),
91
+ },
92
+ serializedTransaction,
93
+ type: 'cip42',
94
+ })
95
+ }
96
+
97
+ const transaction: Partial<TransactionSerializableCIP42> = {
98
+ chainId: hexToNumber(chainId as Hex),
99
+ type: 'cip42',
100
+ }
101
+
102
+ if (isHex(to) && to !== '0x') transaction.to = to
103
+ if (isHex(gas) && gas !== '0x') transaction.gas = hexToBigInt(gas)
104
+ if (isHex(data) && data !== '0x') transaction.data = data
105
+ if (isHex(nonce) && nonce !== '0x') transaction.nonce = hexToNumber(nonce)
106
+ if (isHex(value) && value !== '0x') transaction.value = hexToBigInt(value)
107
+ if (isHex(feeCurrency) && feeCurrency !== '0x')
108
+ transaction.feeCurrency = feeCurrency
109
+ if (isHex(gatewayFeeRecipient) && gatewayFeeRecipient !== '0x')
110
+ transaction.gatewayFeeRecipient = gatewayFeeRecipient
111
+ if (isHex(gatewayFee) && gatewayFee !== '0x')
112
+ transaction.gatewayFee = hexToBigInt(gatewayFee)
113
+ if (isHex(maxFeePerGas) && maxFeePerGas !== '0x')
114
+ transaction.maxFeePerGas = hexToBigInt(maxFeePerGas)
115
+ if (isHex(maxPriorityFeePerGas) && maxPriorityFeePerGas !== '0x')
116
+ transaction.maxPriorityFeePerGas = hexToBigInt(maxPriorityFeePerGas)
117
+ if (accessList.length !== 0 && accessList !== '0x')
118
+ transaction.accessList = parseAccessList(accessList as RecursiveArray<Hex>)
119
+
120
+ assertTransactionCIP42(transaction as TransactionSerializableCIP42)
121
+
122
+ return transaction as TransactionSerializableCIP42
123
+ }