I am using ethers to interact with contracts on Ethereum.
This is how my method looks like:
const tx = await signer
.sendTransaction({
to: contractAddress,
value: ethers.utils.parseEther(price),
data: hex,
gasPrice: ethers.utils.parseUnits(gas, 9),
})
And it works fine. But every time I have to look on Etherscan to find transaction hex and pass it to the data parameter. Is it possible to just pass method name from contract and parameters to it instead of passing this hex value?
Maybe there is any method in ethers to encode that method with parameters into hex value?
What you need is the contract ABI. Generally it's written as a json object you can import in your js script, or it can be written like this (human-readable-ABI):
The ABI contains all (or some) methods of a contract and how to encode/decode them.
With the ABI you can create a contract object like this:
which can be used to send transactions like this: