Ethers.js – Unable to Send Transaction Using Ethers@^6.10.0

ethers.jshardhat

I'm currently trying to set up a script that sends Ethereum from one account to another. Using the hardhat framework, I provided the sender's private key from the local node and the receiver address. But after the transaction has been signed, I try to log out the receipt after sendTransaction() but I don't get any value.

I previously used ethers@^5.7.0, to set the transfer function, which worked successfully but due to incompatibility issues with the [email protected] and the hardhat version, I had to convert to [email protected]. But converting the transfer function to properly work with [email protected] has been the problem.

const ethers = require("ethers");

async function transfer(body) {
    try {
        const { senderPrivateKey, recipientAddress, amountInEth } = body;
    
        // Create a new wallet instance from the sender's private key
        const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545/");
        const signer = new ethers.Wallet(senderPrivateKey, provider);

        /**
        // Alternative method tried
        // const wallet = new ethers.Wallet(senderPrivateKey, provider);
        // const signer = new provider.getSigner(wallet.address) 
        // or 
        // const signer = new provider.getSigner();
        */
        // Get the current nonce for the sender's address
        const nonce = await signer.getNonce();
        console.log(nonce);
        
        // Create a transaction object
        const tx = {
            to: recipientAddress,
            value: ethers.parseEther(amountInEth),
            gasLimit: 21000,
            gasPrice: (await provider.getFeeData()).gasPrice,
            nonce: nonce
        };

        // Sign the transaction
        const signedTx = await signer.signTransaction(tx);
        console.log(signedTx);

        // Send the transaction
        const sent = await signer.sendTransaction(signedTx);
        const receipt = await sent.wait(1);
        console.log(receipt);

        // console.log(receipt);
    } catch (error) {
        return ({ status: 'error', error });
    }
};

And on my hardhat terminal I get a message:
eth_maxPriorityFeePerGas - Method not supported , but if I run it on a testnet I don't get any receipt logged or any message, apart from the logged signedTx.

Best Answer

After debugging the code meticulously. I noticed that in [email protected], there is no need for the signer.signTransaction(tx) because calling signer.sendTransaction(tx) will populate and sign the transaction before sending.

So the issue I had was signing the transaction and passing the signed transaction as a parameter to the signer.sendTransaction(signedTx). This led to an error in the population process of the sendTransaction method.

The correct code is below:

const ethers = require("ethers");

async function transfer(body) {
    try {
        const { senderPrivateKey, recipientAddress, amountInEth } = body;
    
        // Create a new wallet instance from the sender's private key
        const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545/");
        const signer = new ethers.Wallet(senderPrivateKey, provider);

        /**
        // Alternative method tried
        // const wallet = new ethers.Wallet(senderPrivateKey, provider);
        // const signer = new provider.getSigner(wallet.address) 
        // or 
        // const signer = new provider.getSigner();
        */
        // Get the current nonce for the sender's address
        const nonce = await signer.getNonce();
        console.log(nonce);
        
        // Create a transaction object
        const tx = {
            to: recipientAddress,
            value: ethers.parseEther(amountInEth),
            gasLimit: 21000,
            gasPrice: (await provider.getFeeData()).gasPrice,
            nonce: nonce
        };

        // Send the transaction
        const sent = await signer.sendTransaction(tx);
        const receipt = await sent.wait(1);
        console.log(receipt);

        // console.log(receipt);
    } catch (error) {
        return ({ status: 'error', error });
    }
};
Related Topic