Skip to content

Commit

Permalink
new benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Feb 27, 2024
1 parent bf31097 commit 6cbff02
Showing 1 changed file with 84 additions and 28 deletions.
112 changes: 84 additions & 28 deletions packages/thirdweb/benchmarks/read-contract.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import {
defineChain,
} from "..";
import { ThirdwebSDK } from "../../sdk";
import { ethers as ethers5 } from "ethers5";
import { ethers as ethers6 } from "ethers6";
// eslint-disable-next-line no-restricted-imports
import * as viem from "viem";

const SECRET_KEY = process.env.TW_SECRET_KEY as string;

Expand All @@ -28,13 +32,27 @@ const NEW_CONTRACT = getContract({
address: USDC_CONTRACT_ADDRESS,
});

const OLD_CONTRACT = await new ThirdwebSDK(LOCAL_RPC, {
secretKey: SECRET_KEY,
readonlySettings: {
chainId: 1,
rpcUrl: LOCAL_RPC,
const ABI = [
{
inputs: [
{
internalType: "address",
name: "account",
type: "address",
},
],
name: "balanceOf",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
}).getContract(USDC_CONTRACT_ADDRESS);
] as const;

const sdk = new ThirdwebSDK(LOCAL_RPC, {
secretKey: SECRET_KEY,
Expand All @@ -44,6 +62,28 @@ const sdk = new ThirdwebSDK(LOCAL_RPC, {
},
});

const OLD_CONTRACT = await sdk.getContract(USDC_CONTRACT_ADDRESS);

const ethers5Contract = new ethers5.Contract(
USDC_CONTRACT_ADDRESS,
ABI,
ethers5.getDefaultProvider(LOCAL_RPC),
);

const ethers6Contract = new ethers6.Contract(
USDC_CONTRACT_ADDRESS,
ABI,
ethers6.getDefaultProvider(LOCAL_RPC),
);

const viemClient = viem.createPublicClient({ transport: viem.http(LOCAL_RPC) });

const viemContract = viem.getContract({
abi: ABI,
address: USDC_CONTRACT_ADDRESS,
client: viemClient,
});

describe.runIf(SECRET_KEY)("read contract (warm cache)", () => {
bench("thirdweb", async () => {
await readContract({
Expand All @@ -56,6 +96,18 @@ describe.runIf(SECRET_KEY)("read contract (warm cache)", () => {
bench("@thirdweb-dev/sdk", async () => {
await OLD_CONTRACT.call("balanceOf", [VITALIK_WALLET]);
});

bench("ethers@5", async () => {
await ethers5Contract.callStatic.balanceOf(VITALIK_WALLET);
});

bench("ethers@6", async () => {
await ethers6Contract.balanceOf(VITALIK_WALLET);
});

bench("viem", async () => {
await viemContract.read.balanceOf([VITALIK_WALLET]);
});
});

describe.runIf(SECRET_KEY)("read contract (cold cache)", () => {
Expand Down Expand Up @@ -89,29 +141,33 @@ describe.runIf(SECRET_KEY)("read contract (cold cache)", () => {
// actually read from the contract
await contract.call("balanceOf", [VITALIK_WALLET]);
});
});

const ABI = [
{
inputs: [
{
internalType: "address",
name: "account",
type: "address",
},
],
name: "balanceOf",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
] as const;
bench("ethers@5", async () => {
await new ethers5.Contract(
USDC_CONTRACT_ADDRESS,
ABI,
ethers5.getDefaultProvider(LOCAL_RPC),
).callStatic.balanceOf(VITALIK_WALLET);
});

bench("ethers@6", async () => {
await new ethers6.Contract(
USDC_CONTRACT_ADDRESS,
ABI,
ethers6.getDefaultProvider(LOCAL_RPC),
).balanceOf(VITALIK_WALLET);
});

bench("viem", async () => {
await viem
.getContract({
abi: ABI,
address: USDC_CONTRACT_ADDRESS,
client: viem.createPublicClient({ transport: viem.http(LOCAL_RPC) }),
})
.read.balanceOf([VITALIK_WALLET]);
});
});

describe.runIf(SECRET_KEY)("read contract (pre-defined abi)", () => {
bench("thirdweb", async () => {
Expand Down

0 comments on commit 6cbff02

Please sign in to comment.