viem 0.0.1-alpha.2 → 0.0.1-alpha.21

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 (483) hide show
  1. package/chains/package.json +1 -1
  2. package/clients/package.json +1 -1
  3. package/dist/{rpc-655c0ba4.d.ts → chain-32f56cfa.d.ts} +78 -17
  4. package/dist/chain-f12cdc7f.d.ts +5 -0
  5. package/dist/chains.d.ts +1291 -74
  6. package/dist/chains.js +125 -129
  7. package/dist/chains.js.map +1 -0
  8. package/dist/chains.mjs +129 -0
  9. package/dist/chains.mjs.map +1 -0
  10. package/dist/{chunk-YQRTXQ2G.js → chunk-46BO7YAQ.mjs} +3 -15
  11. package/dist/chunk-46BO7YAQ.mjs.map +1 -0
  12. package/dist/chunk-57ZOFERP.mjs +256 -0
  13. package/dist/chunk-57ZOFERP.mjs.map +1 -0
  14. package/dist/{chunk-3EOU525X.js → chunk-62VTYU2V.mjs} +1207 -564
  15. package/dist/chunk-62VTYU2V.mjs.map +1 -0
  16. package/dist/{chunk-4HNVS7AM.js → chunk-DGO77E2H.mjs} +330 -539
  17. package/dist/chunk-DGO77E2H.mjs.map +1 -0
  18. package/dist/chunk-DSPMAIDO.mjs +176 -0
  19. package/dist/chunk-DSPMAIDO.mjs.map +1 -0
  20. package/dist/chunk-EC3NUIJE.js +176 -0
  21. package/dist/chunk-EC3NUIJE.js.map +1 -0
  22. package/dist/chunk-KEHGSYDO.js +259 -0
  23. package/dist/chunk-KEHGSYDO.js.map +1 -0
  24. package/dist/chunk-KZMJR27B.js +889 -0
  25. package/dist/chunk-KZMJR27B.js.map +1 -0
  26. package/dist/chunk-O2GYLJVD.js +2689 -0
  27. package/dist/chunk-O2GYLJVD.js.map +1 -0
  28. package/dist/chunk-W7BWWAC4.js +256 -0
  29. package/dist/chunk-W7BWWAC4.js.map +1 -0
  30. package/dist/clients/index.d.ts +8 -7
  31. package/dist/clients/index.js +24 -25
  32. package/dist/clients/index.js.map +1 -0
  33. package/dist/clients/index.mjs +24 -0
  34. package/dist/clients/index.mjs.map +1 -0
  35. package/dist/contract-9af4608b.d.ts +142 -0
  36. package/dist/createClient-5d316c7e.d.ts +62 -0
  37. package/dist/createPublicClient-9d2b42e1.d.ts +19 -0
  38. package/dist/createTestClient-79498dab.d.ts +34 -0
  39. package/dist/createWalletClient-f69a5230.d.ts +30 -0
  40. package/dist/{eip1193-8f7c22ce.d.ts → eip1193-6c485d63.d.ts} +13 -10
  41. package/dist/index.d.ts +424 -11
  42. package/dist/index.js +395 -292
  43. package/dist/index.js.map +1 -0
  44. package/dist/index.mjs +395 -0
  45. package/dist/index.mjs.map +1 -0
  46. package/dist/{parseGwei-fd7a0f7d.d.ts → parseGwei-4308ad80.d.ts} +82 -139
  47. package/dist/public.d.ts +11 -0
  48. package/dist/public.js +59 -0
  49. package/dist/public.js.map +1 -0
  50. package/dist/public.mjs +59 -0
  51. package/dist/public.mjs.map +1 -0
  52. package/dist/{rpc-3c0e3985.d.ts → rpc-26932bae.d.ts} +1 -38
  53. package/dist/sendTransaction-1c8290a9.d.ts +12 -0
  54. package/dist/stopImpersonatingAccount-7781842a.d.ts +156 -0
  55. package/dist/test.d.ts +6 -0
  56. package/dist/test.js +60 -0
  57. package/dist/test.js.map +1 -0
  58. package/dist/test.mjs +60 -0
  59. package/dist/test.mjs.map +1 -0
  60. package/dist/transactionRequest-341b6ed2.d.ts +15 -0
  61. package/dist/utils/index.d.ts +51 -8
  62. package/dist/utils/index.js +171 -156
  63. package/dist/utils/index.js.map +1 -0
  64. package/dist/utils/index.mjs +171 -0
  65. package/dist/utils/index.mjs.map +1 -0
  66. package/dist/wallet.d.ts +8 -0
  67. package/dist/wallet.js +24 -0
  68. package/dist/wallet.js.map +1 -0
  69. package/dist/wallet.mjs +24 -0
  70. package/dist/wallet.mjs.map +1 -0
  71. package/dist/watchAsset-afaad3c7.d.ts +38 -0
  72. package/dist/{watchAsset-04ab8db5.d.ts → watchPendingTransactions-3b722547.d.ts} +67 -217
  73. package/dist/{webSocket-c6e0d26f.d.ts → webSocket-b180e679.d.ts} +4 -9
  74. package/dist/window.d.ts +3 -2
  75. package/dist/window.js +1 -0
  76. package/dist/window.js.map +1 -0
  77. package/dist/window.mjs +1 -0
  78. package/dist/window.mjs.map +1 -0
  79. package/package.json +30 -66
  80. package/src/_test/abis.ts +1413 -0
  81. package/src/_test/bench.ts +15 -0
  82. package/src/_test/constants.ts +60 -0
  83. package/src/_test/globalSetup.ts +11 -0
  84. package/src/_test/index.ts +23 -0
  85. package/src/_test/setup.ts +8 -0
  86. package/src/_test/utils.ts +112 -0
  87. package/src/actions/index.test.ts +78 -0
  88. package/src/actions/index.ts +174 -0
  89. package/src/actions/public/call.bench.ts +48 -0
  90. package/src/actions/public/call.test.ts +99 -0
  91. package/src/actions/public/call.ts +97 -0
  92. package/src/actions/public/createBlockFilter.bench.ts +11 -0
  93. package/src/actions/public/createBlockFilter.test.ts +9 -0
  94. package/src/actions/public/createBlockFilter.ts +14 -0
  95. package/src/actions/public/createContractEventFilter.test.ts +119 -0
  96. package/src/actions/public/createContractEventFilter.ts +69 -0
  97. package/src/actions/public/createEventFilter.test.ts +245 -0
  98. package/src/actions/public/createEventFilter.ts +76 -0
  99. package/src/actions/public/createPendingTransactionFilter.bench.ts +11 -0
  100. package/src/actions/public/createPendingTransactionFilter.test.ts +9 -0
  101. package/src/actions/public/createPendingTransactionFilter.ts +14 -0
  102. package/src/actions/public/deployContract.test.ts +56 -0
  103. package/src/actions/public/deployContract.ts +38 -0
  104. package/src/actions/public/estimateGas.bench.ts +46 -0
  105. package/src/actions/public/estimateGas.test.ts +92 -0
  106. package/src/actions/public/estimateGas.ts +61 -0
  107. package/src/actions/public/getBalance.test.ts +83 -0
  108. package/src/actions/public/getBalance.ts +37 -0
  109. package/src/actions/public/getBlock.bench.ts +28 -0
  110. package/src/actions/public/getBlock.test.ts +575 -0
  111. package/src/actions/public/getBlock.ts +65 -0
  112. package/src/actions/public/getBlockNumber.bench.ts +28 -0
  113. package/src/actions/public/getBlockNumber.test.ts +27 -0
  114. package/src/actions/public/getBlockNumber.ts +32 -0
  115. package/src/actions/public/getBlockTransactionCount.bench.ts +15 -0
  116. package/src/actions/public/getBlockTransactionCount.test.ts +57 -0
  117. package/src/actions/public/getBlockTransactionCount.ts +52 -0
  118. package/src/actions/public/getBytecode.test.ts +27 -0
  119. package/src/actions/public/getBytecode.ts +32 -0
  120. package/src/actions/public/getChainId.bench.ts +15 -0
  121. package/src/actions/public/getChainId.test.ts +8 -0
  122. package/src/actions/public/getChainId.ts +7 -0
  123. package/src/actions/public/getFeeHistory.bench.ts +18 -0
  124. package/src/actions/public/getFeeHistory.test.ts +137 -0
  125. package/src/actions/public/getFeeHistory.ts +44 -0
  126. package/src/actions/public/getFilterChanges.bench.ts +13 -0
  127. package/src/actions/public/getFilterChanges.test.ts +189 -0
  128. package/src/actions/public/getFilterChanges.ts +23 -0
  129. package/src/actions/public/getFilterLogs.test.ts +104 -0
  130. package/src/actions/public/getFilterLogs.ts +20 -0
  131. package/src/actions/public/getGasPrice.bench.ts +28 -0
  132. package/src/actions/public/getGasPrice.test.ts +8 -0
  133. package/src/actions/public/getGasPrice.ts +15 -0
  134. package/src/actions/public/getLogs.test.ts +107 -0
  135. package/src/actions/public/getLogs.ts +84 -0
  136. package/src/actions/public/getStorageAt.test.ts +34 -0
  137. package/src/actions/public/getStorageAt.ts +32 -0
  138. package/src/actions/public/getTransaction.bench.ts +33 -0
  139. package/src/actions/public/getTransaction.test.ts +311 -0
  140. package/src/actions/public/getTransaction.ts +95 -0
  141. package/src/actions/public/getTransactionConfirmations.test.ts +69 -0
  142. package/src/actions/public/getTransactionConfirmations.ts +38 -0
  143. package/src/actions/public/getTransactionCount.test.ts +56 -0
  144. package/src/actions/public/getTransactionCount.ts +34 -0
  145. package/src/actions/public/getTransactionReceipt.bench.ts +33 -0
  146. package/src/actions/public/getTransactionReceipt.test.ts +179 -0
  147. package/src/actions/public/getTransactionReceipt.ts +34 -0
  148. package/src/actions/public/index.test.ts +43 -0
  149. package/src/actions/public/index.ts +149 -0
  150. package/src/actions/public/readContract.test.ts +128 -0
  151. package/src/actions/public/readContract.ts +87 -0
  152. package/src/actions/public/simulateContract.bench.ts +31 -0
  153. package/src/actions/public/simulateContract.test.ts +238 -0
  154. package/src/actions/public/simulateContract.ts +98 -0
  155. package/src/actions/public/uninstallFilter.bench.ts +13 -0
  156. package/src/actions/public/uninstallFilter.test.ts +65 -0
  157. package/src/actions/public/uninstallFilter.ts +17 -0
  158. package/src/actions/public/waitForTransactionReceipt.test.ts +322 -0
  159. package/src/actions/public/waitForTransactionReceipt.ts +170 -0
  160. package/src/actions/public/watchBlockNumber.test.ts +166 -0
  161. package/src/actions/public/watchBlockNumber.ts +79 -0
  162. package/src/actions/public/watchBlocks.test.ts +210 -0
  163. package/src/actions/public/watchBlocks.ts +114 -0
  164. package/src/actions/public/watchContractEvent.test.ts +301 -0
  165. package/src/actions/public/watchContractEvent.ts +108 -0
  166. package/src/actions/public/watchEvent.test.ts +190 -0
  167. package/src/actions/public/watchEvent.ts +95 -0
  168. package/src/actions/public/watchPendingTransactions.test.ts +116 -0
  169. package/src/actions/public/watchPendingTransactions.ts +74 -0
  170. package/src/actions/test/dropTransaction.test.ts +34 -0
  171. package/src/actions/test/dropTransaction.ts +17 -0
  172. package/src/actions/test/getAutomine.test.ts +14 -0
  173. package/src/actions/test/getAutomine.ts +11 -0
  174. package/src/actions/test/getTxpoolContent.test.ts +45 -0
  175. package/src/actions/test/getTxpoolContent.ts +7 -0
  176. package/src/actions/test/getTxpoolStatus.test.ts +41 -0
  177. package/src/actions/test/getTxpoolStatus.ts +12 -0
  178. package/src/actions/test/impersonateAccount.test.ts +28 -0
  179. package/src/actions/test/impersonateAccount.ts +17 -0
  180. package/src/actions/test/increaseTime.test.ts +18 -0
  181. package/src/actions/test/increaseTime.ts +17 -0
  182. package/src/actions/test/index.test.ts +38 -0
  183. package/src/actions/test/index.ts +77 -0
  184. package/src/actions/test/inspectTxpool.test.ts +50 -0
  185. package/src/actions/test/inspectTxpool.ts +7 -0
  186. package/src/actions/test/mine.test.ts +20 -0
  187. package/src/actions/test/mine.ts +16 -0
  188. package/src/actions/test/removeBlockTimestampInterval.test.ts +23 -0
  189. package/src/actions/test/removeBlockTimestampInterval.ts +7 -0
  190. package/src/actions/test/reset.test.ts +19 -0
  191. package/src/actions/test/reset.ts +18 -0
  192. package/src/actions/test/revert.test.ts +39 -0
  193. package/src/actions/test/revert.ts +14 -0
  194. package/src/actions/test/sendUnsignedTransaction.test.ts +52 -0
  195. package/src/actions/test/sendUnsignedTransaction.ts +19 -0
  196. package/src/actions/test/setAutomine.test.ts +14 -0
  197. package/src/actions/test/setAutomine.ts +8 -0
  198. package/src/actions/test/setBalance.test.ts +29 -0
  199. package/src/actions/test/setBalance.ts +20 -0
  200. package/src/actions/test/setBlockGasLimit.test.ts +21 -0
  201. package/src/actions/test/setBlockGasLimit.ts +17 -0
  202. package/src/actions/test/setBlockTimestampInterval.test.ts +23 -0
  203. package/src/actions/test/setBlockTimestampInterval.ts +16 -0
  204. package/src/actions/test/setCode.test.ts +26 -0
  205. package/src/actions/test/setCode.ts +19 -0
  206. package/src/actions/test/setCoinbase.test.ts +11 -0
  207. package/src/actions/test/setCoinbase.ts +17 -0
  208. package/src/actions/test/setIntervalMining.test.ts +30 -0
  209. package/src/actions/test/setIntervalMining.ts +16 -0
  210. package/src/actions/test/setLoggingEnabled.test.ts +10 -0
  211. package/src/actions/test/setLoggingEnabled.ts +8 -0
  212. package/src/actions/test/setMinGasPrice.test.ts +22 -0
  213. package/src/actions/test/setMinGasPrice.ts +17 -0
  214. package/src/actions/test/setNextBlockBaseFeePerGas.test.ts +23 -0
  215. package/src/actions/test/setNextBlockBaseFeePerGas.ts +17 -0
  216. package/src/actions/test/setNextBlockTimestamp.test.ts +19 -0
  217. package/src/actions/test/setNextBlockTimestamp.ts +17 -0
  218. package/src/actions/test/setNonce.test.ts +28 -0
  219. package/src/actions/test/setNonce.ts +20 -0
  220. package/src/actions/test/setRpcUrl.test.ts +9 -0
  221. package/src/actions/test/setRpcUrl.ts +8 -0
  222. package/src/actions/test/setStorageAt.test.ts +36 -0
  223. package/src/actions/test/setStorageAt.ts +26 -0
  224. package/src/actions/test/snapshot.test.ts +18 -0
  225. package/src/actions/test/snapshot.ts +7 -0
  226. package/src/actions/test/stopImpersonatingAccount.test.ts +31 -0
  227. package/src/actions/test/stopImpersonatingAccount.ts +17 -0
  228. package/src/actions/wallet/addChain.test.ts +14 -0
  229. package/src/actions/wallet/addChain.ts +21 -0
  230. package/src/actions/wallet/getAccounts.test.ts +22 -0
  231. package/src/actions/wallet/getAccounts.ts +7 -0
  232. package/src/actions/wallet/getPermissions.test.ts +24 -0
  233. package/src/actions/wallet/getPermissions.ts +9 -0
  234. package/src/actions/wallet/index.test.ts +20 -0
  235. package/src/actions/wallet/index.ts +30 -0
  236. package/src/actions/wallet/requestAccounts.test.ts +13 -0
  237. package/src/actions/wallet/requestAccounts.ts +7 -0
  238. package/src/actions/wallet/requestPermissions.test.ts +26 -0
  239. package/src/actions/wallet/requestPermissions.ts +19 -0
  240. package/src/actions/wallet/sendTransaction.bench.ts +57 -0
  241. package/src/actions/wallet/sendTransaction.test.ts +440 -0
  242. package/src/actions/wallet/sendTransaction.ts +77 -0
  243. package/src/actions/wallet/signMessage.test.ts +62 -0
  244. package/src/actions/wallet/signMessage.ts +35 -0
  245. package/src/actions/wallet/switchChain.test.ts +22 -0
  246. package/src/actions/wallet/switchChain.ts +19 -0
  247. package/src/actions/wallet/watchAsset.test.ts +41 -0
  248. package/src/actions/wallet/watchAsset.ts +16 -0
  249. package/src/actions/wallet/writeContract.test.ts +54 -0
  250. package/src/actions/wallet/writeContract.ts +56 -0
  251. package/src/chains.test.ts +1912 -0
  252. package/src/chains.ts +99 -0
  253. package/src/clients/createClient.test.ts +295 -0
  254. package/src/clients/createClient.ts +81 -0
  255. package/src/clients/createPublicClient.test.ts +165 -0
  256. package/src/clients/createPublicClient.ts +49 -0
  257. package/src/clients/createTestClient.test.ts +145 -0
  258. package/src/clients/createTestClient.ts +72 -0
  259. package/src/clients/createWalletClient.test.ts +121 -0
  260. package/src/clients/createWalletClient.ts +54 -0
  261. package/src/clients/index.test.ts +19 -0
  262. package/src/clients/index.ts +31 -0
  263. package/src/clients/transports/createTransport.test.ts +58 -0
  264. package/src/clients/transports/createTransport.ts +48 -0
  265. package/src/clients/transports/custom.test.ts +98 -0
  266. package/src/clients/transports/custom.ts +34 -0
  267. package/src/clients/transports/fallback.test.ts +223 -0
  268. package/src/clients/transports/fallback.ts +48 -0
  269. package/src/clients/transports/http.test.ts +110 -0
  270. package/src/clients/transports/http.ts +51 -0
  271. package/src/clients/transports/index.test.ts +15 -0
  272. package/src/clients/transports/index.ts +17 -0
  273. package/src/clients/transports/webSocket.test.ts +164 -0
  274. package/src/clients/transports/webSocket.ts +117 -0
  275. package/src/errors/abi.test.ts +83 -0
  276. package/src/errors/abi.ts +238 -0
  277. package/src/errors/address.test.ts +14 -0
  278. package/src/errors/address.ts +9 -0
  279. package/src/errors/base.test.ts +72 -0
  280. package/src/errors/base.ts +57 -0
  281. package/src/errors/block.test.ts +24 -0
  282. package/src/errors/block.ts +18 -0
  283. package/src/errors/contract.ts +125 -0
  284. package/src/errors/data.ts +20 -0
  285. package/src/errors/encoding.ts +60 -0
  286. package/src/errors/index.ts +77 -0
  287. package/src/errors/log.ts +8 -0
  288. package/src/errors/request.test.ts +331 -0
  289. package/src/errors/request.ts +163 -0
  290. package/src/errors/rpc.test.ts +88 -0
  291. package/src/errors/rpc.ts +113 -0
  292. package/src/errors/transaction.test.ts +83 -0
  293. package/src/errors/transaction.ts +54 -0
  294. package/src/errors/transport.test.ts +12 -0
  295. package/src/errors/transport.ts +12 -0
  296. package/src/index.test.ts +214 -0
  297. package/src/index.ts +373 -0
  298. package/src/public.ts +80 -0
  299. package/src/test.ts +51 -0
  300. package/src/types/block.ts +71 -0
  301. package/src/types/chain.ts +6 -0
  302. package/src/types/contract.ts +342 -0
  303. package/src/types/eip1193.ts +1041 -0
  304. package/src/types/fee.ts +47 -0
  305. package/src/types/filter.ts +8 -0
  306. package/src/types/formatter.ts +23 -0
  307. package/src/types/index.ts +78 -0
  308. package/src/types/log.ts +22 -0
  309. package/src/types/misc.ts +5 -0
  310. package/src/types/rpc.ts +35 -0
  311. package/src/types/transaction.ts +145 -0
  312. package/src/types/utils.ts +67 -0
  313. package/src/types/window.ts +9 -0
  314. package/src/utils/abi/decodeAbi.bench.ts +135 -0
  315. package/src/utils/abi/decodeAbi.test.ts +1615 -0
  316. package/src/utils/abi/decodeAbi.ts +296 -0
  317. package/src/utils/abi/decodeDeployData.test.ts +159 -0
  318. package/src/utils/abi/decodeDeployData.ts +41 -0
  319. package/src/utils/abi/decodeErrorResult.test.ts +130 -0
  320. package/src/utils/abi/decodeErrorResult.ts +28 -0
  321. package/src/utils/abi/decodeFunctionData.test.ts +140 -0
  322. package/src/utils/abi/decodeFunctionData.ts +29 -0
  323. package/src/utils/abi/decodeFunctionResult.test.ts +262 -0
  324. package/src/utils/abi/decodeFunctionResult.ts +53 -0
  325. package/src/utils/abi/encodeAbi.bench.ts +163 -0
  326. package/src/utils/abi/encodeAbi.test.ts +1448 -0
  327. package/src/utils/abi/encodeAbi.ts +264 -0
  328. package/src/utils/abi/encodeDeployData.test.ts +131 -0
  329. package/src/utils/abi/encodeDeployData.ts +37 -0
  330. package/src/utils/abi/encodeErrorResult.test.ts +197 -0
  331. package/src/utils/abi/encodeErrorResult.ts +40 -0
  332. package/src/utils/abi/encodeEventTopics.test.ts +348 -0
  333. package/src/utils/abi/encodeEventTopics.ts +69 -0
  334. package/src/utils/abi/encodeFunctionData.test.ts +140 -0
  335. package/src/utils/abi/encodeFunctionData.ts +38 -0
  336. package/src/utils/abi/encodeFunctionResult.test.ts +284 -0
  337. package/src/utils/abi/encodeFunctionResult.ts +39 -0
  338. package/src/utils/abi/formatAbiItem.test.ts +335 -0
  339. package/src/utils/abi/formatAbiItem.ts +41 -0
  340. package/src/utils/abi/formatAbiItemWithArgs.test.ts +291 -0
  341. package/src/utils/abi/formatAbiItemWithArgs.ts +27 -0
  342. package/src/utils/abi/getAbiItem.ts +5 -0
  343. package/src/utils/abi/index.test.ts +23 -0
  344. package/src/utils/abi/index.ts +38 -0
  345. package/src/utils/address/getAddress.bench.ts +22 -0
  346. package/src/utils/address/getAddress.test.ts +46 -0
  347. package/src/utils/address/getAddress.ts +28 -0
  348. package/src/utils/address/getContractAddress.bench.ts +20 -0
  349. package/src/utils/address/getContractAddress.test.ts +78 -0
  350. package/src/utils/address/getContractAddress.ts +59 -0
  351. package/src/utils/address/index.test.ts +17 -0
  352. package/src/utils/address/index.ts +16 -0
  353. package/src/utils/address/isAddress.test.ts +10 -0
  354. package/src/utils/address/isAddress.ts +10 -0
  355. package/src/utils/address/isAddressEqual.test.ts +55 -0
  356. package/src/utils/address/isAddressEqual.ts +6 -0
  357. package/src/utils/buildRequest.test.ts +365 -0
  358. package/src/utils/buildRequest.ts +45 -0
  359. package/src/utils/chain.test.ts +43 -0
  360. package/src/utils/chain.ts +8 -0
  361. package/src/utils/contract/extractFunctionParts.test.ts +109 -0
  362. package/src/utils/contract/extractFunctionParts.ts +27 -0
  363. package/src/utils/contract/getContractError.test.ts +97 -0
  364. package/src/utils/contract/getContractError.ts +66 -0
  365. package/src/utils/contract/index.test.ts +15 -0
  366. package/src/utils/contract/index.ts +8 -0
  367. package/src/utils/data/concat.test.ts +35 -0
  368. package/src/utils/data/concat.ts +34 -0
  369. package/src/utils/data/index.test.ts +23 -0
  370. package/src/utils/data/index.ts +13 -0
  371. package/src/utils/data/isBytes.test.ts +9 -0
  372. package/src/utils/data/isBytes.ts +7 -0
  373. package/src/utils/data/isHex.test.ts +13 -0
  374. package/src/utils/data/isHex.ts +5 -0
  375. package/src/utils/data/pad.bench.ts +39 -0
  376. package/src/utils/data/pad.test.ts +367 -0
  377. package/src/utils/data/pad.ts +53 -0
  378. package/src/utils/data/size.test.ts +18 -0
  379. package/src/utils/data/size.ts +13 -0
  380. package/src/utils/data/slice.test.ts +203 -0
  381. package/src/utils/data/slice.ts +60 -0
  382. package/src/utils/data/trim.bench.ts +50 -0
  383. package/src/utils/data/trim.test.ts +175 -0
  384. package/src/utils/data/trim.ts +33 -0
  385. package/src/utils/encoding/decodeBytes.bench.ts +40 -0
  386. package/src/utils/encoding/decodeBytes.test.ts +144 -0
  387. package/src/utils/encoding/decodeBytes.ts +63 -0
  388. package/src/utils/encoding/decodeHex.bench.ts +24 -0
  389. package/src/utils/encoding/decodeHex.test.ts +167 -0
  390. package/src/utils/encoding/decodeHex.ts +76 -0
  391. package/src/utils/encoding/decodeRlp.bench.ts +34 -0
  392. package/src/utils/encoding/decodeRlp.test.ts +350 -0
  393. package/src/utils/encoding/decodeRlp.ts +121 -0
  394. package/src/utils/encoding/encodeBytes.bench.ts +41 -0
  395. package/src/utils/encoding/encodeBytes.test.ts +676 -0
  396. package/src/utils/encoding/encodeBytes.ts +59 -0
  397. package/src/utils/encoding/encodeHex.bench.ts +60 -0
  398. package/src/utils/encoding/encodeHex.test.ts +232 -0
  399. package/src/utils/encoding/encodeHex.ts +104 -0
  400. package/src/utils/encoding/encodeRlp.bench.ts +54 -0
  401. package/src/utils/encoding/encodeRlp.test.ts +254 -0
  402. package/src/utils/encoding/encodeRlp.ts +48 -0
  403. package/src/utils/encoding/index.test.ts +32 -0
  404. package/src/utils/encoding/index.ts +36 -0
  405. package/src/utils/formatters/block.bench.ts +51 -0
  406. package/src/utils/formatters/block.test.ts +115 -0
  407. package/src/utils/formatters/block.ts +37 -0
  408. package/src/utils/formatters/extract.test.ts +14 -0
  409. package/src/utils/formatters/extract.ts +18 -0
  410. package/src/utils/formatters/feeHistory.test.ts +66 -0
  411. package/src/utils/formatters/feeHistory.ts +12 -0
  412. package/src/utils/formatters/format.test.ts +93 -0
  413. package/src/utils/formatters/format.ts +90 -0
  414. package/src/utils/formatters/index.test.ts +27 -0
  415. package/src/utils/formatters/index.ts +34 -0
  416. package/src/utils/formatters/log.test.ts +79 -0
  417. package/src/utils/formatters/log.ts +12 -0
  418. package/src/utils/formatters/transaction.test.ts +271 -0
  419. package/src/utils/formatters/transaction.ts +63 -0
  420. package/src/utils/formatters/transactionReceipt.bench.ts +73 -0
  421. package/src/utils/formatters/transactionReceipt.test.ts +151 -0
  422. package/src/utils/formatters/transactionReceipt.ts +63 -0
  423. package/src/utils/formatters/transactionRequest.bench.ts +29 -0
  424. package/src/utils/formatters/transactionRequest.test.ts +237 -0
  425. package/src/utils/formatters/transactionRequest.ts +56 -0
  426. package/src/utils/hash/getEventSignature.test.ts +61 -0
  427. package/src/utils/hash/getEventSignature.ts +4 -0
  428. package/src/utils/hash/getFunctionSignature.test.ts +22 -0
  429. package/src/utils/hash/getFunctionSignature.ts +5 -0
  430. package/src/utils/hash/hashFunction.test.ts +65 -0
  431. package/src/utils/hash/hashFunction.ts +12 -0
  432. package/src/utils/hash/index.test.ts +13 -0
  433. package/src/utils/hash/index.ts +5 -0
  434. package/src/utils/hash/keccak256.test.ts +59 -0
  435. package/src/utils/hash/keccak256.ts +23 -0
  436. package/src/utils/index.test.ts +110 -0
  437. package/src/utils/index.ts +137 -0
  438. package/src/utils/observe.test.ts +176 -0
  439. package/src/utils/observe.ts +66 -0
  440. package/src/utils/poll.test.ts +127 -0
  441. package/src/utils/poll.ts +42 -0
  442. package/src/utils/promise/index.test.ts +14 -0
  443. package/src/utils/promise/index.ts +3 -0
  444. package/src/utils/promise/withCache.test.ts +97 -0
  445. package/src/utils/promise/withCache.ts +73 -0
  446. package/src/utils/promise/withRetry.test.ts +231 -0
  447. package/src/utils/promise/withRetry.ts +61 -0
  448. package/src/utils/promise/withTimeout.test.ts +37 -0
  449. package/src/utils/promise/withTimeout.ts +39 -0
  450. package/src/utils/rpc.test.ts +992 -0
  451. package/src/utils/rpc.ts +292 -0
  452. package/src/utils/stringify.test.ts +13 -0
  453. package/src/utils/stringify.ts +5 -0
  454. package/src/utils/uid.ts +14 -0
  455. package/src/utils/unit/constants.test.ts +22 -0
  456. package/src/utils/unit/constants.ts +12 -0
  457. package/src/utils/unit/formatEther.test.ts +75 -0
  458. package/src/utils/unit/formatEther.ts +6 -0
  459. package/src/utils/unit/formatGwei.test.ts +32 -0
  460. package/src/utils/unit/formatGwei.ts +6 -0
  461. package/src/utils/unit/formatUnit.bench.ts +26 -0
  462. package/src/utils/unit/formatUnit.test.ts +40 -0
  463. package/src/utils/unit/formatUnit.ts +16 -0
  464. package/src/utils/unit/index.test.ts +28 -0
  465. package/src/utils/unit/index.ts +7 -0
  466. package/src/utils/unit/parseEther.test.ts +126 -0
  467. package/src/utils/unit/parseEther.ts +6 -0
  468. package/src/utils/unit/parseGwei.test.ts +50 -0
  469. package/src/utils/unit/parseGwei.ts +6 -0
  470. package/src/utils/unit/parseUnit.bench.ts +26 -0
  471. package/src/utils/unit/parseUnit.test.ts +54 -0
  472. package/src/utils/unit/parseUnit.ts +27 -0
  473. package/src/utils/wait.ts +3 -0
  474. package/src/wallet.ts +23 -0
  475. package/src/window.ts +1 -0
  476. package/utils/package.json +1 -1
  477. package/window/package.json +1 -1
  478. package/actions/package.json +0 -4
  479. package/dist/BaseError-7688f84e.d.ts +0 -18
  480. package/dist/actions/index.d.ts +0 -8
  481. package/dist/actions/index.js +0 -123
  482. package/dist/createWalletClient-915223f3.d.ts +0 -130
  483. package/dist/transactionRequest-ade896ac.d.ts +0 -44
@@ -0,0 +1,292 @@
1
+ import {
2
+ HttpRequestError,
3
+ RpcError,
4
+ TimeoutError,
5
+ WebSocketRequestError,
6
+ } from '../errors'
7
+ import { withRetry } from './promise'
8
+ import { withTimeout } from './promise/withTimeout'
9
+ import { stringify } from './stringify'
10
+
11
+ let id = 0
12
+
13
+ type SuccessResult<T> = {
14
+ method?: never
15
+ result: T
16
+ error?: never
17
+ }
18
+ type ErrorResult<T> = {
19
+ method?: never
20
+ result?: never
21
+ error: T
22
+ }
23
+ type Subscription<TResult, TError> = {
24
+ method: 'eth_subscription'
25
+ error?: never
26
+ result?: never
27
+ params: {
28
+ subscription: string
29
+ } & (
30
+ | {
31
+ result: TResult
32
+ error?: never
33
+ }
34
+ | {
35
+ result?: never
36
+ error: TError
37
+ }
38
+ )
39
+ }
40
+
41
+ type RpcRequest = { method: string; params?: any[] }
42
+
43
+ export type RpcResponse<TResult = any, TError = any> = {
44
+ jsonrpc: `${number}`
45
+ id: number
46
+ } & (
47
+ | SuccessResult<TResult>
48
+ | ErrorResult<TError>
49
+ | Subscription<TResult, TError>
50
+ )
51
+
52
+ ///////////////////////////////////////////////////
53
+ // HTTP
54
+
55
+ async function http(
56
+ url: string,
57
+ {
58
+ body,
59
+ retryDelay = 100,
60
+ retryCount = 2,
61
+ timeout = 0,
62
+ }: {
63
+ // The RPC request body.
64
+ body: RpcRequest
65
+ // The base delay (in ms) between retries.
66
+ retryDelay?: number
67
+ // The max number of times to retry.
68
+ retryCount?: number
69
+ // The timeout (in ms) for the request.
70
+ timeout?: number
71
+ },
72
+ ) {
73
+ const response = await withRetry(
74
+ () =>
75
+ withTimeout(
76
+ async ({ signal }) => {
77
+ const response = await fetch(url, {
78
+ headers: {
79
+ 'Content-Type': 'application/json',
80
+ },
81
+ method: 'POST',
82
+ body: stringify({ jsonrpc: '2.0', id: id++, ...body }),
83
+ signal: timeout > 0 ? signal : undefined,
84
+ })
85
+ return response
86
+ },
87
+ {
88
+ errorInstance: new TimeoutError({ body, url }),
89
+ timeout,
90
+ signal: true,
91
+ },
92
+ ),
93
+ {
94
+ delay: ({ count, data }) => {
95
+ // If we find a Retry-After header, let's retry after the given time.
96
+ const retryAfter = data?.headers.get('Retry-After')
97
+ if (retryAfter?.match(/\d/)) return parseInt(retryAfter) * 1000
98
+
99
+ // Otherwise, let's retry with an exponential backoff.
100
+ return ~~((Math.random() + 0.5) * (1 << count)) * retryDelay
101
+ },
102
+ retryCount,
103
+ shouldRetryOnResponse: async ({ data }) => {
104
+ if (data.status >= 500) return true
105
+ if ([408, 413, 429].includes(data.status)) return true
106
+ return false
107
+ },
108
+ },
109
+ )
110
+
111
+ let data
112
+ if (response.headers.get('Content-Type')?.startsWith('application/json')) {
113
+ data = await response.json()
114
+ } else {
115
+ data = await response.text()
116
+ }
117
+
118
+ if (!response.ok) {
119
+ throw new HttpRequestError({
120
+ body,
121
+ details: stringify(data.error) || response.statusText,
122
+ status: response.status,
123
+ url,
124
+ })
125
+ }
126
+
127
+ if (data.error) {
128
+ throw new RpcError({ body, error: data.error, url })
129
+ }
130
+ return data as RpcResponse
131
+ }
132
+
133
+ ///////////////////////////////////////////////////
134
+ // WebSocket
135
+
136
+ type Id = string | number
137
+ type CallbackFn = (message: any) => void
138
+ type CallbackMap = Map<Id, CallbackFn>
139
+
140
+ export type Socket = WebSocket & {
141
+ requests: CallbackMap
142
+ subscriptions: CallbackMap
143
+ }
144
+
145
+ const sockets = new Map<string, Socket>()
146
+
147
+ export async function getSocket(url_: string) {
148
+ const url = new URL(url_)
149
+ const urlKey = url.toString()
150
+
151
+ let socket = sockets.get(urlKey)
152
+
153
+ // If the socket already exists, return it.
154
+ if (socket) return socket
155
+
156
+ const webSocket = new WebSocket(url)
157
+
158
+ // Set up a cache for incoming "synchronous" requests.
159
+ const requests = new Map<Id, CallbackFn>()
160
+
161
+ // Set up a cache for subscriptions (eth_subscribe).
162
+ const subscriptions = new Map<Id, CallbackFn>()
163
+
164
+ const onMessage = ({ data }: { data: string }) => {
165
+ const message: RpcResponse = JSON.parse(data)
166
+ const isSubscription = message.method === 'eth_subscription'
167
+ const id = isSubscription ? message.params.subscription : message.id
168
+ const cache = isSubscription ? subscriptions : requests
169
+ const callback = cache.get(id)
170
+ if (callback) callback({ data })
171
+ if (!isSubscription) cache.delete(id)
172
+ }
173
+ const onClose = () => {
174
+ sockets.delete(urlKey)
175
+ webSocket.removeEventListener('close', onClose)
176
+ webSocket.removeEventListener('message', onMessage)
177
+ }
178
+
179
+ // Setup event listeners for RPC & subscription responses.
180
+ webSocket.addEventListener('close', onClose)
181
+ webSocket.addEventListener('message', onMessage)
182
+
183
+ // Wait for the socket to open.
184
+ if (webSocket.readyState === WebSocket.CONNECTING) {
185
+ await new Promise((resolve, reject) => {
186
+ if (!webSocket) return
187
+ webSocket.onopen = resolve
188
+ webSocket.onerror = reject
189
+ })
190
+ }
191
+
192
+ // Create a new socket instance.
193
+ socket = Object.assign(webSocket, {
194
+ requests,
195
+ subscriptions,
196
+ })
197
+ sockets.set(urlKey, socket)
198
+
199
+ return socket
200
+ }
201
+
202
+ function webSocket(
203
+ socket: Socket,
204
+ {
205
+ body,
206
+ onData,
207
+ onError,
208
+ }: {
209
+ // The RPC request body.
210
+ body: RpcRequest
211
+ // The callback to invoke when the request is successful.
212
+ onData?: (message: RpcResponse) => void
213
+ // The callback to invoke if the request errors.
214
+ onError?: (message: RpcResponse['error']) => void
215
+ },
216
+ ) {
217
+ if (
218
+ socket.readyState === socket.CLOSED ||
219
+ socket.readyState === socket.CLOSING
220
+ )
221
+ throw new WebSocketRequestError({
222
+ body,
223
+ url: socket.url,
224
+ details: 'Socket is closed.',
225
+ })
226
+
227
+ const id_ = id++
228
+
229
+ const callback = ({ data }: { data: any }) => {
230
+ const message: RpcResponse = JSON.parse(data)
231
+
232
+ if (typeof message.id === 'number' && id_ !== message.id) return
233
+
234
+ if (message.error) {
235
+ onError?.(new RpcError({ body, error: message.error, url: socket.url }))
236
+ } else {
237
+ onData?.(message)
238
+ }
239
+
240
+ // If we are subscribing to a topic, we want to set up a listener for incoming
241
+ // messages.
242
+ if (body.method === 'eth_subscribe' && typeof message.result === 'string') {
243
+ socket.subscriptions.set(message.result, callback)
244
+ }
245
+
246
+ // If we are unsubscribing from a topic, we want to remove the listener.
247
+ if (body.method === 'eth_unsubscribe') {
248
+ socket.subscriptions.delete(body.params?.[0])
249
+ }
250
+ }
251
+ socket.requests.set(id_, callback)
252
+
253
+ socket.send(JSON.stringify({ jsonrpc: '2.0', ...body, id: id_ }))
254
+
255
+ return socket
256
+ }
257
+
258
+ async function webSocketAsync(
259
+ socket: Socket,
260
+ {
261
+ body,
262
+ timeout = 0,
263
+ }: {
264
+ // The RPC request body.
265
+ body: RpcRequest
266
+ // The timeout (in ms) for the request.
267
+ timeout?: number
268
+ },
269
+ ) {
270
+ return withTimeout(
271
+ () =>
272
+ new Promise<RpcResponse>((onData, onError) =>
273
+ rpc.webSocket(socket, {
274
+ body,
275
+ onData,
276
+ onError,
277
+ }),
278
+ ),
279
+ {
280
+ errorInstance: new TimeoutError({ body, url: socket.url }),
281
+ timeout,
282
+ },
283
+ )
284
+ }
285
+
286
+ ///////////////////////////////////////////////////
287
+
288
+ export const rpc = {
289
+ http,
290
+ webSocket,
291
+ webSocketAsync,
292
+ }
@@ -0,0 +1,13 @@
1
+ import { expect, test } from 'vitest'
2
+ import { stringify } from './stringify'
3
+
4
+ test('default', () => {
5
+ expect(
6
+ stringify({
7
+ foo: 'bar',
8
+ baz: {
9
+ value: 69n,
10
+ },
11
+ }),
12
+ ).toEqual('{"foo":"bar","baz":{"value":"69"}}')
13
+ })
@@ -0,0 +1,5 @@
1
+ export function stringify(value: unknown) {
2
+ return JSON.stringify(value, (_, value) =>
3
+ typeof value === 'bigint' ? value.toString() : value,
4
+ )
5
+ }
@@ -0,0 +1,14 @@
1
+ let size = 256
2
+ let index = size
3
+ let buffer: string
4
+
5
+ export function uid(length = 11) {
6
+ if (!buffer || index + length > size * 2) {
7
+ buffer = ''
8
+ index = 0
9
+ for (let i = 0; i < size; i++) {
10
+ buffer += ((256 + Math.random() * 256) | 0).toString(16).substring(1)
11
+ }
12
+ }
13
+ return buffer.substring(index, index++ + length)
14
+ }
@@ -0,0 +1,22 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import * as constants from './constants'
4
+
5
+ test('exports constants', () => {
6
+ expect(constants).toMatchInlineSnapshot(`
7
+ {
8
+ "etherUnits": {
9
+ "gwei": 9,
10
+ "wei": 18,
11
+ },
12
+ "gweiUnits": {
13
+ "ether": -9,
14
+ "wei": 9,
15
+ },
16
+ "weiUnits": {
17
+ "ether": -18,
18
+ "gwei": -9,
19
+ },
20
+ }
21
+ `)
22
+ })
@@ -0,0 +1,12 @@
1
+ export const etherUnits = {
2
+ gwei: 9,
3
+ wei: 18,
4
+ }
5
+ export const gweiUnits = {
6
+ ether: -9,
7
+ wei: 9,
8
+ }
9
+ export const weiUnits = {
10
+ ether: -18,
11
+ gwei: -9,
12
+ }
@@ -0,0 +1,75 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import { formatEther } from './formatEther'
4
+
5
+ test('converts wei to ether', () => {
6
+ expect(formatEther(6942069420123456789123450000n)).toMatchInlineSnapshot(
7
+ '"6942069420.12345678912345"',
8
+ )
9
+ expect(formatEther(6942069420000000000000000000n)).toMatchInlineSnapshot(
10
+ '"6942069420"',
11
+ )
12
+ expect(formatEther(1000000000000000000n)).toMatchInlineSnapshot('"1"')
13
+ expect(formatEther(500000000000000000n)).toMatchInlineSnapshot('"0.5"')
14
+ expect(formatEther(100000000000000000n)).toMatchInlineSnapshot('"0.1"')
15
+ expect(formatEther(10000000n)).toMatchInlineSnapshot('"0.00000000001"')
16
+ expect(formatEther(1000000n)).toMatchInlineSnapshot('"0.000000000001"')
17
+ expect(formatEther(100000n)).toMatchInlineSnapshot('"0.0000000000001"')
18
+ expect(formatEther(10000n)).toMatchInlineSnapshot('"0.00000000000001"')
19
+ expect(formatEther(1000n)).toMatchInlineSnapshot('"0.000000000000001"')
20
+ expect(formatEther(100n)).toMatchInlineSnapshot('"0.0000000000000001"')
21
+ expect(formatEther(10n)).toMatchInlineSnapshot('"0.00000000000000001"')
22
+ expect(formatEther(1n)).toMatchInlineSnapshot('"0.000000000000000001"')
23
+ expect(formatEther(-6942069420123456789123450000n)).toMatchInlineSnapshot(
24
+ '"-6942069420.12345678912345"',
25
+ )
26
+ expect(formatEther(-6942069420000000000000000000n)).toMatchInlineSnapshot(
27
+ '"-6942069420"',
28
+ )
29
+ expect(formatEther(-1000000000000000000n)).toMatchInlineSnapshot('"-1"')
30
+ expect(formatEther(-500000000000000000n)).toMatchInlineSnapshot('"-0.5"')
31
+ expect(formatEther(-100000000000000000n)).toMatchInlineSnapshot('"-0.1"')
32
+ expect(formatEther(-10000000n)).toMatchInlineSnapshot('"-0.00000000001"')
33
+ expect(formatEther(-1000000n)).toMatchInlineSnapshot('"-0.000000000001"')
34
+ expect(formatEther(-100000n)).toMatchInlineSnapshot('"-0.0000000000001"')
35
+ expect(formatEther(-10000n)).toMatchInlineSnapshot('"-0.00000000000001"')
36
+ expect(formatEther(-1000n)).toMatchInlineSnapshot('"-0.000000000000001"')
37
+ expect(formatEther(-100n)).toMatchInlineSnapshot('"-0.0000000000000001"')
38
+ expect(formatEther(-10n)).toMatchInlineSnapshot('"-0.00000000000000001"')
39
+ expect(formatEther(-1n)).toMatchInlineSnapshot('"-0.000000000000000001"')
40
+ })
41
+
42
+ test('converts gwei to ether', () => {
43
+ expect(formatEther(69420123456700n, 'gwei')).toMatchInlineSnapshot(
44
+ '"69420.1234567"',
45
+ )
46
+ expect(formatEther(69420000000000n, 'gwei')).toMatchInlineSnapshot('"69420"')
47
+ expect(formatEther(1000000000n, 'gwei')).toMatchInlineSnapshot('"1"')
48
+ expect(formatEther(500000000n, 'gwei')).toMatchInlineSnapshot('"0.5"')
49
+ expect(formatEther(100000000n, 'gwei')).toMatchInlineSnapshot('"0.1"')
50
+ expect(formatEther(10000000n, 'gwei')).toMatchInlineSnapshot('"1000000"')
51
+ expect(formatEther(1000000n, 'gwei')).toMatchInlineSnapshot('"10000"')
52
+ expect(formatEther(100000n, 'gwei')).toMatchInlineSnapshot('"100"')
53
+ expect(formatEther(10000n, 'gwei')).toMatchInlineSnapshot('"1"')
54
+ expect(formatEther(1000n, 'gwei')).toMatchInlineSnapshot('"0.000001"')
55
+ expect(formatEther(100n, 'gwei')).toMatchInlineSnapshot('"0.0000001"')
56
+ expect(formatEther(10n, 'gwei')).toMatchInlineSnapshot('"0.00000001"')
57
+ expect(formatEther(1n, 'gwei')).toMatchInlineSnapshot('"0.000000001"')
58
+ expect(formatEther(-69420123456700n, 'gwei')).toMatchInlineSnapshot(
59
+ '"-69420.1234567"',
60
+ )
61
+ expect(formatEther(-69420000000000n, 'gwei')).toMatchInlineSnapshot(
62
+ '"-69420"',
63
+ )
64
+ expect(formatEther(-1000000000n, 'gwei')).toMatchInlineSnapshot('"-1"')
65
+ expect(formatEther(-500000000n, 'gwei')).toMatchInlineSnapshot('"-0.5"')
66
+ expect(formatEther(-100000000n, 'gwei')).toMatchInlineSnapshot('"-0.1"')
67
+ expect(formatEther(-10000000n, 'gwei')).toMatchInlineSnapshot('"-1000000"')
68
+ expect(formatEther(-1000000n, 'gwei')).toMatchInlineSnapshot('"-10000"')
69
+ expect(formatEther(-100000n, 'gwei')).toMatchInlineSnapshot('"-100"')
70
+ expect(formatEther(-10000n, 'gwei')).toMatchInlineSnapshot('"-1"')
71
+ expect(formatEther(-1000n, 'gwei')).toMatchInlineSnapshot('"-0.000001"')
72
+ expect(formatEther(-100n, 'gwei')).toMatchInlineSnapshot('"-0.0000001"')
73
+ expect(formatEther(-10n, 'gwei')).toMatchInlineSnapshot('"-0.00000001"')
74
+ expect(formatEther(-1n, 'gwei')).toMatchInlineSnapshot('"-0.000000001"')
75
+ })
@@ -0,0 +1,6 @@
1
+ import { etherUnits } from '../../utils'
2
+ import { formatUnit } from './formatUnit'
3
+
4
+ export function formatEther(wei: bigint, unit: 'wei' | 'gwei' = 'wei') {
5
+ return formatUnit(wei, etherUnits[unit])
6
+ }
@@ -0,0 +1,32 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import { formatGwei } from './formatGwei'
4
+
5
+ test('converts wei to gwei', () => {
6
+ expect(formatGwei(69420123456700n)).toMatchInlineSnapshot('"69420.1234567"')
7
+ expect(formatGwei(69420000000000n)).toMatchInlineSnapshot('"69420"')
8
+ expect(formatGwei(1000000000n)).toMatchInlineSnapshot('"1"')
9
+ expect(formatGwei(500000000n)).toMatchInlineSnapshot('"0.5"')
10
+ expect(formatGwei(100000000n)).toMatchInlineSnapshot('"0.1"')
11
+ expect(formatGwei(10000000n)).toMatchInlineSnapshot('"1000000"')
12
+ expect(formatGwei(1000000n)).toMatchInlineSnapshot('"10000"')
13
+ expect(formatGwei(100000n)).toMatchInlineSnapshot('"100"')
14
+ expect(formatGwei(10000n)).toMatchInlineSnapshot('"1"')
15
+ expect(formatGwei(1000n)).toMatchInlineSnapshot('"0.000001"')
16
+ expect(formatGwei(100n)).toMatchInlineSnapshot('"0.0000001"')
17
+ expect(formatGwei(10n)).toMatchInlineSnapshot('"0.00000001"')
18
+ expect(formatGwei(1n)).toMatchInlineSnapshot('"0.000000001"')
19
+ expect(formatGwei(-69420123456700n)).toMatchInlineSnapshot('"-69420.1234567"')
20
+ expect(formatGwei(-69420000000000n)).toMatchInlineSnapshot('"-69420"')
21
+ expect(formatGwei(-1000000000n)).toMatchInlineSnapshot('"-1"')
22
+ expect(formatGwei(-500000000n)).toMatchInlineSnapshot('"-0.5"')
23
+ expect(formatGwei(-100000000n)).toMatchInlineSnapshot('"-0.1"')
24
+ expect(formatGwei(-10000000n)).toMatchInlineSnapshot('"-1000000"')
25
+ expect(formatGwei(-1000000n)).toMatchInlineSnapshot('"-10000"')
26
+ expect(formatGwei(-100000n)).toMatchInlineSnapshot('"-100"')
27
+ expect(formatGwei(-10000n)).toMatchInlineSnapshot('"-1"')
28
+ expect(formatGwei(-1000n)).toMatchInlineSnapshot('"-0.000001"')
29
+ expect(formatGwei(-100n)).toMatchInlineSnapshot('"-0.0000001"')
30
+ expect(formatGwei(-10n)).toMatchInlineSnapshot('"-0.00000001"')
31
+ expect(formatGwei(-1n)).toMatchInlineSnapshot('"-0.000000001"')
32
+ })
@@ -0,0 +1,6 @@
1
+ import { gweiUnits } from '../../utils'
2
+ import { formatUnit } from './formatUnit'
3
+
4
+ export function formatGwei(wei: bigint, unit: 'wei' = 'wei') {
5
+ return formatUnit(wei, gweiUnits[unit])
6
+ }
@@ -0,0 +1,26 @@
1
+ import { bench, describe } from 'vitest'
2
+ import { utils } from 'ethers'
3
+ import Web3 from 'web3'
4
+ import { weiToEther } from 'essential-eth'
5
+
6
+ import { formatUnit } from './formatUnit'
7
+
8
+ const web3 = new Web3()
9
+
10
+ describe('Format Unit', () => {
11
+ bench('viem: `formatUnit`', () => {
12
+ formatUnit(40000000000000000000n, 18)
13
+ })
14
+
15
+ bench('ethers: `formatUnits`', () => {
16
+ utils.formatUnits(40000000000000000000n, 18)
17
+ })
18
+
19
+ bench('web3.js: `toWei`', () => {
20
+ web3.utils.toWei('40000000000000000000')
21
+ })
22
+
23
+ bench('essential-eth: `etherToWei`', () => {
24
+ weiToEther('40000000000000000000')
25
+ })
26
+ })
@@ -0,0 +1,40 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import { formatUnit } from './formatUnit'
4
+
5
+ test('converts value to number', () => {
6
+ expect(formatUnit(69n, 0)).toMatchInlineSnapshot('"69"')
7
+ expect(formatUnit(69n, 5)).toMatchInlineSnapshot('"0.00069"')
8
+ expect(formatUnit(690n, 1)).toMatchInlineSnapshot('"69"')
9
+ expect(formatUnit(1300000n, 5)).toMatchInlineSnapshot('"13"')
10
+ expect(formatUnit(4200000000000n, 10)).toMatchInlineSnapshot('"420"')
11
+ expect(formatUnit(20000000000n, 9)).toMatchInlineSnapshot('"20"')
12
+ expect(formatUnit(40000000000000000000n, 18)).toMatchInlineSnapshot('"40"')
13
+ expect(formatUnit(12345n, 4)).toMatchInlineSnapshot('"1.2345"')
14
+ expect(formatUnit(12345n, 4)).toMatchInlineSnapshot('"1.2345"')
15
+ expect(formatUnit(6942069420123456789123450000n, 18)).toMatchInlineSnapshot(
16
+ '"6942069420.12345678912345"',
17
+ )
18
+ expect(
19
+ formatUnit(
20
+ 694212312312306942012345444446789123450000000000000000000000000000000n,
21
+ 50,
22
+ ),
23
+ ).toMatchInlineSnapshot('"6942123123123069420.1234544444678912345"')
24
+ expect(formatUnit(-690n, 1)).toMatchInlineSnapshot('"-69"')
25
+ expect(formatUnit(-1300000n, 5)).toMatchInlineSnapshot('"-13"')
26
+ expect(formatUnit(-4200000000000n, 10)).toMatchInlineSnapshot('"-420"')
27
+ expect(formatUnit(-20000000000n, 9)).toMatchInlineSnapshot('"-20"')
28
+ expect(formatUnit(-40000000000000000000n, 18)).toMatchInlineSnapshot('"-40"')
29
+ expect(formatUnit(-12345n, 4)).toMatchInlineSnapshot('"-1.2345"')
30
+ expect(formatUnit(-12345n, 4)).toMatchInlineSnapshot('"-1.2345"')
31
+ expect(formatUnit(-6942069420123456789123450000n, 18)).toMatchInlineSnapshot(
32
+ '"-6942069420.12345678912345"',
33
+ )
34
+ expect(
35
+ formatUnit(
36
+ -694212312312306942012345444446789123450000000000000000000000000000000n,
37
+ 50,
38
+ ),
39
+ ).toMatchInlineSnapshot('"-6942123123123069420.1234544444678912345"')
40
+ })
@@ -0,0 +1,16 @@
1
+ export function formatUnit(value: bigint, decimals: number) {
2
+ let display = value.toString()
3
+
4
+ const negative = display.startsWith('-')
5
+ if (negative) display = display.slice(1)
6
+
7
+ let [integer, fraction] = [
8
+ display.slice(0, display.length - decimals),
9
+ display.slice(display.length - decimals),
10
+ ]
11
+ fraction = fraction.padStart(decimals, '0')
12
+ fraction = fraction.replace(/(0+)$/, '')
13
+ return `${negative ? '-' : ''}${integer || '0'}${
14
+ fraction ? `.${fraction}` : ''
15
+ }`
16
+ }
@@ -0,0 +1,28 @@
1
+ import { expect, test } from 'vitest'
2
+
3
+ import * as value from './index'
4
+
5
+ test('exports value utils', () => {
6
+ expect(value).toMatchInlineSnapshot(`
7
+ {
8
+ "etherUnits": {
9
+ "gwei": 9,
10
+ "wei": 18,
11
+ },
12
+ "formatEther": [Function],
13
+ "formatGwei": [Function],
14
+ "formatUnit": [Function],
15
+ "gweiUnits": {
16
+ "ether": -9,
17
+ "wei": 9,
18
+ },
19
+ "parseEther": [Function],
20
+ "parseGwei": [Function],
21
+ "parseUnit": [Function],
22
+ "weiUnits": {
23
+ "ether": -18,
24
+ "gwei": -9,
25
+ },
26
+ }
27
+ `)
28
+ })
@@ -0,0 +1,7 @@
1
+ export { etherUnits, gweiUnits, weiUnits } from './constants'
2
+ export { formatEther } from './formatEther'
3
+ export { formatGwei } from './formatGwei'
4
+ export { formatUnit } from './formatUnit'
5
+ export { parseUnit } from './parseUnit'
6
+ export { parseEther } from './parseEther'
7
+ export { parseGwei } from './parseGwei'