Replies: 1 comment
-
I was able to get typing working using this const result = await client.multicall({
contracts: Object.entries(ERC20_TOKENS_ALLOWED).map(([name, token]) => ({
abi: ERC20_ABI,
address: token.address,
functionName: "balanceOf",
args: [input.address]
})) as {
abi: typeof ERC20_ABI,
address: `0x${string}`,
functionName: "balanceOf",
args: [`0x${string}`]
}[]
});
return result.map((res) => {
res.result // bigint | undefined
}) But was feeling up to doing some type gymnastics so I was able to write a generic function for it, hope this helps! import { type Address, type Narrow, type AbiStateMutability } from "abitype";
import { type GetFunctionArgs, type Abi, type InferFunctionName } from "viem";
const result2 = await client.multicall({
contracts: typedMulticall(ERC20_ABI, "balanceOf", Object.entries(ERC20_TOKENS_ALLOWED).map(([name, token]) => ({
address: token.address,
args: [input.address] as const
})))
});
function typedMulticall<
const TAbi extends Abi | readonly unknown[] = Abi, TFunctionName extends string = string,
TAbiStateMutability extends AbiStateMutability = AbiStateMutability
>(
abi: Narrow<TAbi>, functionName: InferFunctionName<TAbi, TFunctionName, TAbiStateMutability>,
calls: {
address: Address,
args: GetFunctionArgs<TAbi, TFunctionName>["args"]
}[]
) {
return calls.map((call) => ({
abi,
address: call.address,
functionName,
args: call.args
}));
} The calls all have to be the same function from an ABI, but they can all be different addresses and args. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Imagine that I want to create some functions on top of viem multicaller because I need to create
contracts calls
dynamically.In this fork I created a new test that shows the idea.
Example based on your multicall test where type inference is not working
Same example with extra generic hint so that inference works but TS throws an error:
How would you type this example to make it work with type inference? I think that I lack of TS knowledge 🧙
Beta Was this translation helpful? Give feedback.
All reactions