0

I am trying to execute a transaction by first signing it with my private key using ethereumjs-tx and then using sendSignedTransaction to send the transaction to the network. But somehow the transactions are stuck in pending state and I am not getting any receipt.

Below is the complete code that I am using.


const Web3 = require('web3') // ^1.0.0-beta.55

const abi = [
    {
        "constant": false,
        "inputs": [
            {
                "name": "name",
                "type": "string"
            }
        ],
        "name": "addName",
        "outputs": [],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "constant": true,
        "inputs": [],
        "name": "current_name",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    }
]

const address = "0xffae257a6c0734ffbfb4c74fac865d95882d1153"

const network = {
    ropsten: "https://ropsten.infura.io"
}

var web3 = new Web3(new Web3.providers.HttpProvider(network.ropsten))

//Test Keys
const keystore = {
    ropsten: {
        address: "0x232D1038Ca6d21DF85F2109E6155E3f7c0Eea808",
        privateKey: "dd3853d79745bd65a3a2691052e9fe27c39a314e45780b389e5ceb236d615f6c"
    }
}

const TestContract = new web3.eth.Contract(abi, address,{
    from: keystore.ropsten.address,
    gas: 3000000,
  })


const sendTRX = async () => {
    let data = TestContract.methods.addName("vasa").encodeABI()

    const Tx = require('ethereumjs-tx')
    const privateKey = new Buffer(keystore.ropsten.privateKey, 'hex')

    let nonce = await web3.eth.getTransactionCount(keystore.ropsten.address);

    console.log("nonce: ", nonce)

    const rawTx = {
        nonce: nonce,
        gasPrice: '100',
        gasLimit: '300',
        to: address,
        data: data
    }

    const tx = new Tx(rawTx);
    tx.sign(privateKey);

    const serializedTx = tx.serialize()

    web3.eth.sendSignedTransaction('0x' + 
    serializedTx.toString('hex'))
    .on('receipt', (receipt)=> {
        console.log(receipt)
    });
}

sendTRX()
  • Comments are not for extended discussion; this conversation has been moved to chat. – eth May 12 at 23:55
0

Probably the problem is that you are setting the gasPrice to 100 and the gasLimit to 300. A miner won't include your transaction into a block if there are others with a higher price and thus, more profitable.

Gas prices are always fluctuating based on market demand and what users are willing to pay, so hardcoding a gas price is sometimes not ideal. Instead, you can use await web3.eth.getGasPrice() to get the average gas price based on previous blocks.

The gas limit should be as high as you expect your transaction will consume, and the amount of gas a transaction consumes depends on what it does. To get an estimation, you can use web3.eth.estimateGas, this executes a message call or transaction, which is directly executed in the VM of the node, but never mined into the blockchain and returns the amount of the gas used. (See the Javascript API)

NOTE: The gas limit set by the estimateGas() method is based on the current state of the blockchain, and is just an estimate. If your transactions are constantly failing, or if you prefer to have full control over the amount of gas your application spends, you may want to set this value manually. (Ref.)

  • Thanks for your answer. The Question has already been answered here – Vaibhav Saini May 14 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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