From 873119278ce7f0a73f9e96a9332795309417598e Mon Sep 17 00:00:00 2001 From: Timur Badretdinov Date: Mon, 18 Mar 2024 00:57:24 +0000 Subject: [PATCH] Fix typing --- src/call.ts | 36 +++++++++++++++++++++++++++--------- src/multicall.ts | 20 ++++++++++++++++---- src/provider.ts | 6 +++++- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/call.ts b/src/call.ts index f2f4c7f..bb3a69b 100644 --- a/src/call.ts +++ b/src/call.ts @@ -62,14 +62,18 @@ async function all( callData, }; }); - const response = contract + const response = contract && contract.aggregate ? await contract.aggregate(callRequests, overrides || {}) : await callDeployless(provider, callRequests, overrides?.blockTag); const callCount = calls.length; const callResult: T[] = []; for (let i = 0; i < callCount; i++) { - const name = calls[i].name; - const outputs = calls[i].outputs; + const call = calls[i]; + if (!call) { + throw new Error("Unable to access the call"); + } + const name = call.name; + const outputs = call.outputs; const returnData = response.returnData[i]; const params = Abi.decode(name, outputs, returnData); const result = outputs.length === 1 ? params[0] : params; @@ -94,15 +98,22 @@ async function tryAll( callData, }; }); - const response: CallResult[] = contract + const response: CallResult[] = contract && contract.tryAggregate ? await contract.tryAggregate(false, callRequests, overrides || {}) : await callDeployless2(provider, callRequests, overrides?.blockTag); const callCount = calls.length; const callResult: (T | null)[] = []; for (let i = 0; i < callCount; i++) { - const name = calls[i].name; - const outputs = calls[i].outputs; + const call = calls[i]; + if (!call) { + throw new Error("Unable to access the call"); + } + const name = call.name; + const outputs = call.outputs; const result = response[i]; + if (!result) { + throw new Error("Unable to access the result"); + } if (!result.success) { callResult.push(null); } else { @@ -136,15 +147,22 @@ async function tryEach( callData, }; }); - const response: CallResult[] = contract + const response: CallResult[] = contract && contract.aggregate3 ? await contract.aggregate3(callRequests, overrides || {}) : await callDeployless3(provider, callRequests, overrides?.blockTag); const callCount = calls.length; const callResult: (T | null)[] = []; for (let i = 0; i < callCount; i++) { - const name = calls[i].name; - const outputs = calls[i].outputs; + const call = calls[i]; + if (!call) { + throw new Error("Unable to access the call"); + } + const name = call.name; + const outputs = call.outputs; const result = response[i]; + if (!result) { + throw new Error("Unable to access the result"); + } if (!result.success) { callResult.push(null); } else { diff --git a/src/multicall.ts b/src/multicall.ts index 09c8fb7..2774bdc 100644 --- a/src/multicall.ts +++ b/src/multicall.ts @@ -5,7 +5,7 @@ interface Multicall { } function getMulticall(chainId: number): Multicall | null { - const addressMap: Record = { + const addressMap: Record = { 1: { address: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441', block: 7929876, @@ -135,7 +135,11 @@ function getMulticall(chainId: number): Multicall | null { block: 14080409, }, }; - return addressMap[chainId]; + const chainAddressMap = addressMap[chainId]; + if (!chainAddressMap) { + return null; + } + return chainAddressMap; } function getMulticall2(chainId: number): Multicall | null { @@ -225,7 +229,11 @@ function getMulticall2(chainId: number): Multicall | null { block: 14080778, }, }; - return addressMap[chainId]; + const chainAddressMap = addressMap[chainId]; + if (!chainAddressMap) { + return null; + } + return chainAddressMap; } function getMulticall3(chainId: number): Multicall | null { @@ -364,7 +372,11 @@ function getMulticall3(chainId: number): Multicall | null { block: 14080843, }, }; - return addressMap[chainId]; + const chainAddressMap = addressMap[chainId]; + if (!chainAddressMap) { + return null; + } + return chainAddressMap; } const deploylessMulticallBytecode = diff --git a/src/provider.ts b/src/provider.ts index a386127..345f070 100644 --- a/src/provider.ts +++ b/src/provider.ts @@ -115,9 +115,13 @@ class Provider { const multicall = this.#getContract('TRY_EACH', overrides?.blockTag); const provider = this.#provider; const failableCalls = calls.map((call, index) => { + const callCanFail = canFail[index]; + if (callCanFail === undefined) { + throw new Error("Unable to access the canFail value"); + } return { ...call, - canFail: canFail[index], + canFail: callCanFail, }; }); return await callTryEach(provider, multicall, failableCalls, overrides);