viem 0.0.1-alpha.15 → 0.0.1-alpha.17
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.
- package/dist/chains.d.ts +1 -1
- package/dist/chains.js +6 -5
- package/dist/chains.js.map +1 -0
- package/dist/chains.mjs +2 -1
- package/dist/chains.mjs.map +1 -0
- package/dist/{chunk-IMYI7Z6M.js → chunk-BL2ZZ7UN.js} +14 -13
- package/dist/chunk-BL2ZZ7UN.js.map +1 -0
- package/dist/{chunk-KGXH5DYI.js → chunk-EOY65E5E.js} +36 -12
- package/dist/chunk-EOY65E5E.js.map +1 -0
- package/dist/{chunk-HTYEJEWI.js → chunk-EVOCIEHK.js} +109 -52
- package/dist/chunk-EVOCIEHK.js.map +1 -0
- package/dist/{chunk-SX7GPOCZ.mjs → chunk-HF44N2P3.mjs} +2 -1
- package/dist/chunk-HF44N2P3.mjs.map +1 -0
- package/dist/{chunk-PHAG5KUF.mjs → chunk-JNAHFK7Z.mjs} +26 -2
- package/dist/chunk-JNAHFK7Z.mjs.map +1 -0
- package/dist/{chunk-EMQSYKNY.js → chunk-KCXMY4FZ.js} +12 -11
- package/dist/chunk-KCXMY4FZ.js.map +1 -0
- package/dist/{chunk-2HENAFQN.mjs → chunk-KQI5IRRL.mjs} +3 -1
- package/dist/chunk-KQI5IRRL.mjs.map +1 -0
- package/dist/{chunk-PPDHFNFM.mjs → chunk-KWHCOWNW.mjs} +63 -6
- package/dist/chunk-KWHCOWNW.mjs.map +1 -0
- package/dist/{chunk-NYXBQHNJ.mjs → chunk-WQFS6KDQ.mjs} +2 -1
- package/dist/chunk-WQFS6KDQ.mjs.map +1 -0
- package/dist/{chunk-QMLDI5JU.js → chunk-XEGTPY6M.js} +3 -1
- package/dist/chunk-XEGTPY6M.js.map +1 -0
- package/dist/clients/index.d.ts +7 -7
- package/dist/clients/index.js +4 -3
- package/dist/clients/index.js.map +1 -0
- package/dist/clients/index.mjs +3 -2
- package/dist/clients/index.mjs.map +1 -0
- package/dist/{createClient-cd948138.d.ts → createClient-aadeff37.d.ts} +1 -1
- package/dist/{createPublicClient-989a0556.d.ts → createPublicClient-88f35518.d.ts} +2 -2
- package/dist/{createTestClient-81507f58.d.ts → createTestClient-fbf66ec2.d.ts} +2 -2
- package/dist/{createWalletClient-43f801b9.d.ts → createWalletClient-b13dabd6.d.ts} +2 -2
- package/dist/{eip1193-4330b722.d.ts → eip1193-3a40c941.d.ts} +9 -6
- package/dist/index.d.ts +47 -16
- package/dist/index.js +11 -6
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +14 -9
- package/dist/index.mjs.map +1 -0
- package/dist/{parseGwei-f2d23de6.d.ts → parseGwei-e6f49667.d.ts} +2 -2
- package/dist/public.d.ts +9 -9
- package/dist/public.js +5 -4
- package/dist/public.js.map +1 -0
- package/dist/public.mjs +6 -5
- package/dist/public.mjs.map +1 -0
- package/dist/{rpc-b77c5aee.d.ts → rpc-858670f1.d.ts} +12 -1
- package/dist/{sendTransaction-7a9d241a.d.ts → sendTransaction-bd109cd4.d.ts} +3 -3
- package/dist/{stopImpersonatingAccount-8113150e.d.ts → stopImpersonatingAccount-6603ebdd.d.ts} +2 -2
- package/dist/test.d.ts +5 -5
- package/dist/test.js +4 -3
- package/dist/test.js.map +1 -0
- package/dist/test.mjs +3 -2
- package/dist/test.mjs.map +1 -0
- package/dist/{transactionReceipt-5d332aab.d.ts → transactionReceipt-86c1094c.d.ts} +30 -4
- package/dist/{transactionRequest-327eb7c2.d.ts → transactionRequest-8e970b0e.d.ts} +1 -1
- package/dist/utils/index.d.ts +5 -5
- package/dist/utils/index.js +3 -2
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +2 -1
- package/dist/utils/index.mjs.map +1 -0
- package/dist/wallet.d.ts +7 -7
- package/dist/wallet.js +4 -3
- package/dist/wallet.js.map +1 -0
- package/dist/wallet.mjs +3 -2
- package/dist/wallet.mjs.map +1 -0
- package/dist/{watchAsset-0088384c.d.ts → watchAsset-7ef25553.d.ts} +3 -3
- package/dist/{watchPendingTransactions-670a7ca3.d.ts → watchPendingTransactions-2563071e.d.ts} +26 -11
- package/dist/{webSocket-9a3b0b26.d.ts → webSocket-2a77cdb3.d.ts} +2 -2
- package/dist/window.d.ts +2 -2
- package/dist/window.js +1 -1
- package/dist/window.js.map +1 -0
- package/dist/window.mjs +1 -0
- package/dist/window.mjs.map +1 -0
- package/package.json +3 -1
- package/src/_test/abis.ts +1225 -0
- package/src/_test/bench.ts +15 -0
- package/src/_test/constants.ts +63 -0
- package/src/_test/globalSetup.ts +11 -0
- package/src/_test/index.ts +20 -0
- package/src/_test/setup.ts +8 -0
- package/src/_test/utils.ts +112 -0
- package/src/actions/index.test.ts +74 -0
- package/src/actions/index.ts +162 -0
- package/src/actions/public/call.bench.ts +48 -0
- package/src/actions/public/call.test.ts +99 -0
- package/src/actions/public/call.ts +96 -0
- package/src/actions/public/createBlockFilter.bench.ts +11 -0
- package/src/actions/public/createBlockFilter.test.ts +9 -0
- package/src/actions/public/createBlockFilter.ts +14 -0
- package/src/actions/public/createEventFilter.test.ts +245 -0
- package/src/actions/public/createEventFilter.ts +79 -0
- package/src/actions/public/createPendingTransactionFilter.bench.ts +11 -0
- package/src/actions/public/createPendingTransactionFilter.test.ts +9 -0
- package/src/actions/public/createPendingTransactionFilter.ts +14 -0
- package/src/actions/public/deployContract.test.ts +56 -0
- package/src/actions/public/deployContract.ts +39 -0
- package/src/actions/public/estimateGas.bench.ts +46 -0
- package/src/actions/public/estimateGas.test.ts +92 -0
- package/src/actions/public/estimateGas.ts +61 -0
- package/src/actions/public/getBalance.test.ts +83 -0
- package/src/actions/public/getBalance.ts +37 -0
- package/src/actions/public/getBlock.bench.ts +28 -0
- package/src/actions/public/getBlock.test.ts +575 -0
- package/src/actions/public/getBlock.ts +66 -0
- package/src/actions/public/getBlockNumber.bench.ts +28 -0
- package/src/actions/public/getBlockNumber.test.ts +27 -0
- package/src/actions/public/getBlockNumber.ts +32 -0
- package/src/actions/public/getBlockTransactionCount.bench.ts +15 -0
- package/src/actions/public/getBlockTransactionCount.test.ts +57 -0
- package/src/actions/public/getBlockTransactionCount.ts +53 -0
- package/src/actions/public/getBytecode.test.ts +27 -0
- package/src/actions/public/getBytecode.ts +32 -0
- package/src/actions/public/getChainId.bench.ts +15 -0
- package/src/actions/public/getChainId.test.ts +8 -0
- package/src/actions/public/getChainId.ts +7 -0
- package/src/actions/public/getFeeHistory.bench.ts +18 -0
- package/src/actions/public/getFeeHistory.test.ts +137 -0
- package/src/actions/public/getFeeHistory.ts +44 -0
- package/src/actions/public/getFilterChanges.bench.ts +13 -0
- package/src/actions/public/getFilterChanges.test.ts +187 -0
- package/src/actions/public/getFilterChanges.ts +23 -0
- package/src/actions/public/getFilterLogs.test.ts +102 -0
- package/src/actions/public/getFilterLogs.ts +20 -0
- package/src/actions/public/getGasPrice.bench.ts +28 -0
- package/src/actions/public/getGasPrice.test.ts +8 -0
- package/src/actions/public/getGasPrice.ts +15 -0
- package/src/actions/public/getLogs.test.ts +105 -0
- package/src/actions/public/getLogs.ts +83 -0
- package/src/actions/public/getTransaction.bench.ts +33 -0
- package/src/actions/public/getTransaction.test.ts +311 -0
- package/src/actions/public/getTransaction.ts +96 -0
- package/src/actions/public/getTransactionConfirmations.test.ts +69 -0
- package/src/actions/public/getTransactionConfirmations.ts +39 -0
- package/src/actions/public/getTransactionCount.test.ts +56 -0
- package/src/actions/public/getTransactionCount.ts +34 -0
- package/src/actions/public/getTransactionReceipt.bench.ts +33 -0
- package/src/actions/public/getTransactionReceipt.test.ts +217 -0
- package/src/actions/public/getTransactionReceipt.ts +35 -0
- package/src/actions/public/index.test.ts +37 -0
- package/src/actions/public/index.ts +123 -0
- package/src/actions/public/simulateContract.bench.ts +31 -0
- package/src/actions/public/simulateContract.test.ts +238 -0
- package/src/actions/public/simulateContract.ts +102 -0
- package/src/actions/public/uninstallFilter.bench.ts +13 -0
- package/src/actions/public/uninstallFilter.test.ts +65 -0
- package/src/actions/public/uninstallFilter.ts +17 -0
- package/src/actions/public/waitForTransactionReceipt.test.ts +322 -0
- package/src/actions/public/waitForTransactionReceipt.ts +171 -0
- package/src/actions/public/watchBlockNumber.test.ts +166 -0
- package/src/actions/public/watchBlockNumber.ts +79 -0
- package/src/actions/public/watchBlocks.test.ts +185 -0
- package/src/actions/public/watchBlocks.ts +95 -0
- package/src/actions/public/watchPendingTransactions.test.ts +116 -0
- package/src/actions/public/watchPendingTransactions.ts +73 -0
- package/src/actions/test/dropTransaction.test.ts +34 -0
- package/src/actions/test/dropTransaction.ts +17 -0
- package/src/actions/test/getAutomine.test.ts +14 -0
- package/src/actions/test/getAutomine.ts +11 -0
- package/src/actions/test/getTxpoolContent.test.ts +45 -0
- package/src/actions/test/getTxpoolContent.ts +7 -0
- package/src/actions/test/getTxpoolStatus.test.ts +41 -0
- package/src/actions/test/getTxpoolStatus.ts +12 -0
- package/src/actions/test/impersonateAccount.test.ts +28 -0
- package/src/actions/test/impersonateAccount.ts +17 -0
- package/src/actions/test/increaseTime.test.ts +18 -0
- package/src/actions/test/increaseTime.ts +17 -0
- package/src/actions/test/index.test.ts +38 -0
- package/src/actions/test/index.ts +77 -0
- package/src/actions/test/inspectTxpool.test.ts +50 -0
- package/src/actions/test/inspectTxpool.ts +7 -0
- package/src/actions/test/mine.test.ts +20 -0
- package/src/actions/test/mine.ts +16 -0
- package/src/actions/test/removeBlockTimestampInterval.test.ts +23 -0
- package/src/actions/test/removeBlockTimestampInterval.ts +7 -0
- package/src/actions/test/reset.test.ts +19 -0
- package/src/actions/test/reset.ts +18 -0
- package/src/actions/test/revert.test.ts +39 -0
- package/src/actions/test/revert.ts +14 -0
- package/src/actions/test/sendUnsignedTransaction.test.ts +52 -0
- package/src/actions/test/sendUnsignedTransaction.ts +19 -0
- package/src/actions/test/setAutomine.test.ts +14 -0
- package/src/actions/test/setAutomine.ts +8 -0
- package/src/actions/test/setBalance.test.ts +29 -0
- package/src/actions/test/setBalance.ts +20 -0
- package/src/actions/test/setBlockGasLimit.test.ts +21 -0
- package/src/actions/test/setBlockGasLimit.ts +17 -0
- package/src/actions/test/setBlockTimestampInterval.test.ts +23 -0
- package/src/actions/test/setBlockTimestampInterval.ts +16 -0
- package/src/actions/test/setCode.test.ts +26 -0
- package/src/actions/test/setCode.ts +19 -0
- package/src/actions/test/setCoinbase.test.ts +11 -0
- package/src/actions/test/setCoinbase.ts +17 -0
- package/src/actions/test/setIntervalMining.test.ts +30 -0
- package/src/actions/test/setIntervalMining.ts +16 -0
- package/src/actions/test/setLoggingEnabled.test.ts +10 -0
- package/src/actions/test/setLoggingEnabled.ts +8 -0
- package/src/actions/test/setMinGasPrice.test.ts +22 -0
- package/src/actions/test/setMinGasPrice.ts +17 -0
- package/src/actions/test/setNextBlockBaseFeePerGas.test.ts +23 -0
- package/src/actions/test/setNextBlockBaseFeePerGas.ts +17 -0
- package/src/actions/test/setNextBlockTimestamp.test.ts +19 -0
- package/src/actions/test/setNextBlockTimestamp.ts +17 -0
- package/src/actions/test/setNonce.test.ts +28 -0
- package/src/actions/test/setNonce.ts +20 -0
- package/src/actions/test/setRpcUrl.test.ts +9 -0
- package/src/actions/test/setRpcUrl.ts +8 -0
- package/src/actions/test/setStorageAt.test.ts +36 -0
- package/src/actions/test/setStorageAt.ts +26 -0
- package/src/actions/test/snapshot.test.ts +18 -0
- package/src/actions/test/snapshot.ts +7 -0
- package/src/actions/test/stopImpersonatingAccount.test.ts +31 -0
- package/src/actions/test/stopImpersonatingAccount.ts +17 -0
- package/src/actions/wallet/addChain.test.ts +14 -0
- package/src/actions/wallet/addChain.ts +21 -0
- package/src/actions/wallet/getAccounts.test.ts +22 -0
- package/src/actions/wallet/getAccounts.ts +7 -0
- package/src/actions/wallet/getPermissions.test.ts +24 -0
- package/src/actions/wallet/getPermissions.ts +9 -0
- package/src/actions/wallet/index.test.ts +20 -0
- package/src/actions/wallet/index.ts +30 -0
- package/src/actions/wallet/requestAccounts.test.ts +13 -0
- package/src/actions/wallet/requestAccounts.ts +7 -0
- package/src/actions/wallet/requestPermissions.test.ts +26 -0
- package/src/actions/wallet/requestPermissions.ts +19 -0
- package/src/actions/wallet/sendTransaction.bench.ts +57 -0
- package/src/actions/wallet/sendTransaction.test.ts +440 -0
- package/src/actions/wallet/sendTransaction.ts +76 -0
- package/src/actions/wallet/signMessage.test.ts +62 -0
- package/src/actions/wallet/signMessage.ts +35 -0
- package/src/actions/wallet/switchChain.test.ts +22 -0
- package/src/actions/wallet/switchChain.ts +19 -0
- package/src/actions/wallet/watchAsset.test.ts +41 -0
- package/src/actions/wallet/watchAsset.ts +16 -0
- package/src/actions/wallet/writeContract.test.ts +54 -0
- package/src/actions/wallet/writeContract.ts +61 -0
- package/src/chains.test.ts +1134 -0
- package/src/chains.ts +167 -0
- package/src/clients/createClient.test.ts +295 -0
- package/src/clients/createClient.ts +81 -0
- package/src/clients/createPublicClient.test.ts +165 -0
- package/src/clients/createPublicClient.ts +49 -0
- package/src/clients/createTestClient.test.ts +145 -0
- package/src/clients/createTestClient.ts +72 -0
- package/src/clients/createWalletClient.test.ts +121 -0
- package/src/clients/createWalletClient.ts +54 -0
- package/src/clients/index.test.ts +19 -0
- package/src/clients/index.ts +31 -0
- package/src/clients/transports/createTransport.test.ts +58 -0
- package/src/clients/transports/createTransport.ts +48 -0
- package/src/clients/transports/custom.test.ts +98 -0
- package/src/clients/transports/custom.ts +34 -0
- package/src/clients/transports/fallback.test.ts +223 -0
- package/src/clients/transports/fallback.ts +48 -0
- package/src/clients/transports/http.test.ts +110 -0
- package/src/clients/transports/http.ts +51 -0
- package/src/clients/transports/index.test.ts +15 -0
- package/src/clients/transports/index.ts +17 -0
- package/src/clients/transports/webSocket.test.ts +164 -0
- package/src/clients/transports/webSocket.ts +117 -0
- package/src/constants.test.ts +27 -0
- package/src/constants.ts +18 -0
- package/src/errors/abi.test.ts +83 -0
- package/src/errors/abi.ts +238 -0
- package/src/errors/address.test.ts +14 -0
- package/src/errors/address.ts +9 -0
- package/src/errors/base.test.ts +72 -0
- package/src/errors/base.ts +57 -0
- package/src/errors/block.test.ts +24 -0
- package/src/errors/block.ts +18 -0
- package/src/errors/contract.ts +125 -0
- package/src/errors/data.ts +20 -0
- package/src/errors/encoding.ts +60 -0
- package/src/errors/index.ts +77 -0
- package/src/errors/log.ts +8 -0
- package/src/errors/request.test.ts +331 -0
- package/src/errors/request.ts +163 -0
- package/src/errors/rpc.test.ts +88 -0
- package/src/errors/rpc.ts +113 -0
- package/src/errors/transaction.test.ts +83 -0
- package/src/errors/transaction.ts +54 -0
- package/src/errors/transport.test.ts +12 -0
- package/src/errors/transport.ts +12 -0
- package/src/index.test.ts +210 -0
- package/src/index.ts +359 -0
- package/src/public.ts +80 -0
- package/src/test.ts +51 -0
- package/src/types/block.ts +71 -0
- package/src/types/contract.ts +327 -0
- package/src/types/eip1193.ts +1041 -0
- package/src/types/fee.ts +47 -0
- package/src/types/filter.ts +8 -0
- package/src/types/index.ts +72 -0
- package/src/types/log.ts +22 -0
- package/src/types/misc.ts +5 -0
- package/src/types/rpc.ts +35 -0
- package/src/types/transaction.ts +145 -0
- package/src/types/utils.ts +67 -0
- package/src/types/window.ts +9 -0
- package/src/utils/abi/decodeAbi.bench.ts +135 -0
- package/src/utils/abi/decodeAbi.test.ts +1615 -0
- package/src/utils/abi/decodeAbi.ts +296 -0
- package/src/utils/abi/decodeDeployData.test.ts +159 -0
- package/src/utils/abi/decodeDeployData.ts +41 -0
- package/src/utils/abi/decodeErrorResult.test.ts +130 -0
- package/src/utils/abi/decodeErrorResult.ts +28 -0
- package/src/utils/abi/decodeFunctionData.test.ts +140 -0
- package/src/utils/abi/decodeFunctionData.ts +29 -0
- package/src/utils/abi/decodeFunctionResult.test.ts +262 -0
- package/src/utils/abi/decodeFunctionResult.ts +53 -0
- package/src/utils/abi/encodeAbi.bench.ts +163 -0
- package/src/utils/abi/encodeAbi.test.ts +1414 -0
- package/src/utils/abi/encodeAbi.ts +258 -0
- package/src/utils/abi/encodeDeployData.test.ts +131 -0
- package/src/utils/abi/encodeDeployData.ts +37 -0
- package/src/utils/abi/encodeErrorResult.test.ts +197 -0
- package/src/utils/abi/encodeErrorResult.ts +40 -0
- package/src/utils/abi/encodeEventTopics.test.ts +348 -0
- package/src/utils/abi/encodeEventTopics.ts +69 -0
- package/src/utils/abi/encodeFunctionData.test.ts +140 -0
- package/src/utils/abi/encodeFunctionData.ts +38 -0
- package/src/utils/abi/encodeFunctionResult.test.ts +284 -0
- package/src/utils/abi/encodeFunctionResult.ts +39 -0
- package/src/utils/abi/formatAbiItemWithArgs.test.ts +291 -0
- package/src/utils/abi/formatAbiItemWithArgs.ts +27 -0
- package/src/utils/abi/formatAbiItemWithParams.test.ts +335 -0
- package/src/utils/abi/formatAbiItemWithParams.ts +41 -0
- package/src/utils/abi/getAbiItem.ts +5 -0
- package/src/utils/abi/index.test.ts +23 -0
- package/src/utils/abi/index.ts +38 -0
- package/src/utils/address/getAddress.bench.ts +22 -0
- package/src/utils/address/getAddress.test.ts +46 -0
- package/src/utils/address/getAddress.ts +28 -0
- package/src/utils/address/getContractAddress.bench.ts +20 -0
- package/src/utils/address/getContractAddress.test.ts +78 -0
- package/src/utils/address/getContractAddress.ts +59 -0
- package/src/utils/address/index.test.ts +17 -0
- package/src/utils/address/index.ts +16 -0
- package/src/utils/address/isAddress.test.ts +10 -0
- package/src/utils/address/isAddress.ts +10 -0
- package/src/utils/address/isAddressEqual.test.ts +55 -0
- package/src/utils/address/isAddressEqual.ts +6 -0
- package/src/utils/buildRequest.test.ts +365 -0
- package/src/utils/buildRequest.ts +45 -0
- package/src/utils/contract/extractFunctionParts.test.ts +109 -0
- package/src/utils/contract/extractFunctionParts.ts +27 -0
- package/src/utils/contract/getContractError.test.ts +97 -0
- package/src/utils/contract/getContractError.ts +70 -0
- package/src/utils/contract/index.test.ts +15 -0
- package/src/utils/contract/index.ts +8 -0
- package/src/utils/data/concat.test.ts +35 -0
- package/src/utils/data/concat.ts +34 -0
- package/src/utils/data/index.test.ts +23 -0
- package/src/utils/data/index.ts +13 -0
- package/src/utils/data/isBytes.test.ts +9 -0
- package/src/utils/data/isBytes.ts +7 -0
- package/src/utils/data/isHex.test.ts +13 -0
- package/src/utils/data/isHex.ts +5 -0
- package/src/utils/data/pad.bench.ts +39 -0
- package/src/utils/data/pad.test.ts +367 -0
- package/src/utils/data/pad.ts +53 -0
- package/src/utils/data/size.test.ts +18 -0
- package/src/utils/data/size.ts +13 -0
- package/src/utils/data/slice.test.ts +203 -0
- package/src/utils/data/slice.ts +60 -0
- package/src/utils/data/trim.bench.ts +50 -0
- package/src/utils/data/trim.test.ts +175 -0
- package/src/utils/data/trim.ts +33 -0
- package/src/utils/encoding/decodeBytes.bench.ts +40 -0
- package/src/utils/encoding/decodeBytes.test.ts +144 -0
- package/src/utils/encoding/decodeBytes.ts +63 -0
- package/src/utils/encoding/decodeHex.bench.ts +24 -0
- package/src/utils/encoding/decodeHex.test.ts +167 -0
- package/src/utils/encoding/decodeHex.ts +76 -0
- package/src/utils/encoding/decodeRlp.bench.ts +34 -0
- package/src/utils/encoding/decodeRlp.test.ts +350 -0
- package/src/utils/encoding/decodeRlp.ts +121 -0
- package/src/utils/encoding/encodeBytes.bench.ts +41 -0
- package/src/utils/encoding/encodeBytes.test.ts +676 -0
- package/src/utils/encoding/encodeBytes.ts +59 -0
- package/src/utils/encoding/encodeHex.bench.ts +60 -0
- package/src/utils/encoding/encodeHex.test.ts +232 -0
- package/src/utils/encoding/encodeHex.ts +104 -0
- package/src/utils/encoding/encodeRlp.bench.ts +54 -0
- package/src/utils/encoding/encodeRlp.test.ts +254 -0
- package/src/utils/encoding/encodeRlp.ts +48 -0
- package/src/utils/encoding/index.test.ts +32 -0
- package/src/utils/encoding/index.ts +36 -0
- package/src/utils/formatters/block.bench.ts +51 -0
- package/src/utils/formatters/block.test.ts +115 -0
- package/src/utils/formatters/block.ts +36 -0
- package/src/utils/formatters/extract.test.ts +14 -0
- package/src/utils/formatters/extract.ts +18 -0
- package/src/utils/formatters/feeHistory.test.ts +66 -0
- package/src/utils/formatters/feeHistory.ts +12 -0
- package/src/utils/formatters/format.test.ts +93 -0
- package/src/utils/formatters/format.ts +53 -0
- package/src/utils/formatters/index.test.ts +17 -0
- package/src/utils/formatters/index.ts +24 -0
- package/src/utils/formatters/log.test.ts +79 -0
- package/src/utils/formatters/log.ts +12 -0
- package/src/utils/formatters/transaction.test.ts +271 -0
- package/src/utils/formatters/transaction.ts +51 -0
- package/src/utils/formatters/transactionReceipt.bench.ts +73 -0
- package/src/utils/formatters/transactionReceipt.test.ts +151 -0
- package/src/utils/formatters/transactionReceipt.ts +54 -0
- package/src/utils/formatters/transactionRequest.bench.ts +29 -0
- package/src/utils/formatters/transactionRequest.test.ts +237 -0
- package/src/utils/formatters/transactionRequest.ts +47 -0
- package/src/utils/hash/getEventSignature.test.ts +61 -0
- package/src/utils/hash/getEventSignature.ts +4 -0
- package/src/utils/hash/getFunctionSignature.test.ts +22 -0
- package/src/utils/hash/getFunctionSignature.ts +5 -0
- package/src/utils/hash/hashFunction.test.ts +65 -0
- package/src/utils/hash/hashFunction.ts +12 -0
- package/src/utils/hash/index.test.ts +13 -0
- package/src/utils/hash/index.ts +5 -0
- package/src/utils/hash/keccak256.test.ts +59 -0
- package/src/utils/hash/keccak256.ts +23 -0
- package/src/utils/index.test.ts +87 -0
- package/src/utils/index.ts +126 -0
- package/src/utils/observe.test.ts +176 -0
- package/src/utils/observe.ts +66 -0
- package/src/utils/poll.test.ts +127 -0
- package/src/utils/poll.ts +42 -0
- package/src/utils/promise/index.test.ts +14 -0
- package/src/utils/promise/index.ts +3 -0
- package/src/utils/promise/withCache.test.ts +97 -0
- package/src/utils/promise/withCache.ts +73 -0
- package/src/utils/promise/withRetry.test.ts +231 -0
- package/src/utils/promise/withRetry.ts +61 -0
- package/src/utils/promise/withTimeout.test.ts +37 -0
- package/src/utils/promise/withTimeout.ts +39 -0
- package/src/utils/rpc.test.ts +992 -0
- package/src/utils/rpc.ts +292 -0
- package/src/utils/stringify.test.ts +13 -0
- package/src/utils/stringify.ts +5 -0
- package/src/utils/uid.ts +14 -0
- package/src/utils/unit/formatEther.test.ts +75 -0
- package/src/utils/unit/formatEther.ts +6 -0
- package/src/utils/unit/formatGwei.test.ts +32 -0
- package/src/utils/unit/formatGwei.ts +6 -0
- package/src/utils/unit/formatUnit.bench.ts +26 -0
- package/src/utils/unit/formatUnit.test.ts +40 -0
- package/src/utils/unit/formatUnit.ts +16 -0
- package/src/utils/unit/index.test.ts +16 -0
- package/src/utils/unit/index.ts +6 -0
- package/src/utils/unit/parseEther.test.ts +126 -0
- package/src/utils/unit/parseEther.ts +6 -0
- package/src/utils/unit/parseGwei.test.ts +50 -0
- package/src/utils/unit/parseGwei.ts +6 -0
- package/src/utils/unit/parseUnit.bench.ts +26 -0
- package/src/utils/unit/parseUnit.test.ts +54 -0
- package/src/utils/unit/parseUnit.ts +27 -0
- package/src/utils/wait.ts +3 -0
- package/src/wallet.ts +23 -0
- package/src/window.ts +1 -0
@@ -0,0 +1,102 @@
|
|
1
|
+
import { Abi } from 'abitype'
|
2
|
+
|
3
|
+
import type { Chain, Formatter } from '../../chains'
|
4
|
+
import type { PublicClient } from '../../clients'
|
5
|
+
import type {
|
6
|
+
Address,
|
7
|
+
ExtractArgsFromAbi,
|
8
|
+
ExtractResultFromAbi,
|
9
|
+
ExtractFunctionNameFromAbi,
|
10
|
+
GetValue,
|
11
|
+
} from '../../types'
|
12
|
+
import {
|
13
|
+
EncodeFunctionDataArgs,
|
14
|
+
decodeFunctionResult,
|
15
|
+
encodeFunctionData,
|
16
|
+
getContractError,
|
17
|
+
} from '../../utils'
|
18
|
+
import { WriteContractArgs } from '../wallet'
|
19
|
+
import { call, CallArgs, FormattedCall } from './call'
|
20
|
+
|
21
|
+
export type FormattedSimulateContract<
|
22
|
+
TFormatter extends Formatter | undefined = Formatter,
|
23
|
+
> = FormattedCall<TFormatter>
|
24
|
+
|
25
|
+
export type SimulateContractArgs<
|
26
|
+
TChain extends Chain = Chain,
|
27
|
+
TAbi extends Abi | readonly unknown[] = Abi,
|
28
|
+
TFunctionName extends string = any,
|
29
|
+
> = Omit<CallArgs<TChain>, 'to' | 'data' | 'value'> & {
|
30
|
+
address: Address
|
31
|
+
abi: TAbi
|
32
|
+
functionName: ExtractFunctionNameFromAbi<
|
33
|
+
TAbi,
|
34
|
+
TFunctionName,
|
35
|
+
'payable' | 'nonpayable'
|
36
|
+
>
|
37
|
+
value?: GetValue<TAbi, TFunctionName, CallArgs<TChain>['value']>
|
38
|
+
} & ExtractArgsFromAbi<TAbi, TFunctionName>
|
39
|
+
|
40
|
+
export type SimulateContractResponse<
|
41
|
+
TChain extends Chain = Chain,
|
42
|
+
TAbi extends Abi | readonly unknown[] = Abi,
|
43
|
+
TFunctionName extends string = string,
|
44
|
+
> = {
|
45
|
+
result: ExtractResultFromAbi<TAbi, TFunctionName>
|
46
|
+
request: WriteContractArgs<TChain, TAbi, TFunctionName> & {
|
47
|
+
address: Address
|
48
|
+
abi: TAbi
|
49
|
+
functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName>
|
50
|
+
} & ExtractArgsFromAbi<TAbi, TFunctionName>
|
51
|
+
}
|
52
|
+
|
53
|
+
export async function simulateContract<
|
54
|
+
TChain extends Chain,
|
55
|
+
TAbi extends Abi = Abi,
|
56
|
+
TFunctionName extends string = any,
|
57
|
+
>(
|
58
|
+
client: PublicClient,
|
59
|
+
{
|
60
|
+
abi,
|
61
|
+
address,
|
62
|
+
args,
|
63
|
+
functionName,
|
64
|
+
...callRequest
|
65
|
+
}: SimulateContractArgs<TChain, TAbi, TFunctionName>,
|
66
|
+
): Promise<SimulateContractResponse<TChain, TAbi, TFunctionName>> {
|
67
|
+
const calldata = encodeFunctionData({
|
68
|
+
abi,
|
69
|
+
args,
|
70
|
+
functionName,
|
71
|
+
} as unknown as EncodeFunctionDataArgs<TAbi, TFunctionName>)
|
72
|
+
try {
|
73
|
+
const { data } = await call(client, {
|
74
|
+
data: calldata,
|
75
|
+
to: address,
|
76
|
+
...callRequest,
|
77
|
+
} as unknown as CallArgs<TChain>)
|
78
|
+
const result = decodeFunctionResult({
|
79
|
+
abi,
|
80
|
+
functionName,
|
81
|
+
data: data || '0x',
|
82
|
+
})
|
83
|
+
return {
|
84
|
+
result,
|
85
|
+
request: {
|
86
|
+
abi,
|
87
|
+
address,
|
88
|
+
args,
|
89
|
+
functionName,
|
90
|
+
...callRequest,
|
91
|
+
},
|
92
|
+
} as unknown as SimulateContractResponse<TChain, TAbi, TFunctionName>
|
93
|
+
} catch (err) {
|
94
|
+
throw getContractError(err, {
|
95
|
+
abi,
|
96
|
+
address,
|
97
|
+
args,
|
98
|
+
functionName,
|
99
|
+
sender: callRequest.from,
|
100
|
+
})
|
101
|
+
}
|
102
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import { bench, describe } from 'vitest'
|
2
|
+
|
3
|
+
import { publicClient } from '../../_test'
|
4
|
+
import { createPendingTransactionFilter } from './createPendingTransactionFilter'
|
5
|
+
import { uninstallFilter } from './uninstallFilter'
|
6
|
+
|
7
|
+
const filter = await createPendingTransactionFilter(publicClient)
|
8
|
+
|
9
|
+
describe('Get Filter Changes', () => {
|
10
|
+
bench('viem: `uninstallFilter`', async () => {
|
11
|
+
await uninstallFilter(publicClient, { filter })
|
12
|
+
})
|
13
|
+
})
|
@@ -0,0 +1,65 @@
|
|
1
|
+
import { assertType, expect, test } from 'vitest'
|
2
|
+
|
3
|
+
import { accounts, publicClient, testClient, walletClient } from '../../_test'
|
4
|
+
|
5
|
+
import { createPendingTransactionFilter } from './createPendingTransactionFilter'
|
6
|
+
import { getFilterChanges } from './getFilterChanges'
|
7
|
+
import { uninstallFilter } from './uninstallFilter'
|
8
|
+
import { mine } from '../test'
|
9
|
+
import { sendTransaction } from '../wallet'
|
10
|
+
import { parseEther } from '../../utils'
|
11
|
+
import type { Hash } from '../../types'
|
12
|
+
|
13
|
+
test('default', async () => {
|
14
|
+
const filter = await createPendingTransactionFilter(publicClient)
|
15
|
+
expect(await uninstallFilter(publicClient, { filter })).toBeTruthy()
|
16
|
+
})
|
17
|
+
|
18
|
+
test('pending txns', async () => {
|
19
|
+
const filter = await createPendingTransactionFilter(publicClient)
|
20
|
+
|
21
|
+
await sendTransaction(walletClient, {
|
22
|
+
from: accounts[0].address,
|
23
|
+
to: accounts[1].address,
|
24
|
+
value: parseEther('1'),
|
25
|
+
})
|
26
|
+
await sendTransaction(walletClient, {
|
27
|
+
from: accounts[0].address,
|
28
|
+
to: accounts[1].address,
|
29
|
+
value: parseEther('1'),
|
30
|
+
})
|
31
|
+
|
32
|
+
let hashes = await getFilterChanges(publicClient, { filter })
|
33
|
+
assertType<Hash[]>(hashes)
|
34
|
+
expect(hashes.length).toBe(2)
|
35
|
+
|
36
|
+
mine(testClient, { blocks: 1 })
|
37
|
+
|
38
|
+
hashes = await getFilterChanges(publicClient, { filter })
|
39
|
+
expect(hashes.length).toBe(0)
|
40
|
+
|
41
|
+
expect(await uninstallFilter(publicClient, { filter })).toBeTruthy()
|
42
|
+
|
43
|
+
await sendTransaction(walletClient, {
|
44
|
+
from: accounts[0].address,
|
45
|
+
to: accounts[1].address,
|
46
|
+
value: parseEther('1'),
|
47
|
+
})
|
48
|
+
|
49
|
+
hashes = await getFilterChanges(publicClient, { filter })
|
50
|
+
expect(hashes.length).toBe(0)
|
51
|
+
})
|
52
|
+
|
53
|
+
test('filter does not exist', async () => {
|
54
|
+
expect(
|
55
|
+
await uninstallFilter(publicClient, {
|
56
|
+
filter: { id: '0x1', type: 'default' },
|
57
|
+
}),
|
58
|
+
).toBeFalsy()
|
59
|
+
})
|
60
|
+
|
61
|
+
test('filter already uninstalled', async () => {
|
62
|
+
const filter = await createPendingTransactionFilter(publicClient)
|
63
|
+
expect(await uninstallFilter(publicClient, { filter })).toBeTruthy()
|
64
|
+
expect(await uninstallFilter(publicClient, { filter })).toBeFalsy()
|
65
|
+
})
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import type { PublicClient } from '../../clients'
|
2
|
+
import type { Filter } from '../../types'
|
3
|
+
|
4
|
+
export type UninstallFilterArgs = {
|
5
|
+
filter: Filter<any>
|
6
|
+
}
|
7
|
+
export type UninstallFilterResponse = boolean
|
8
|
+
|
9
|
+
export async function uninstallFilter(
|
10
|
+
client: PublicClient,
|
11
|
+
{ filter }: UninstallFilterArgs,
|
12
|
+
): Promise<UninstallFilterResponse> {
|
13
|
+
return client.request({
|
14
|
+
method: 'eth_uninstallFilter',
|
15
|
+
params: [filter.id],
|
16
|
+
})
|
17
|
+
}
|
@@ -0,0 +1,322 @@
|
|
1
|
+
import { describe, expect, test } from 'vitest'
|
2
|
+
|
3
|
+
import { accounts, publicClient, testClient, walletClient } from '../../_test'
|
4
|
+
import { WaitForTransactionReceiptTimeoutError } from '../../errors'
|
5
|
+
import { wait } from '../../utils/wait'
|
6
|
+
import { waitForTransactionReceipt } from './waitForTransactionReceipt'
|
7
|
+
import { hexToNumber, parseEther, parseGwei } from '../../utils'
|
8
|
+
import { sendTransaction } from '..'
|
9
|
+
import { mine, setIntervalMining } from '../test'
|
10
|
+
|
11
|
+
const sourceAccount = accounts[0]
|
12
|
+
const targetAccount = accounts[1]
|
13
|
+
|
14
|
+
test('waits for transaction (send -> wait -> mine)', async () => {
|
15
|
+
const hash = await sendTransaction(walletClient, {
|
16
|
+
from: sourceAccount.address,
|
17
|
+
to: targetAccount.address,
|
18
|
+
value: parseEther('1'),
|
19
|
+
})
|
20
|
+
const { status } = await waitForTransactionReceipt(publicClient, {
|
21
|
+
hash,
|
22
|
+
})
|
23
|
+
expect(status).toBe('success')
|
24
|
+
})
|
25
|
+
|
26
|
+
test('waits for transaction (send -> mine -> wait)', async () => {
|
27
|
+
const hash = await sendTransaction(walletClient, {
|
28
|
+
from: sourceAccount.address,
|
29
|
+
to: targetAccount.address,
|
30
|
+
value: parseEther('1'),
|
31
|
+
})
|
32
|
+
await mine(testClient, { blocks: 1 })
|
33
|
+
const { status } = await waitForTransactionReceipt(publicClient, {
|
34
|
+
hash,
|
35
|
+
})
|
36
|
+
expect(status).toBe('success')
|
37
|
+
})
|
38
|
+
|
39
|
+
describe('replaced transactions', () => {
|
40
|
+
test('repriced', async () => {
|
41
|
+
await mine(testClient, { blocks: 10 })
|
42
|
+
await setIntervalMining(testClient, { interval: 0 })
|
43
|
+
|
44
|
+
const nonce = hexToNumber(
|
45
|
+
(await publicClient.request({
|
46
|
+
method: 'eth_getTransactionCount',
|
47
|
+
params: [sourceAccount.address, 'latest'],
|
48
|
+
})) ?? '0x0',
|
49
|
+
)
|
50
|
+
|
51
|
+
const hash = await sendTransaction(walletClient, {
|
52
|
+
from: sourceAccount.address,
|
53
|
+
to: targetAccount.address,
|
54
|
+
value: parseEther('1'),
|
55
|
+
maxFeePerGas: parseGwei('10'),
|
56
|
+
nonce,
|
57
|
+
})
|
58
|
+
|
59
|
+
let replacement: any
|
60
|
+
const [receipt] = await Promise.all([
|
61
|
+
waitForTransactionReceipt(publicClient, {
|
62
|
+
hash,
|
63
|
+
onReplaced: (replacement_) => (replacement = replacement_),
|
64
|
+
}),
|
65
|
+
(async () => {
|
66
|
+
await wait(100)
|
67
|
+
|
68
|
+
// speed up
|
69
|
+
await sendTransaction(walletClient, {
|
70
|
+
from: sourceAccount.address,
|
71
|
+
to: targetAccount.address,
|
72
|
+
value: parseEther('1'),
|
73
|
+
nonce,
|
74
|
+
maxFeePerGas: parseGwei('20'),
|
75
|
+
})
|
76
|
+
|
77
|
+
await setIntervalMining(testClient, { interval: 1 })
|
78
|
+
})(),
|
79
|
+
])
|
80
|
+
|
81
|
+
expect(receipt !== null).toBeTruthy()
|
82
|
+
expect(replacement.reason).toBe('repriced')
|
83
|
+
expect(replacement.replacedTransaction).toBeDefined()
|
84
|
+
expect(replacement.transaction).toBeDefined()
|
85
|
+
expect(replacement.transactionReceipt).toBeDefined()
|
86
|
+
})
|
87
|
+
|
88
|
+
test('repriced (skipped blocks)', async () => {
|
89
|
+
await mine(testClient, { blocks: 10 })
|
90
|
+
await setIntervalMining(testClient, { interval: 0 })
|
91
|
+
|
92
|
+
const nonce = hexToNumber(
|
93
|
+
(await publicClient.request({
|
94
|
+
method: 'eth_getTransactionCount',
|
95
|
+
params: [sourceAccount.address, 'latest'],
|
96
|
+
})) ?? '0x0',
|
97
|
+
)
|
98
|
+
|
99
|
+
const hash = await sendTransaction(walletClient, {
|
100
|
+
from: sourceAccount.address,
|
101
|
+
to: targetAccount.address,
|
102
|
+
value: parseEther('1'),
|
103
|
+
maxFeePerGas: parseGwei('10'),
|
104
|
+
nonce,
|
105
|
+
})
|
106
|
+
|
107
|
+
const [receipt] = await Promise.all([
|
108
|
+
waitForTransactionReceipt(publicClient, {
|
109
|
+
hash,
|
110
|
+
}),
|
111
|
+
(async () => {
|
112
|
+
await wait(100)
|
113
|
+
|
114
|
+
// speed up
|
115
|
+
await sendTransaction(walletClient, {
|
116
|
+
from: sourceAccount.address,
|
117
|
+
to: targetAccount.address,
|
118
|
+
value: parseEther('1'),
|
119
|
+
nonce,
|
120
|
+
maxFeePerGas: parseGwei('20'),
|
121
|
+
})
|
122
|
+
|
123
|
+
await wait(1000)
|
124
|
+
await mine(testClient, { blocks: 5 })
|
125
|
+
})(),
|
126
|
+
])
|
127
|
+
|
128
|
+
expect(receipt !== null).toBeTruthy()
|
129
|
+
|
130
|
+
await setIntervalMining(testClient, { interval: 1 })
|
131
|
+
})
|
132
|
+
|
133
|
+
test('cancelled', async () => {
|
134
|
+
await mine(testClient, { blocks: 10 })
|
135
|
+
await setIntervalMining(testClient, { interval: 0 })
|
136
|
+
|
137
|
+
const nonce = hexToNumber(
|
138
|
+
(await publicClient.request({
|
139
|
+
method: 'eth_getTransactionCount',
|
140
|
+
params: [sourceAccount.address, 'latest'],
|
141
|
+
})) ?? '0x0',
|
142
|
+
)
|
143
|
+
|
144
|
+
const hash = await sendTransaction(walletClient, {
|
145
|
+
from: sourceAccount.address,
|
146
|
+
to: targetAccount.address,
|
147
|
+
value: parseEther('1'),
|
148
|
+
maxFeePerGas: parseGwei('10'),
|
149
|
+
nonce,
|
150
|
+
})
|
151
|
+
|
152
|
+
let replacement: any
|
153
|
+
const [receipt] = await Promise.all([
|
154
|
+
waitForTransactionReceipt(publicClient, {
|
155
|
+
hash,
|
156
|
+
onReplaced: (replacement_) => (replacement = replacement_),
|
157
|
+
}),
|
158
|
+
(async () => {
|
159
|
+
await wait(100)
|
160
|
+
|
161
|
+
// speed up
|
162
|
+
await sendTransaction(walletClient, {
|
163
|
+
from: sourceAccount.address,
|
164
|
+
to: sourceAccount.address,
|
165
|
+
value: parseEther('0'),
|
166
|
+
nonce,
|
167
|
+
maxFeePerGas: parseGwei('20'),
|
168
|
+
})
|
169
|
+
|
170
|
+
await setIntervalMining(testClient, { interval: 1 })
|
171
|
+
})(),
|
172
|
+
])
|
173
|
+
|
174
|
+
expect(receipt !== null).toBeTruthy()
|
175
|
+
expect(replacement.reason).toBe('cancelled')
|
176
|
+
expect(replacement.replacedTransaction).toBeDefined()
|
177
|
+
expect(replacement.transaction).toBeDefined()
|
178
|
+
expect(replacement.transactionReceipt).toBeDefined()
|
179
|
+
})
|
180
|
+
|
181
|
+
test('replaced', async () => {
|
182
|
+
await mine(testClient, { blocks: 10 })
|
183
|
+
await setIntervalMining(testClient, { interval: 0 })
|
184
|
+
|
185
|
+
const nonce = hexToNumber(
|
186
|
+
(await publicClient.request({
|
187
|
+
method: 'eth_getTransactionCount',
|
188
|
+
params: [sourceAccount.address, 'latest'],
|
189
|
+
})) ?? '0x0',
|
190
|
+
)
|
191
|
+
|
192
|
+
const hash = await sendTransaction(walletClient, {
|
193
|
+
from: sourceAccount.address,
|
194
|
+
to: targetAccount.address,
|
195
|
+
value: parseEther('1'),
|
196
|
+
maxFeePerGas: parseGwei('10'),
|
197
|
+
nonce,
|
198
|
+
})
|
199
|
+
|
200
|
+
let replacement: any
|
201
|
+
const [receipt] = await Promise.all([
|
202
|
+
waitForTransactionReceipt(publicClient, {
|
203
|
+
hash,
|
204
|
+
onReplaced: (replacement_) => (replacement = replacement_),
|
205
|
+
}),
|
206
|
+
(async () => {
|
207
|
+
await wait(100)
|
208
|
+
|
209
|
+
// speed up
|
210
|
+
await sendTransaction(walletClient, {
|
211
|
+
from: sourceAccount.address,
|
212
|
+
to: targetAccount.address,
|
213
|
+
value: parseEther('2'),
|
214
|
+
nonce,
|
215
|
+
maxFeePerGas: parseGwei('20'),
|
216
|
+
})
|
217
|
+
|
218
|
+
await setIntervalMining(testClient, { interval: 1 })
|
219
|
+
})(),
|
220
|
+
])
|
221
|
+
|
222
|
+
expect(receipt !== null).toBeTruthy()
|
223
|
+
expect(replacement.reason).toBe('replaced')
|
224
|
+
expect(replacement.replacedTransaction).toBeDefined()
|
225
|
+
expect(replacement.transaction).toBeDefined()
|
226
|
+
expect(replacement.transactionReceipt).toBeDefined()
|
227
|
+
})
|
228
|
+
})
|
229
|
+
|
230
|
+
describe('args: confirmations', () => {
|
231
|
+
test('waits for confirmations', async () => {
|
232
|
+
const hash = await sendTransaction(walletClient, {
|
233
|
+
from: sourceAccount.address,
|
234
|
+
to: targetAccount.address,
|
235
|
+
value: parseEther('1'),
|
236
|
+
maxFeePerGas: parseGwei('10'),
|
237
|
+
})
|
238
|
+
|
239
|
+
const start = Date.now()
|
240
|
+
const receipt = await waitForTransactionReceipt(publicClient, {
|
241
|
+
hash,
|
242
|
+
confirmations: 3,
|
243
|
+
})
|
244
|
+
const end = Date.now()
|
245
|
+
|
246
|
+
expect(receipt !== null).toBeTruthy()
|
247
|
+
expect(end - start).toBeGreaterThan(3000 - 100)
|
248
|
+
expect(end - start).toBeLessThanOrEqual(3000 + 100)
|
249
|
+
})
|
250
|
+
|
251
|
+
test('waits for confirmations (replaced)', async () => {
|
252
|
+
await mine(testClient, { blocks: 10 })
|
253
|
+
await setIntervalMining(testClient, { interval: 0 })
|
254
|
+
|
255
|
+
const nonce = hexToNumber(
|
256
|
+
(await publicClient.request({
|
257
|
+
method: 'eth_getTransactionCount',
|
258
|
+
params: [sourceAccount.address, 'latest'],
|
259
|
+
})) ?? '0x0',
|
260
|
+
)
|
261
|
+
|
262
|
+
const hash = await sendTransaction(walletClient, {
|
263
|
+
from: sourceAccount.address,
|
264
|
+
to: targetAccount.address,
|
265
|
+
value: parseEther('1'),
|
266
|
+
maxFeePerGas: parseGwei('10'),
|
267
|
+
nonce,
|
268
|
+
})
|
269
|
+
|
270
|
+
const [receipt] = await Promise.all([
|
271
|
+
waitForTransactionReceipt(publicClient, {
|
272
|
+
confirmations: 3,
|
273
|
+
hash,
|
274
|
+
}),
|
275
|
+
(async () => {
|
276
|
+
await wait(100)
|
277
|
+
|
278
|
+
// speed up
|
279
|
+
await sendTransaction(walletClient, {
|
280
|
+
from: sourceAccount.address,
|
281
|
+
to: targetAccount.address,
|
282
|
+
value: parseEther('1'),
|
283
|
+
nonce,
|
284
|
+
maxFeePerGas: parseGwei('20'),
|
285
|
+
})
|
286
|
+
|
287
|
+
await wait(1000)
|
288
|
+
await setIntervalMining(testClient, { interval: 1 })
|
289
|
+
})(),
|
290
|
+
])
|
291
|
+
|
292
|
+
expect(receipt !== null).toBeTruthy()
|
293
|
+
})
|
294
|
+
})
|
295
|
+
|
296
|
+
test('args: timeout', async () => {
|
297
|
+
const hash = await sendTransaction(walletClient, {
|
298
|
+
from: sourceAccount.address,
|
299
|
+
to: targetAccount.address,
|
300
|
+
value: parseEther('1'),
|
301
|
+
})
|
302
|
+
await expect(() =>
|
303
|
+
waitForTransactionReceipt(publicClient, {
|
304
|
+
hash,
|
305
|
+
timeout: 500,
|
306
|
+
}),
|
307
|
+
).rejects.toThrowError(WaitForTransactionReceiptTimeoutError)
|
308
|
+
})
|
309
|
+
|
310
|
+
describe('errors', () => {
|
311
|
+
test('throws when transaction not found', async () => {
|
312
|
+
await expect(() =>
|
313
|
+
waitForTransactionReceipt(publicClient, {
|
314
|
+
hash: '0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98f',
|
315
|
+
}),
|
316
|
+
).rejects.toThrowErrorMatchingInlineSnapshot(`
|
317
|
+
"Transaction with hash \\"0xa4b1f606b66105fa45cb5db23d2f6597075701e7f0e2367f4e6a39d17a8cf98f\\" could not be found.
|
318
|
+
|
319
|
+
Version: viem@1.0.2"
|
320
|
+
`)
|
321
|
+
})
|
322
|
+
})
|
@@ -0,0 +1,171 @@
|
|
1
|
+
import type { Chain } from '../../chains'
|
2
|
+
import type { PublicClient } from '../../clients'
|
3
|
+
import {
|
4
|
+
TransactionNotFoundError,
|
5
|
+
TransactionReceiptNotFoundError,
|
6
|
+
WaitForTransactionReceiptTimeoutError,
|
7
|
+
} from '../../errors'
|
8
|
+
import type { Hash, Transaction } from '../../types'
|
9
|
+
import { observe } from '../../utils/observe'
|
10
|
+
import { getBlock, watchBlockNumber } from '../public'
|
11
|
+
|
12
|
+
import type { GetTransactionResponse } from './getTransaction'
|
13
|
+
import { getTransaction } from './getTransaction'
|
14
|
+
import type { GetTransactionReceiptResponse } from './getTransactionReceipt'
|
15
|
+
import { getTransactionReceipt } from './getTransactionReceipt'
|
16
|
+
|
17
|
+
export type ReplacementReason = 'cancelled' | 'replaced' | 'repriced'
|
18
|
+
export type ReplacementResponse<TChain extends Chain = Chain> = {
|
19
|
+
reason: ReplacementReason
|
20
|
+
replacedTransaction: Transaction
|
21
|
+
transaction: Transaction
|
22
|
+
transactionReceipt: GetTransactionReceiptResponse<TChain>
|
23
|
+
}
|
24
|
+
|
25
|
+
export type WaitForTransactionReceiptResponse<TChain extends Chain = Chain> =
|
26
|
+
GetTransactionReceiptResponse<TChain>
|
27
|
+
|
28
|
+
export type WaitForTransactionReceiptArgs<TChain extends Chain = Chain> = {
|
29
|
+
/** The number of confirmations (blocks that have passed) to wait before resolving. */
|
30
|
+
confirmations?: number
|
31
|
+
/** The hash of the transaction. */
|
32
|
+
hash: Hash
|
33
|
+
onReplaced?: (response: ReplacementResponse<TChain>) => void
|
34
|
+
/** Polling frequency (in ms). Defaults to the client's pollingInterval config. */
|
35
|
+
pollingInterval?: number
|
36
|
+
/** Optional timeout (in milliseconds) to wait before stopping polling. */
|
37
|
+
timeout?: number
|
38
|
+
}
|
39
|
+
|
40
|
+
export async function waitForTransactionReceipt<TChain extends Chain>(
|
41
|
+
client: PublicClient<any, TChain>,
|
42
|
+
{
|
43
|
+
confirmations = 1,
|
44
|
+
hash,
|
45
|
+
onReplaced,
|
46
|
+
pollingInterval = client.pollingInterval,
|
47
|
+
timeout,
|
48
|
+
}: WaitForTransactionReceiptArgs<TChain>,
|
49
|
+
): Promise<WaitForTransactionReceiptResponse<TChain>> {
|
50
|
+
const observerId = JSON.stringify([
|
51
|
+
'waitForTransactionReceipt',
|
52
|
+
client.uid,
|
53
|
+
hash,
|
54
|
+
])
|
55
|
+
|
56
|
+
let transaction: GetTransactionResponse<TChain> | undefined
|
57
|
+
let replacedTransaction: GetTransactionResponse<TChain> | undefined
|
58
|
+
let receipt: GetTransactionReceiptResponse<TChain>
|
59
|
+
|
60
|
+
return new Promise((resolve, reject) => {
|
61
|
+
if (timeout)
|
62
|
+
setTimeout(
|
63
|
+
() => reject(new WaitForTransactionReceiptTimeoutError({ hash })),
|
64
|
+
timeout,
|
65
|
+
)
|
66
|
+
|
67
|
+
const unobserve = observe(
|
68
|
+
observerId,
|
69
|
+
{ onReplaced, resolve, reject },
|
70
|
+
(emit) => {
|
71
|
+
const unwatch = watchBlockNumber(client, {
|
72
|
+
emitMissed: true,
|
73
|
+
emitOnBegin: true,
|
74
|
+
pollingInterval,
|
75
|
+
async onBlockNumber(blockNumber) {
|
76
|
+
const done = async (fn: () => void) => {
|
77
|
+
unwatch()
|
78
|
+
fn()
|
79
|
+
unobserve()
|
80
|
+
}
|
81
|
+
|
82
|
+
try {
|
83
|
+
// If we already have a valid receipt, let's check if we have enough
|
84
|
+
// confirmations. If we do, then we can resolve.
|
85
|
+
if (receipt) {
|
86
|
+
if (blockNumber - receipt.blockNumber + 1n < confirmations)
|
87
|
+
return
|
88
|
+
|
89
|
+
done(() => emit.resolve(receipt))
|
90
|
+
return
|
91
|
+
}
|
92
|
+
|
93
|
+
// Get the transaction to check if it's been replaced.
|
94
|
+
transaction = await getTransaction(client, { hash })
|
95
|
+
|
96
|
+
// Get the receipt to check if it's been processed.
|
97
|
+
receipt = await getTransactionReceipt(client, { hash })
|
98
|
+
|
99
|
+
// Check if we have enough confirmations. If not, continue polling.
|
100
|
+
if (blockNumber - receipt.blockNumber + 1n < confirmations) return
|
101
|
+
|
102
|
+
done(() => emit.resolve(receipt))
|
103
|
+
} catch (err) {
|
104
|
+
// If the receipt is not found, the transaction will be pending.
|
105
|
+
// We need to check if it has potentially been replaced.
|
106
|
+
if (
|
107
|
+
transaction &&
|
108
|
+
(err instanceof TransactionNotFoundError ||
|
109
|
+
err instanceof TransactionReceiptNotFoundError)
|
110
|
+
) {
|
111
|
+
replacedTransaction = transaction
|
112
|
+
|
113
|
+
// Let's retrieve the transactions from the current block.
|
114
|
+
const block = await getBlock(client, {
|
115
|
+
blockNumber,
|
116
|
+
includeTransactions: true,
|
117
|
+
})
|
118
|
+
|
119
|
+
const replacementTransaction = (
|
120
|
+
block.transactions as Transaction[]
|
121
|
+
).find(
|
122
|
+
({ from, nonce }) =>
|
123
|
+
from === replacedTransaction!.from &&
|
124
|
+
nonce === replacedTransaction!.nonce,
|
125
|
+
)
|
126
|
+
|
127
|
+
// If we couldn't find a replacement transaction, continue polling.
|
128
|
+
if (!replacementTransaction) return
|
129
|
+
|
130
|
+
// If we found a replacement transaction, return it's receipt.
|
131
|
+
receipt = await getTransactionReceipt(client, {
|
132
|
+
hash: replacementTransaction.hash,
|
133
|
+
})
|
134
|
+
|
135
|
+
// Check if we have enough confirmations. If not, continue polling.
|
136
|
+
if (blockNumber - receipt.blockNumber + 1n < confirmations)
|
137
|
+
return
|
138
|
+
|
139
|
+
let reason: ReplacementReason = 'replaced'
|
140
|
+
if (
|
141
|
+
replacementTransaction.to === replacedTransaction.to &&
|
142
|
+
replacementTransaction.value === replacedTransaction.value
|
143
|
+
) {
|
144
|
+
reason = 'repriced'
|
145
|
+
} else if (
|
146
|
+
replacementTransaction.from === replacementTransaction.to &&
|
147
|
+
replacementTransaction.value === 0n
|
148
|
+
) {
|
149
|
+
reason = 'cancelled'
|
150
|
+
}
|
151
|
+
|
152
|
+
done(() => {
|
153
|
+
emit.onReplaced?.({
|
154
|
+
reason,
|
155
|
+
replacedTransaction: replacedTransaction!,
|
156
|
+
transaction: replacementTransaction,
|
157
|
+
transactionReceipt: receipt,
|
158
|
+
})
|
159
|
+
emit.resolve(receipt)
|
160
|
+
})
|
161
|
+
} else {
|
162
|
+
done(() => emit.reject(err))
|
163
|
+
}
|
164
|
+
}
|
165
|
+
},
|
166
|
+
})
|
167
|
+
return unwatch
|
168
|
+
},
|
169
|
+
)
|
170
|
+
})
|
171
|
+
}
|