1

Trying to do a simple transfer of Eth in Ethers js. When manually setting the maxFeePerGas and the maxPriorityFeePerGas, the transaction goes through. Yet when I use feeData from wallet.getFeeData(), even if the values are lower than the manual setting, the Tx fails, citing "insufficient funds for intrinsic transaction cost".

This test was repeated a number of times, with the same result. What do you think is going on? Your help is greatly appreciated.

async transfer(destinationAddress: string, amountInWei: ethers.BigNumberish) {
    // eslint-disable-next-line no-console
    console.log(`transferring ${amountInWei} wei to ${destinationAddress} over ${this.network.toString()}`)
    const privateKey = await this.getSecretKey('wallet-private-key')
    const config = await this.getConfig()
    const alchemy = new Alchemy(config)
    const wallet = new Wallet(privateKey, alchemy)
    const chainId = (await alchemy.core.getNetwork()).chainId
    const nonce = await alchemy.core.getTransactionCount(wallet.address, 'latest')

    const gasLimit = await wallet.estimateGas({
      to: destinationAddress,
      value: amountInWei,
    })

    const feeData = await wallet.getFeeData()

    // const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? Utils.parseUnits('15', 'gwei')
    const maxPriorityFeePerGas = Utils.parseUnits('15', 'gwei')
    // const maxFeePerGas = feeData.maxFeePerGas ?? Utils.parseUnits('620', 'gwei')
    const maxFeePerGas = Utils.parseUnits('620', 'gwei')

    console.log(
      `Signing trx with - nonce-${nonce} gasLimit=${gasLimit} maxPriorityFeePerGas=${maxPriorityFeePerGas} default=${Utils.parseUnits('20', 'gwei')} maxFeePerGas=${maxFeePerGas}, default=${Utils.parseUnits('500', 'gwei')}`,
    )

    const rawTransaction = await wallet.signTransaction({
      to: destinationAddress,
      value: amountInWei,
      gasLimit,
      maxPriorityFeePerGas,
      maxFeePerGas,
      nonce,
      chainId,
      type: 2,
    })

    const tx = await alchemy.core.sendTransaction(rawTransaction)
    console.log('Sent transaction', tx) // eslint-disable-line
    const ret: Tx = {
      hash: tx.hash,
      from: tx.from,
      to: tx.to,
      timestamp: tx.timestamp,
      type: tx.type ?? undefined,
      value: tx.value.toString(),
    }
    return ret
  }

1 Answer 1

2

Using values from getFeeData() to send transactions seems to be working fine.

I tried it and couldn't reproduce the error you're getting.

Here's the script to send a transaction using values from getFeeData() through Alchemy SDK:

// Imports the Alchemy SDK
const { Alchemy, Network, Wallet, Utils } = require("alchemy-sdk");
const ethers = require('ethers');

// Configures the Alchemy SDK
const config = {
    apiKey: "demo", // Replace with your API key
    network: Network.ETH_GOERLI, // Replace with your network
};

// Creates an Alchemy object instance with the config to use for making requests
const alchemy = new Alchemy(config);


const PRIVATE_KEY = "your-private-key";

const main = async () => {

    let feeData = await alchemy.core.getFeeData()

    console.log(feeData)

    const wallet = new Wallet(PRIVATE_KEY);

    const transaction = {
        to: "0xFd706f0A70074FB3A6c251C2d65B0433E2e902c8",
        value: Utils.parseEther("0.1"),
        gasLimit: "21000",
        maxPriorityFeePerGas: feeData.maxPriorityFeePerGas,
        maxFeePerGas: feeData.maxFeePerGas,
        nonce: await alchemy.core.getTransactionCount(wallet.getAddress()),
        type: 2,
        chainId: 5, // Corresponds to ETH_GOERLI
      };

    const rawTransaction = await wallet.signTransaction(transaction);
    const responseTx = await alchemy.transact.sendTransaction(rawTransaction)
    const result = await responseTx.wait();

    // Logging the response to the console
    console.log(result)

};

main();

Remember to replace the values for your API key, private key and network.

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.