diff --git a/packages/thirdweb/benchmarks/read-contract.bench.ts b/packages/thirdweb/benchmarks/read-contract.bench.ts new file mode 100644 index 00000000000..d004d9db1c3 --- /dev/null +++ b/packages/thirdweb/benchmarks/read-contract.bench.ts @@ -0,0 +1,95 @@ +import { describe, bench } from "vitest"; + +// local imports +import { + readContract, + createThirdwebClient, + getContract, + defineChain, +} from ".."; +import { ThirdwebSDK } from "../../sdk"; + +// eslint-disable-next-line @typescript-eslint/no-var-requires +require("dotenv-mono").load(); + +const SECRET_KEY = process.env.TW_SECRET_KEY as string; + +const LOCAL_RPC = "http://localhost:8555"; +const USDC_CONTRACT_ADDRESS = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; +const VITALIK_WALLET = "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B"; + +const client = createThirdwebClient({ + secretKey: SECRET_KEY, +}); + +const NEW_CONTRACT = getContract({ + chain: defineChain({ + id: 1, + rpc: LOCAL_RPC, + }), + client, + address: USDC_CONTRACT_ADDRESS, +}); + +const OLD_CONTRACT = await new ThirdwebSDK(LOCAL_RPC, { + secretKey: SECRET_KEY, + readonlySettings: { + chainId: 1, + rpcUrl: LOCAL_RPC, + }, +}).getContract(USDC_CONTRACT_ADDRESS); + +describe.runIf(SECRET_KEY)("read contract (warm cache)", () => { + bench("thirdweb", async () => { + await readContract({ + contract: NEW_CONTRACT, + method: "function balanceOf(address) returns (uint256)", + params: [VITALIK_WALLET], + }); + }); + + bench("@thirdweb-dev/sdk", async () => { + await OLD_CONTRACT.call("balanceOf", [VITALIK_WALLET]); + }); +}); + +describe.runIf(SECRET_KEY)("read contract (cold cache)", () => { + bench("thirdweb", async () => { + // init the client + const newClient = createThirdwebClient({ + secretKey: SECRET_KEY, + }); + // define chain + const chain = defineChain({ + id: 1, + rpc: LOCAL_RPC, + }); + // get contract + const contract = getContract({ + chain, + client: newClient, + address: USDC_CONTRACT_ADDRESS, + }); + // actually read from the contract + await readContract({ + contract, + method: "function balanceOf(address) returns (uint256)", + params: [VITALIK_WALLET], + }); + }); + + bench("@thirdweb-dev/sdk", async () => { + // init the sdk + const sdk = new ThirdwebSDK(LOCAL_RPC, { + secretKey: SECRET_KEY, + readonlySettings: { + chainId: 1, + rpcUrl: LOCAL_RPC, + }, + }); + //get the contract + const contract = await sdk.getContract(USDC_CONTRACT_ADDRESS); + // actually read from the contract + await contract.call("balanceOf", [VITALIK_WALLET]); + }); +}); diff --git a/packages/thirdweb/comparison/setup.md b/packages/thirdweb/comparison/setup.md deleted file mode 100644 index d6766cb6561..00000000000 --- a/packages/thirdweb/comparison/setup.md +++ /dev/null @@ -1,101 +0,0 @@ -# Setup Comparison - -## `thirdweb@alpha` - -### Install - -```bash -npm install thirdweb@alpha -``` - -### Hello World - -```javascript -import { createThirdwebClient, getContract } from "thirdweb"; -import { totalSupply } from "thirdweb/extensions/erc20"; - -const client = createThirdwebClient({ - clientId: "", -}); - -const contract = getContract({ - client, - chainId: 1, - address: "0x...", -}); - -const supply = await totalSupply({ - contract, -}); -``` - -## `@thirdweb-dev/sdk` - -### Install - -```bash -npm install @thirdweb-dev/sdk ethers@^5 -``` - -### Hello World - -```javascript -import { ThirdwebSDK } from "@thirdweb-dev/sdk"; - -const sdk = new ThirdwebSDK(1, { - clientId: "", -}); - -const contract = await sdk.getContract("0x..."); - -const supply = await contract.erc20.totalSupply(); -``` - -## `viem` - -### Install - -```bash -npm install viem -``` - -### Hello World - -```javascript -import { createPublicClient, http } from "viem"; -import { mainnet } from "viem/chains"; - -// the ABI of the contract -const abi = [...]; - -const client = createPublicClient({ - chain: mainnet, - transport: http(), -}); - -const contract = getContract({ address: '0x...', abi, client }) -const supply = await contract.read.totalSupply() -``` - -## `ethers` - -### Install - -```bash -npm install ethers -``` - -### Hello World - -```javascript -import { getDefaultProvider, Contract } from 'ethers' - -// the ABI of the contract -const abi = [...]; - -const provider = getDefaultProvider() - -// Create a contract -const contract = new Contract('0x...', abi, provider) -const supply = await contract.totalSupply() -``` diff --git a/packages/thirdweb/package.json b/packages/thirdweb/package.json index b019c3ba87a..3b76b236fac 100644 --- a/packages/thirdweb/package.json +++ b/packages/thirdweb/package.json @@ -182,7 +182,7 @@ } }, "scripts": { - "bench": "vitest bench", + "bench": "vitest -c ./test/vitest.config.ts bench", "format": "prettier --write 'src/**/*'", "lint": "eslint src/", "knip": "knip",