From d6a29f5223324660cd98c2a6aaf345c207b2cd97 Mon Sep 17 00:00:00 2001 From: jxom Date: Tue, 21 Feb 2023 07:01:13 +1100 Subject: [PATCH] feat: decorate clients with actions (#77) * feat: fall back to getLogs if filters are not supported * Update src/actions/public/watchContractEvent.test.ts Co-authored-by: awkweb * pr review * feat: decode event log topics & data * wip: public * wip: add ens * docs: public actions * wip: wallet actions * docs: wallet actions * wip: test actions * wip: add utils to main entrypoint * tests: add tests * playgrounds: update * chore: changeset * test * refactor: wire up decorator generics * fix conflicts * format --------- Co-authored-by: awkweb --- .changeset/unlucky-poems-rush.md | 21 ++ .../src/components/actions/AddChain.tsx | 3 +- .../src/components/actions/GetBalance.tsx | 3 +- .../src/components/actions/GetBlock.tsx | 5 +- .../src/components/actions/GetBlockNumber.tsx | 3 +- .../src/components/actions/GetPermissions.tsx | 3 +- .../src/components/actions/GetTransaction.tsx | 12 +- .../actions/RequestAccountAddresses.tsx | 3 +- .../components/actions/RequestPermissions.tsx | 3 +- .../components/actions/SendTransaction.tsx | 7 +- .../src/components/actions/SwitchChain.tsx | 3 +- .../components/actions/WatchBlockNumber.tsx | 3 +- .../src/components/actions/WatchBlocks.tsx | 3 +- .../actions/WatchPendingTransactions.tsx | 21 +- playgrounds/bun/index.ts | 9 +- site/docs/actions/public/call.md | 33 +- site/docs/actions/public/createBlockFilter.md | 9 +- site/docs/actions/public/createEventFilter.md | 35 +- .../public/createPendingTransactionFilter.md | 9 +- site/docs/actions/public/estimateGas.md | 27 +- site/docs/actions/public/getBalance.md | 17 +- site/docs/actions/public/getBlock.md | 15 +- site/docs/actions/public/getBlockNumber.md | 11 +- .../public/getBlockTransactionCount.md | 15 +- site/docs/actions/public/getChainId.md | 9 +- site/docs/actions/public/getFeeHistory.md | 17 +- site/docs/actions/public/getFilterChanges.md | 35 +- site/docs/actions/public/getFilterLogs.md | 17 +- site/docs/actions/public/getGasPrice.md | 9 +- site/docs/actions/public/getLogs.md | 39 +-- site/docs/actions/public/getTransaction.md | 19 +- .../public/getTransactionConfirmations.md | 19 +- .../actions/public/getTransactionCount.md | 15 +- .../actions/public/getTransactionReceipt.md | 11 +- site/docs/actions/public/uninstallFilter.md | 15 +- .../public/waitForTransactionReceipt.md | 22 +- site/docs/actions/public/watchBlockNumber.md | 19 +- site/docs/actions/public/watchBlocks.md | 28 +- site/docs/actions/public/watchEvent.md | 55 ++- .../public/watchPendingTransactions.md | 21 +- site/docs/actions/test/dropTransaction.md | 11 +- site/docs/actions/test/getAutomine.md | 9 +- site/docs/actions/test/getTxpoolContent.md | 9 +- site/docs/actions/test/getTxpoolStatus.md | 9 +- site/docs/actions/test/impersonateAccount.md | 11 +- site/docs/actions/test/increaseTime.md | 11 +- site/docs/actions/test/inspectTxpool.md | 9 +- site/docs/actions/test/mine.md | 13 +- .../test/removeBlockTimestampInterval.md | 9 +- site/docs/actions/test/reset.md | 13 +- site/docs/actions/test/revert.md | 11 +- .../actions/test/sendUnsignedTransaction.md | 27 +- site/docs/actions/test/setAutomine.md | 11 +- site/docs/actions/test/setBalance.md | 14 +- site/docs/actions/test/setBlockGasLimit.md | 11 +- .../actions/test/setBlockTimestampInterval.md | 11 +- site/docs/actions/test/setCode.md | 13 +- site/docs/actions/test/setCoinbase.md | 11 +- site/docs/actions/test/setIntervalMining.md | 11 +- site/docs/actions/test/setLoggingEnabled.md | 9 +- site/docs/actions/test/setMinGasPrice.md | 12 +- .../actions/test/setNextBlockBaseFeePerGas.md | 11 +- .../actions/test/setNextBlockTimestamp.md | 11 +- site/docs/actions/test/setNonce.md | 13 +- site/docs/actions/test/setRpcUrl.md | 9 +- site/docs/actions/test/setStorageAt.md | 15 +- site/docs/actions/test/snapshot.md | 9 +- .../actions/test/stopImpersonatingAccount.md | 11 +- site/docs/actions/wallet/addChain.md | 9 +- site/docs/actions/wallet/getAccounts.md | 9 +- site/docs/actions/wallet/getPermissions.md | 9 +- site/docs/actions/wallet/requestAccounts.md | 9 +- .../docs/actions/wallet/requestPermissions.md | 9 +- site/docs/actions/wallet/sendTransaction.md | 27 +- site/docs/actions/wallet/signMessage.md | 13 +- site/docs/actions/wallet/switchChain.md | 9 +- site/docs/actions/wallet/watchAsset.md | 19 +- .../contract/createContractEventFilter.md | 31 +- site/docs/contract/deployContract.md | 17 +- site/docs/contract/estimateContractGas.md | 38 +-- site/docs/contract/getBytecode.md | 11 +- site/docs/contract/getStorageAt.md | 15 +- site/docs/contract/multicall.md | 21 +- site/docs/contract/readContract.md | 25 +- site/docs/contract/simulateContract.md | 43 +-- site/docs/contract/watchContractEvent.md | 31 +- site/docs/contract/writeContract.md | 41 +-- site/docs/ens/actions/getEnsAddress.md | 33 +- site/docs/ens/actions/getEnsName.md | 17 +- site/docs/ens/utilities/labelhash.md | 15 +- site/docs/ens/utilities/namehash.md | 9 +- site/docs/getting-started.md | 3 +- site/docs/utilities/concat.md | 4 +- site/docs/utilities/formatEther.md | 4 +- site/docs/utilities/formatGwei.md | 4 +- site/docs/utilities/formatUnit.md | 4 +- site/docs/utilities/fromBytes.md | 14 +- site/docs/utilities/fromHex.md | 14 +- site/docs/utilities/fromRlp.md | 4 +- site/docs/utilities/getAddress.md | 4 +- site/docs/utilities/getContractAddress.md | 4 +- site/docs/utilities/getEventSignature.md | 4 +- site/docs/utilities/getFunctionSignature.md | 4 +- site/docs/utilities/isAddress.md | 4 +- site/docs/utilities/isAddressEqual.md | 4 +- site/docs/utilities/isBytes.md | 4 +- site/docs/utilities/isHex.md | 4 +- site/docs/utilities/keccak256.md | 6 +- site/docs/utilities/pad.md | 4 +- site/docs/utilities/parseEther.md | 4 +- site/docs/utilities/parseGwei.md | 4 +- site/docs/utilities/parseUnit.md | 4 +- site/docs/utilities/size.md | 4 +- site/docs/utilities/slice.md | 4 +- site/docs/utilities/toBytes.md | 12 +- site/docs/utilities/toHex.md | 12 +- site/docs/utilities/toRlp.md | 4 +- site/docs/utilities/trim.md | 4 +- site/index.md | 3 +- src/actions/ens/getEnsAddress.ts | 4 +- src/actions/ens/getEnsName.test.ts | 2 +- src/actions/ens/getEnsName.ts | 4 +- src/actions/ens/index.ts | 12 +- src/actions/index.ts | 3 + src/actions/public/estimateGas.ts | 2 +- src/actions/public/getBlockNumber.ts | 2 +- src/actions/public/getChainId.ts | 6 +- src/actions/public/getStorageAt.test.ts | 1 - .../public/getTransactionConfirmations.ts | 2 +- src/actions/public/index.ts | 1 + src/actions/public/watchBlockNumber.ts | 2 +- src/actions/test/getTxpoolContent.ts | 11 +- src/actions/test/getTxpoolStatus.ts | 9 +- src/actions/test/index.ts | 4 + src/actions/test/inspectTxpool.ts | 10 +- src/actions/wallet/addChain.test.ts | 6 +- src/actions/wallet/addChain.ts | 6 +- src/actions/wallet/deployContract.ts | 5 +- src/actions/wallet/getAccounts.ts | 7 +- src/actions/wallet/index.ts | 8 +- src/actions/wallet/requestAccounts.ts | 7 +- src/clients/createPublicClient.test.ts | 136 ++++++++ src/clients/createPublicClient.ts | 20 +- src/clients/createTestClient.test.ts | 84 +++++ src/clients/createTestClient.ts | 13 +- src/clients/createWalletClient.test.ts | 44 +++ src/clients/createWalletClient.ts | 18 +- src/clients/decorators/index.ts | 8 + src/clients/decorators/public.test.ts | 321 ++++++++++++++++++ src/clients/decorators/public.ts | 263 ++++++++++++++ src/clients/decorators/test.test.ts | 223 ++++++++++++ src/clients/decorators/test.ts | 133 ++++++++ src/clients/decorators/wallet.test.ts | 105 ++++++ src/clients/decorators/wallet.ts | 79 +++++ src/contract.test.ts | 1 + src/contract.ts | 8 +- src/ens.ts | 3 +- src/index.test.ts | 88 +++++ src/index.ts | 109 ++++++ src/public.test.ts | 1 + src/public.ts | 3 + src/utils/encoding/index.ts | 2 +- src/utils/encoding/toRlp.ts | 4 +- src/utils/ens/index.test.ts | 1 - src/utils/ens/index.ts | 2 - src/utils/ens/labelhash.test.ts | 3 +- src/utils/ens/labelhash.ts | 3 +- src/utils/ens/namehash.test.ts | 3 +- src/utils/ens/namehash.ts | 3 +- src/utils/index.ts | 2 +- src/wallet.ts | 3 + 171 files changed, 2256 insertions(+), 1099 deletions(-) create mode 100644 .changeset/unlucky-poems-rush.md create mode 100644 src/clients/decorators/index.ts create mode 100644 src/clients/decorators/public.test.ts create mode 100644 src/clients/decorators/public.ts create mode 100644 src/clients/decorators/test.test.ts create mode 100644 src/clients/decorators/test.ts create mode 100644 src/clients/decorators/wallet.test.ts create mode 100644 src/clients/decorators/wallet.ts diff --git a/.changeset/unlucky-poems-rush.md b/.changeset/unlucky-poems-rush.md new file mode 100644 index 0000000000..1e359ef61b --- /dev/null +++ b/.changeset/unlucky-poems-rush.md @@ -0,0 +1,21 @@ +--- +"viem": patch +--- + +Decorated Clients with their respective Actions. + +Example: + +```diff +import { createPublicClient, http } from 'viem' +import { mainnet } from 'viem/chains' +-import { getBlockNumber } from 'viem/public' + +const client = createPublicClient({ + chain: mainnet, + transport: http(), +}) + +- const blockNumber = await getBlockNumber(client) ++ const blockNumber = await client.getBlockNumber() +``` \ No newline at end of file diff --git a/playgrounds/browser/src/components/actions/AddChain.tsx b/playgrounds/browser/src/components/actions/AddChain.tsx index a3a6aac511..f60f38bb27 100644 --- a/playgrounds/browser/src/components/actions/AddChain.tsx +++ b/playgrounds/browser/src/components/actions/AddChain.tsx @@ -1,13 +1,12 @@ import type { WalletClient } from 'viem' import { celo } from 'viem/chains' -import { addChain } from 'viem/wallet' export function AddChain({ client }: { client: WalletClient }) { return (