Skip to content

Instantly share code, notes, and snippets.

@kien-ngo
Created July 28, 2024 12:51
Show Gist options
  • Select an option

  • Save kien-ngo/3c1f982be0d5268d5473927ef2828a8c to your computer and use it in GitHub Desktop.

Select an option

Save kien-ngo/3c1f982be0d5268d5473927ef2828a8c to your computer and use it in GitHub Desktop.
multicall3 example
import { client } from "@/components/client";
import {
encode,
getContract,
prepareContractCall,
sendAndConfirmTransaction,
} from "thirdweb";
import { avalancheFuji } from "thirdweb/chains";
import { aggregate3 } from "thirdweb/extensions/multicall3";
import { privateKeyToAccount } from "thirdweb/wallets";
export default async function handler() {
const account = privateKeyToAccount({
client,
privateKey: process.env.THIRDWEB_ADMIN_PRIVATE_KEY as string,
});
const target = "0x...."; // the contract address you want to call
const targetContract = getContract({
address: target,
chain: avalancheFuji,
client,
})
const multicallContract = getContract({
address: "0xcA11bde05977b3631167028862bE2a173976CA11", // replace with your own multicall3 contract address if need be
chain: avalancheFuji,
client,
});
const _cats = ["0xsomething", "0xsomething2"];
const calls:{ target: string; callData: `0x${string}`, allowFailure: boolean }[] = [];
_cats.forEach(async (_cat) => {
const preparedTx = prepareContractCall({
contract: targetContract,
method: "function get(bytes32 _cat) view returns (uint256)",
params: [_cat as `0x${string}`],
});
const callData = await encode(preparedTx);
const allowFailure = false; // up to you
calls.push({target, callData, allowFailure });
});
const transaction = aggregate3({
contract: multicallContract,
calls,
});
const receipt = await sendAndConfirmTransaction({transaction, account})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment