Skip to content

Commit

Permalink
feat: improve Multicall3 utils (#710)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul <[email protected]>
  • Loading branch information
gsteenkamp89 and pxrl authored Aug 26, 2024
1 parent fdecf9a commit 0dc5430
Show file tree
Hide file tree
Showing 12 changed files with 1,026 additions and 38 deletions.
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
},
"scripts": {
"start": "nodemon -e ts,tsx,json,js,jsx --watch ./src --ignore ./dist --exec 'yarn build'",
"build": "yarn run clean && yarn run build:cjs & yarn run build:esm & yarn run build:types; wait",
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist/cjs --removeComments --verbatimModuleSyntax false && echo > ./dist/cjs/package.json '{\"type\":\"commonjs\"}' && yarn copy-abi:cjs",
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir ./dist/esm && echo > ./dist/esm/package.json '{\"type\":\"module\",\"sideEffects\":false}' && yarn copy-abi:esm",
"build": "yarn run clean && yarn typechain && yarn run build:cjs & yarn run build:esm & yarn run build:types; wait",
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist/cjs --removeComments --verbatimModuleSyntax false && echo > ./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir ./dist/esm && echo > ./dist/esm/package.json '{\"type\":\"module\",\"sideEffects\":false}'",
"build:types": "tsc --project tsconfig.build.json --module esnext --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
"copy-abi:cjs": "DIR=cjs yarn copy-abi",
"copy-abi:esm": "DIR=esm yarn copy-abi",
"copy-abi": "abi=utils/abi/contracts; dstdir=\"./dist/${DIR}/${abi}\"; mkdir -p \"${dstdir}\"; cp ./src/${abi}/*.json \"${dstdir}\"",
"test": "hardhat test",
"test:watch": "hardhat watch test",
"test:run:arweave": "npx -y arlocal",
Expand All @@ -31,7 +28,8 @@
"clean": "rm -rf ./dist",
"bump-version:major": "yarn version --major --no-git-tag-version --no-commit-hooks && git commit -m 'chore: bump version' ./package.json --no-verify",
"bump-version:minor": "yarn version --minor --no-git-tag-version --no-commit-hooks && git commit -m 'chore: bump version' ./package.json --no-verify",
"bump-version:patch": "yarn version --patch --no-git-tag-version --no-commit-hooks && git commit -m 'chore: bump version' ./package.json --no-verify"
"bump-version:patch": "yarn version --patch --no-git-tag-version --no-commit-hooks && git commit -m 'chore: bump version' ./package.json --no-verify",
"typechain": "typechain --target ethers-v5 --out-dir src/utils/abi/typechain 'src/utils/abi/contracts/*.json' && eslint --fix src/utils/abi/typechain && yarn prettier --write \"src/utils/abi/typechain/**/*.ts\""
},
"lint-staged": {
"*.ts": "yarn lint"
Expand Down
68 changes: 38 additions & 30 deletions src/utils/Multicall.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BigNumber, Contract, providers, Signer, utils as ethersUtils } from "ethers";
import { getABI } from "./abi";
import { CHAIN_IDs } from "@across-protocol/constants";
import { Multicall3, Multicall3__factory } from "./abi/typechain";

type Provider = providers.Provider;
type BlockTag = providers.BlockTag;
Expand All @@ -12,41 +13,48 @@ export type Call3 = {
args?: any[];
};

// Multicall3 Constants:
export const multicall3Addresses: Record<number, string> = {
1: "0xcA11bde05977b3631167028862bE2a173976CA11",
10: "0xcA11bde05977b3631167028862bE2a173976CA11",
137: "0xcA11bde05977b3631167028862bE2a173976CA11",
324: "0xF9cda624FBC7e059355ce98a31693d299FACd963",
8453: "0xcA11bde05977b3631167028862bE2a173976CA11",
34443: "0xcA11bde05977b3631167028862bE2a173976CA11",
42161: "0xcA11bde05977b3631167028862bE2a173976CA11",
59144: "0xcA11bde05977b3631167028862bE2a173976CA11",
534352: "0xcA11bde05977b3631167028862bE2a173976CA11",
// Testnets
5: "0xcA11bde05977b3631167028862bE2a173976CA11",
300: "0xF9cda624FBC7e059355ce98a31693d299FACd963",
59140: "0xcA11bde05977b3631167028862bE2a173976CA11",
59141: "0xcA11bde05977b3631167028862bE2a173976CA11",
80002: "0xcA11bde05977b3631167028862bE2a173976CA11",
84531: "0xcA11bde05977b3631167028862bE2a173976CA11",
84532: "0xcA11bde05977b3631167028862bE2a173976CA11",
421613: "0xcA11bde05977b3631167028862bE2a173976CA11",
534351: "0xcA11bde05977b3631167028862bE2a173976CA11",
11155111: "0xcA11bde05977b3631167028862bE2a173976CA11",
11155420: "0xcA11bde05977b3631167028862bE2a173976CA11",
const DETERMINISTIC_MULTICALL_ADDRESS = "0xcA11bde05977b3631167028862bE2a173976CA11";

const NON_DETERMINISTIC_MULTICALL_ADDRESSES = {
[CHAIN_IDs.ZK_SYNC]: "0xF9cda624FBC7e059355ce98a31693d299FACd963",
};

export async function getMulticall3(
chainId: number,
signerOrProvider?: Signer | Provider
): Promise<Contract | undefined> {
const address = multicall3Addresses[chainId];
const DETERMINISTIC_MULTICALL_CHAINS = [
CHAIN_IDs.ARBITRUM,
CHAIN_IDs.BASE,
CHAIN_IDs.BLAST,
CHAIN_IDs.BOBA,
CHAIN_IDs.LINEA,
CHAIN_IDs.LISK,
CHAIN_IDs.MAINNET,
CHAIN_IDs.MODE,
CHAIN_IDs.OPTIMISM,
CHAIN_IDs.POLYGON,
CHAIN_IDs.REDSTONE,
CHAIN_IDs.SCROLL,
CHAIN_IDs.ZORA,
// Testnet:
CHAIN_IDs.BASE_SEPOLIA,
CHAIN_IDs.BLAST_SEPOLIA,
CHAIN_IDs.POLYGON_AMOY,
CHAIN_IDs.SCROLL_SEPOLIA,
CHAIN_IDs.SEPOLIA,
];

export function getMulticallAddress(chainId: number): string | undefined {
if (DETERMINISTIC_MULTICALL_CHAINS.includes(chainId)) {
return DETERMINISTIC_MULTICALL_ADDRESS;
}
return NON_DETERMINISTIC_MULTICALL_ADDRESSES[chainId];
}

export function getMulticall3(chainId: number, signerOrProvider: Signer | Provider): Multicall3 | undefined {
const address = getMulticallAddress(chainId);
if (!address) {
return undefined;
}

return new Contract(address, await getABI("Multicall3"), signerOrProvider);
return Multicall3__factory.connect(address, signerOrProvider);
}

export async function aggregate(multicall3: Contract, calls: Call3[], blockTag?: BlockTag): Promise<Result[]> {
Expand Down
1 change: 1 addition & 0 deletions src/utils/abi/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Multicall3Abi } from "./Multicall3.json";
Loading

0 comments on commit 0dc5430

Please sign in to comment.