Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify MultiCall contract address #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tokensex
Copy link

@tokensex tokensex commented Jun 8, 2023

Description

Adds the ability to select multicall contracts and return unformatted balanceOf.

Defaults to current behavior.

Detail

Checklist

  • 馃挴 maintains 100% unit test coverage
  • 馃И package has been tested locally using npm link
  • 馃寪 examples/web and examples/node demo works

@hzhu hzhu self-requested a review June 11, 2023 08:38
@@ -51,6 +51,7 @@
"@openzeppelin/contracts": "^4.8.0",
"@vitest/coverage-c8": "^0.31.2",
"esbuild": "^0.17.5",
"ethers": "^6.5.1",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ethers package should remain a peerDependency.

@@ -415,7 +415,7 @@ describe("utilities", () => {
"throws when using a network that does not support ENS",
async () => {
const mockProvider = {
getNetwork: async () => ({ chainId: 137n, name: "matic" }),
getNetwork: async () => ({ chainId: BigInt(137), name: "matic" }),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change necessary? I'd prefer to keep the changeset minimal, focusing solely on the capability to specify a MultiCall address. Note that BigInt(137) evaluates to 137n.

Comment on lines +10 to +29
import {abi as erc20Abi} from "@openzeppelin/contracts/build/contracts/ERC20.json";
import multicallAbi from "./abi/multicall.json";
import type { BytesLike, Provider } from "ethers";
import type {BytesLike, Provider} from "ethers";

const coder = AbiCoder.defaultAbiCoder();
const erc20Interface = new Interface(erc20Abi);

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;
},
{}
(typesByName, abiItem) => {
const {name, outputs} = abiItem;
if (outputs) {
return {
...typesByName,
[name]: outputs[0].type,
};
}
return typesByName;
},
{}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there is extra formatting in this file. Can this be removed. As a separate task, I should probably add a prettier config. Sorry about that!

Comment on lines +185 to +214
metaResults: CallResult[],
context: CallContext[]
): MetaByContract {
return metaResults.reduce((meta: BalancesByContract, result, index) => {
let methodValue;
const { 1: data } = result;
const { contractAddress, methodName } = context[index];

try {
const type = fragmentTypes[methodName];
[methodValue] = coder.decode([type], data);
} catch (error) {
console.info(
`Problem decoding ${methodName} for ${contractAddress}. The contract is likely not ERC-20 compliant.`
);
methodValue = decodeBytes32String(data);
}

if (methodName === "decimals") {
methodValue = Number(methodValue);
}

return {
...meta,
[contractAddress]: {
...meta[contractAddress],
[methodName]: methodValue,
},
};
}, {});
return metaResults.reduce((meta: BalancesByContract, result, index) => {
let methodValue;
const {1: data} = result;
const {contractAddress, methodName} = context[index];

try {
const type = fragmentTypes[methodName];
[methodValue] = coder.decode([type], data);
} catch (error) {
console.info(
`Problem decoding ${methodName} for ${contractAddress}. The contract is likely not ERC-20 compliant.`
);
methodValue = decodeBytes32String(data);
}

if (methodName === "decimals") {
methodValue = Number(methodValue);
}

return {
...meta,
[contractAddress]: {
...meta[contractAddress],
[methodName]: methodValue,
},
};
}, {});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that some additional formatting has been inadvertently added. Could these be removed? I think adding a Prettier config should be handled as a separate future task. Thanks!

@hzhu
Copy link
Owner

hzhu commented Jun 12, 2023

Thanks for the PR! I left a few comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants