Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { ContractCallContext, Multicall } from "ethereum-multicall";
import {
CallContext,
ContractCallOptions,
ContractCallResults,
} from "ethereum-multicall/dist/esm/models";
import { Contract, providers } from "ethers";
import { FormatTypes } from "ethers/lib/utils";
let multicallAddress: undefined | string = undefined;
export function overrideMulticallAddress(address: string) {
multicallAddress = address;
}
// for simple single-contract, single-result per contract method call cases
export type CallParams = { function: string; params: unknown[] };
export async function multiCallOneContract(
contract: Contract,
calls: CallParams[],
blockNumber?: string | number
): Promise<unknown[]> {
const functionToIndex: Record<string, number> = calls.reduce(
(prev, current, index) => ({
...prev,
[`${current.function}_${index}`]: index,
}),
{}
);
const callContexts = calls.map((call, index) => ({
reference: `${call.function}_${index}`,
methodName: call.function,
methodParameters: call.params,
}));
const multiCallContext: ContractCallContext = {
reference: "this",
contractAddress: contract.address,
abi: JSON.parse(
contract.interface.format(FormatTypes.json) as string
) as unknown[],
calls: callContexts,
};
const multicall = new Multicall({
tryAggregate: false,
ethersProvider: contract.provider,
multicallCustomContractAddress: multicallAddress!,
});
const results = await multicall.call(multiCallContext, {
blockNumber: blockNumber?.toString(),
});
const resultsInOrder: unknown[] = new Array(calls.length);
for (const result of results.results["this"].callsReturnContext) {
const index = functionToIndex[result.reference];
Iif (result.returnValues.length > 1) {
throw new Error(
`ethereum-multicall returned more then one decoded response, which was unexpected. Fix code.`
);
}
resultsInOrder[index] = result.returnValues[0];
}
return resultsInOrder;
}
// for generic multi-contract or multi-result per contract method call cases
export class MulticallResult {
constructor(private multicallResults: ContractCallResults) {}
getResult<T = unknown>(
contractLabel: string,
callLabel: string,
returnValueIndex: number
): T {
return this.getResults<T>(contractLabel, callLabel)[returnValueIndex];
}
getResults<T = unknown>(contractLabel: string, callLabel: string): T[] {
return this.multicallResults.results[contractLabel].callsReturnContext.find(
(result) => result.reference === callLabel
)!.returnValues as T[];
}
getCallParam<T = unknown>(
contractLabel: string,
callLabel: string,
paramIndex: number
): T {
return this.getCallParams<T>(contractLabel, callLabel)[paramIndex];
}
getCallParams<T = unknown>(contractLabel: string, callLabel: string): T[] {
return this.multicallResults.results[contractLabel].callsReturnContext.find(
(result) => result.reference === callLabel
)!.methodParameters as T[];
}
}
export const prepareContractCall = (
reference: string,
contractAddress: string,
abi: unknown[],
calls: CallContext[]
): ContractCallContext => {
return {
reference,
contractAddress,
abi,
calls,
};
};
export const prepareCall = (
reference: string,
methodName: string,
methodParameters: unknown[] = []
): CallContext => {
return {
reference,
methodName,
methodParameters,
};
};
export const callMulticall = async (
provider: providers.Provider,
contractCallContexts: ContractCallContext[] | ContractCallContext,
contractCallOptions?: ContractCallOptions
) => {
const result = await new Multicall({
tryAggregate: false,
ethersProvider: provider,
multicallCustomContractAddress: multicallAddress!,
}).call(contractCallContexts, contractCallOptions);
return new MulticallResult(result);
};
|