viem 0.3.3 → 0.3.5
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/README.md +6 -0
- package/dist/cjs/actions/public/call.js +97 -10
- package/dist/cjs/actions/public/call.js.map +1 -1
- package/dist/cjs/actions/public/simulateContract.js +1 -0
- package/dist/cjs/actions/public/simulateContract.js.map +1 -1
- package/dist/cjs/chains.js +1 -24
- package/dist/cjs/chains.js.map +1 -1
- package/dist/cjs/clients/createPublicClient.js +12 -9
- package/dist/cjs/clients/createPublicClient.js.map +1 -1
- package/dist/cjs/clients/transports/fallback.js +1 -1
- package/dist/cjs/clients/transports/fallback.js.map +1 -1
- package/dist/cjs/constants/contract.js +5 -0
- package/dist/cjs/constants/contract.js.map +1 -0
- package/dist/cjs/constants/index.js +3 -1
- package/dist/cjs/constants/index.js.map +1 -1
- package/dist/cjs/errors/chain.js +13 -1
- package/dist/cjs/errors/chain.js.map +1 -1
- package/dist/cjs/errors/contract.js +1 -1
- package/dist/cjs/errors/contract.js.map +1 -1
- package/dist/cjs/errors/encoding.js +13 -1
- package/dist/cjs/errors/encoding.js.map +1 -1
- package/dist/cjs/errors/index.js +5 -2
- package/dist/cjs/errors/index.js.map +1 -1
- package/dist/cjs/errors/version.js +1 -1
- package/dist/cjs/index.js +5 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/encoding/fromBytes.js +29 -12
- package/dist/cjs/utils/encoding/fromBytes.js.map +1 -1
- package/dist/cjs/utils/encoding/fromHex.js +32 -10
- package/dist/cjs/utils/encoding/fromHex.js.map +1 -1
- package/dist/cjs/utils/encoding/toBytes.js +31 -15
- package/dist/cjs/utils/encoding/toBytes.js.map +1 -1
- package/dist/cjs/utils/encoding/toHex.js +31 -20
- package/dist/cjs/utils/encoding/toHex.js.map +1 -1
- package/dist/cjs/utils/errors/getContractError.js +7 -5
- package/dist/cjs/utils/errors/getContractError.js.map +1 -1
- package/dist/cjs/utils/promise/createBatchScheduler.js +47 -0
- package/dist/cjs/utils/promise/createBatchScheduler.js.map +1 -0
- package/dist/cjs/utils/promise/index.js +3 -1
- package/dist/cjs/utils/promise/index.js.map +1 -1
- package/dist/esm/actions/public/call.js +91 -4
- package/dist/esm/actions/public/call.js.map +1 -1
- package/dist/esm/actions/public/simulateContract.js +1 -0
- package/dist/esm/actions/public/simulateContract.js.map +1 -1
- package/dist/esm/clients/createPublicClient.js +12 -9
- package/dist/esm/clients/createPublicClient.js.map +1 -1
- package/dist/esm/clients/transports/fallback.js +1 -1
- package/dist/esm/clients/transports/fallback.js.map +1 -1
- package/dist/esm/constants/contract.js +2 -0
- package/dist/esm/constants/contract.js.map +1 -0
- package/dist/esm/constants/index.js +1 -0
- package/dist/esm/constants/index.js.map +1 -1
- package/dist/esm/errors/chain.js +11 -0
- package/dist/esm/errors/chain.js.map +1 -1
- package/dist/esm/errors/contract.js +1 -1
- package/dist/esm/errors/contract.js.map +1 -1
- package/dist/esm/errors/encoding.js +11 -0
- package/dist/esm/errors/encoding.js.map +1 -1
- package/dist/esm/errors/index.js +2 -2
- package/dist/esm/errors/index.js.map +1 -1
- package/dist/esm/errors/version.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/encoding/fromBytes.js +30 -13
- package/dist/esm/utils/encoding/fromBytes.js.map +1 -1
- package/dist/esm/utils/encoding/fromHex.js +31 -10
- package/dist/esm/utils/encoding/fromHex.js.map +1 -1
- package/dist/esm/utils/encoding/toBytes.js +31 -15
- package/dist/esm/utils/encoding/toBytes.js.map +1 -1
- package/dist/esm/utils/encoding/toHex.js +28 -17
- package/dist/esm/utils/encoding/toHex.js.map +1 -1
- package/dist/esm/utils/errors/getContractError.js +7 -5
- package/dist/esm/utils/errors/getContractError.js.map +1 -1
- package/dist/esm/utils/promise/createBatchScheduler.js +43 -0
- package/dist/esm/utils/promise/createBatchScheduler.js.map +1 -0
- package/dist/esm/utils/promise/index.js +1 -0
- package/dist/esm/utils/promise/index.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/public/call.ts +141 -7
- package/src/actions/public/simulateContract.ts +2 -1
- package/src/clients/createPublicClient.ts +27 -9
- package/src/clients/transports/fallback.ts +1 -1
- package/src/constants/contract.ts +1 -0
- package/src/constants/index.ts +2 -0
- package/src/errors/chain.ts +8 -0
- package/src/errors/contract.ts +1 -1
- package/src/errors/encoding.ts +9 -0
- package/src/errors/index.ts +2 -0
- package/src/errors/version.ts +1 -1
- package/src/index.ts +1 -0
- package/src/types/eip1193.ts +1 -1
- package/src/utils/encoding/fromBytes.ts +147 -18
- package/src/utils/encoding/fromHex.ts +162 -19
- package/src/utils/encoding/toBytes.ts +148 -18
- package/src/utils/encoding/toHex.ts +151 -26
- package/src/utils/errors/getContractError.ts +9 -5
- package/src/utils/promise/createBatchScheduler.ts +82 -0
- package/src/utils/promise/index.ts +1 -0
- package/dist/types/abi.d.ts +0 -3
- package/dist/types/abi.d.ts.map +0 -1
- package/dist/types/accounts/generateMnemonic.d.ts +0 -9
- package/dist/types/accounts/generateMnemonic.d.ts.map +0 -1
- package/dist/types/accounts/generatePrivateKey.d.ts +0 -8
- package/dist/types/accounts/generatePrivateKey.d.ts.map +0 -1
- package/dist/types/accounts/hdKeyToAccount.d.ts +0 -9
- package/dist/types/accounts/hdKeyToAccount.d.ts.map +0 -1
- package/dist/types/accounts/index.d.ts +0 -21
- package/dist/types/accounts/index.d.ts.map +0 -1
- package/dist/types/accounts/mnemonicToAccount.d.ts +0 -8
- package/dist/types/accounts/mnemonicToAccount.d.ts.map +0 -1
- package/dist/types/accounts/privateKeyToAccount.d.ts +0 -9
- package/dist/types/accounts/privateKeyToAccount.d.ts.map +0 -1
- package/dist/types/accounts/toAccount.d.ts +0 -11
- package/dist/types/accounts/toAccount.d.ts.map +0 -1
- package/dist/types/accounts/types.d.ts +0 -45
- package/dist/types/accounts/types.d.ts.map +0 -1
- package/dist/types/accounts/utils/index.d.ts +0 -10
- package/dist/types/accounts/utils/index.d.ts.map +0 -1
- package/dist/types/accounts/utils/parseAccount.d.ts +0 -4
- package/dist/types/accounts/utils/parseAccount.d.ts.map +0 -1
- package/dist/types/accounts/utils/publicKeyToAddress.d.ts +0 -10
- package/dist/types/accounts/utils/publicKeyToAddress.d.ts.map +0 -1
- package/dist/types/accounts/utils/sign.d.ts +0 -16
- package/dist/types/accounts/utils/sign.d.ts.map +0 -1
- package/dist/types/accounts/utils/signMessage.d.ts +0 -16
- package/dist/types/accounts/utils/signMessage.d.ts.map +0 -1
- package/dist/types/accounts/utils/signTransaction.d.ts +0 -9
- package/dist/types/accounts/utils/signTransaction.d.ts.map +0 -1
- package/dist/types/accounts/utils/signTypedData.d.ts +0 -19
- package/dist/types/accounts/utils/signTypedData.d.ts.map +0 -1
- package/dist/types/accounts/utils/signatureToHex.d.ts +0 -3
- package/dist/types/accounts/utils/signatureToHex.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/czech.d.ts +0 -2
- package/dist/types/accounts/wordlists/czech.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/english.d.ts +0 -2
- package/dist/types/accounts/wordlists/english.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/french.d.ts +0 -2
- package/dist/types/accounts/wordlists/french.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/italian.d.ts +0 -2
- package/dist/types/accounts/wordlists/italian.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/japanese.d.ts +0 -2
- package/dist/types/accounts/wordlists/japanese.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/korean.d.ts +0 -2
- package/dist/types/accounts/wordlists/korean.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/simplifiedChinese.d.ts +0 -2
- package/dist/types/accounts/wordlists/simplifiedChinese.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/spanish.d.ts +0 -2
- package/dist/types/accounts/wordlists/spanish.d.ts.map +0 -1
- package/dist/types/accounts/wordlists/traditionalChinese.d.ts +0 -2
- package/dist/types/accounts/wordlists/traditionalChinese.d.ts.map +0 -1
- package/dist/types/actions/ens/getEnsAddress.d.ts +0 -40
- package/dist/types/actions/ens/getEnsAddress.d.ts.map +0 -1
- package/dist/types/actions/ens/getEnsAvatar.d.ts +0 -38
- package/dist/types/actions/ens/getEnsAvatar.d.ts.map +0 -1
- package/dist/types/actions/ens/getEnsName.d.ts +0 -38
- package/dist/types/actions/ens/getEnsName.d.ts.map +0 -1
- package/dist/types/actions/ens/getEnsResolver.d.ts +0 -40
- package/dist/types/actions/ens/getEnsResolver.d.ts.map +0 -1
- package/dist/types/actions/ens/getEnsText.d.ts +0 -43
- package/dist/types/actions/ens/getEnsText.d.ts.map +0 -1
- package/dist/types/actions/ens/index.d.ts +0 -6
- package/dist/types/actions/ens/index.d.ts.map +0 -1
- package/dist/types/actions/getContract.d.ts +0 -261
- package/dist/types/actions/getContract.d.ts.map +0 -1
- package/dist/types/actions/index.d.ts +0 -11
- package/dist/types/actions/index.d.ts.map +0 -1
- package/dist/types/actions/public/call.d.ts +0 -48
- package/dist/types/actions/public/call.d.ts.map +0 -1
- package/dist/types/actions/public/createBlockFilter.d.ts +0 -26
- package/dist/types/actions/public/createBlockFilter.d.ts.map +0 -1
- package/dist/types/actions/public/createContractEventFilter.d.ts +0 -41
- package/dist/types/actions/public/createContractEventFilter.d.ts.map +0 -1
- package/dist/types/actions/public/createEventFilter.d.ts +0 -46
- package/dist/types/actions/public/createEventFilter.d.ts.map +0 -1
- package/dist/types/actions/public/createPendingTransactionFilter.d.ts +0 -26
- package/dist/types/actions/public/createPendingTransactionFilter.d.ts.map +0 -1
- package/dist/types/actions/public/estimateContractGas.d.ts +0 -35
- package/dist/types/actions/public/estimateContractGas.d.ts.map +0 -1
- package/dist/types/actions/public/estimateGas.d.ts +0 -45
- package/dist/types/actions/public/estimateGas.d.ts.map +0 -1
- package/dist/types/actions/public/getBalance.d.ts +0 -52
- package/dist/types/actions/public/getBalance.d.ts.map +0 -1
- package/dist/types/actions/public/getBlock.d.ts +0 -52
- package/dist/types/actions/public/getBlock.d.ts.map +0 -1
- package/dist/types/actions/public/getBlockNumber.d.ts +0 -54
- package/dist/types/actions/public/getBlockNumber.d.ts.map +0 -1
- package/dist/types/actions/public/getBlockTransactionCount.d.ts +0 -44
- package/dist/types/actions/public/getBlockTransactionCount.d.ts.map +0 -1
- package/dist/types/actions/public/getBytecode.d.ts +0 -37
- package/dist/types/actions/public/getBytecode.d.ts.map +0 -1
- package/dist/types/actions/public/getChainId.d.ts +0 -26
- package/dist/types/actions/public/getChainId.d.ts.map +0 -1
- package/dist/types/actions/public/getFeeHistory.d.ts +0 -50
- package/dist/types/actions/public/getFeeHistory.d.ts.map +0 -1
- package/dist/types/actions/public/getFilterChanges.d.ts +0 -91
- package/dist/types/actions/public/getFilterChanges.d.ts.map +0 -1
- package/dist/types/actions/public/getFilterLogs.d.ts +0 -36
- package/dist/types/actions/public/getFilterLogs.d.ts.map +0 -1
- package/dist/types/actions/public/getGasPrice.d.ts +0 -25
- package/dist/types/actions/public/getGasPrice.d.ts.map +0 -1
- package/dist/types/actions/public/getLogs.d.ts +0 -49
- package/dist/types/actions/public/getLogs.d.ts.map +0 -1
- package/dist/types/actions/public/getStorageAt.d.ts +0 -39
- package/dist/types/actions/public/getStorageAt.d.ts.map +0 -1
- package/dist/types/actions/public/getTransaction.d.ts +0 -62
- package/dist/types/actions/public/getTransaction.d.ts.map +0 -1
- package/dist/types/actions/public/getTransactionConfirmations.d.ts +0 -39
- package/dist/types/actions/public/getTransactionConfirmations.d.ts.map +0 -1
- package/dist/types/actions/public/getTransactionCount.d.ts +0 -40
- package/dist/types/actions/public/getTransactionCount.d.ts.map +0 -1
- package/dist/types/actions/public/getTransactionReceipt.d.ts +0 -34
- package/dist/types/actions/public/getTransactionReceipt.d.ts.map +0 -1
- package/dist/types/actions/public/index.d.ts +0 -67
- package/dist/types/actions/public/index.d.ts.map +0 -1
- package/dist/types/actions/public/multicall.d.ts +0 -52
- package/dist/types/actions/public/multicall.d.ts.map +0 -1
- package/dist/types/actions/public/readContract.d.ts +0 -39
- package/dist/types/actions/public/readContract.d.ts.map +0 -1
- package/dist/types/actions/public/simulateContract.d.ts +0 -47
- package/dist/types/actions/public/simulateContract.d.ts.map +0 -1
- package/dist/types/actions/public/uninstallFilter.d.ts +0 -32
- package/dist/types/actions/public/uninstallFilter.d.ts.map +0 -1
- package/dist/types/actions/public/waitForTransactionReceipt.d.ts +0 -69
- package/dist/types/actions/public/waitForTransactionReceipt.d.ts.map +0 -1
- package/dist/types/actions/public/watchBlockNumber.d.ts +0 -57
- package/dist/types/actions/public/watchBlockNumber.d.ts.map +0 -1
- package/dist/types/actions/public/watchBlocks.d.ts +0 -64
- package/dist/types/actions/public/watchBlocks.d.ts.map +0 -1
- package/dist/types/actions/public/watchContractEvent.d.ts +0 -55
- package/dist/types/actions/public/watchContractEvent.d.ts.map +0 -1
- package/dist/types/actions/public/watchEvent.d.ts +0 -61
- package/dist/types/actions/public/watchEvent.d.ts.map +0 -1
- package/dist/types/actions/public/watchPendingTransactions.d.ts +0 -71
- package/dist/types/actions/public/watchPendingTransactions.d.ts.map +0 -1
- package/dist/types/actions/test/dropTransaction.d.ts +0 -30
- package/dist/types/actions/test/dropTransaction.d.ts.map +0 -1
- package/dist/types/actions/test/getAutomine.d.ts +0 -25
- package/dist/types/actions/test/getAutomine.d.ts.map +0 -1
- package/dist/types/actions/test/getTxpoolContent.d.ts +0 -31
- package/dist/types/actions/test/getTxpoolContent.d.ts.map +0 -1
- package/dist/types/actions/test/getTxpoolStatus.d.ts +0 -28
- package/dist/types/actions/test/getTxpoolStatus.d.ts.map +0 -1
- package/dist/types/actions/test/impersonateAccount.d.ts +0 -30
- package/dist/types/actions/test/impersonateAccount.d.ts.map +0 -1
- package/dist/types/actions/test/increaseTime.d.ts +0 -30
- package/dist/types/actions/test/increaseTime.d.ts.map +0 -1
- package/dist/types/actions/test/index.d.ts +0 -52
- package/dist/types/actions/test/index.d.ts.map +0 -1
- package/dist/types/actions/test/inspectTxpool.d.ts +0 -28
- package/dist/types/actions/test/inspectTxpool.d.ts.map +0 -1
- package/dist/types/actions/test/mine.d.ts +0 -30
- package/dist/types/actions/test/mine.d.ts.map +0 -1
- package/dist/types/actions/test/removeBlockTimestampInterval.d.ts +0 -23
- package/dist/types/actions/test/removeBlockTimestampInterval.d.ts.map +0 -1
- package/dist/types/actions/test/reset.d.ts +0 -30
- package/dist/types/actions/test/reset.d.ts.map +0 -1
- package/dist/types/actions/test/revert.d.ts +0 -28
- package/dist/types/actions/test/revert.d.ts.map +0 -1
- package/dist/types/actions/test/sendUnsignedTransaction.d.ts +0 -31
- package/dist/types/actions/test/sendUnsignedTransaction.d.ts.map +0 -1
- package/dist/types/actions/test/setAutomine.d.ts +0 -23
- package/dist/types/actions/test/setAutomine.d.ts.map +0 -1
- package/dist/types/actions/test/setBalance.d.ts +0 -33
- package/dist/types/actions/test/setBalance.d.ts.map +0 -1
- package/dist/types/actions/test/setBlockGasLimit.d.ts +0 -28
- package/dist/types/actions/test/setBlockGasLimit.d.ts.map +0 -1
- package/dist/types/actions/test/setBlockTimestampInterval.d.ts +0 -28
- package/dist/types/actions/test/setBlockTimestampInterval.d.ts.map +0 -1
- package/dist/types/actions/test/setCode.d.ts +0 -33
- package/dist/types/actions/test/setCode.d.ts.map +0 -1
- package/dist/types/actions/test/setCoinbase.d.ts +0 -30
- package/dist/types/actions/test/setCoinbase.d.ts.map +0 -1
- package/dist/types/actions/test/setIntervalMining.d.ts +0 -28
- package/dist/types/actions/test/setIntervalMining.d.ts.map +0 -1
- package/dist/types/actions/test/setLoggingEnabled.d.ts +0 -23
- package/dist/types/actions/test/setLoggingEnabled.d.ts.map +0 -1
- package/dist/types/actions/test/setMinGasPrice.d.ts +0 -32
- package/dist/types/actions/test/setMinGasPrice.d.ts.map +0 -1
- package/dist/types/actions/test/setNextBlockBaseFeePerGas.d.ts +0 -30
- package/dist/types/actions/test/setNextBlockBaseFeePerGas.d.ts.map +0 -1
- package/dist/types/actions/test/setNextBlockTimestamp.d.ts +0 -28
- package/dist/types/actions/test/setNextBlockTimestamp.d.ts.map +0 -1
- package/dist/types/actions/test/setNonce.d.ts +0 -33
- package/dist/types/actions/test/setNonce.d.ts.map +0 -1
- package/dist/types/actions/test/setRpcUrl.d.ts +0 -24
- package/dist/types/actions/test/setRpcUrl.d.ts.map +0 -1
- package/dist/types/actions/test/setStorageAt.d.ts +0 -36
- package/dist/types/actions/test/setStorageAt.d.ts.map +0 -1
- package/dist/types/actions/test/snapshot.d.ts +0 -23
- package/dist/types/actions/test/snapshot.d.ts.map +0 -1
- package/dist/types/actions/test/stopImpersonatingAccount.d.ts +0 -30
- package/dist/types/actions/test/stopImpersonatingAccount.d.ts.map +0 -1
- package/dist/types/actions/wallet/addChain.d.ts +0 -27
- package/dist/types/actions/wallet/addChain.d.ts.map +0 -1
- package/dist/types/actions/wallet/deployContract.d.ts +0 -38
- package/dist/types/actions/wallet/deployContract.d.ts.map +0 -1
- package/dist/types/actions/wallet/getAddresses.d.ts +0 -26
- package/dist/types/actions/wallet/getAddresses.d.ts.map +0 -1
- package/dist/types/actions/wallet/getPermissions.d.ts +0 -26
- package/dist/types/actions/wallet/getPermissions.d.ts.map +0 -1
- package/dist/types/actions/wallet/index.d.ts +0 -27
- package/dist/types/actions/wallet/index.d.ts.map +0 -1
- package/dist/types/actions/wallet/requestAddresses.d.ts +0 -30
- package/dist/types/actions/wallet/requestAddresses.d.ts.map +0 -1
- package/dist/types/actions/wallet/requestPermissions.d.ts +0 -34
- package/dist/types/actions/wallet/requestPermissions.d.ts.map +0 -1
- package/dist/types/actions/wallet/sendTransaction.d.ts +0 -53
- package/dist/types/actions/wallet/sendTransaction.d.ts.map +0 -1
- package/dist/types/actions/wallet/signMessage.d.ts +0 -54
- package/dist/types/actions/wallet/signMessage.d.ts.map +0 -1
- package/dist/types/actions/wallet/signTypedData.d.ts +0 -109
- package/dist/types/actions/wallet/signTypedData.d.ts.map +0 -1
- package/dist/types/actions/wallet/switchChain.d.ts +0 -28
- package/dist/types/actions/wallet/switchChain.d.ts.map +0 -1
- package/dist/types/actions/wallet/watchAsset.d.ts +0 -35
- package/dist/types/actions/wallet/watchAsset.d.ts.map +0 -1
- package/dist/types/actions/wallet/writeContract.d.ts +0 -58
- package/dist/types/actions/wallet/writeContract.d.ts.map +0 -1
- package/dist/types/adapters/ethers.d.ts +0 -27
- package/dist/types/adapters/ethers.d.ts.map +0 -1
- package/dist/types/chains.d.ts +0 -2487
- package/dist/types/chains.d.ts.map +0 -1
- package/dist/types/clients/createClient.d.ts +0 -43
- package/dist/types/clients/createClient.d.ts.map +0 -1
- package/dist/types/clients/createPublicClient.d.ts +0 -28
- package/dist/types/clients/createPublicClient.d.ts.map +0 -1
- package/dist/types/clients/createTestClient.d.ts +0 -38
- package/dist/types/clients/createTestClient.d.ts.map +0 -1
- package/dist/types/clients/createWalletClient.d.ts +0 -51
- package/dist/types/clients/createWalletClient.d.ts.map +0 -1
- package/dist/types/clients/decorators/index.d.ts +0 -7
- package/dist/types/clients/decorators/index.d.ts.map +0 -1
- package/dist/types/clients/decorators/public.d.ts +0 -1043
- package/dist/types/clients/decorators/public.d.ts.map +0 -1
- package/dist/types/clients/decorators/test.d.ts +0 -572
- package/dist/types/clients/decorators/test.d.ts.map +0 -1
- package/dist/types/clients/decorators/wallet.d.ts +0 -436
- package/dist/types/clients/decorators/wallet.d.ts.map +0 -1
- package/dist/types/clients/index.d.ts +0 -11
- package/dist/types/clients/index.d.ts.map +0 -1
- package/dist/types/clients/transports/createTransport.d.ts +0 -37
- package/dist/types/clients/transports/createTransport.d.ts.map +0 -1
- package/dist/types/clients/transports/custom.d.ts +0 -21
- package/dist/types/clients/transports/custom.d.ts.map +0 -1
- package/dist/types/clients/transports/fallback.d.ts +0 -75
- package/dist/types/clients/transports/fallback.d.ts.map +0 -1
- package/dist/types/clients/transports/http.d.ts +0 -29
- package/dist/types/clients/transports/http.d.ts.map +0 -1
- package/dist/types/clients/transports/index.d.ts +0 -11
- package/dist/types/clients/transports/index.d.ts.map +0 -1
- package/dist/types/clients/transports/webSocket.d.ts +0 -46
- package/dist/types/clients/transports/webSocket.d.ts.map +0 -1
- package/dist/types/constants/abis.d.ts +0 -79
- package/dist/types/constants/abis.d.ts.map +0 -1
- package/dist/types/constants/index.d.ts +0 -4
- package/dist/types/constants/index.d.ts.map +0 -1
- package/dist/types/constants/solidity.d.ts +0 -15
- package/dist/types/constants/solidity.d.ts.map +0 -1
- package/dist/types/constants/unit.d.ts +0 -13
- package/dist/types/constants/unit.d.ts.map +0 -1
- package/dist/types/contract.d.ts +0 -7
- package/dist/types/contract.d.ts.map +0 -1
- package/dist/types/ens.d.ts +0 -5
- package/dist/types/ens.d.ts.map +0 -1
- package/dist/types/errors/abi.d.ts +0 -165
- package/dist/types/errors/abi.d.ts.map +0 -1
- package/dist/types/errors/account.d.ts +0 -8
- package/dist/types/errors/account.d.ts.map +0 -1
- package/dist/types/errors/address.d.ts +0 -8
- package/dist/types/errors/address.d.ts.map +0 -1
- package/dist/types/errors/base.d.ts +0 -22
- package/dist/types/errors/base.d.ts.map +0 -1
- package/dist/types/errors/block.d.ts +0 -10
- package/dist/types/errors/block.d.ts.map +0 -1
- package/dist/types/errors/chain.d.ts +0 -31
- package/dist/types/errors/chain.d.ts.map +0 -1
- package/dist/types/errors/contract.d.ts +0 -58
- package/dist/types/errors/contract.d.ts.map +0 -1
- package/dist/types/errors/data.d.ts +0 -10
- package/dist/types/errors/data.d.ts.map +0 -1
- package/dist/types/errors/encoding.d.ts +0 -46
- package/dist/types/errors/encoding.d.ts.map +0 -1
- package/dist/types/errors/ens.d.ts +0 -26
- package/dist/types/errors/ens.d.ts.map +0 -1
- package/dist/types/errors/estimateGas.d.ts +0 -13
- package/dist/types/errors/estimateGas.d.ts.map +0 -1
- package/dist/types/errors/index.d.ts +0 -18
- package/dist/types/errors/index.d.ts.map +0 -1
- package/dist/types/errors/log.d.ts +0 -6
- package/dist/types/errors/log.d.ts.map +0 -1
- package/dist/types/errors/node.d.ts +0 -105
- package/dist/types/errors/node.d.ts.map +0 -1
- package/dist/types/errors/request.d.ts +0 -53
- package/dist/types/errors/request.d.ts.map +0 -1
- package/dist/types/errors/rpc.d.ts +0 -199
- package/dist/types/errors/rpc.d.ts.map +0 -1
- package/dist/types/errors/transaction.d.ts +0 -75
- package/dist/types/errors/transaction.d.ts.map +0 -1
- package/dist/types/errors/transport.d.ts +0 -5
- package/dist/types/errors/transport.d.ts.map +0 -1
- package/dist/types/errors/utils.d.ts +0 -5
- package/dist/types/errors/utils.d.ts.map +0 -1
- package/dist/types/errors/version.d.ts +0 -2
- package/dist/types/errors/version.d.ts.map +0 -1
- package/dist/types/ethers.d.ts +0 -2
- package/dist/types/ethers.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/public.d.ts +0 -3
- package/dist/types/public.d.ts.map +0 -1
- package/dist/types/test.d.ts +0 -3
- package/dist/types/test.d.ts.map +0 -1
- package/dist/types/types/account.d.ts +0 -11
- package/dist/types/types/account.d.ts.map +0 -1
- package/dist/types/types/block.d.ts +0 -62
- package/dist/types/types/block.d.ts.map +0 -1
- package/dist/types/types/chain.d.ts +0 -17
- package/dist/types/types/chain.d.ts.map +0 -1
- package/dist/types/types/contract.d.ts +0 -159
- package/dist/types/types/contract.d.ts.map +0 -1
- package/dist/types/types/eip1193.d.ts +0 -1002
- package/dist/types/types/eip1193.d.ts.map +0 -1
- package/dist/types/types/ens.d.ts +0 -5
- package/dist/types/types/ens.d.ts.map +0 -1
- package/dist/types/types/fee.d.ts +0 -28
- package/dist/types/types/fee.d.ts.map +0 -1
- package/dist/types/types/filter.d.ts +0 -27
- package/dist/types/types/filter.d.ts.map +0 -1
- package/dist/types/types/formatter.d.ts +0 -13
- package/dist/types/types/formatter.d.ts.map +0 -1
- package/dist/types/types/index.d.ts +0 -19
- package/dist/types/types/index.d.ts.map +0 -1
- package/dist/types/types/log.d.ts +0 -42
- package/dist/types/types/log.d.ts.map +0 -1
- package/dist/types/types/misc.d.ts +0 -10
- package/dist/types/types/misc.d.ts.map +0 -1
- package/dist/types/types/multicall.d.ts +0 -48
- package/dist/types/types/multicall.d.ts.map +0 -1
- package/dist/types/types/rpc.d.ts +0 -19
- package/dist/types/types/rpc.d.ts.map +0 -1
- package/dist/types/types/transaction.d.ts +0 -132
- package/dist/types/types/transaction.d.ts.map +0 -1
- package/dist/types/types/transport.d.ts +0 -3
- package/dist/types/types/transport.d.ts.map +0 -1
- package/dist/types/types/typedData.d.ts +0 -47
- package/dist/types/types/typedData.d.ts.map +0 -1
- package/dist/types/types/utils.d.ts +0 -156
- package/dist/types/types/utils.d.ts.map +0 -1
- package/dist/types/types/window.d.ts +0 -7
- package/dist/types/types/window.d.ts.map +0 -1
- package/dist/types/utils/abi/decodeAbiParameters.d.ts +0 -5
- package/dist/types/utils/abi/decodeAbiParameters.d.ts.map +0 -1
- package/dist/types/utils/abi/decodeDeployData.d.ts +0 -13
- package/dist/types/utils/abi/decodeDeployData.d.ts.map +0 -1
- package/dist/types/utils/abi/decodeErrorResult.d.ts +0 -13
- package/dist/types/utils/abi/decodeErrorResult.d.ts.map +0 -1
- package/dist/types/utils/abi/decodeEventLog.d.ts +0 -13
- package/dist/types/utils/abi/decodeEventLog.d.ts.map +0 -1
- package/dist/types/utils/abi/decodeFunctionData.d.ts +0 -14
- package/dist/types/utils/abi/decodeFunctionData.d.ts.map +0 -1
- package/dist/types/utils/abi/decodeFunctionResult.d.ts +0 -10
- package/dist/types/utils/abi/decodeFunctionResult.d.ts.map +0 -1
- package/dist/types/utils/abi/encodeAbiParameters.d.ts +0 -9
- package/dist/types/utils/abi/encodeAbiParameters.d.ts.map +0 -1
- package/dist/types/utils/abi/encodeDeployData.d.ts +0 -8
- package/dist/types/utils/abi/encodeDeployData.d.ts.map +0 -1
- package/dist/types/utils/abi/encodeErrorResult.d.ts +0 -8
- package/dist/types/utils/abi/encodeErrorResult.d.ts.map +0 -1
- package/dist/types/utils/abi/encodeEventTopics.d.ts +0 -9
- package/dist/types/utils/abi/encodeEventTopics.d.ts.map +0 -1
- package/dist/types/utils/abi/encodeFunctionData.d.ts +0 -8
- package/dist/types/utils/abi/encodeFunctionData.d.ts.map +0 -1
- package/dist/types/utils/abi/encodeFunctionResult.d.ts +0 -9
- package/dist/types/utils/abi/encodeFunctionResult.d.ts.map +0 -1
- package/dist/types/utils/abi/encodePacked.d.ts +0 -11
- package/dist/types/utils/abi/encodePacked.d.ts.map +0 -1
- package/dist/types/utils/abi/formatAbiItem.d.ts +0 -9
- package/dist/types/utils/abi/formatAbiItem.d.ts.map +0 -1
- package/dist/types/utils/abi/formatAbiItemWithArgs.d.ts +0 -8
- package/dist/types/utils/abi/formatAbiItemWithArgs.d.ts.map +0 -1
- package/dist/types/utils/abi/getAbiItem.d.ts +0 -12
- package/dist/types/utils/abi/getAbiItem.d.ts.map +0 -1
- package/dist/types/utils/abi/index.d.ts +0 -30
- package/dist/types/utils/abi/index.d.ts.map +0 -1
- package/dist/types/utils/accounts.d.ts +0 -3
- package/dist/types/utils/accounts.d.ts.map +0 -1
- package/dist/types/utils/address/getAddress.d.ts +0 -4
- package/dist/types/utils/address/getAddress.d.ts.map +0 -1
- package/dist/types/utils/address/getContractAddress.d.ts +0 -19
- package/dist/types/utils/address/getContractAddress.d.ts.map +0 -1
- package/dist/types/utils/address/index.d.ts +0 -6
- package/dist/types/utils/address/index.d.ts.map +0 -1
- package/dist/types/utils/address/isAddress.d.ts +0 -3
- package/dist/types/utils/address/isAddress.d.ts.map +0 -1
- package/dist/types/utils/address/isAddressEqual.d.ts +0 -3
- package/dist/types/utils/address/isAddressEqual.d.ts.map +0 -1
- package/dist/types/utils/buildRequest.d.ts +0 -6
- package/dist/types/utils/buildRequest.d.ts.map +0 -1
- package/dist/types/utils/chain.d.ts +0 -8
- package/dist/types/utils/chain.d.ts.map +0 -1
- package/dist/types/utils/contract/extractFunctionParts.d.ts +0 -13
- package/dist/types/utils/contract/extractFunctionParts.d.ts.map +0 -1
- package/dist/types/utils/contract/index.d.ts +0 -2
- package/dist/types/utils/contract/index.d.ts.map +0 -1
- package/dist/types/utils/data/concat.d.ts +0 -6
- package/dist/types/utils/data/concat.d.ts.map +0 -1
- package/dist/types/utils/data/index.d.ts +0 -8
- package/dist/types/utils/data/index.d.ts.map +0 -1
- package/dist/types/utils/data/isBytes.d.ts +0 -3
- package/dist/types/utils/data/isBytes.d.ts.map +0 -1
- package/dist/types/utils/data/isHex.d.ts +0 -3
- package/dist/types/utils/data/isHex.d.ts.map +0 -1
- package/dist/types/utils/data/pad.d.ts +0 -11
- package/dist/types/utils/data/pad.d.ts.map +0 -1
- package/dist/types/utils/data/size.d.ts +0 -9
- package/dist/types/utils/data/size.d.ts.map +0 -1
- package/dist/types/utils/data/slice.d.ts +0 -27
- package/dist/types/utils/data/slice.d.ts.map +0 -1
- package/dist/types/utils/data/trim.d.ts +0 -8
- package/dist/types/utils/data/trim.d.ts.map +0 -1
- package/dist/types/utils/encoding/fromBytes.d.ts +0 -25
- package/dist/types/utils/encoding/fromBytes.d.ts.map +0 -1
- package/dist/types/utils/encoding/fromHex.d.ts +0 -28
- package/dist/types/utils/encoding/fromHex.d.ts.map +0 -1
- package/dist/types/utils/encoding/fromRlp.d.ts +0 -6
- package/dist/types/utils/encoding/fromRlp.d.ts.map +0 -1
- package/dist/types/utils/encoding/index.d.ts +0 -8
- package/dist/types/utils/encoding/index.d.ts.map +0 -1
- package/dist/types/utils/encoding/toBytes.d.ts +0 -21
- package/dist/types/utils/encoding/toBytes.d.ts.map +0 -1
- package/dist/types/utils/encoding/toHex.d.ts +0 -29
- package/dist/types/utils/encoding/toHex.d.ts.map +0 -1
- package/dist/types/utils/encoding/toRlp.d.ts +0 -8
- package/dist/types/utils/encoding/toRlp.d.ts.map +0 -1
- package/dist/types/utils/ens/avatar/index.d.ts +0 -2
- package/dist/types/utils/ens/avatar/index.d.ts.map +0 -1
- package/dist/types/utils/ens/avatar/parseAvatarRecord.d.ts +0 -7
- package/dist/types/utils/ens/avatar/parseAvatarRecord.d.ts.map +0 -1
- package/dist/types/utils/ens/avatar/utils.d.ts +0 -34
- package/dist/types/utils/ens/avatar/utils.d.ts.map +0 -1
- package/dist/types/utils/ens/index.d.ts +0 -5
- package/dist/types/utils/ens/index.d.ts.map +0 -1
- package/dist/types/utils/ens/labelhash.d.ts +0 -11
- package/dist/types/utils/ens/labelhash.d.ts.map +0 -1
- package/dist/types/utils/ens/namehash.d.ts +0 -13
- package/dist/types/utils/ens/namehash.d.ts.map +0 -1
- package/dist/types/utils/ens/normalize.d.ts +0 -12
- package/dist/types/utils/ens/normalize.d.ts.map +0 -1
- package/dist/types/utils/ens/packetToBytes.d.ts +0 -3
- package/dist/types/utils/ens/packetToBytes.d.ts.map +0 -1
- package/dist/types/utils/errors/getCallError.d.ts +0 -9
- package/dist/types/utils/errors/getCallError.d.ts.map +0 -1
- package/dist/types/utils/errors/getContractError.d.ts +0 -13
- package/dist/types/utils/errors/getContractError.d.ts.map +0 -1
- package/dist/types/utils/errors/getEstimateGasError.d.ts +0 -10
- package/dist/types/utils/errors/getEstimateGasError.d.ts.map +0 -1
- package/dist/types/utils/errors/getNodeError.d.ts +0 -6
- package/dist/types/utils/errors/getNodeError.d.ts.map +0 -1
- package/dist/types/utils/errors/getTransactionError.d.ts +0 -10
- package/dist/types/utils/errors/getTransactionError.d.ts.map +0 -1
- package/dist/types/utils/errors/index.d.ts +0 -6
- package/dist/types/utils/errors/index.d.ts.map +0 -1
- package/dist/types/utils/filters/createFilterRequestScope.d.ts +0 -16
- package/dist/types/utils/filters/createFilterRequestScope.d.ts.map +0 -1
- package/dist/types/utils/formatters/block.d.ts +0 -14
- package/dist/types/utils/formatters/block.d.ts.map +0 -1
- package/dist/types/utils/formatters/extract.d.ts +0 -8
- package/dist/types/utils/formatters/extract.d.ts.map +0 -1
- package/dist/types/utils/formatters/feeHistory.d.ts +0 -3
- package/dist/types/utils/formatters/feeHistory.d.ts.map +0 -1
- package/dist/types/utils/formatters/format.d.ts +0 -36
- package/dist/types/utils/formatters/format.d.ts.map +0 -1
- package/dist/types/utils/formatters/index.d.ts +0 -13
- package/dist/types/utils/formatters/index.d.ts.map +0 -1
- package/dist/types/utils/formatters/log.d.ts +0 -16
- package/dist/types/utils/formatters/log.d.ts.map +0 -1
- package/dist/types/utils/formatters/transaction.d.ts +0 -19
- package/dist/types/utils/formatters/transaction.d.ts.map +0 -1
- package/dist/types/utils/formatters/transactionReceipt.d.ts +0 -14
- package/dist/types/utils/formatters/transactionReceipt.d.ts.map +0 -1
- package/dist/types/utils/formatters/transactionRequest.d.ts +0 -14
- package/dist/types/utils/formatters/transactionRequest.d.ts.map +0 -1
- package/dist/types/utils/hash/getEventSelector.d.ts +0 -3
- package/dist/types/utils/hash/getEventSelector.d.ts.map +0 -1
- package/dist/types/utils/hash/getFunctionSelector.d.ts +0 -2
- package/dist/types/utils/hash/getFunctionSelector.d.ts.map +0 -1
- package/dist/types/utils/hash/hashFunction.d.ts +0 -2
- package/dist/types/utils/hash/hashFunction.d.ts.map +0 -1
- package/dist/types/utils/hash/index.d.ts +0 -5
- package/dist/types/utils/hash/index.d.ts.map +0 -1
- package/dist/types/utils/hash/isHash.d.ts +0 -3
- package/dist/types/utils/hash/isHash.d.ts.map +0 -1
- package/dist/types/utils/hash/keccak256.d.ts +0 -6
- package/dist/types/utils/hash/keccak256.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts +0 -26
- package/dist/types/utils/index.d.ts.map +0 -1
- package/dist/types/utils/observe.d.ts +0 -17
- package/dist/types/utils/observe.d.ts.map +0 -1
- package/dist/types/utils/poll.d.ts +0 -13
- package/dist/types/utils/poll.d.ts.map +0 -1
- package/dist/types/utils/promise/index.d.ts +0 -4
- package/dist/types/utils/promise/index.d.ts.map +0 -1
- package/dist/types/utils/promise/withCache.d.ts +0 -36
- package/dist/types/utils/promise/withCache.d.ts.map +0 -1
- package/dist/types/utils/promise/withRetry.d.ts +0 -12
- package/dist/types/utils/promise/withRetry.d.ts.map +0 -1
- package/dist/types/utils/promise/withTimeout.d.ts +0 -8
- package/dist/types/utils/promise/withTimeout.d.ts.map +0 -1
- package/dist/types/utils/regex.d.ts +0 -4
- package/dist/types/utils/regex.d.ts.map +0 -1
- package/dist/types/utils/rpc.d.ts +0 -64
- package/dist/types/utils/rpc.d.ts.map +0 -1
- package/dist/types/utils/signature/hashMessage.d.ts +0 -6
- package/dist/types/utils/signature/hashMessage.d.ts.map +0 -1
- package/dist/types/utils/signature/hashTypedData.d.ts +0 -10
- package/dist/types/utils/signature/hashTypedData.d.ts.map +0 -1
- package/dist/types/utils/signature/index.d.ts +0 -16
- package/dist/types/utils/signature/index.d.ts.map +0 -1
- package/dist/types/utils/signature/recoverAddress.d.ts +0 -8
- package/dist/types/utils/signature/recoverAddress.d.ts.map +0 -1
- package/dist/types/utils/signature/recoverMessageAddress.d.ts +0 -8
- package/dist/types/utils/signature/recoverMessageAddress.d.ts.map +0 -1
- package/dist/types/utils/signature/recoverPublicKey.d.ts +0 -8
- package/dist/types/utils/signature/recoverPublicKey.d.ts.map +0 -1
- package/dist/types/utils/signature/recoverTypedDataAddress.d.ts +0 -12
- package/dist/types/utils/signature/recoverTypedDataAddress.d.ts.map +0 -1
- package/dist/types/utils/signature/verifyMessage.d.ts +0 -10
- package/dist/types/utils/signature/verifyMessage.d.ts.map +0 -1
- package/dist/types/utils/signature/verifyTypedData.d.ts +0 -13
- package/dist/types/utils/signature/verifyTypedData.d.ts.map +0 -1
- package/dist/types/utils/stringify.d.ts +0 -2
- package/dist/types/utils/stringify.d.ts.map +0 -1
- package/dist/types/utils/transaction/assertRequest.d.ts +0 -4
- package/dist/types/utils/transaction/assertRequest.d.ts.map +0 -1
- package/dist/types/utils/transaction/assertTransaction.d.ts +0 -5
- package/dist/types/utils/transaction/assertTransaction.d.ts.map +0 -1
- package/dist/types/utils/transaction/getSerializedTransactionType.d.ts +0 -4
- package/dist/types/utils/transaction/getSerializedTransactionType.d.ts.map +0 -1
- package/dist/types/utils/transaction/getTransactionType.d.ts +0 -4
- package/dist/types/utils/transaction/getTransactionType.d.ts.map +0 -1
- package/dist/types/utils/transaction/index.d.ts +0 -10
- package/dist/types/utils/transaction/index.d.ts.map +0 -1
- package/dist/types/utils/transaction/parseTransaction.d.ts +0 -5
- package/dist/types/utils/transaction/parseTransaction.d.ts.map +0 -1
- package/dist/types/utils/transaction/prepareRequest.d.ts +0 -21
- package/dist/types/utils/transaction/prepareRequest.d.ts.map +0 -1
- package/dist/types/utils/transaction/serializeTransaction.d.ts +0 -5
- package/dist/types/utils/transaction/serializeTransaction.d.ts.map +0 -1
- package/dist/types/utils/typedData.d.ts +0 -6
- package/dist/types/utils/typedData.d.ts.map +0 -1
- package/dist/types/utils/uid.d.ts +0 -2
- package/dist/types/utils/uid.d.ts.map +0 -1
- package/dist/types/utils/unit/formatEther.d.ts +0 -2
- package/dist/types/utils/unit/formatEther.d.ts.map +0 -1
- package/dist/types/utils/unit/formatGwei.d.ts +0 -2
- package/dist/types/utils/unit/formatGwei.d.ts.map +0 -1
- package/dist/types/utils/unit/formatUnits.d.ts +0 -2
- package/dist/types/utils/unit/formatUnits.d.ts.map +0 -1
- package/dist/types/utils/unit/index.d.ts +0 -7
- package/dist/types/utils/unit/index.d.ts.map +0 -1
- package/dist/types/utils/unit/parseEther.d.ts +0 -2
- package/dist/types/utils/unit/parseEther.d.ts.map +0 -1
- package/dist/types/utils/unit/parseGwei.d.ts +0 -2
- package/dist/types/utils/unit/parseGwei.d.ts.map +0 -1
- package/dist/types/utils/unit/parseUnits.d.ts +0 -2
- package/dist/types/utils/unit/parseUnits.d.ts.map +0 -1
- package/dist/types/utils/wait.d.ts +0 -2
- package/dist/types/utils/wait.d.ts.map +0 -1
- package/dist/types/wallet.d.ts +0 -3
- package/dist/types/wallet.d.ts.map +0 -1
- package/dist/types/window.d.ts +0 -2
- package/dist/types/window.d.ts.map +0 -1
@@ -1,9 +1,35 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
InvalidHexBooleanError,
|
3
|
+
SizeOverflowError,
|
4
|
+
} from '../../errors/index.js'
|
2
5
|
import type { ByteArray, Hex } from '../../types/index.js'
|
6
|
+
import { size as size_ } from '../data/size.js'
|
3
7
|
import { trim } from '../data/index.js'
|
4
8
|
import { hexToBytes } from './toBytes.js'
|
5
9
|
|
6
|
-
|
10
|
+
export function assertSize(
|
11
|
+
hexOrBytes: Hex | ByteArray,
|
12
|
+
{ size }: { size: number },
|
13
|
+
): void {
|
14
|
+
if (size_(hexOrBytes) > size)
|
15
|
+
throw new SizeOverflowError({
|
16
|
+
givenSize: size_(hexOrBytes),
|
17
|
+
maxSize: size,
|
18
|
+
})
|
19
|
+
}
|
20
|
+
|
21
|
+
export type FromHexParameters<
|
22
|
+
TTo extends 'string' | 'bigint' | 'number' | 'bytes' | 'boolean',
|
23
|
+
> =
|
24
|
+
| TTo
|
25
|
+
| {
|
26
|
+
/** Size (in bytes) of the hex value. */
|
27
|
+
size?: number
|
28
|
+
/** Type to convert to. */
|
29
|
+
to: TTo
|
30
|
+
}
|
31
|
+
|
32
|
+
export type FromHexReturnType<TTo> = TTo extends 'string'
|
7
33
|
? string
|
8
34
|
: TTo extends 'bigint'
|
9
35
|
? bigint
|
@@ -16,29 +42,77 @@ type FromHexReturnType<TTo> = TTo extends 'string'
|
|
16
42
|
: never
|
17
43
|
|
18
44
|
/**
|
19
|
-
*
|
45
|
+
* Decodes a hex string into a string, number, bigint, boolean, or byte array.
|
46
|
+
*
|
47
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html
|
48
|
+
* - Example: https://viem.sh/docs/utilities/fromHex.html#usage
|
49
|
+
*
|
50
|
+
* @param hex Hex string to decode.
|
51
|
+
* @param toOrOpts Type to convert to or options.
|
52
|
+
* @returns Decoded value.
|
53
|
+
*
|
54
|
+
* @example
|
55
|
+
* import { fromHex } from 'viem'
|
56
|
+
* const data = fromHex('0x1a4', 'number')
|
57
|
+
* // 420
|
58
|
+
*
|
59
|
+
* @example
|
60
|
+
* import { fromHex } from 'viem'
|
61
|
+
* const data = fromHex('0x48656c6c6f20576f726c6421', 'string')
|
62
|
+
* // 'Hello world'
|
63
|
+
*
|
64
|
+
* @example
|
65
|
+
* import { fromHex } from 'viem'
|
66
|
+
* const data = fromHex('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', {
|
67
|
+
* size: 32,
|
68
|
+
* to: 'string'
|
69
|
+
* })
|
70
|
+
* // 'Hello world'
|
20
71
|
*/
|
21
72
|
export function fromHex<
|
22
73
|
TTo extends 'string' | 'bigint' | 'number' | 'bytes' | 'boolean',
|
23
|
-
>(hex: Hex,
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
if (to === '
|
28
|
-
return
|
74
|
+
>(hex: Hex, toOrOpts: FromHexParameters<TTo>): FromHexReturnType<TTo> {
|
75
|
+
const opts = typeof toOrOpts === 'string' ? { to: toOrOpts } : toOrOpts
|
76
|
+
const to = opts.to
|
77
|
+
|
78
|
+
if (to === 'number') return hexToNumber(hex, opts) as FromHexReturnType<TTo>
|
79
|
+
if (to === 'bigint') return hexToBigInt(hex, opts) as FromHexReturnType<TTo>
|
80
|
+
if (to === 'string') return hexToString(hex, opts) as FromHexReturnType<TTo>
|
81
|
+
if (to === 'boolean') return hexToBool(hex, opts) as FromHexReturnType<TTo>
|
82
|
+
return hexToBytes(hex, opts) as FromHexReturnType<TTo>
|
29
83
|
}
|
30
84
|
|
31
85
|
export type HexToBigIntOpts = {
|
32
|
-
|
86
|
+
/** Whether or not the number of a signed representation. */
|
33
87
|
signed?: boolean
|
88
|
+
/** Size (in bytes) of the hex value. */
|
89
|
+
size?: number
|
34
90
|
}
|
35
91
|
|
36
92
|
/**
|
37
|
-
*
|
93
|
+
* Decodes a hex value into a bigint.
|
94
|
+
*
|
95
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextobigint
|
96
|
+
*
|
97
|
+
* @param hex Hex value to decode.
|
98
|
+
* @param opts Options.
|
99
|
+
* @returns BigInt value.
|
100
|
+
*
|
101
|
+
* @example
|
102
|
+
* import { hexToBigInt } from 'viem'
|
103
|
+
* const data = hexToBigInt('0x1a4', { signed: true })
|
104
|
+
* // 420n
|
105
|
+
*
|
106
|
+
* @example
|
107
|
+
* import { hexToBigInt } from 'viem'
|
108
|
+
* const data = hexToBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
|
109
|
+
* // 420n
|
38
110
|
*/
|
39
111
|
export function hexToBigInt(hex: Hex, opts: HexToBigIntOpts = {}): bigint {
|
40
112
|
const { signed } = opts
|
41
113
|
|
114
|
+
if (opts.size) assertSize(hex, { size: opts.size })
|
115
|
+
|
42
116
|
const value = BigInt(hex)
|
43
117
|
if (!signed) return value
|
44
118
|
|
@@ -49,28 +123,97 @@ export function hexToBigInt(hex: Hex, opts: HexToBigIntOpts = {}): bigint {
|
|
49
123
|
return value - BigInt(`0x${'f'.padStart(size * 2, 'f')}`) - 1n
|
50
124
|
}
|
51
125
|
|
126
|
+
export type HexToBoolOpts = {
|
127
|
+
/** Size (in bytes) of the hex value. */
|
128
|
+
size?: number
|
129
|
+
}
|
130
|
+
|
52
131
|
/**
|
53
|
-
*
|
132
|
+
* Decodes a hex value into a boolean.
|
133
|
+
*
|
134
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextobool
|
135
|
+
*
|
136
|
+
* @param hex Hex value to decode.
|
137
|
+
* @param opts Options.
|
138
|
+
* @returns Boolean value.
|
139
|
+
*
|
140
|
+
* @example
|
141
|
+
* import { hexToBool } from 'viem'
|
142
|
+
* const data = hexToBool('0x1')
|
143
|
+
* // true
|
144
|
+
*
|
145
|
+
* @example
|
146
|
+
* import { hexToBool } from 'viem'
|
147
|
+
* const data = hexToBool('0x0000000000000000000000000000000000000000000000000000000000000001', { size: 32 })
|
148
|
+
* // true
|
54
149
|
*/
|
55
|
-
export function hexToBool(
|
150
|
+
export function hexToBool(hex_: Hex, opts: HexToBoolOpts = {}): boolean {
|
151
|
+
let hex = hex_
|
152
|
+
if (opts.size) {
|
153
|
+
assertSize(hex, { size: opts.size })
|
154
|
+
hex = trim(hex)
|
155
|
+
}
|
56
156
|
if (trim(hex) === '0x0') return false
|
57
157
|
if (trim(hex) === '0x1') return true
|
58
158
|
throw new InvalidHexBooleanError(hex)
|
59
159
|
}
|
60
160
|
|
61
|
-
type
|
161
|
+
export type HexToNumberOpts = HexToBigIntOpts
|
62
162
|
|
63
163
|
/**
|
64
|
-
*
|
164
|
+
* Decodes a hex string into a number.
|
165
|
+
*
|
166
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextonumber
|
167
|
+
*
|
168
|
+
* @param hex Hex value to decode.
|
169
|
+
* @param opts Options.
|
170
|
+
* @returns Number value.
|
171
|
+
*
|
172
|
+
* @example
|
173
|
+
* import { hexToNumber } from 'viem'
|
174
|
+
* const data = hexToNumber('0x1a4')
|
175
|
+
* // 420
|
176
|
+
*
|
177
|
+
* @example
|
178
|
+
* import { hexToNumber } from 'viem'
|
179
|
+
* const data = hexToBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
|
180
|
+
* // 420
|
65
181
|
*/
|
66
|
-
export function hexToNumber(hex: Hex, opts:
|
182
|
+
export function hexToNumber(hex: Hex, opts: HexToNumberOpts = {}): number {
|
67
183
|
return Number(hexToBigInt(hex, opts))
|
68
184
|
}
|
69
185
|
|
186
|
+
export type HexToStringOpts = {
|
187
|
+
/** Size (in bytes) of the hex value. */
|
188
|
+
size?: number
|
189
|
+
}
|
190
|
+
|
70
191
|
/**
|
71
|
-
*
|
192
|
+
* Decodes a hex value into a UTF-8 string.
|
193
|
+
*
|
194
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextostring
|
195
|
+
*
|
196
|
+
* @param hex Hex value to decode.
|
197
|
+
* @param opts Options.
|
198
|
+
* @returns String value.
|
199
|
+
*
|
200
|
+
* @example
|
201
|
+
* import { hexToString } from 'viem'
|
202
|
+
* const data = hexToString('0x48656c6c6f20576f726c6421')
|
203
|
+
* // 'Hello world!'
|
204
|
+
*
|
205
|
+
* @example
|
206
|
+
* import { hexToString } from 'viem'
|
207
|
+
* const data = hexToString('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', {
|
208
|
+
* size: 32,
|
209
|
+
* })
|
210
|
+
* // 'Hello world'
|
72
211
|
*/
|
73
|
-
export function hexToString(hex: Hex): string {
|
74
|
-
|
212
|
+
export function hexToString(hex: Hex, opts: HexToStringOpts = {}): string {
|
213
|
+
let bytes = hexToBytes(hex)
|
214
|
+
if (opts.size) {
|
215
|
+
assertSize(bytes, { size: opts.size })
|
216
|
+
bytes = trim(bytes, { dir: 'right' })
|
217
|
+
}
|
75
218
|
return new TextDecoder().decode(bytes)
|
76
219
|
}
|
@@ -1,62 +1,192 @@
|
|
1
1
|
import { BaseError } from '../../errors/index.js'
|
2
2
|
import type { ByteArray, Hex } from '../../types/index.js'
|
3
3
|
import { isHex } from '../data/isHex.js'
|
4
|
+
import { pad } from '../data/pad.js'
|
5
|
+
import { assertSize } from './fromHex.js'
|
4
6
|
import type { NumberToHexOpts } from './toHex.js'
|
5
7
|
import { numberToHex } from './toHex.js'
|
6
8
|
|
7
9
|
const encoder = new TextEncoder()
|
8
10
|
|
9
|
-
|
11
|
+
export type ToBytesParameters = {
|
12
|
+
/** Size of the output bytes. */
|
13
|
+
size?: number
|
14
|
+
}
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.
|
18
|
+
*
|
19
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html
|
20
|
+
* - Example: https://viem.sh/docs/utilities/toBytes.html#usage
|
21
|
+
*
|
22
|
+
* @param value Value to encode.
|
23
|
+
* @param opts Options.
|
24
|
+
* @returns Byte array value.
|
25
|
+
*
|
26
|
+
* @example
|
27
|
+
* import { toBytes } from 'viem'
|
28
|
+
* const data = toBytes('Hello world')
|
29
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
30
|
+
*
|
31
|
+
* @example
|
32
|
+
* import { toBytes } from 'viem'
|
33
|
+
* const data = toBytes(420)
|
34
|
+
* // Uint8Array([1, 164])
|
35
|
+
*
|
36
|
+
* @example
|
37
|
+
* import { toBytes } from 'viem'
|
38
|
+
* const data = toBytes(420, { size: 4 })
|
39
|
+
* // Uint8Array([0, 0, 1, 164])
|
40
|
+
*/
|
10
41
|
export function toBytes(
|
11
42
|
value: string | bigint | number | boolean | Hex,
|
43
|
+
opts: ToBytesParameters = {},
|
12
44
|
): ByteArray {
|
13
45
|
if (typeof value === 'number' || typeof value === 'bigint')
|
14
|
-
return numberToBytes(value)
|
15
|
-
if (typeof value === 'boolean') return boolToBytes(value)
|
16
|
-
if (isHex(value)) return hexToBytes(value)
|
17
|
-
return stringToBytes(value)
|
46
|
+
return numberToBytes(value, opts)
|
47
|
+
if (typeof value === 'boolean') return boolToBytes(value, opts)
|
48
|
+
if (isHex(value)) return hexToBytes(value, opts)
|
49
|
+
return stringToBytes(value, opts)
|
50
|
+
}
|
51
|
+
|
52
|
+
export type BoolToHexOpts = {
|
53
|
+
/** Size of the output bytes. */
|
54
|
+
size?: number
|
18
55
|
}
|
19
56
|
|
20
57
|
/**
|
21
|
-
*
|
58
|
+
* Encodes a boolean into a byte array.
|
59
|
+
*
|
60
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#booltobytes
|
61
|
+
*
|
62
|
+
* @param value Boolean value to encode.
|
63
|
+
* @param opts Options.
|
64
|
+
* @returns Byte array value.
|
65
|
+
*
|
66
|
+
* @example
|
67
|
+
* import { boolToBytes } from 'viem'
|
68
|
+
* const data = boolToBytes(true)
|
69
|
+
* // Uint8Array([1])
|
70
|
+
*
|
71
|
+
* @example
|
72
|
+
* import { boolToBytes } from 'viem'
|
73
|
+
* const data = boolToBytes(true, { size: 32 })
|
74
|
+
* // Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
|
22
75
|
*/
|
23
|
-
export function boolToBytes(value: boolean) {
|
76
|
+
export function boolToBytes(value: boolean, opts: BoolToHexOpts = {}) {
|
24
77
|
const bytes = new Uint8Array(1)
|
25
78
|
bytes[0] = Number(value)
|
79
|
+
if (typeof opts.size === 'number') {
|
80
|
+
assertSize(bytes, { size: opts.size })
|
81
|
+
return pad(bytes, { size: opts.size })
|
82
|
+
}
|
26
83
|
return bytes
|
27
84
|
}
|
28
85
|
|
86
|
+
export type HexToBytesOpts = {
|
87
|
+
/** Size of the output bytes. */
|
88
|
+
size?: number
|
89
|
+
}
|
90
|
+
|
29
91
|
/**
|
30
|
-
*
|
92
|
+
* Encodes a hex string into a byte array.
|
93
|
+
*
|
94
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#hextobytes
|
95
|
+
*
|
96
|
+
* @param hex Hex string to encode.
|
97
|
+
* @param opts Options.
|
98
|
+
* @returns Byte array value.
|
99
|
+
*
|
100
|
+
* @example
|
101
|
+
* import { hexToBytes } from 'viem'
|
102
|
+
* const data = hexToBytes('0x48656c6c6f20776f726c6421')
|
103
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
104
|
+
*
|
105
|
+
* @example
|
106
|
+
* import { hexToBytes } from 'viem'
|
107
|
+
* const data = hexToBytes('0x48656c6c6f20776f726c6421', { size: 32 })
|
108
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
31
109
|
*/
|
32
|
-
export function hexToBytes(hex_: Hex): ByteArray {
|
33
|
-
let hex = hex_
|
110
|
+
export function hexToBytes(hex_: Hex, opts: HexToBytesOpts = {}): ByteArray {
|
111
|
+
let hex = hex_
|
112
|
+
if (opts.size) {
|
113
|
+
assertSize(hex, { size: opts.size })
|
114
|
+
hex = pad(hex, { dir: 'right', size: opts.size })
|
115
|
+
}
|
34
116
|
|
35
|
-
|
117
|
+
let hexString = hex.slice(2) as string
|
118
|
+
if (hexString.length % 2) hexString = `0${hexString}`
|
36
119
|
|
37
|
-
const bytes = new Uint8Array(
|
120
|
+
const bytes = new Uint8Array(hexString.length / 2)
|
38
121
|
for (let index = 0; index < bytes.length; index++) {
|
39
122
|
const start = index * 2
|
40
|
-
const hexByte =
|
123
|
+
const hexByte = hexString.slice(start, start + 2)
|
41
124
|
const byte = Number.parseInt(hexByte, 16)
|
42
125
|
if (Number.isNaN(byte) || byte < 0)
|
43
|
-
throw new BaseError(
|
126
|
+
throw new BaseError(
|
127
|
+
`Invalid byte sequence ("${hexByte}" in "${hexString}").`,
|
128
|
+
)
|
44
129
|
bytes[index] = byte
|
45
130
|
}
|
46
131
|
return bytes
|
47
132
|
}
|
48
133
|
|
49
134
|
/**
|
50
|
-
*
|
135
|
+
* Encodes a number into a byte array.
|
136
|
+
*
|
137
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#numbertobytes
|
138
|
+
*
|
139
|
+
* @param value Number to encode.
|
140
|
+
* @param opts Options.
|
141
|
+
* @returns Byte array value.
|
142
|
+
*
|
143
|
+
* @example
|
144
|
+
* import { numberToBytes } from 'viem'
|
145
|
+
* const data = numberToBytes(420)
|
146
|
+
* // Uint8Array([1, 164])
|
147
|
+
*
|
148
|
+
* @example
|
149
|
+
* import { numberToBytes } from 'viem'
|
150
|
+
* const data = numberToBytes(420, { size: 4 })
|
151
|
+
* // Uint8Array([0, 0, 1, 164])
|
51
152
|
*/
|
52
153
|
export function numberToBytes(value: bigint | number, opts?: NumberToHexOpts) {
|
53
154
|
const hex = numberToHex(value, opts)
|
54
155
|
return hexToBytes(hex)
|
55
156
|
}
|
56
157
|
|
158
|
+
export type StringToBytesOpts = {
|
159
|
+
/** Size of the output bytes. */
|
160
|
+
size?: number
|
161
|
+
}
|
162
|
+
|
57
163
|
/**
|
58
|
-
*
|
164
|
+
* Encodes a UTF-8 string into a byte array.
|
165
|
+
*
|
166
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#stringtobytes
|
167
|
+
*
|
168
|
+
* @param value String to encode.
|
169
|
+
* @param opts Options.
|
170
|
+
* @returns Byte array value.
|
171
|
+
*
|
172
|
+
* @example
|
173
|
+
* import { stringToBytes } from 'viem'
|
174
|
+
* const data = stringToBytes('Hello world!')
|
175
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
|
176
|
+
*
|
177
|
+
* @example
|
178
|
+
* import { stringToBytes } from 'viem'
|
179
|
+
* const data = stringToBytes('Hello world!', { size: 32 })
|
180
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
59
181
|
*/
|
60
|
-
export function stringToBytes(
|
61
|
-
|
182
|
+
export function stringToBytes(
|
183
|
+
value: string,
|
184
|
+
opts: StringToBytesOpts = {},
|
185
|
+
): ByteArray {
|
186
|
+
const bytes = encoder.encode(value)
|
187
|
+
if (typeof opts.size === 'number') {
|
188
|
+
assertSize(bytes, { size: opts.size })
|
189
|
+
return pad(bytes, { dir: 'right', size: opts.size })
|
190
|
+
}
|
191
|
+
return bytes
|
62
192
|
}
|
@@ -1,58 +1,162 @@
|
|
1
1
|
import { pad } from '../data/index.js'
|
2
2
|
import type { ByteArray, Hex } from '../../types/index.js'
|
3
3
|
import { IntegerOutOfRangeError } from '../../errors/index.js'
|
4
|
+
import { assertSize } from './fromHex.js'
|
4
5
|
|
5
6
|
const hexes = Array.from({ length: 256 }, (_v, i) =>
|
6
7
|
i.toString(16).padStart(2, '0'),
|
7
8
|
)
|
8
9
|
|
10
|
+
export type ToHexParameters = {
|
11
|
+
/** The size (in bytes) of the output hex value. */
|
12
|
+
size?: number
|
13
|
+
}
|
14
|
+
|
9
15
|
/**
|
10
|
-
*
|
16
|
+
* Encodes a string, number, bigint, or ByteArray into a hex string
|
17
|
+
*
|
18
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html
|
19
|
+
* - Example: https://viem.sh/docs/utilities/toHex.html#usage
|
20
|
+
*
|
21
|
+
* @param value Value to encode.
|
22
|
+
* @param opts Options.
|
23
|
+
* @returns Hex value.
|
24
|
+
*
|
25
|
+
* @example
|
26
|
+
* import { toHex } from 'viem'
|
27
|
+
* const data = toHex('Hello world')
|
28
|
+
* // '0x48656c6c6f20776f726c6421'
|
29
|
+
*
|
30
|
+
* @example
|
31
|
+
* import { toHex } from 'viem'
|
32
|
+
* const data = toHex(420)
|
33
|
+
* // '0x1a4'
|
34
|
+
*
|
35
|
+
* @example
|
36
|
+
* import { toHex } from 'viem'
|
37
|
+
* const data = toHex('Hello world', { size: 32 })
|
38
|
+
* // '0x48656c6c6f20776f726c64210000000000000000000000000000000000000000'
|
11
39
|
*/
|
12
|
-
export function
|
13
|
-
|
40
|
+
export function toHex(
|
41
|
+
value: string | number | bigint | boolean | ByteArray,
|
42
|
+
opts: ToHexParameters = {},
|
43
|
+
): Hex {
|
44
|
+
if (typeof value === 'number' || typeof value === 'bigint')
|
45
|
+
return numberToHex(value, opts)
|
46
|
+
if (typeof value === 'string') {
|
47
|
+
return stringToHex(value, opts)
|
48
|
+
}
|
49
|
+
if (typeof value === 'boolean') return boolToHex(value, opts)
|
50
|
+
return bytesToHex(value, opts)
|
51
|
+
}
|
52
|
+
|
53
|
+
export type BoolToHexOpts = {
|
54
|
+
/** The size (in bytes) of the output hex value. */
|
55
|
+
size?: number
|
14
56
|
}
|
15
57
|
|
16
58
|
/**
|
17
|
-
*
|
59
|
+
* Encodes a boolean into a hex string
|
60
|
+
*
|
61
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#booltohex
|
62
|
+
*
|
63
|
+
* @param value Value to encode.
|
64
|
+
* @param opts Options.
|
65
|
+
* @returns Hex value.
|
66
|
+
*
|
67
|
+
* @example
|
68
|
+
* import { boolToHex } from 'viem'
|
69
|
+
* const data = boolToHex(true)
|
70
|
+
* // '0x1'
|
71
|
+
*
|
72
|
+
* @example
|
73
|
+
* import { boolToHex } from 'viem'
|
74
|
+
* const data = boolToHex(false)
|
75
|
+
* // '0x0'
|
76
|
+
*
|
77
|
+
* @example
|
78
|
+
* import { boolToHex } from 'viem'
|
79
|
+
* const data = boolToHex(true, { size: 32 })
|
80
|
+
* // '0x0000000000000000000000000000000000000000000000000000000000000001'
|
18
81
|
*/
|
19
|
-
export function
|
20
|
-
|
21
|
-
|
22
|
-
hex
|
82
|
+
export function boolToHex(value: boolean, opts: BoolToHexOpts = {}): Hex {
|
83
|
+
const hex: Hex = `0x${Number(value)}`
|
84
|
+
if (typeof opts.size === 'number') {
|
85
|
+
assertSize(hex, { size: opts.size })
|
86
|
+
return pad(hex, { size: opts.size })
|
23
87
|
}
|
24
|
-
return
|
88
|
+
return hex
|
89
|
+
}
|
90
|
+
|
91
|
+
export type BytesToHexOpts = {
|
92
|
+
/** The size (in bytes) of the output hex value. */
|
93
|
+
size?: number
|
25
94
|
}
|
26
95
|
|
27
96
|
/**
|
28
|
-
*
|
97
|
+
* Encodes a bytes array into a hex string
|
98
|
+
*
|
99
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#bytestohex
|
100
|
+
*
|
101
|
+
* @param value Value to encode.
|
102
|
+
* @param opts Options.
|
103
|
+
* @returns Hex value.
|
104
|
+
*
|
105
|
+
* @example
|
106
|
+
* import { bytesToHex } from 'viem'
|
107
|
+
* const data = bytesToHex(Uint8Array.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
108
|
+
* // '0x48656c6c6f20576f726c6421'
|
109
|
+
*
|
110
|
+
* @example
|
111
|
+
* import { bytesToHex } from 'viem'
|
112
|
+
* const data = bytesToHex(Uint8Array.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]), { size: 32 })
|
113
|
+
* // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
|
29
114
|
*/
|
30
|
-
export function
|
31
|
-
|
32
|
-
)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
115
|
+
export function bytesToHex(value: ByteArray, opts: BytesToHexOpts = {}): Hex {
|
116
|
+
let hexString = ''
|
117
|
+
for (let i = 0; i < value.length; i++) {
|
118
|
+
hexString += hexes[value[i]]
|
119
|
+
}
|
120
|
+
|
121
|
+
const hex: Hex = `0x${hexString}`
|
122
|
+
if (typeof opts.size === 'number') {
|
123
|
+
assertSize(hex, { size: opts.size })
|
124
|
+
return pad(hex, { dir: 'right', size: opts.size })
|
37
125
|
}
|
38
|
-
|
39
|
-
return bytesToHex(value)
|
126
|
+
return hex
|
40
127
|
}
|
41
128
|
|
42
129
|
export type NumberToHexOpts =
|
43
130
|
| {
|
44
|
-
|
131
|
+
/** Whether or not the number of a signed representation. */
|
45
132
|
signed?: boolean
|
46
|
-
|
133
|
+
/** The size (in bytes) of the output hex value. */
|
47
134
|
size: number
|
48
135
|
}
|
49
136
|
| {
|
50
137
|
signed?: never
|
51
|
-
size
|
138
|
+
/** The size (in bytes) of the output hex value. */
|
139
|
+
size?: number
|
52
140
|
}
|
53
141
|
|
54
142
|
/**
|
55
|
-
*
|
143
|
+
* Encodes a number or bigint into a hex string
|
144
|
+
*
|
145
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#numbertohex
|
146
|
+
*
|
147
|
+
* @param value Value to encode.
|
148
|
+
* @param opts Options.
|
149
|
+
* @returns Hex value.
|
150
|
+
*
|
151
|
+
* @example
|
152
|
+
* import { numberToHex } from 'viem'
|
153
|
+
* const data = numberToHex(420)
|
154
|
+
* // '0x1a4'
|
155
|
+
*
|
156
|
+
* @example
|
157
|
+
* import { numberToHex } from 'viem'
|
158
|
+
* const data = numberToHex(420, { size: 32 })
|
159
|
+
* // '0x00000000000000000000000000000000000000000000000000000000000001a4'
|
56
160
|
*/
|
57
161
|
export function numberToHex(
|
58
162
|
value_: number | bigint,
|
@@ -91,12 +195,33 @@ export function numberToHex(
|
|
91
195
|
return hex
|
92
196
|
}
|
93
197
|
|
198
|
+
export type StringToHexOpts = {
|
199
|
+
/** The size (in bytes) of the output hex value. */
|
200
|
+
size?: number
|
201
|
+
}
|
202
|
+
|
94
203
|
const encoder = new TextEncoder()
|
95
204
|
|
96
205
|
/**
|
97
|
-
*
|
206
|
+
* Encodes a UTF-8 string into a hex string
|
207
|
+
*
|
208
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#stringtohex
|
209
|
+
*
|
210
|
+
* @param value Value to encode.
|
211
|
+
* @param opts Options.
|
212
|
+
* @returns Hex value.
|
213
|
+
*
|
214
|
+
* @example
|
215
|
+
* import { stringToHex } from 'viem'
|
216
|
+
* const data = stringToHex('Hello World!')
|
217
|
+
* // '0x48656c6c6f20576f726c6421'
|
218
|
+
*
|
219
|
+
* @example
|
220
|
+
* import { stringToHex } from 'viem'
|
221
|
+
* const data = stringToHex('Hello World!', { size: 32 })
|
222
|
+
* // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
|
98
223
|
*/
|
99
|
-
export function stringToHex(value_: string): Hex {
|
224
|
+
export function stringToHex(value_: string, opts: StringToHexOpts = {}): Hex {
|
100
225
|
const value = encoder.encode(value_)
|
101
|
-
return
|
226
|
+
return bytesToHex(value, opts)
|
102
227
|
}
|
@@ -33,11 +33,12 @@ export function getContractError(
|
|
33
33
|
sender?: Address
|
34
34
|
},
|
35
35
|
) {
|
36
|
-
const { code, data, message } = (
|
36
|
+
const { code, data, message, shortMessage } = (
|
37
37
|
err instanceof RawContractError
|
38
38
|
? err
|
39
|
-
: err
|
40
|
-
err instanceof
|
39
|
+
: !(err.cause && 'data' in (err.cause as BaseError)) &&
|
40
|
+
(err instanceof CallExecutionError ||
|
41
|
+
err instanceof EstimateGasExecutionError)
|
41
42
|
? ((err.cause as BaseError)?.cause as BaseError)?.cause || {}
|
42
43
|
: err.cause || {}
|
43
44
|
) as RawContractError
|
@@ -45,12 +46,15 @@ export function getContractError(
|
|
45
46
|
let cause = err
|
46
47
|
if (err instanceof AbiDecodingZeroDataError) {
|
47
48
|
cause = new ContractFunctionZeroDataError({ functionName })
|
48
|
-
} else if (
|
49
|
+
} else if (
|
50
|
+
code === EXECUTION_REVERTED_ERROR_CODE &&
|
51
|
+
(data || message || shortMessage)
|
52
|
+
) {
|
49
53
|
cause = new ContractFunctionRevertedError({
|
50
54
|
abi,
|
51
55
|
data,
|
52
56
|
functionName,
|
53
|
-
message,
|
57
|
+
message: shortMessage ?? message,
|
54
58
|
})
|
55
59
|
}
|
56
60
|
|