Skip to content

Commit

Permalink
feat(zk-sync): add multicall3 for zksync
Browse files Browse the repository at this point in the history
fix #48
  • Loading branch information
Rubilmax committed Jul 18, 2023
1 parent 836e162 commit 94f2090
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
12 changes: 11 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
// same address on all networks: https://github.com/mds1/multicall#multicall2-contract-addresses
export const multicall2Address = "0x5BA1e12693Dc8F9c48aAD8770482f4739bEeD696";

// same address on all networks: https://github.com/mds1/multicall#multicall3-contract-addresses
// same address on all networks: https://www.multicall3.com/deployments
export const multicall3Address = "0xcA11bde05977b3631167028862bE2a173976CA11";

export const multicall3ZkSyncAddress = "0xF9cda624FBC7e059355ce98a31693d299FACd963";

export const multicallAddresses = new Set([
multicall2Address.toLowerCase(),
multicall3Address.toLowerCase(),
multicall3ZkSyncAddress.toLowerCase(),
]);

export const multicall3ChainAddress: { [chainId: number]: string } = {
280: multicall3ZkSyncAddress, // zkSync Ero Goerli
324: multicall3ZkSyncAddress, // zkSync Era
};

export const multicall3DeploymentBlockNumbers: { [chainId: number]: number } = {
1: 14353601, // Mainnet
3: 12063863, // Ropsten
Expand Down Expand Up @@ -38,6 +46,8 @@ export const multicall3DeploymentBlockNumbers: { [chainId: number]: number } = {
25: 1963112, // Cronos
122: 16146628, // Fuse
14: 3002461, // Flare
280: 5885690, // zkSync Ero Goerli
324: 3908235, // zkSync Era
};

export const multicall2DeploymentBlockNumbers: { [chainId: number]: number } = {
Expand Down
25 changes: 8 additions & 17 deletions src/multicall-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ import _debounce from "lodash/debounce";

import { BaseProvider, BlockTag, TransactionRequest } from "@ethersproject/providers";

import { multicall2Address, multicall3Address, multicallAddresses } from "./constants";
import { Multicall2__factory, Multicall3__factory } from "./types";
import { getBlockNumber, getMulticallVersion } from "./utils";

export enum MulticallVersion {
V2 = "2",
V3 = "3",
}
import { multicallAddresses } from "./constants";
import { Multicall2, Multicall3 } from "./types";
import { getBlockNumber, getMulticall } from "./utils";

export interface ContractCall {
to: string;
data: BytesLike;
blockTag: BlockTag;
multicallVersion: MulticallVersion;
multicall: Multicall2 | Multicall3;
resolve: (value: string | PromiseLike<string>) => void;
reject: (reason?: any) => void;
}
Expand Down Expand Up @@ -109,9 +104,6 @@ export class MulticallWrapper {

// Define execution context

const multicall2 = Multicall2__factory.connect(multicall2Address, provider);
const multicall3 = Multicall3__factory.connect(multicall3Address, provider);

let queuedCalls: ContractCall[] = [];

multicallProvider._performMulticall = async function () {
Expand Down Expand Up @@ -153,8 +145,7 @@ export class MulticallWrapper {
calls[calls.length - 1].push(callStruct);
});

const { blockTag, multicallVersion } = blockTagQueuedCalls[0];
const multicall = multicallVersion === MulticallVersion.V2 ? multicall2 : multicall3;
const { blockTag, multicall } = blockTagQueuedCalls[0];

try {
const res = (
Expand Down Expand Up @@ -200,9 +191,9 @@ export class MulticallWrapper {
};

const blockNumber = getBlockNumber(blockTag);
const multicallVersion = getMulticallVersion(blockNumber, this.network.chainId);
const multicall = getMulticall(blockNumber, this.network.chainId, provider);

if (!to || !data || multicallVersion == null || multicallAddresses.has(to.toLowerCase()))
if (!to || !data || multicall == null || multicallAddresses.has(to.toLowerCase()))
return _perform(method, params);

this._debouncedPerformMulticall();
Expand All @@ -212,7 +203,7 @@ export class MulticallWrapper {
to,
data,
blockTag,
multicallVersion,
multicall,
resolve,
reject,
});
Expand Down
26 changes: 20 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Signer } from "ethers";
import { isHexString } from "ethers/lib/utils";

import { BlockTag } from "@ethersproject/providers";
import { BlockTag, Provider } from "@ethersproject/providers";

import { multicall2DeploymentBlockNumbers, multicall3DeploymentBlockNumbers } from "./constants";
import { MulticallVersion } from "./multicall-provider";
import {
multicall2Address,
multicall2DeploymentBlockNumbers,
multicall3Address,
multicall3ChainAddress,
multicall3DeploymentBlockNumbers,
} from "./constants";
import { Multicall2__factory, Multicall3__factory } from "./types";

export const getBlockNumber = (blockTag: BlockTag) => {
if (isHexString(blockTag)) return parseInt(blockTag as string, 16);
Expand All @@ -13,14 +20,21 @@ export const getBlockNumber = (blockTag: BlockTag) => {
return null;
};

export const getMulticallVersion = (blockNumber: number | null, chainId: number) => {
export const getMulticall = (
blockNumber: number | null,
chainId: number,
provider: Signer | Provider
) => {
if (blockNumber != null) {
if (blockNumber <= (multicall3DeploymentBlockNumbers[chainId] ?? Infinity)) {
if (blockNumber <= (multicall2DeploymentBlockNumbers[chainId] ?? Infinity)) return null;

return MulticallVersion.V2;
return Multicall2__factory.connect(multicall2Address, provider);
}
}

return MulticallVersion.V3;
return Multicall3__factory.connect(
multicall3ChainAddress[chainId] || multicall3Address,
provider
);
};

0 comments on commit 94f2090

Please sign in to comment.