I use ethers to interact with solidity contract. I would like to execute multiple transactions at the same time. To do that I know I have to define noonce that would be +1 for each next transaction. I know how to do that when I do
await signer.sendTransaction()
but how can I execute multiple transactions with custom noonce when I create Contract object and execute function on it? I tried
contract.methodName(methodParams, {
gasLimit: gasLimit,
gasPrice,
value: ethers.utils.parseEther(
"0.01"
),
noonce: nextNoonce
})
but it doesn't work and I get error cannot override "noonce". How can I define it here?
To get new nonce every time use getNonce() function
const nonce = await wallet.getNonce();
Complete Code
const { ethers, JsonRpcProvider } = require('ethers');
const fs = require('fs-extra');
async function main() {
const provider = new JsonRpcProvider('http://127.0.0.1:7545');
const wallet = new ethers.Wallet(
'Your private key here',
provider
);
const nonce = await wallet.getNonce();
const txn = {
nonce,
gasPrice: 20000000000,
gasLimit: 1000000,
to: null,
value: 0,
data: '0xpaste binary here',
chainId: 1337, //Change this chain Id, I am using Ganache
};
const sentTxnResponse = await wallet.sendTransaction(txn);
console.log(sentTxnResponse);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Note : I am using ethers version 6.2.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With