0

I have a contract on polygon and I want to call a write function setPrice(uint256 _price) but when I run this code then I am getting an error:

{ code: -32000, message: 'transaction underpriced' }

Smart Contract: https://polygonscan.com/address/0x8835c31178f8f3407d4588be49532de1c02c3a04

this is my code:

const Web3 = require("web3");
const WalletProvider  = require("@truffle/hdwallet-provider");
const http = require("http");
const { release } = require("os");
const mysql = require('mysql');


const conn = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database:"nft",
});

conn.connect(function(err) {
  if (err) throw err;
  //console.error("Connected!");
});


let provider = new WalletProvider({
  mnemonic: {
    phrase: '*****myWalletPhrase*****'
  },
  providerOrUrl: "https://rpc-mainnet.matic.quiknode.pro/"
});

const web3 = new Web3(provider);

const dexABI ="smartContractAbi";

const contract_address = "0x8835c31178F8f3407d4588Be49532De1c02C3A04";
const contract = new web3.eth.Contract(dexABI, contract_address);

function toFixed(x) {
  // some code
}


http.createServer(async (req, res) => {
  if(req.url != '/favicon.ico')
    {
        try {
            const accounts =  await web3.eth.getAccounts();
            //console.error(accounts);
          
            let user= accounts[6];
            let amount=0;
            conn.query(`SELECT * FROM dollor_rate`, async function(err,result){
                      if (err) return err;
                      console.error("this",result[0].usd_rate);
                      amount=toFixed(result[0].usd_rate).toString();
                      await contract.methods.setPrice(''+amount).send({from:user,gas:21000},function(err1,receipt){
                          if(err1) 
                          console.error("Error==>",err1);
                          else
                          console.error("Erorr1=======",receipt);
                        });
                  });
        } catch (error) {
            console.log(error);
        }
    res.end();
  }
    })  
  .listen(8080);

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Does this answer your question? [transaction underpriced in BEP-20 Token transaction](https://stackoverflow.com/questions/67972674/transaction-underpriced-in-bep-20-token-transaction) – TylerH Aug 04 '22 at 16:44

1 Answers1

0

The gas fee you are providing with the transaction is low. Try increasing the gas you provide with the transaction. This is similar to this, https://stackoverflow.com/a/67974018/7453857

Debashish
  • 511
  • 4
  • 13
  • but when I use gasPrice:402480500000 then it's work but it takes a lot of gas fee(0.01 matic). – Sumit Sharma Aug 04 '22 at 10:17
  • You can ignore the gas price or use `contract.methods.setPrice(''+amount).estimateGas({from:user})` to determine gas. – Debashish Aug 04 '22 at 10:30
  • I used the above code **contract.methods.setPrice(''+amount).estimateGas({from:user}).then(function(d){ console.log(d); }); // returns 28222** then I used that value (28222) in gas like this **contract.methods.setPrice(''+amount).send({from:user,gas:28222},function(err1,receipt){** but it still showing same error – Sumit Sharma Aug 04 '22 at 10:43