Skip to content

Commit

Permalink
refactor: pull fragment types out of function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhu committed Feb 5, 2023
1 parent 590657a commit 8cb451b
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import type { Provider } from "@ethersproject/providers";

const MULTICALL3_CONTRACT = "0xcA11bde05977b3631167028862bE2a173976CA11";

const fragmentTypes = erc20Abi.reduce<Record<string, string>>(
(typesByName, abiItem) => {
const { name, outputs } = abiItem;
if (outputs) {
return {
...typesByName,
[name]: outputs[0].type,
};
}
return typesByName;
},
{}
);

interface Call {
target: string;
callData: string;
Expand Down Expand Up @@ -168,24 +182,13 @@ export function decodeMetaResults(
metaResults: CallResult[],
context: CallContext[]
): MetaByContract {
const fragmentTypeByName = erc20Abi.reduce<Record<string, string>>(
(typesByName, abiItem) => {
const { name, outputs } = abiItem;
if (outputs === undefined) return typesByName;
return {
...typesByName,
[name]: outputs[0].type,
};
},
{}
);
return metaResults.reduce((meta: BalancesByContract, result, index) => {
let methodValue;
const { 1: data } = result;
const { contractAddress, methodName } = context[index];

try {
const type = fragmentTypeByName[methodName];
const type = fragmentTypes[methodName];
[methodValue] = defaultAbiCoder.decode([type], data);
} catch (error) {
console.info(
Expand Down Expand Up @@ -263,7 +266,7 @@ export async function getAddress(addressOrName: string, provider: Provider) {
}

/**
* Returns the ERC20 balances for an Ethereum address using the
* Returns the ERC20 balances for an Ethereum address using the
* {@link https://github.com/mds1/multicall#multicall--- multicall smart contract}
*
* @param BalanceRequest - The request used to fetch ERC20 balances
Expand Down Expand Up @@ -315,7 +318,7 @@ export async function aggregate(calls: Call[], provider: Provider) {
*
* @param array - The array to be chunked
* @param size - The size of each chunk
* @returns An array of chunks
* @returns An array of chunks
*/
export function chunk<T>(array: T[], size: number) {
const chunked: T[][] = [];
Expand Down

0 comments on commit 8cb451b

Please sign in to comment.