3

I have a method that is supposed to create a transaction, send some wei and validate. When looking around I red that you can use infura.io. I also checked that you need to use raw transactions because infura doesn't handle it and it doesn't handle events anymore. But when trying to commit a tx it blocks at the data level. I am not sure if i am just calling my method wrong or if something is missing. I have tested with a method of type view and it returns true / false but transactions are not going through.
I am using ethereumjs-tx and web3 0.x.x (0.20.3)

var web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/XXXXXXXX'));
var contract = web3.eth.contract(abi);
var contractInstance = contract.at('contract address');

// ERROR here / JSON RPC Error
    var data = contractInstance.myMethodInContract(100, {from: 'client address', value: payment_amount}); 

var rawTx = {  
      from: 'client address',
      to: 'contract address', 
      value: payment_amount, 
      data: data 
    }

    var privateKey = new Buffer('private key here', 'hex')

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

    var serializedTx = tx.serialize();

    web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
        if (!err)
            console.log(hash); 
        else
            console.error(err);
    });

Update Following the comments about adding nonce and fixing the data fields. I did so as follow.

var nonce = web3.eth.getTransactionCount('client adress') + 1;
var data = dataPass.contractInstance.getData(100);

var rawTx = {
    ...
    nonce: web3.toHex(nonce),
    ...
}

I got a transaction hash and it returns a tx hash but i have an error transaction underpriced, please note that i red the docs and checked it mutliple times therefore in my work / code i only included what i needed to skip what would be calculated by itself.

I have added the gasPrice and gasLimit to get rid of the transaction underpriced and intristic gas too low errors.

var gasPrice = web3.eth.gasPrice.toNumber() * 1.40
var rawTx = {
     ...
     gasPrice: web3.toHex(gasPrice),
     gasLimit: web3.toHex(100000),
     ....
}

I get a hash but i get nothing when i check the status on https://ropsten.etherscan.io i am told Sorry, we are unable to locate this Transaction Hash.

0x0a7605689071e...e

When reading around (not sure if true or not) the chainId for the rawTx was needed but to get the network Id you need web3 1.0 as i checked 0.20 doesn't have it implement (might be wrong) and even to hardcore the chainId doesn't seem good but for a started i ll get it but i am unable to find a way to get the chainId from transactions (viewing detail of any tx on the blockchain). When trying static numbers like in examples i saw i get a Error: invalid sender.
Following this Can not send eth on Ropsten using Infura node I have added the proper chainId but i am not seeing the transaction on the https://ropsten.etherscan.io Sorry, we are unable to locate this Transaction Hash.
I was adding a + 1 to my nonce value which apparently made it not go through. To who wonders I added a + 1 because someone encountered an issue around where his tx wasn't going through and the proposed solution was the + 1 to nonce due to a potential pending tx already attempting to go through.

var nonce = web3.eth.getTransactionCount('client adress');
3
  • Try to add 'nonce' to your raw transaction. I'm not 100% sure about it, but it might help. Mar 26, 2018 at 13:57
  • Also to calculate the data field you have to call getData in web3 v0.20, ie something like this var data = contractInstance.myMethodInContract.getData(100);
    – Ismael
    Mar 26, 2018 at 19:21
  • How exactly is this issue marked resolved?
    – Bai Bing
    Sep 9, 2022 at 7:47

0

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.

Browse other questions tagged or ask your own question.