1
const contractAddress = "Contract Address";
const ABI = "ABI data JSON"

const Web3 = require("web3");
 const transaction = {
    'from' :account.address, 
    'gas': 95000,
    'gasPrice' : web3.utils.toWei("35", "gwei"),
 };
const contract = new web3.eth.Contract(abi,contractAddress)
let result = await contract.methods.mint(to,amount).send(transaction); // require transaction data

In the case of the web3JS module, when calling a smart contract function, the gas fee must be written in the transaction data.

const contractAddress = "Contract Address";
const ABI = "ABI data JSON"

const { ethers,Contract } = require("ethers");
const contract = new Contract(contractAddress,ABI,signer);
let result = await contract.connect(signer).mint(to,amount); // no transaction data

When using the ethersJS module, the transaction is sent by predicting the gas fee without specifying the gas fee to be used.

Does EthersJS automatically estimate and calculate gas costs?

+When sending a transaction with EthersJS, how do I write the code to directly enter the gas fee like web3?

1 Answer 1

2

In EthersJS, when estimating the required amount of gas for a transaction, a node is queried for its best guess. If a node is unable (or unwilling) to predict the cost, the UNPREDICTABLE_GAS_LIMIT error occurs.

See: https://docs.ethers.io/v5/api/utils/logger/#errors--unpredicatable-gas-limit

You can follow the example below to write the code to directly enter the gas fee like web3 when sending a transaction with EthersJS.

const url = `provider url`
const provider = new ethers.providers.JsonRpcProvider(url)
const wallet = new ethers.Wallet(privKey, provider)

const tx = await wallet.sendTransaction({
       to: account,
       value: 90000000000,
       gasPrice: 250000000000,
       gasLimit: 21000,
   })

const receipt = await tx.wait()

See: https://medium.com/klaytn/using-ethereum-tools-in-klaytn-dc068d48de04

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.