UnhandledPromiseRejectionWarning: Error: Transaction was not mined within 750 seconds, please make sure your transaction was properly sent

nodejssendtransactionsoliditytruffle-contractweb3js

I am trying to run the below code:

// Import modules
var Web3 = require("web3");
const HDWalletProvider = require("truffle-hdwallet-provider");
const { interface, bytecode } = require("./compile");

// Get provider link!
const Project_ID_Infura = "put your own one";
const apiKey = "https://rinkeby.infura.io/v3/" + Project_ID_Infura;
const Menmonic_Phrase_2 = "Create your own wallet";
const provider = new HDWalletProvider(Menmonic_Phrase_2, apiKey);

// Create a web3 instance
const web3 = new Web3(provider);

const deploy = async () => {
  const accounts = await web3.eth.getAccounts();

  console.log("Attempting to deploy from account", accounts[0]);

  const result = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({ data: bytecode, arguments: ["Hi there!"] })
    .send({ gas: "1000000", gasPrice: "5000000000", from: accounts[0] });
  // instance with the new contract address
  console.log("Contract deployed to", result.options.address);
};
deploy();

Where the compile.js is here:

const path = require("path");
const fs = require("fs");
const solc = require("solc");

const inboxPath = path.resolve(__dirname, "contracts", "Inbox.sol");
const source = fs.readFileSync(inboxPath, "utf8");

// console.log(solc.compile(source, 1));
a = solc.compile(source, 1).contracts[":Inbox"];
module.exports = a;
// console.log(a.interface);

and the Inbox.sol code which is available in a separate folder called contracts within the working directory has the following code:

pragma solidity ^0.4.17;

contract Inbox {
    string public message;

    function Inbox(string initialMessage) public {
        message=initialMessage;
    }

    function setMessage(string newMessage) public {
        message=newMessage;
    }

}

I am getting the following error:

(node:29588) UnhandledPromiseRejectionWarning: Error: Transaction was
not mined within 750 seconds, please make sure your transaction was
properly sent. Be aware that it might still be mined!

I do not know what is the problem, I do have enough test ether and I put a good gas price. Please help me understand the problem.

Best Answer

I am very thankful to someone who provided me with an answer I wouldn't have understood it if it was not for him.

So the main problem is that there is uncertainty regarding the gasPrice and it seems that there are update packages that take this into account.

So these are the steps that need to be taken once you have seen this error that I wrote in my question.

  1. I visit this web app to cancel the last transaction whereby it request me to connect to Metamask and transfer .001 test ether on the Rinkeby testnet!

  2. I installed the newest wallet-provider library which remove the necessity of the gasPrice as this will be handled internally. Go to the PowerShell window and code npm install @truffle/hdwallet-provider

  3. Change a couple of cosmetic lines in the code such as removing gasPrice and updating the required package. Add the provider.engine.stop() call line to the bottom of the deploy.js to support graceful exiting

The new code is given by

// Import modules
var Web3 = require("web3");
// const HDWalletProvider = require("truffle-hdwallet-provider");
const HDWalletProvider = require("@truffle/hdwallet-provider");
const { interface, bytecode } = require("./compile");

// Get provider link!
const Project_ID_Infura = "Put your own id";
const apiKey = "https://rinkeby.infura.io/v3/" + Project_ID_Infura;
const Menmonic_Phrase_2 =
  "Put your own Mnemonic";
const provider = new HDWalletProvider(Menmonic_Phrase_2, apiKey);

// Create a web3 instance
// const web3 = new Web3(provider);
const ganache = require("ganache-cli");
const web3 = new Web3(ganache.provider());

const deploy = async () => {
  const accounts = await web3.eth.getAccounts();

  console.log("Attempting to deploy from account", accounts[0]);

  const result = await new web3.eth.Contract(JSON.parse(interface))
    .deploy({ data: bytecode, arguments: ["Hi there!"] })
    .send({ gas: "1000000", from: accounts[0] });
  // instance with the new contract address
  console.log("Contract deployed to", result.options.address);
  provider.engine.stop();
};
deploy();