Replies: 3 comments
-
I've just figured right now that the only way to safely interact with the deployed contract using typescript and ethers v6 (no hardhat) is to make use of
And the code looks something like: import { ethers } from "ethers";
import { env, abi } from "./constants";
import fs from "fs";
import { SimpleStorage_sol_SimpleStorage } from "./SimpleStorage_sol_SimpleStorage";
async function main() {
const ABI = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8");
const PRIVATE_KEY = env.privateKey;
const provider = new ethers.JsonRpcProvider(env.rpcUrl);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const ct = new ethers.Contract(
"0xE390BA08b14020CCAf3a18d272AC0f52824b54A7",
ABI
);
const contract = ct.connect(wallet) as SimpleStorage_sol_SimpleStorage;
const addPerson = await contract.addPerson("Sewkito", "2024");
console.log("Please wait...");
await addPerson.wait(1);
console.log("PERSON", await contract.people("0"));
console.log("\nplease wait, getting favorite number...");
console.log("\n[FAVORITE NUMBER]: ", (await contract.retrieve()).toString());
}
main()
.then(() => {
process.exit(0);
})
.catch((e) => {
console.log("[ERROR]: ", e);
process.exit(1);
}); It worked for me. |
Beta Was this translation helpful? Give feedback.
-
As @S0mt0 replied, it is indeed recommended to use typechain. However (if like me) you are struggling with implementing it, the const favoriteNumber = await contract.getFunction("retrieve")();
console.log("Favorite Number: ", favoriteNumber.toString());
works for me. |
Beta Was this translation helpful? Give feedback.
-
In Ethers v6, when you deploy a contract using: The returned object is a BaseContract, which does not automatically expose methods defined in the ABI.
Generate TypeScript Types for Contracts Update Code to Use the Generated Interface After running the command, TypeChain will create a TypeScript interface like: Now use this interface when interacting with the contract: async function main() {
} main().catch(console.error); Key Takeaways |
Beta Was this translation helpful? Give feedback.
-
Hi, I noticed that a lot of things have changed since ethers v6. When I log SimpleStorage contract to the console, it doesn't show all the contract methods that are in the ABI, and I also can't call any of the methods or properties as typescript would warn that such properties don't exist on type BaseContract.
Here's a snippet of the code:
Here's what the contract looks like when logged to the console:
And I get the following error when I try to use the "retrieve" method:
Please how do I proceed from here?
Beta Was this translation helpful? Give feedback.
All reactions