How to properly set up abiTypes to work with ethers v6? #211
-
We have been using Typechain until now, but aim to change to ABItype. Although we really like the proposition of ABItypes, we have found it difficult to set up our contracts using only the documentation. Our goal is to use ABItype with ethers, and that's what we have so far: import {BaseContract as BaseContractEthers } from 'ethers'
type ReadFunctionNames<TAbi extends Abi> = ExtractAbiFunctionNames<TAbi, 'pure' | 'view'>
type FunctionArgsTypes<TAbi extends Abi> = AbiParametersToPrimitiveTypes<
ExtractAbiFunction<TAbi, ReadFunctionNames<TAbi>>['inputs'],
'inputs'
>[0]
type FunctionReturnTypes<TAbi extends Abi> = AbiParametersToPrimitiveTypes<
ExtractAbiFunction<TAbi, ReadFunctionNames<TAbi>>['outputs'],
'outputs'
>[0]
type ContractMethods<TAbi extends Abi> = {
[name in ReadFunctionNames<TAbi>]: (...args: Array<FunctionArgsTypes<TAbi>>) => Promise<FunctionReturnTypes<TAbi>>
}
class BaseContract extends BaseContractEthers {}
export type BaseContractType<TAbi extends Abi> = BaseContract & ContractMethods<TAbi>
export const getContract = <TAbi extends Abi>(
address: string,
abi: InterfaceAbi,
provider: JsonRpcProvider,
): BaseContractType<TAbi> => {
const contract = new BaseContract(address, abi, provider) as BaseContractType<TAbi>
return contract
}
//usage:
const contract = getContract<typeof ContractABI>(address, ContractABI, provider)
console.log(await contract.functionName()) But this is not giving us exactly what we need. The input and output types are not correct. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
You can look at our examples in the repository that you can find here Here is also a example to wrap it on ethers that I made a while ago. Another recomendation that I suggest is looking at the source code from wagmi pre migration to viem. You can find that here and here I hope this helps! |
Beta Was this translation helpful? Give feedback.
-
Hi @BravoNatalie, I'm facing the same issue, and even though @Raiden1411's answer was really helpful, I still need a bit of help finding a working solution. The problem is that TS expects the args to be an array, but for ethers to work, the array needs to be spread (which gives a compilation error...). Would it be possible to post a minimal working setup based on the accepted answer to see what I am missing? Thank you. Edit: I just noticed that the answer wasn't marked by you, so you might also have the same errors. |
Beta Was this translation helpful? Give feedback.
-
Hello @BravoNatalie and @jtourkos we created a draft PR with a example implementation of a wrapper for ethers v6 using abitype. Hopefully this helps your use cases. |
Beta Was this translation helpful? Give feedback.
You can look at our examples in the repository that you can find here
Here is also a example to wrap it on ethers that I made a while ago.
Another recomendation that I suggest is looking at the source code from wagmi pre migration to viem. You can find that here and here
I hope this helps!