-
Hi, I'm using TypeScript for this course with solidity version 0.8.19. When I try to call a method on my SimpleStorage.sol contract from my typescript file, I'm getting the following type error:
This is the SimpeStorage contract: // SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract SimpleStorage {
uint256 favoriteNumber;
// view, pure
function retrieve() public view returns (uint256){
return favoriteNumber;
}
}
This is the way I'm interacting with the contract from my deploy.ts file: import { BaseContract, ContractFactory, ContractTransactionReceipt, JsonRpcProvider, Wallet, ethers } from "ethers";
async function main() {
const contractFactory: ContractFactory<any[]> = new ContractFactory(abi, binary, wallet);
/* Type "any" is working */
const contract: BaseContract = await contractFactory.deploy({ gasLimit: 2000000 });
/* TYPE ERROR => retrieve doesn't exist on type BaseContract */
const currentFavoriteNumber = await contract.retrieve()
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
}); When I assign type "any" to my contract variable, it's working fine but that not the purpose of TypeScript. Does any one know how to fix this type error? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Hello @r-bytes Contracts are not type any. First of all you should import contracts like below: import { BaseContract } from "../../typechain-types" then you can specify it like below: let baseContract: BaseContract
baseContract = await ethers.getContract("BaseContract")
const currentNum = await baseContract.retrieve() or do below with Factory import { SimpleStorage, SimpleStorage__factory } from "../typechain"
let simpleStorage: SimpleStorage
let SimpleStorageFactory: SimpleStorage__factory
SimpleStorageFactory = (await ethers.getContractFactory("SimpleStorage")) as SimpleStorage__factory
simpleStorage = await SimpleStorageFactory.deploy()
const num = simpleStorage.retrieve() |
Beta Was this translation helpful? Give feedback.
-
Update: I tried several solutions including the one mentioned here from @Neftyr. Unfortunately I was unable to fix the issue. So finally I choose to downgrade ethers to version 5.7.2 and with success.. For more info: https://github.com/ethers-io/ethers.js/issues/4287 |
Beta Was this translation helpful? Give feedback.
-
This is what you should do in ethersjs v6: import { Contract } from "ethers";
//...
const response = await (contract!.connect(signer!) as Contract).retrieve()
//... No stress ❤ |
Beta Was this translation helpful? Give feedback.
Update: I tried several solutions including the one mentioned here from @Neftyr.
Unfortunately I was unable to fix the issue. So finally I choose to downgrade ethers to version 5.7.2 and with success..
I created an issue on the github from ethers.
For more info: https://github.com/ethers-io/ethers.js/issues/4287