2

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.

2 Answers 2

0

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 });
    }
};
0

It seems like you're facing a couple of issues with your script for sending Ethereum using the Hardhat framework and ethers.js. Let's address them step by step:

[email protected] Compatibility Issue: You mentioned that you encountered compatibility issues with [email protected] and the Hardhat version, leading you to upgrade to [email protected]. However, this upgrade introduced challenges in adapting your transfer function.

Logging Receipt: You're not receiving any value when trying to log out the receipt after sendTransaction(). This issue persists even when you run the script on a testnet.

Error Message eth_maxPriorityFeePerGas - Method not supported: You're encountering this error message in your Hardhat terminal.

Let's address these points:

[email protected] Compatibility Issue:

Given that you've upgraded to [email protected], you need to ensure that your transaction creation and sending process aligns with the updates introduced in this version. The most notable change in ethers@6 is the introduction of the overrides parameter in transaction objects, which replaces gasPrice and gasLimit.

Here's how you can update your transaction object creation to adapt to [email protected]:

    // Create a transaction object
const tx = {
    to: recipientAddress,
    value: ethers.utils.parseEther(amountInEth),
    nonce: nonce,
    gasPrice: await provider.getGasPrice(),
    gasLimit: ethers.utils.hexlify(21000), // Adjust gasLimit as needed
    // Add overrides parameter for [email protected] compatibility
    overrides: {
        gasLimit: ethers.utils.hexlify(21000), // Adjust gasLimit as needed
        gasPrice: await provider.getGasPrice(), // Or specify your desired gas price
    }
};

Logging Receipt Issue:

After sending the transaction, you're trying to log the receipt using await sent.wait(1). However, the issue might be related to how you're handling the receipt. Instead of using await sent.wait(1), you should handle the receipt using the transaction hash.

Here's how you can update your code to properly handle the receipt:

    // Send the transaction
const sent = await signer.sendTransaction(signedTx);
console.log("Transaction sent with hash:", sent.hash);

// Wait for the transaction to be mined
const receipt = await sent.wait();
console.log("Transaction receipt:", receipt);

This way, you're waiting for the transaction to be mined and then logging the receipt.

Error Message eth_maxPriorityFeePerGas - Method not supported:

This error message might be unrelated to your script and could be caused by a specific configuration or limitation in your local Ethereum node or network setup. It's worth investigating further if it persists after addressing the compatibility and logging receipt issues.

Remember to adjust the gas limit and gas price according to your specific requirements and network conditions. Additionally, ensure that your local node is synchronized and running correctly to facilitate transaction processing.

I hope this helps you resolve the issues you're facing with your script! If you have any further questions or encounter any other challenges, feel free to ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.