viem 2.53.1 → 2.54.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/_cjs/actions/index.js +3 -2
- package/_cjs/actions/index.js.map +1 -1
- package/_cjs/actions/token/approve.js +62 -0
- package/_cjs/actions/token/approve.js.map +1 -0
- package/_cjs/actions/token/approveSync.js +25 -0
- package/_cjs/actions/token/approveSync.js.map +1 -0
- package/_cjs/actions/token/getAllowance.js +32 -0
- package/_cjs/actions/token/getAllowance.js.map +1 -0
- package/_cjs/actions/token/getBalance.js +41 -0
- package/_cjs/actions/token/getBalance.js.map +1 -0
- package/_cjs/actions/token/getMetadata.js +40 -0
- package/_cjs/actions/token/getMetadata.js.map +1 -0
- package/_cjs/actions/token/getTotalSupply.js +32 -0
- package/_cjs/actions/token/getTotalSupply.js.map +1 -0
- package/_cjs/actions/token/index.js +20 -0
- package/_cjs/actions/token/index.js.map +1 -0
- package/_cjs/actions/token/internal.js +125 -0
- package/_cjs/actions/token/internal.js.map +1 -0
- package/_cjs/actions/token/transfer.js +70 -0
- package/_cjs/actions/token/transfer.js.map +1 -0
- package/_cjs/actions/token/transferSync.js +25 -0
- package/_cjs/actions/token/transferSync.js.map +1 -0
- package/_cjs/chains/definitions/battlechain.js +31 -0
- package/_cjs/chains/definitions/battlechain.js.map +1 -0
- package/_cjs/chains/definitions/marooTestnet.js +30 -0
- package/_cjs/chains/definitions/marooTestnet.js.map +1 -0
- package/_cjs/chains/index.js +18 -14
- package/_cjs/chains/index.js.map +1 -1
- package/_cjs/clients/createClient.js +39 -1
- package/_cjs/clients/createClient.js.map +1 -1
- package/_cjs/clients/createPublicClient.js.map +1 -1
- package/_cjs/clients/createWalletClient.js.map +1 -1
- package/_cjs/clients/decorators/public.js +11 -0
- package/_cjs/clients/decorators/public.js.map +1 -1
- package/_cjs/clients/decorators/wallet.js +8 -0
- package/_cjs/clients/decorators/wallet.js.map +1 -1
- package/_cjs/clients/transports/http.js +2 -1
- package/_cjs/clients/transports/http.js.map +1 -1
- package/_cjs/errors/encoding.js +9 -1
- package/_cjs/errors/encoding.js.map +1 -1
- package/_cjs/errors/request.js +24 -1
- package/_cjs/errors/request.js.map +1 -1
- package/_cjs/errors/transaction.js +9 -1
- package/_cjs/errors/transaction.js.map +1 -1
- package/_cjs/errors/version.js +1 -1
- package/_cjs/index.js +9 -6
- package/_cjs/index.js.map +1 -1
- package/_cjs/tempo/Client.js.map +1 -1
- package/_cjs/tempo/Decorator.js +234 -215
- package/_cjs/tempo/Decorator.js.map +1 -1
- package/_cjs/tempo/actions/token.js +210 -112
- package/_cjs/tempo/actions/token.js.map +1 -1
- package/_cjs/tempo/internal/utils.js +108 -0
- package/_cjs/tempo/internal/utils.js.map +1 -1
- package/_cjs/tokens/defineToken.js +21 -0
- package/_cjs/tokens/defineToken.js.map +1 -0
- package/_cjs/tokens/definitions/alphausd.js +14 -0
- package/_cjs/tokens/definitions/alphausd.js.map +1 -0
- package/_cjs/tokens/definitions/betausd.js +14 -0
- package/_cjs/tokens/definitions/betausd.js.map +1 -0
- package/_cjs/tokens/definitions/brla.js +14 -0
- package/_cjs/tokens/definitions/brla.js.map +1 -0
- package/_cjs/tokens/definitions/cbbtc.js +14 -0
- package/_cjs/tokens/definitions/cbbtc.js.map +1 -0
- package/_cjs/tokens/definitions/chfau.js +14 -0
- package/_cjs/tokens/definitions/chfau.js.map +1 -0
- package/_cjs/tokens/definitions/cusd.js +14 -0
- package/_cjs/tokens/definitions/cusd.js.map +1 -0
- package/_cjs/tokens/definitions/dlusd.js +14 -0
- package/_cjs/tokens/definitions/dlusd.js.map +1 -0
- package/_cjs/tokens/definitions/eurau.js +14 -0
- package/_cjs/tokens/definitions/eurau.js.map +1 -0
- package/_cjs/tokens/definitions/eurce.js +15 -0
- package/_cjs/tokens/definitions/eurce.js.map +1 -0
- package/_cjs/tokens/definitions/frxusd.js +14 -0
- package/_cjs/tokens/definitions/frxusd.js.map +1 -0
- package/_cjs/tokens/definitions/gbpa.js +14 -0
- package/_cjs/tokens/definitions/gbpa.js.map +1 -0
- package/_cjs/tokens/definitions/gusd.js +14 -0
- package/_cjs/tokens/definitions/gusd.js.map +1 -0
- package/_cjs/tokens/definitions/iusd.js +14 -0
- package/_cjs/tokens/definitions/iusd.js.map +1 -0
- package/_cjs/tokens/definitions/pathusd.js +15 -0
- package/_cjs/tokens/definitions/pathusd.js.map +1 -0
- package/_cjs/tokens/definitions/reusd.js +14 -0
- package/_cjs/tokens/definitions/reusd.js.map +1 -0
- package/_cjs/tokens/definitions/rusd.js +14 -0
- package/_cjs/tokens/definitions/rusd.js.map +1 -0
- package/_cjs/tokens/definitions/sbc.js +14 -0
- package/_cjs/tokens/definitions/sbc.js.map +1 -0
- package/_cjs/tokens/definitions/siusd.js +14 -0
- package/_cjs/tokens/definitions/siusd.js.map +1 -0
- package/_cjs/tokens/definitions/stcusd.js +14 -0
- package/_cjs/tokens/definitions/stcusd.js.map +1 -0
- package/_cjs/tokens/definitions/susde.js +14 -0
- package/_cjs/tokens/definitions/susde.js.map +1 -0
- package/_cjs/tokens/definitions/syrupusdc.js +14 -0
- package/_cjs/tokens/definitions/syrupusdc.js.map +1 -0
- package/_cjs/tokens/definitions/thetausd.js +14 -0
- package/_cjs/tokens/definitions/thetausd.js.map +1 -0
- package/_cjs/tokens/definitions/usd1.js +14 -0
- package/_cjs/tokens/definitions/usd1.js.map +1 -0
- package/_cjs/tokens/definitions/usdb.js +14 -0
- package/_cjs/tokens/definitions/usdb.js.map +1 -0
- package/_cjs/tokens/definitions/usdc.js +41 -0
- package/_cjs/tokens/definitions/usdc.js.map +1 -0
- package/_cjs/tokens/definitions/usdce.js +15 -0
- package/_cjs/tokens/definitions/usdce.js.map +1 -0
- package/_cjs/tokens/definitions/usde.js +14 -0
- package/_cjs/tokens/definitions/usde.js.map +1 -0
- package/_cjs/tokens/definitions/usdt0.js +14 -0
- package/_cjs/tokens/definitions/usdt0.js.map +1 -0
- package/_cjs/tokens/definitions/wsrusd.js +14 -0
- package/_cjs/tokens/definitions/wsrusd.js.map +1 -0
- package/_cjs/tokens/index.js +64 -0
- package/_cjs/tokens/index.js.map +1 -0
- package/_cjs/utils/encoding/fromRlp.js +7 -4
- package/_cjs/utils/encoding/fromRlp.js.map +1 -1
- package/_cjs/utils/rpc/http.js +58 -3
- package/_cjs/utils/rpc/http.js.map +1 -1
- package/_cjs/utils/transaction/parseTransaction.js +4 -1
- package/_cjs/utils/transaction/parseTransaction.js.map +1 -1
- package/_esm/actions/index.js +1 -0
- package/_esm/actions/index.js.map +1 -1
- package/_esm/actions/token/approve.js +123 -0
- package/_esm/actions/token/approve.js.map +1 -0
- package/_esm/actions/token/approveSync.js +50 -0
- package/_esm/actions/token/approveSync.js.map +1 -0
- package/_esm/actions/token/getAllowance.js +62 -0
- package/_esm/actions/token/getAllowance.js.map +1 -0
- package/_esm/actions/token/getBalance.js +70 -0
- package/_esm/actions/token/getBalance.js.map +1 -0
- package/_esm/actions/token/getMetadata.js +60 -0
- package/_esm/actions/token/getMetadata.js.map +1 -0
- package/_esm/actions/token/getTotalSupply.js +60 -0
- package/_esm/actions/token/getTotalSupply.js.map +1 -0
- package/_esm/actions/token/index.js +10 -0
- package/_esm/actions/token/index.js.map +1 -0
- package/_esm/actions/token/internal.js +188 -0
- package/_esm/actions/token/internal.js.map +1 -0
- package/_esm/actions/token/transfer.js +147 -0
- package/_esm/actions/token/transfer.js.map +1 -0
- package/_esm/actions/token/transferSync.js +54 -0
- package/_esm/actions/token/transferSync.js.map +1 -0
- package/_esm/chains/definitions/battlechain.js +28 -0
- package/_esm/chains/definitions/battlechain.js.map +1 -0
- package/_esm/chains/definitions/marooTestnet.js +27 -0
- package/_esm/chains/definitions/marooTestnet.js.map +1 -0
- package/_esm/chains/index.js +2 -0
- package/_esm/chains/index.js.map +1 -1
- package/_esm/clients/createClient.js +53 -1
- package/_esm/clients/createClient.js.map +1 -1
- package/_esm/clients/createPublicClient.js.map +1 -1
- package/_esm/clients/createWalletClient.js.map +1 -1
- package/_esm/clients/decorators/public.js +17 -0
- package/_esm/clients/decorators/public.js.map +1 -1
- package/_esm/clients/decorators/wallet.js +8 -0
- package/_esm/clients/decorators/wallet.js.map +1 -1
- package/_esm/clients/transports/http.js +2 -1
- package/_esm/clients/transports/http.js.map +1 -1
- package/_esm/errors/encoding.js +7 -0
- package/_esm/errors/encoding.js.map +1 -1
- package/_esm/errors/request.js +22 -0
- package/_esm/errors/request.js.map +1 -1
- package/_esm/errors/transaction.js +7 -0
- package/_esm/errors/transaction.js.map +1 -1
- package/_esm/errors/version.js +1 -1
- package/_esm/index.js +3 -3
- package/_esm/index.js.map +1 -1
- package/_esm/tempo/Client.js.map +1 -1
- package/_esm/tempo/Decorator.js +234 -218
- package/_esm/tempo/Decorator.js.map +1 -1
- package/_esm/tempo/actions/token.js +335 -186
- package/_esm/tempo/actions/token.js.map +1 -1
- package/_esm/tempo/actions/wallet.js +1 -1
- package/_esm/tempo/internal/utils.js +154 -0
- package/_esm/tempo/internal/utils.js.map +1 -1
- package/_esm/tokens/defineToken.js +57 -0
- package/_esm/tokens/defineToken.js.map +1 -0
- package/_esm/tokens/definitions/alphausd.js +12 -0
- package/_esm/tokens/definitions/alphausd.js.map +1 -0
- package/_esm/tokens/definitions/betausd.js +12 -0
- package/_esm/tokens/definitions/betausd.js.map +1 -0
- package/_esm/tokens/definitions/brla.js +12 -0
- package/_esm/tokens/definitions/brla.js.map +1 -0
- package/_esm/tokens/definitions/cbbtc.js +12 -0
- package/_esm/tokens/definitions/cbbtc.js.map +1 -0
- package/_esm/tokens/definitions/chfau.js +12 -0
- package/_esm/tokens/definitions/chfau.js.map +1 -0
- package/_esm/tokens/definitions/cusd.js +12 -0
- package/_esm/tokens/definitions/cusd.js.map +1 -0
- package/_esm/tokens/definitions/dlusd.js +12 -0
- package/_esm/tokens/definitions/dlusd.js.map +1 -0
- package/_esm/tokens/definitions/eurau.js +12 -0
- package/_esm/tokens/definitions/eurau.js.map +1 -0
- package/_esm/tokens/definitions/eurce.js +13 -0
- package/_esm/tokens/definitions/eurce.js.map +1 -0
- package/_esm/tokens/definitions/frxusd.js +12 -0
- package/_esm/tokens/definitions/frxusd.js.map +1 -0
- package/_esm/tokens/definitions/gbpa.js +12 -0
- package/_esm/tokens/definitions/gbpa.js.map +1 -0
- package/_esm/tokens/definitions/gusd.js +12 -0
- package/_esm/tokens/definitions/gusd.js.map +1 -0
- package/_esm/tokens/definitions/iusd.js +12 -0
- package/_esm/tokens/definitions/iusd.js.map +1 -0
- package/_esm/tokens/definitions/pathusd.js +13 -0
- package/_esm/tokens/definitions/pathusd.js.map +1 -0
- package/_esm/tokens/definitions/reusd.js +12 -0
- package/_esm/tokens/definitions/reusd.js.map +1 -0
- package/_esm/tokens/definitions/rusd.js +12 -0
- package/_esm/tokens/definitions/rusd.js.map +1 -0
- package/_esm/tokens/definitions/sbc.js +12 -0
- package/_esm/tokens/definitions/sbc.js.map +1 -0
- package/_esm/tokens/definitions/siusd.js +12 -0
- package/_esm/tokens/definitions/siusd.js.map +1 -0
- package/_esm/tokens/definitions/stcusd.js +12 -0
- package/_esm/tokens/definitions/stcusd.js.map +1 -0
- package/_esm/tokens/definitions/susde.js +12 -0
- package/_esm/tokens/definitions/susde.js.map +1 -0
- package/_esm/tokens/definitions/syrupusdc.js +12 -0
- package/_esm/tokens/definitions/syrupusdc.js.map +1 -0
- package/_esm/tokens/definitions/thetausd.js +12 -0
- package/_esm/tokens/definitions/thetausd.js.map +1 -0
- package/_esm/tokens/definitions/usd1.js +12 -0
- package/_esm/tokens/definitions/usd1.js.map +1 -0
- package/_esm/tokens/definitions/usdb.js +12 -0
- package/_esm/tokens/definitions/usdb.js.map +1 -0
- package/_esm/tokens/definitions/usdc.js +67 -0
- package/_esm/tokens/definitions/usdc.js.map +1 -0
- package/_esm/tokens/definitions/usdce.js +13 -0
- package/_esm/tokens/definitions/usdce.js.map +1 -0
- package/_esm/tokens/definitions/usde.js +12 -0
- package/_esm/tokens/definitions/usde.js.map +1 -0
- package/_esm/tokens/definitions/usdt0.js +12 -0
- package/_esm/tokens/definitions/usdt0.js.map +1 -0
- package/_esm/tokens/definitions/wsrusd.js +12 -0
- package/_esm/tokens/definitions/wsrusd.js.map +1 -0
- package/_esm/tokens/index.js +32 -0
- package/_esm/tokens/index.js.map +1 -0
- package/_esm/utils/encoding/fromRlp.js +8 -5
- package/_esm/utils/encoding/fromRlp.js.map +1 -1
- package/_esm/utils/rpc/http.js +59 -4
- package/_esm/utils/rpc/http.js.map +1 -1
- package/_esm/utils/transaction/parseTransaction.js +5 -2
- package/_esm/utils/transaction/parseTransaction.js.map +1 -1
- package/_types/actions/index.d.ts +1 -0
- package/_types/actions/index.d.ts.map +1 -1
- package/_types/actions/token/approve.d.ts +253 -0
- package/_types/actions/token/approve.d.ts.map +1 -0
- package/_types/actions/token/approveSync.d.ts +57 -0
- package/_types/actions/token/approveSync.d.ts.map +1 -0
- package/_types/actions/token/getAllowance.d.ts +77 -0
- package/_types/actions/token/getAllowance.d.ts.map +1 -0
- package/_types/actions/token/getBalance.d.ts +69 -0
- package/_types/actions/token/getBalance.d.ts.map +1 -0
- package/_types/actions/token/getMetadata.d.ts +43 -0
- package/_types/actions/token/getMetadata.d.ts.map +1 -0
- package/_types/actions/token/getTotalSupply.d.ts +63 -0
- package/_types/actions/token/getTotalSupply.d.ts.map +1 -0
- package/_types/actions/token/index.d.ts +9 -0
- package/_types/actions/token/index.d.ts.map +1 -0
- package/_types/actions/token/internal.d.ts +168 -0
- package/_types/actions/token/internal.d.ts.map +1 -0
- package/_types/actions/token/transfer.d.ts +288 -0
- package/_types/actions/token/transfer.d.ts.map +1 -0
- package/_types/actions/token/transferSync.d.ts +61 -0
- package/_types/actions/token/transferSync.d.ts.map +1 -0
- package/_types/chains/definitions/battlechain.d.ts +447 -0
- package/_types/chains/definitions/battlechain.d.ts.map +1 -0
- package/_types/chains/definitions/marooTestnet.d.ts +49 -0
- package/_types/chains/definitions/marooTestnet.d.ts.map +1 -0
- package/_types/chains/index.d.ts +2 -0
- package/_types/chains/index.d.ts.map +1 -1
- package/_types/clients/createClient.d.ts +24 -6
- package/_types/clients/createClient.d.ts.map +1 -1
- package/_types/clients/createPublicClient.d.ts +4 -3
- package/_types/clients/createPublicClient.d.ts.map +1 -1
- package/_types/clients/createWalletClient.d.ts +4 -3
- package/_types/clients/createWalletClient.d.ts.map +1 -1
- package/_types/clients/decorators/public.d.ts +128 -3
- package/_types/clients/decorators/public.d.ts.map +1 -1
- package/_types/clients/decorators/wallet.d.ts +208 -3
- package/_types/clients/decorators/wallet.d.ts.map +1 -1
- package/_types/clients/transports/http.d.ts +2 -0
- package/_types/clients/transports/http.d.ts.map +1 -1
- package/_types/errors/encoding.d.ts +8 -0
- package/_types/errors/encoding.d.ts.map +1 -1
- package/_types/errors/request.d.ts +11 -0
- package/_types/errors/request.d.ts.map +1 -1
- package/_types/errors/transaction.d.ts +8 -0
- package/_types/errors/transaction.d.ts.map +1 -1
- package/_types/errors/version.d.ts +1 -1
- package/_types/index.d.ts +4 -3
- package/_types/index.d.ts.map +1 -1
- package/_types/tempo/Client.d.ts +1 -1
- package/_types/tempo/Client.d.ts.map +1 -1
- package/_types/tempo/Decorator.d.ts +88 -5
- package/_types/tempo/Decorator.d.ts.map +1 -1
- package/_types/tempo/actions/token.d.ts +223 -151
- package/_types/tempo/actions/token.d.ts.map +1 -1
- package/_types/tempo/actions/wallet.d.ts +3 -3
- package/_types/tempo/internal/types.d.ts +17 -0
- package/_types/tempo/internal/types.d.ts.map +1 -1
- package/_types/tempo/internal/utils.d.ts +73 -0
- package/_types/tempo/internal/utils.d.ts.map +1 -1
- package/_types/tokens/defineToken.d.ts +112 -0
- package/_types/tokens/defineToken.d.ts.map +1 -0
- package/_types/tokens/definitions/alphausd.d.ts +11 -0
- package/_types/tokens/definitions/alphausd.d.ts.map +1 -0
- package/_types/tokens/definitions/betausd.d.ts +11 -0
- package/_types/tokens/definitions/betausd.d.ts.map +1 -0
- package/_types/tokens/definitions/brla.d.ts +11 -0
- package/_types/tokens/definitions/brla.d.ts.map +1 -0
- package/_types/tokens/definitions/cbbtc.d.ts +11 -0
- package/_types/tokens/definitions/cbbtc.d.ts.map +1 -0
- package/_types/tokens/definitions/chfau.d.ts +11 -0
- package/_types/tokens/definitions/chfau.d.ts.map +1 -0
- package/_types/tokens/definitions/cusd.d.ts +11 -0
- package/_types/tokens/definitions/cusd.d.ts.map +1 -0
- package/_types/tokens/definitions/dlusd.d.ts +11 -0
- package/_types/tokens/definitions/dlusd.d.ts.map +1 -0
- package/_types/tokens/definitions/eurau.d.ts +11 -0
- package/_types/tokens/definitions/eurau.d.ts.map +1 -0
- package/_types/tokens/definitions/eurce.d.ts +12 -0
- package/_types/tokens/definitions/eurce.d.ts.map +1 -0
- package/_types/tokens/definitions/frxusd.d.ts +11 -0
- package/_types/tokens/definitions/frxusd.d.ts.map +1 -0
- package/_types/tokens/definitions/gbpa.d.ts +11 -0
- package/_types/tokens/definitions/gbpa.d.ts.map +1 -0
- package/_types/tokens/definitions/gusd.d.ts +11 -0
- package/_types/tokens/definitions/gusd.d.ts.map +1 -0
- package/_types/tokens/definitions/iusd.d.ts +11 -0
- package/_types/tokens/definitions/iusd.d.ts.map +1 -0
- package/_types/tokens/definitions/pathusd.d.ts +12 -0
- package/_types/tokens/definitions/pathusd.d.ts.map +1 -0
- package/_types/tokens/definitions/reusd.d.ts +11 -0
- package/_types/tokens/definitions/reusd.d.ts.map +1 -0
- package/_types/tokens/definitions/rusd.d.ts +11 -0
- package/_types/tokens/definitions/rusd.d.ts.map +1 -0
- package/_types/tokens/definitions/sbc.d.ts +11 -0
- package/_types/tokens/definitions/sbc.d.ts.map +1 -0
- package/_types/tokens/definitions/siusd.d.ts +11 -0
- package/_types/tokens/definitions/siusd.d.ts.map +1 -0
- package/_types/tokens/definitions/stcusd.d.ts +11 -0
- package/_types/tokens/definitions/stcusd.d.ts.map +1 -0
- package/_types/tokens/definitions/susde.d.ts +11 -0
- package/_types/tokens/definitions/susde.d.ts.map +1 -0
- package/_types/tokens/definitions/syrupusdc.d.ts +11 -0
- package/_types/tokens/definitions/syrupusdc.d.ts.map +1 -0
- package/_types/tokens/definitions/thetausd.d.ts +11 -0
- package/_types/tokens/definitions/thetausd.d.ts.map +1 -0
- package/_types/tokens/definitions/usd1.d.ts +11 -0
- package/_types/tokens/definitions/usd1.d.ts.map +1 -0
- package/_types/tokens/definitions/usdb.d.ts +11 -0
- package/_types/tokens/definitions/usdb.d.ts.map +1 -0
- package/_types/tokens/definitions/usdc.d.ts +67 -0
- package/_types/tokens/definitions/usdc.d.ts.map +1 -0
- package/_types/tokens/definitions/usdce.d.ts +12 -0
- package/_types/tokens/definitions/usdce.d.ts.map +1 -0
- package/_types/tokens/definitions/usde.d.ts +11 -0
- package/_types/tokens/definitions/usde.d.ts.map +1 -0
- package/_types/tokens/definitions/usdt0.d.ts +11 -0
- package/_types/tokens/definitions/usdt0.d.ts.map +1 -0
- package/_types/tokens/definitions/wsrusd.d.ts +11 -0
- package/_types/tokens/definitions/wsrusd.d.ts.map +1 -0
- package/_types/tokens/index.d.ts +31 -0
- package/_types/tokens/index.d.ts.map +1 -0
- package/_types/utils/encoding/fromRlp.d.ts +2 -2
- package/_types/utils/encoding/fromRlp.d.ts.map +1 -1
- package/_types/utils/rpc/http.d.ts +6 -2
- package/_types/utils/rpc/http.d.ts.map +1 -1
- package/_types/utils/transaction/parseTransaction.d.ts +2 -2
- package/_types/utils/transaction/parseTransaction.d.ts.map +1 -1
- package/actions/index.ts +1 -0
- package/actions/token/approve.ts +202 -0
- package/actions/token/approveSync.ts +95 -0
- package/actions/token/getAllowance.ts +105 -0
- package/actions/token/getBalance.ts +115 -0
- package/actions/token/getMetadata.ts +98 -0
- package/actions/token/getTotalSupply.ts +97 -0
- package/actions/token/index.ts +9 -0
- package/actions/token/internal.ts +367 -0
- package/actions/token/transfer.ts +230 -0
- package/actions/token/transferSync.ts +99 -0
- package/chains/definitions/battlechain.ts +28 -0
- package/chains/definitions/marooTestnet.ts +27 -0
- package/chains/index.ts +2 -0
- package/clients/createClient.ts +90 -8
- package/clients/createPublicClient.ts +22 -4
- package/clients/createWalletClient.ts +22 -4
- package/clients/decorators/public.ts +166 -4
- package/clients/decorators/wallet.ts +242 -3
- package/clients/transports/http.ts +4 -0
- package/errors/encoding.ts +11 -0
- package/errors/request.ts +17 -0
- package/errors/transaction.ts +11 -0
- package/errors/version.ts +1 -1
- package/index.ts +7 -0
- package/package.json +6 -1
- package/tempo/Client.ts +4 -2
- package/tempo/Decorator.ts +398 -337
- package/tempo/actions/token.ts +519 -296
- package/tempo/actions/wallet.ts +3 -3
- package/tempo/internal/types.ts +19 -0
- package/tempo/internal/utils.ts +204 -0
- package/tokens/defineToken.ts +142 -0
- package/tokens/definitions/alphausd.ts +13 -0
- package/tokens/definitions/betausd.ts +13 -0
- package/tokens/definitions/brla.ts +13 -0
- package/tokens/definitions/cbbtc.ts +13 -0
- package/tokens/definitions/chfau.ts +13 -0
- package/tokens/definitions/cusd.ts +13 -0
- package/tokens/definitions/dlusd.ts +13 -0
- package/tokens/definitions/eurau.ts +13 -0
- package/tokens/definitions/eurce.ts +14 -0
- package/tokens/definitions/frxusd.ts +13 -0
- package/tokens/definitions/gbpa.ts +13 -0
- package/tokens/definitions/gusd.ts +13 -0
- package/tokens/definitions/iusd.ts +13 -0
- package/tokens/definitions/pathusd.ts +14 -0
- package/tokens/definitions/reusd.ts +13 -0
- package/tokens/definitions/rusd.ts +13 -0
- package/tokens/definitions/sbc.ts +13 -0
- package/tokens/definitions/siusd.ts +13 -0
- package/tokens/definitions/stcusd.ts +13 -0
- package/tokens/definitions/susde.ts +13 -0
- package/tokens/definitions/syrupusdc.ts +13 -0
- package/tokens/definitions/thetausd.ts +13 -0
- package/tokens/definitions/usd1.ts +13 -0
- package/tokens/definitions/usdb.ts +13 -0
- package/tokens/definitions/usdc.ts +67 -0
- package/tokens/definitions/usdce.ts +14 -0
- package/tokens/definitions/usde.ts +13 -0
- package/tokens/definitions/usdt0.ts +13 -0
- package/tokens/definitions/wsrusd.ts +13 -0
- package/tokens/index.ts +31 -0
- package/tokens/package.json +6 -0
- package/utils/encoding/fromRlp.ts +22 -3
- package/utils/rpc/http.ts +70 -2
- package/utils/transaction/parseTransaction.ts +7 -2
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import type { Address } from 'abitype'
|
|
2
|
+
import type { Account } from '../../accounts/types.js'
|
|
3
|
+
import type { Client } from '../../clients/createClient.js'
|
|
4
|
+
import type { Transport } from '../../clients/transports/createTransport.js'
|
|
5
|
+
import { erc20Abi } from '../../constants/abis.js'
|
|
6
|
+
import type { BaseErrorType } from '../../errors/base.js'
|
|
7
|
+
import type { Tokens } from '../../tokens/defineToken.js'
|
|
8
|
+
import type { Chain } from '../../types/chain.js'
|
|
9
|
+
import type { Log } from '../../types/log.js'
|
|
10
|
+
import { parseEventLogs } from '../../utils/abi/parseEventLogs.js'
|
|
11
|
+
import { estimateContractGas } from '../public/estimateContractGas.js'
|
|
12
|
+
import {
|
|
13
|
+
type SimulateContractReturnType,
|
|
14
|
+
simulateContract,
|
|
15
|
+
} from '../public/simulateContract.js'
|
|
16
|
+
import type { WriteContractReturnType } from '../wallet/writeContract.js'
|
|
17
|
+
import { writeContract } from '../wallet/writeContract.js'
|
|
18
|
+
import type { writeContractSync } from '../wallet/writeContractSync.js'
|
|
19
|
+
import {
|
|
20
|
+
type AmountInput,
|
|
21
|
+
defineCall,
|
|
22
|
+
pickWriteParameters,
|
|
23
|
+
resolveToken,
|
|
24
|
+
type TokenParameter,
|
|
25
|
+
toBaseUnits,
|
|
26
|
+
type WriteParameters,
|
|
27
|
+
} from './internal.js'
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Approves a spender to transfer ERC-20 tokens on behalf of the caller.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { createClient, http } from 'viem'
|
|
35
|
+
* import { mainnet } from 'viem/chains'
|
|
36
|
+
* import { privateKeyToAccount } from 'viem/accounts'
|
|
37
|
+
* import { token } from 'viem/actions'
|
|
38
|
+
*
|
|
39
|
+
* const client = createClient({
|
|
40
|
+
* account: privateKeyToAccount('0x...'),
|
|
41
|
+
* chain: mainnet,
|
|
42
|
+
* transport: http(),
|
|
43
|
+
* })
|
|
44
|
+
*
|
|
45
|
+
* const hash = await token.approve(client, {
|
|
46
|
+
* amount: 100000000n,
|
|
47
|
+
* spender: '0x...',
|
|
48
|
+
* token: '0x...',
|
|
49
|
+
* })
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param client - Client.
|
|
53
|
+
* @param parameters - Parameters.
|
|
54
|
+
* @returns The transaction hash.
|
|
55
|
+
*/
|
|
56
|
+
export async function approve<
|
|
57
|
+
chain extends Chain | undefined,
|
|
58
|
+
account extends Account | undefined,
|
|
59
|
+
tokens extends Tokens | undefined = undefined,
|
|
60
|
+
>(
|
|
61
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
62
|
+
parameters: approve.Parameters<chain, account, tokens>,
|
|
63
|
+
): Promise<approve.ReturnValue> {
|
|
64
|
+
return approve.inner(writeContract, client, parameters)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export namespace approve {
|
|
68
|
+
export type Args<
|
|
69
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
70
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
71
|
+
> = {
|
|
72
|
+
/** Amount to approve in base units, or as a formatted helper. */
|
|
73
|
+
amount: AmountInput
|
|
74
|
+
/** Address of the spender. */
|
|
75
|
+
spender: Address
|
|
76
|
+
} & TokenParameter<chain, tokens>
|
|
77
|
+
export type Parameters<
|
|
78
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
79
|
+
account extends Account | undefined = Account | undefined,
|
|
80
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
81
|
+
> = WriteParameters<chain, account> & Args<chain, tokens>
|
|
82
|
+
export type ReturnValue = WriteContractReturnType
|
|
83
|
+
// TODO: exhaustive error type
|
|
84
|
+
export type ErrorType = BaseErrorType
|
|
85
|
+
|
|
86
|
+
/** @internal */
|
|
87
|
+
export async function inner<
|
|
88
|
+
action extends typeof writeContract | typeof writeContractSync,
|
|
89
|
+
chain extends Chain | undefined,
|
|
90
|
+
account extends Account | undefined,
|
|
91
|
+
tokens extends Tokens | undefined = undefined,
|
|
92
|
+
>(
|
|
93
|
+
action: action,
|
|
94
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
95
|
+
parameters: approve.Parameters<chain, account, tokens>,
|
|
96
|
+
): Promise<ReturnType<action>> {
|
|
97
|
+
return (await action(client, {
|
|
98
|
+
...parameters,
|
|
99
|
+
...approve.call(client, parameters as never),
|
|
100
|
+
} as never)) as never
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Defines a call to the `approve` function.
|
|
105
|
+
*
|
|
106
|
+
* Can be passed as a parameter to `estimateContractGas`, `simulateContract`,
|
|
107
|
+
* `sendCalls`, `sendTransaction` (`calls`), or `multicall`. The token is
|
|
108
|
+
* selected by `token`, which is either a token symbol (resolved from the
|
|
109
|
+
* client's `tokens` array) or a contract `address`; `amount.decimals`
|
|
110
|
+
* is inferred from declared client tokens when omitted.
|
|
111
|
+
*
|
|
112
|
+
* @param client - Client.
|
|
113
|
+
* @param parameters - Parameters.
|
|
114
|
+
* @returns The call.
|
|
115
|
+
*/
|
|
116
|
+
export function call<
|
|
117
|
+
chain extends Chain | undefined,
|
|
118
|
+
account extends Account | undefined,
|
|
119
|
+
tokens extends Tokens | undefined = undefined,
|
|
120
|
+
>(
|
|
121
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
122
|
+
parameters: Args<chain, tokens>,
|
|
123
|
+
) {
|
|
124
|
+
return defineCall(getCall(client, parameters as approve.Args))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Estimates the gas required to approve a spender. `amount.decimals` is
|
|
129
|
+
* inferred from declared client tokens when omitted.
|
|
130
|
+
*
|
|
131
|
+
* @param client - Client.
|
|
132
|
+
* @param parameters - Parameters.
|
|
133
|
+
* @returns The gas estimate.
|
|
134
|
+
*/
|
|
135
|
+
export async function estimateGas<
|
|
136
|
+
chain extends Chain | undefined,
|
|
137
|
+
account extends Account | undefined,
|
|
138
|
+
tokens extends Tokens | undefined = undefined,
|
|
139
|
+
>(
|
|
140
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
141
|
+
parameters: approve.Parameters<chain, account, tokens>,
|
|
142
|
+
): Promise<bigint> {
|
|
143
|
+
return estimateContractGas(client, {
|
|
144
|
+
...pickWriteParameters(parameters as never),
|
|
145
|
+
...approve.call(client, parameters as never),
|
|
146
|
+
} as never)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Simulates an approval of a spender. `amount.decimals` is inferred from
|
|
151
|
+
* declared client tokens when omitted.
|
|
152
|
+
*
|
|
153
|
+
* @param client - Client.
|
|
154
|
+
* @param parameters - Parameters.
|
|
155
|
+
* @returns The simulation result and write request.
|
|
156
|
+
*/
|
|
157
|
+
export async function simulate<
|
|
158
|
+
chain extends Chain | undefined,
|
|
159
|
+
account extends Account | undefined,
|
|
160
|
+
tokens extends Tokens | undefined = undefined,
|
|
161
|
+
>(
|
|
162
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
163
|
+
parameters: approve.Parameters<chain, account, tokens>,
|
|
164
|
+
): Promise<SimulateContractReturnType<typeof erc20Abi, 'approve'>> {
|
|
165
|
+
return simulateContract(client, {
|
|
166
|
+
...pickWriteParameters(parameters as never),
|
|
167
|
+
...approve.call(client, parameters as never),
|
|
168
|
+
} as never) as never
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Extracts the `Approval` event from logs.
|
|
173
|
+
*
|
|
174
|
+
* @param logs - The logs.
|
|
175
|
+
* @returns The `Approval` event.
|
|
176
|
+
*/
|
|
177
|
+
export function extractEvent(logs: Log[]) {
|
|
178
|
+
const [log] = parseEventLogs({
|
|
179
|
+
abi: erc20Abi,
|
|
180
|
+
logs,
|
|
181
|
+
eventName: 'Approval',
|
|
182
|
+
strict: true,
|
|
183
|
+
})
|
|
184
|
+
if (!log) throw new Error('`Approval` event not found.')
|
|
185
|
+
return log
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Builds the underlying `approve` contract call. @internal */
|
|
190
|
+
function getCall(
|
|
191
|
+
client: Client<Transport, Chain | undefined>,
|
|
192
|
+
parameters: approve.Args,
|
|
193
|
+
) {
|
|
194
|
+
const { amount, spender, token } = parameters
|
|
195
|
+
const { address, decimals } = resolveToken(client, { token })
|
|
196
|
+
return {
|
|
197
|
+
abi: erc20Abi,
|
|
198
|
+
address,
|
|
199
|
+
args: [spender, toBaseUnits(amount, decimals)],
|
|
200
|
+
functionName: 'approve',
|
|
201
|
+
} as const
|
|
202
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { Account } from '../../accounts/types.js'
|
|
2
|
+
import type { Client } from '../../clients/createClient.js'
|
|
3
|
+
import type { Transport } from '../../clients/transports/createTransport.js'
|
|
4
|
+
import type { erc20Abi } from '../../constants/abis.js'
|
|
5
|
+
import type { BaseErrorType } from '../../errors/base.js'
|
|
6
|
+
import type { Tokens } from '../../tokens/defineToken.js'
|
|
7
|
+
import type { Chain } from '../../types/chain.js'
|
|
8
|
+
import type { GetEventArgs } from '../../types/contract.js'
|
|
9
|
+
import type { TransactionReceipt } from '../../types/transaction.js'
|
|
10
|
+
import type { Compute } from '../../types/utils.js'
|
|
11
|
+
import { formatUnits } from '../../utils/unit/formatUnits.js'
|
|
12
|
+
import { writeContractSync } from '../wallet/writeContractSync.js'
|
|
13
|
+
import { approve } from './approve.js'
|
|
14
|
+
import { resolveAmountDecimals, resolveToken } from './internal.js'
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Approves a spender to transfer ERC-20 tokens on behalf of the caller, and
|
|
18
|
+
* waits for the transaction to be confirmed.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { createClient, http } from 'viem'
|
|
23
|
+
* import { mainnet } from 'viem/chains'
|
|
24
|
+
* import { privateKeyToAccount } from 'viem/accounts'
|
|
25
|
+
* import { token } from 'viem/actions'
|
|
26
|
+
*
|
|
27
|
+
* const client = createClient({
|
|
28
|
+
* account: privateKeyToAccount('0x...'),
|
|
29
|
+
* chain: mainnet,
|
|
30
|
+
* transport: http(),
|
|
31
|
+
* })
|
|
32
|
+
*
|
|
33
|
+
* const { receipt, ...event } = await token.approveSync(client, {
|
|
34
|
+
* amount: 100000000n,
|
|
35
|
+
* spender: '0x...',
|
|
36
|
+
* token: '0x...',
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @param client - Client.
|
|
41
|
+
* @param parameters - Parameters.
|
|
42
|
+
* @returns The transaction receipt and event data.
|
|
43
|
+
*/
|
|
44
|
+
export async function approveSync<
|
|
45
|
+
chain extends Chain | undefined,
|
|
46
|
+
account extends Account | undefined,
|
|
47
|
+
tokens extends Tokens | undefined = undefined,
|
|
48
|
+
>(
|
|
49
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
50
|
+
parameters: approveSync.Parameters<chain, account, tokens>,
|
|
51
|
+
): Promise<approveSync.ReturnValue> {
|
|
52
|
+
const { amount, token, throwOnReceiptRevert = true } = parameters
|
|
53
|
+
const { decimals } = resolveToken(client, { token })
|
|
54
|
+
const resolved = resolveAmountDecimals(amount, decimals)
|
|
55
|
+
const receipt = await approve.inner(writeContractSync, client, {
|
|
56
|
+
...parameters,
|
|
57
|
+
throwOnReceiptRevert,
|
|
58
|
+
} as never)
|
|
59
|
+
const { args } = approve.extractEvent(receipt.logs)
|
|
60
|
+
return {
|
|
61
|
+
...args,
|
|
62
|
+
...(resolved === undefined
|
|
63
|
+
? {}
|
|
64
|
+
: { decimals: resolved, formatted: formatUnits(args.value, resolved) }),
|
|
65
|
+
receipt,
|
|
66
|
+
} as never
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export namespace approveSync {
|
|
70
|
+
export type Parameters<
|
|
71
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
72
|
+
account extends Account | undefined = Account | undefined,
|
|
73
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
74
|
+
> = approve.Parameters<chain, account, tokens>
|
|
75
|
+
export type Args<
|
|
76
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
77
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
78
|
+
> = approve.Args<chain, tokens>
|
|
79
|
+
export type ReturnValue = Compute<
|
|
80
|
+
GetEventArgs<
|
|
81
|
+
typeof erc20Abi,
|
|
82
|
+
'Approval',
|
|
83
|
+
{ IndexedOnly: false; Required: true }
|
|
84
|
+
> & {
|
|
85
|
+
/** Token decimals used to derive `formatted`, if known. */
|
|
86
|
+
decimals?: number | undefined
|
|
87
|
+
/** Approved amount formatted with the token's `decimals`, if known. */
|
|
88
|
+
formatted?: string | undefined
|
|
89
|
+
/** Transaction receipt. */
|
|
90
|
+
receipt: TransactionReceipt
|
|
91
|
+
}
|
|
92
|
+
>
|
|
93
|
+
// TODO: exhaustive error type
|
|
94
|
+
export type ErrorType = BaseErrorType
|
|
95
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import type { Address } from 'abitype'
|
|
2
|
+
import type { Account } from '../../accounts/types.js'
|
|
3
|
+
import type { Client } from '../../clients/createClient.js'
|
|
4
|
+
import type { Transport } from '../../clients/transports/createTransport.js'
|
|
5
|
+
import { erc20Abi } from '../../constants/abis.js'
|
|
6
|
+
import type { Tokens } from '../../tokens/defineToken.js'
|
|
7
|
+
import type { Chain } from '../../types/chain.js'
|
|
8
|
+
import { readContract } from '../public/readContract.js'
|
|
9
|
+
import {
|
|
10
|
+
type Amount,
|
|
11
|
+
defineCall,
|
|
12
|
+
type ReadParameters,
|
|
13
|
+
resolveToken,
|
|
14
|
+
resolveTokenWithDecimals,
|
|
15
|
+
type TokenParameters,
|
|
16
|
+
toAmount,
|
|
17
|
+
} from './internal.js'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Gets the ERC-20 allowance a spender has over an account's tokens.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { createClient, http } from 'viem'
|
|
25
|
+
* import { mainnet } from 'viem/chains'
|
|
26
|
+
* import { token } from 'viem/actions'
|
|
27
|
+
*
|
|
28
|
+
* const client = createClient({ chain: mainnet, transport: http() })
|
|
29
|
+
*
|
|
30
|
+
* const allowance = await token.getAllowance(client, {
|
|
31
|
+
* account: '0x...',
|
|
32
|
+
* spender: '0x...',
|
|
33
|
+
* token: '0x...',
|
|
34
|
+
* })
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param client - Client.
|
|
38
|
+
* @param parameters - Parameters.
|
|
39
|
+
* @returns The allowance, in base units and human-readable form.
|
|
40
|
+
*/
|
|
41
|
+
export async function getAllowance<
|
|
42
|
+
chain extends Chain | undefined,
|
|
43
|
+
account extends Account | undefined,
|
|
44
|
+
tokens extends Tokens | undefined = undefined,
|
|
45
|
+
>(
|
|
46
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
47
|
+
parameters: getAllowance.Parameters<chain, tokens>,
|
|
48
|
+
): Promise<getAllowance.ReturnValue> {
|
|
49
|
+
const { account, decimals, spender, token, ...rest } = parameters
|
|
50
|
+
const [amount, { decimals: resolved }] = await Promise.all([
|
|
51
|
+
readContract(client, {
|
|
52
|
+
...rest,
|
|
53
|
+
...getAllowance.call(client, { account, spender, token } as never),
|
|
54
|
+
}),
|
|
55
|
+
resolveTokenWithDecimals(client, {
|
|
56
|
+
decimals,
|
|
57
|
+
token,
|
|
58
|
+
}),
|
|
59
|
+
])
|
|
60
|
+
return toAmount(amount as bigint, resolved)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export namespace getAllowance {
|
|
64
|
+
export type Args<
|
|
65
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
66
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
67
|
+
> = {
|
|
68
|
+
/** Account that owns the tokens. */
|
|
69
|
+
account: Address
|
|
70
|
+
/** Spender of the tokens. */
|
|
71
|
+
spender: Address
|
|
72
|
+
} & TokenParameters<chain, tokens>
|
|
73
|
+
export type Parameters<
|
|
74
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
75
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
76
|
+
> = ReadParameters & Args<chain, tokens>
|
|
77
|
+
export type ReturnValue = Amount
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Defines a call to the `allowance` function.
|
|
81
|
+
*
|
|
82
|
+
* Can be passed as a parameter to `multicall`, `simulateContract`, or any
|
|
83
|
+
* other action that accepts a contract call. The token is selected by `token`
|
|
84
|
+
* symbol (resolved from the client's `tokens` array) or contract address.
|
|
85
|
+
*
|
|
86
|
+
* @param client - Client.
|
|
87
|
+
* @param args - Arguments.
|
|
88
|
+
* @returns The call.
|
|
89
|
+
*/
|
|
90
|
+
export function call<
|
|
91
|
+
chain extends Chain | undefined,
|
|
92
|
+
account extends Account | undefined,
|
|
93
|
+
tokens extends Tokens | undefined = undefined,
|
|
94
|
+
>(
|
|
95
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
96
|
+
args: Args<chain, tokens>,
|
|
97
|
+
) {
|
|
98
|
+
return defineCall({
|
|
99
|
+
address: resolveToken(client, args).address,
|
|
100
|
+
abi: erc20Abi,
|
|
101
|
+
functionName: 'allowance',
|
|
102
|
+
args: [args.account, args.spender],
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { Address } from 'abitype'
|
|
2
|
+
import type { Account } from '../../accounts/types.js'
|
|
3
|
+
import { parseAccount } from '../../accounts/utils/parseAccount.js'
|
|
4
|
+
import type { Client } from '../../clients/createClient.js'
|
|
5
|
+
import type { Transport } from '../../clients/transports/createTransport.js'
|
|
6
|
+
import { erc20Abi } from '../../constants/abis.js'
|
|
7
|
+
import { AccountNotFoundError } from '../../errors/account.js'
|
|
8
|
+
import type { Tokens } from '../../tokens/defineToken.js'
|
|
9
|
+
import type { GetAccountParameter } from '../../types/account.js'
|
|
10
|
+
import type { Chain } from '../../types/chain.js'
|
|
11
|
+
import { readContract } from '../public/readContract.js'
|
|
12
|
+
import {
|
|
13
|
+
type Amount,
|
|
14
|
+
defineCall,
|
|
15
|
+
type ReadParameters,
|
|
16
|
+
resolveToken,
|
|
17
|
+
resolveTokenWithDecimals,
|
|
18
|
+
type TokenParameters,
|
|
19
|
+
toAmount,
|
|
20
|
+
} from './internal.js'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Gets the ERC-20 token balance of an account.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { createClient, http } from 'viem'
|
|
28
|
+
* import { mainnet } from 'viem/chains'
|
|
29
|
+
* import { token } from 'viem/actions'
|
|
30
|
+
*
|
|
31
|
+
* const client = createClient({ chain: mainnet, transport: http() })
|
|
32
|
+
*
|
|
33
|
+
* const balance = await token.getBalance(client, {
|
|
34
|
+
* account: '0x...',
|
|
35
|
+
* token: '0x...',
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param client - Client.
|
|
40
|
+
* @param parameters - Parameters.
|
|
41
|
+
* @returns The token balance, in base units and human-readable form.
|
|
42
|
+
*/
|
|
43
|
+
export async function getBalance<
|
|
44
|
+
chain extends Chain | undefined,
|
|
45
|
+
account extends Account | undefined,
|
|
46
|
+
tokens extends Tokens | undefined = undefined,
|
|
47
|
+
>(
|
|
48
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
49
|
+
parameters: getBalance.Parameters<chain, account, tokens>,
|
|
50
|
+
): Promise<getBalance.ReturnValue> {
|
|
51
|
+
const {
|
|
52
|
+
account: account_ = client.account,
|
|
53
|
+
decimals,
|
|
54
|
+
token,
|
|
55
|
+
...rest
|
|
56
|
+
} = parameters
|
|
57
|
+
if (!account_) throw new AccountNotFoundError()
|
|
58
|
+
const account = parseAccount(account_).address
|
|
59
|
+
const [amount, { decimals: resolved }] = await Promise.all([
|
|
60
|
+
readContract(client, {
|
|
61
|
+
...rest,
|
|
62
|
+
...getBalance.call(client, { account, token } as never),
|
|
63
|
+
}),
|
|
64
|
+
resolveTokenWithDecimals(client, {
|
|
65
|
+
decimals,
|
|
66
|
+
token,
|
|
67
|
+
}),
|
|
68
|
+
])
|
|
69
|
+
return toAmount(amount, resolved)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export namespace getBalance {
|
|
73
|
+
export type Args<
|
|
74
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
75
|
+
account extends Account | undefined = Account | undefined,
|
|
76
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
77
|
+
> = GetAccountParameter<account, Account | Address> &
|
|
78
|
+
TokenParameters<chain, tokens>
|
|
79
|
+
export type Parameters<
|
|
80
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
81
|
+
account extends Account | undefined = Account | undefined,
|
|
82
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
83
|
+
> = Omit<ReadParameters, 'account'> & Args<chain, account, tokens>
|
|
84
|
+
export type ReturnValue = Amount
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Defines a call to the `balanceOf` function.
|
|
88
|
+
*
|
|
89
|
+
* Can be passed as a parameter to `multicall`, `simulateContract`, or any
|
|
90
|
+
* other action that accepts a contract call. The token is selected by `token`
|
|
91
|
+
* symbol (resolved from the client's `tokens` array) or contract address.
|
|
92
|
+
*
|
|
93
|
+
* @param client - Client.
|
|
94
|
+
* @param args - Arguments.
|
|
95
|
+
* @returns The call.
|
|
96
|
+
*/
|
|
97
|
+
export function call<
|
|
98
|
+
chain extends Chain | undefined,
|
|
99
|
+
account extends Account | undefined,
|
|
100
|
+
tokens extends Tokens | undefined = undefined,
|
|
101
|
+
>(
|
|
102
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
103
|
+
args: Args<chain, account, tokens>,
|
|
104
|
+
) {
|
|
105
|
+
const account_ = args.account ?? client.account
|
|
106
|
+
if (!account_) throw new AccountNotFoundError()
|
|
107
|
+
const account = parseAccount(account_).address
|
|
108
|
+
return defineCall({
|
|
109
|
+
address: resolveToken(client, args).address,
|
|
110
|
+
abi: erc20Abi,
|
|
111
|
+
functionName: 'balanceOf',
|
|
112
|
+
args: [account],
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Account } from '../../accounts/types.js'
|
|
2
|
+
import type { Client } from '../../clients/createClient.js'
|
|
3
|
+
import type { Transport } from '../../clients/transports/createTransport.js'
|
|
4
|
+
import { erc20Abi } from '../../constants/abis.js'
|
|
5
|
+
import type { Tokens } from '../../tokens/defineToken.js'
|
|
6
|
+
import type { Chain } from '../../types/chain.js'
|
|
7
|
+
import { readContract } from '../public/readContract.js'
|
|
8
|
+
import {
|
|
9
|
+
findDeclaredToken,
|
|
10
|
+
type ReadParameters,
|
|
11
|
+
resolveToken,
|
|
12
|
+
type TokenParameter,
|
|
13
|
+
} from './internal.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Gets the metadata (`decimals`, `name`, `symbol`) of an ERC-20 token.
|
|
17
|
+
*
|
|
18
|
+
* Fields declared on the Client's `tokens` array are used as-is; any missing
|
|
19
|
+
* field is fetched from the token contract.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { createClient, http } from 'viem'
|
|
24
|
+
* import { mainnet } from 'viem/chains'
|
|
25
|
+
* import { token } from 'viem/actions'
|
|
26
|
+
*
|
|
27
|
+
* const client = createClient({ chain: mainnet, transport: http() })
|
|
28
|
+
*
|
|
29
|
+
* const metadata = await token.getMetadata(client, {
|
|
30
|
+
* token: '0x...',
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @param client - Client.
|
|
35
|
+
* @param parameters - Parameters.
|
|
36
|
+
* @returns The token metadata (`decimals`, `name`, `symbol`).
|
|
37
|
+
*/
|
|
38
|
+
export async function getMetadata<
|
|
39
|
+
chain extends Chain | undefined,
|
|
40
|
+
account extends Account | undefined,
|
|
41
|
+
tokens extends Tokens | undefined = undefined,
|
|
42
|
+
>(
|
|
43
|
+
client: Client<Transport, chain, account, undefined, undefined, tokens>,
|
|
44
|
+
parameters: getMetadata.Parameters<chain, tokens>,
|
|
45
|
+
): Promise<getMetadata.ReturnValue> {
|
|
46
|
+
const { token, ...rest } = parameters
|
|
47
|
+
const { address } = resolveToken(client, { token })
|
|
48
|
+
const declared = findDeclaredToken(client, token)
|
|
49
|
+
|
|
50
|
+
const [decimals_, name, symbol] = await Promise.all([
|
|
51
|
+
declared?.decimals ??
|
|
52
|
+
readContract(client, {
|
|
53
|
+
...rest,
|
|
54
|
+
abi: erc20Abi,
|
|
55
|
+
address,
|
|
56
|
+
functionName: 'decimals',
|
|
57
|
+
}),
|
|
58
|
+
declared?.name ??
|
|
59
|
+
readContract(client, {
|
|
60
|
+
...rest,
|
|
61
|
+
abi: erc20Abi,
|
|
62
|
+
address,
|
|
63
|
+
functionName: 'name',
|
|
64
|
+
}),
|
|
65
|
+
declared?.symbol ??
|
|
66
|
+
readContract(client, {
|
|
67
|
+
...rest,
|
|
68
|
+
abi: erc20Abi,
|
|
69
|
+
address,
|
|
70
|
+
functionName: 'symbol',
|
|
71
|
+
}),
|
|
72
|
+
])
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
decimals: decimals_ as number,
|
|
76
|
+
name: name as string,
|
|
77
|
+
symbol: symbol as string,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export namespace getMetadata {
|
|
82
|
+
export type Args<
|
|
83
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
84
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
85
|
+
> = TokenParameter<chain, tokens>
|
|
86
|
+
export type Parameters<
|
|
87
|
+
chain extends Chain | undefined = Chain | undefined,
|
|
88
|
+
tokens extends Tokens | undefined = Tokens | undefined,
|
|
89
|
+
> = Omit<ReadParameters, 'account'> & Args<chain, tokens>
|
|
90
|
+
export type ReturnValue = {
|
|
91
|
+
/** Number of decimals the token uses. */
|
|
92
|
+
decimals: number
|
|
93
|
+
/** Human-readable name of the token. */
|
|
94
|
+
name: string
|
|
95
|
+
/** Ticker symbol of the token. */
|
|
96
|
+
symbol: string
|
|
97
|
+
}
|
|
98
|
+
}
|