Error: You cannot invite ‘write’ to the Ethereum agreement using Ethers.js
When working with intelligent contracts in the Ethereum block chain, it is common to use tools similar to Ether. However, there is a decisive phase that many developers ignore when creating their first Ethereum contracts for Eters.js: call methods in the contract.
In this article, we will examine why you can find an error when trying to “write” Ethereum with Ether.js and give instructions to solve it.
Subject:
When informing the local local network of the contract using the following code:
`JavaScript
Const accounts
You create a new example of the Ethereum agreement. However, Ethers.js does not automatically record the contract with the Ethereum Network. If you want to interact with the local hardhat network contract, you must call it and use the "call" function of Eters.js.
Solution:
To correct this problem, you can add the following code before calling the contract method method:
JavaScript
Agreement = new agreement (Tilust0);
This creates a new contract contract using the account address sent. You must also call the "call" function of Ether.js to complete the method call contract.
Example code:
Here is an updated example of how to proclaim the contract and call it to the methods using Ethers.js:
JavaScript
const {ethterit} = require ("hardhat");
// Specify an agreement for ABI (binary application interface)
Const abi = [
{
Income: [],
Stemutability: "show",
Type: "Function"
},
{
Income: ["chain", "int", "Boolean"],
Stemutability: "Write",
Type: "Function"
}
]
// Create a new Hardhat contract
ASYNC MAIN FUNCTION () {
Const accounts
CONST CONTRACTISTANCE = WAIT EThers.getConttfactory ("His contract name");
Const agreement = wait for the contract. Deploy (account0);
// Call the method agreement:
Result const = wait the contract.methods.sourmethod (). Call ();
Console.log (result); // Output: Return value
// Delete the contract manifestation
wait for the contract.
}
// Execute the main function with hardhat
Major ();
`
Conclusion:
In this article, we have emphasized an essential step to interact with intelligent contracts in the Ethereum block chain with Ethers.js. By creating a new contract and inviting your methods, you can do different activities in the local Hardhat network.
Remember to update your code accordingly to use the variable “Contractance” throughout the function. Good coding!