0

I'm trying to send a transaction that writes somesthing on ethereum(kovan) but it seems my transaction is not mined and probably not even send to the network? All my view requests to ethereum work, so my guess is that there is a problem with the signing of my transaction? After firing my script I receive a txHash but I can't find that txHash on kovan.etherscan.io. I thought I might put in a too low gas price and that my transaction is stuck in the mining pool, but I can't even find my transaction in the mining pool so I guess my script didn't even send it correctly?

const Web3 = require('web3');
var Tx = require('ethereumjs-tx').Transaction
const web3 = new Web3(new Web3.providers.HttpProvider("https://kovan.infura.io/v3/-XXX-"));
var contractAddress = "0x07026847C635D66d243971838E69365c9675Cd68";
var abi = -XXX-;
var contract = new web3.eth.Contract(abi, contractAddress);
var account = "-XXX-";
const privateKey = Buffer.from('-XXX-', 'hex');

const data = contract.methods.createLocation("test-location7").encodeABI()

contract.methods.viewNewestComment().call().then(console.log);

web3.eth.getTransactionCount(account, (err, txCount) => {
    console.log(txCount);
    const txObject = {
        nonce: web3.utils.toHex(txCount),
        gasLimit: web3.utils.toHex(200000),
        gasPrice: web3.utils.toHex(web3.utils.toWei('9', 'gwei')),
        to: contractAddress,
        data: data
    }

    const tx = new Tx(txObject)
    tx.sign(privateKey)

    const serializedTx = tx.serialize()
    const raw = '0x' + serializedTx.toString('hex')

    web3.eth.sendSignedTransaction(raw, (err, txHash) => {
        console.log('err:', err, 'txHash:', txHash)
    })
})

So what my script should do after firing it is: using the createLocation methods and "write" a new location name on the blockchain, signing the transaction and finally give me the txHash. But even though I get a txHash I don't think the transation was successful. After 750 seconds I get the following error message in my console: "Error: Transaction was not mined within 750 seconds, please make sure your transaction was properly sent."

I deployed my contract with remix.ethereum.org. And when I use the createLocation method in remix it works! Metamask ask me to sign the transaction and after that I can find my transaction on etherscan.

pragma solidity ^0.5.12;

contract NewContract {

    struct Location {
        string JSON;
        uint time;
        string comment;
    }

    Location[] locations;

    function createLocation(string memory _json) public {
        locations.push(Location(_json, now, 'no comment'));
    }

    function addComment(string memory _comment, uint _id) public {
        locations[_id].comment = _comment;
    }

    function addCommentToNewest(string memory _comment) public {
        uint _id = locations.length - 1;
        locations[_id].comment = _comment;
    }

    function viewLocation(uint _id) public view returns(string memory) {
        string memory result = locations[_id].JSON;
        return result;
    }

    function viewTimestamp(uint _id) public view returns(uint) {
        uint result = locations[_id].time;
        return result;
    }

    function viewComment(uint _id) public view returns(string memory) {
        string memory result = locations[_id].comment;
        return result;
    }

    function viewEverything(uint _id) public view returns(string memory, uint, string memory) {
        string memory result1 = locations[_id].JSON;
        uint result2 = locations[_id].time;
        string memory result3 = locations[_id].comment;
        return (result1, result2, result3);
    }

    function viewNewestLocation() public view returns(string memory) {
        uint _id = locations.length - 1;
        string memory result = locations[_id].JSON;
        return result;
    } 

    function viewNewestTimestamp() public view returns(uint) {
        uint _id = locations.length - 1;
        uint result = locations[_id].time;
        return result;
    }

    function viewNewestComment() public view returns(string memory) {
        uint _id = locations.length - 1;
        string memory result = locations[_id].comment;
        return result;
    }

    function viewNewestEverything() public view returns(string memory, uint, string memory) {
        uint _id = locations.length - 1;
        string memory result1 = locations[_id].JSON;
        uint result2 = locations[_id].time;
        string memory result3 = locations[_id].comment;
        return (result1, result2, result3);
    }   
}

This is my smart contract btw. I actually want to create new Locations names as strings and want to add comments after they are created.

I hope you can help me :)

2 Answers 2

0

I actually solved the problem by reading the doucmentation for ethereumjs-tx it says: When use the package ethereumjs-tx at the version of 2.0.0, if we don’t specify the parameter chain, it will use mainnet, so if you wan to use at the other network, you should add this parameter chain to specify.

So if you specify your transaction to kovan ({'chain':'kovan'}) or ropsten it actually works :)

0

The error only indicates that your transaction hasn't been mined yet, not that there is anything wrong with it:

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

Moreover, it even tells you to make sure that it has been sent (though "mined" would probably be more suitable here).

You can do that by inspecting the transaction list of either the account which you have sent the transaction from, or the destination address which you have sent the transaction to.

In any case, this scenario (I wouldn't even call it "error") typically occurs when your gas-price isn't high enough, since the lower it is - the longer it takes for your transaction to complete.

By increasing this value, you may be able to decrease the execution time below 750 seconds and avoid that scenario.


UPDATE:

I've noticed that you use a constant value of gas-limit without considering the actual amount needed for your specific transaction to complete.

This parameter, in conjunction with the one mentioned above (gas-price), determines how fast your transaction will be executed by one of the miners.

In contrast with gas-price, which increases the average execution time as it grows, gas-limit decreases the average execution time as it grows.

So ideally, you should pass the exact amount required for your transaction to be executed.

Here is how you can do that:

const transaction = contract.methods.createLocation("test-location7")

web3.eth.getTransactionCount(account, (err, txCount) => {
    transaction.estimateGas({from: account}).then(gas => {
        console.log(txCount, gas);
        const txObject = {
            nonce: web3.utils.toHex(txCount),
            gasLimit: web3.utils.toHex(gas), // instead of 200000
            gasPrice: web3.utils.toHex(web3.utils.toWei('9', 'gwei')),
            to: contractAddress,
            data: transaction.encodeABI()
        }
        ...
    })
}
4
  • Thank you for your answer. The problem is I use the same gas price in remix as well and it works. Moreover I actually tried to increase the gas price as well. In addition to that I waited 2 days to see if my transactions might still get mined but they didn't :/ I also checked my infura address, my contract address, the abi, the account with ether on it and the privatekey to ensure that I don't use wrong login parameter
    – Mr. 124
    Mar 29, 2020 at 8:38
  • @Mr.124: Please see updated answer. Mar 29, 2020 at 9:19
  • Oh that's great, that was another question I was wondering about! I will definitly incorporating that method. Thanks!
    – Mr. 124
    Mar 29, 2020 at 9:24
  • @Mr.124: You're welcome. Please note that in some rare cases, estimateGas returns an underestimate of the amount of gas needed. What I do in order to workaround that, is I define some minimum gas limit in my script, for example, const MIN_GAS_LIMIT = 50000;, and then, instead of passing gas, I pass Math.max(gas, MIN_GAS_LIMIT). Mar 29, 2020 at 9:30

Your Answer

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

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