passing abi types to an object through a class #140
|
Hi, I'm needing a little bit of help, I'm trying to use abitype in a class that creates a Contract object that has a function called type CallInfo<funcName> = {
name: funcName;
params: any[];
}I'm trying to set abitype in the class constructor and trying to get the abi functions type in the type CallInfo<funcName> = {
name: funcName;
params: any[];
}
export class InitContractTest {
abi: unknown
address: `0x${string}`
chain: Chain
contract: ethers.Contract
constructor({address, abi, chain}: ContractInfo){
this.abi = abi
this.address = address
this.chain = chain
const rpcProvider = new ethers.providers.JsonRpcProvider(this.chain.rpcUrls[0])
this.contract = new ethers.Contract(this.address, this.abi as ethers.ContractInterface, rpcProvider)
}
async write(callInfo: CallInfo<ExtractAbiFunctionNames<typeof this.abi, 'payable'>>, setStatus?: (status: string)=> void){
//...call contract function
}I'm getting this error: |
Answered by
Raiden1411
May 20, 2023
Replies: 1 comment 6 replies
|
I got rid of the errors with this: import { Abi, ExtractAbiFunctionNames } from 'abitype'
export type ContractInfo<ABI = any> = {
address:`0x${string}`
abi:ABI
chain:Chain
}
export class InitContractTest<ABI extends Abi> {
abi: ABI
address: `0x${string}`
chain: Chain
contract: ethers.Contract
constructor({address, abi, chain}: ContractInfo<ABI>){
this.abi = abi
this.address = address
this.chain = chain
const rpcProvider = new ethers.providers.JsonRpcProvider(this.chain.rpcUrls[0])
this.contract = new ethers.Contract(this.address, this.abi as ethers.ContractInterface, rpcProvider)
}
async write(callInfo: CallInfo<ExtractAbiFunctionNames<typeof this.abi, 'payable'>>, setStatus?: (status: string)=> void){
//...call contract function
}but I'm not getting autocomplete here: import { abiERC20 } from './abiERC20'
const newContract = new InitContractTest<typeof abiERC20>(contractInfo)
const res = await newContract.read({
name:'', // <-- no autocomplete here
params:[]
}) |
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


The abi that you are using is missing the
stateMutabilityproperty and that's why you are not getting that autocomplete. The abi that you are using seems like it's from a old solidity version whereconstantswere used to represent the mutability of a contract. That property is now considered deprecated and it was replaced by thestateMutability.Abitype stills supports the constants properties but the
stateMutabilitymust always be present on the abi. You can use the providederc20Abifromabitype/testfor a generic erc20 abi.Made a pr on that repo to fix the problem.