Skip to content

Commit

Permalink
super early benchmarking etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Jan 29, 2024
1 parent c7da3d2 commit 2d08c42
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
74 changes: 74 additions & 0 deletions packages/thirdweb/benchmark/contract-read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable better-tree-shaking/no-top-level-side-effects */
import { Bench } from "tinybench";
import { Contract, getDefaultProvider } from "ethers6";
import { createPublicClient, http } from "viem";
import { mainnet } from "viem/chains";
import { ThirdwebSDK } from "@thirdweb-dev/sdk";
import { createClient, contract } from "../src";
import { totalSupply } from "../src/extensions/erc20";

const USDC_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";

const bench = new Bench({ iterations: 10, warmupIterations: 1, throws: true });
bench.add("thirdweb@alpha", async () => {
const client = createClient({
clientId: "benchmark",
});

const myContract = contract({
client,
chainId: 1,
address: USDC_ADDRESS,
});

await totalSupply({
contract: myContract,
});
});
bench.add("@thirdweb-dev/sdk", async () => {
const sdk = new ThirdwebSDK(1);

const c = await sdk.getContract(USDC_ADDRESS);

await c.erc20.totalSupply();
});
bench.add("viem", async () => {
// the ABI of the contract
const abi = [
{
inputs: [],
name: "totalSupply",
outputs: [
{
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
] as const;

const client = createPublicClient({
chain: mainnet,
transport: http(),
});

await client.readContract({
address: USDC_ADDRESS,
abi,
functionName: "totalSupply",
});
});
bench.add("ethers@6", async () => {
const abi = ["function totalSupply() view returns (uint256)"];

const provider = getDefaultProvider(1);

const c = new Contract(USDC_ADDRESS, abi, provider);
await c.totalSupply();
});

await bench.warmup();
await bench.run();

console.table(bench.table());
4 changes: 1 addition & 3 deletions packages/thirdweb/comparison/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ const sdk = new ThirdwebSDK(1, {
clientId: "<my-client-id>",
});

const contract = await sdk.getContract({
address: "0x...",
});
const contract = await sdk.getContract("0x...");

const supply = await contract.erc20.totalSupply();
```
Expand Down
2 changes: 2 additions & 0 deletions packages/thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@
"@tanstack/eslint-plugin-query": "^5.17.22",
"@types/bun": "^1.0.4",
"@types/react": "^18.2.48",
"@thirdweb-dev/sdk": "workspace:*",
"eslint-config-thirdweb": "workspace:*",
"eslint-plugin-better-tree-shaking": "0.0.4",
"eslint-plugin-tsdoc": "^0.2.16",
"ethers5": "npm:ethers@^5.0.0",
"ethers6": "npm:ethers@^6.0.0",
"react": "^18.2.0",
"rimraf": "^5.0.5",
"tinybench": "^2.6.0",
"typescript": "^5.3.3"
},
"peerDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d08c42

Please sign in to comment.