-
Notifications
You must be signed in to change notification settings - Fork 2
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to specify MultiCall contract address #23
base: main
Are you sure you want to change the base?
Conversation
@@ -51,6 +51,7 @@ | |||
"@openzeppelin/contracts": "^4.8.0", | |||
"@vitest/coverage-c8": "^0.31.2", | |||
"esbuild": "^0.17.5", | |||
"ethers": "^6.5.1", |
There was a problem hiding this comment.
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" }), |
There was a problem hiding this comment.
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
.
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; | ||
}, | ||
{} |
There was a problem hiding this comment.
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!
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, | ||
}, | ||
}; | ||
}, {}); |
There was a problem hiding this comment.
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!
Thanks for the PR! I left a few comments. |
Description
Adds the ability to select multicall contracts and return unformatted
balanceOf
.Defaults to current behavior.
Detail
Checklist
npm link
examples/web
andexamples/node
demo works