1

I am using ropsten.infura as my provider. It is my very first time to use infura so please bear with me.

This is my code for requesting the transaction.

web3().eth.getTransactionCount(coinbase)
    .then(nonce => {
            console.log(nonce)
            const privateKey = new Buffer(pKey, 'hex')
            let data = newContractInstance.methods.redeem('0x91320aBaa4eA71BcDaE54f99eD6a6D36D44539C7', 5).encodeABI()
            let rawTx = {
                nonce: nonce,
                gasPrice: 10000000000,
                gasLimit: 3000000,
                data: data
            }

            let Tx = new ETHtx(rawTx)
            Tx.sign(privateKey)
            let serializedTx = '0x' + Tx.serialize().toString('hex')

            web3().eth.sendSignedTransaction(serializedTx)
            .then(result => {
                console.log(result)
                resolve(result)
            })
            .catch(error => {
                console.error(error)
                reject(error)
            })

I believe that it is successfully contacting the contract but the transaction is being reverted with this error below.

index.js:2178 Error: Transaction has been reverted by the EVM:

{ "blockHash": "0x50af6435e625d8f52043c376ebd7d890651f64fcc847089a781bb700967ea048", "blockNumber": 4063823, "contractAddress": "0x253cE628Df83F20AFAD04C86852D154707e1BF62", "cumulativeGasUsed": 5180761, "from": "0x91320abaa4ea71bcdae54f99ed6a6d36d44539c7", "gasUsed": 3000000, "logs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": false, "to": null, "transactionHash": "0xc52822264a278afdc521b3759d9b4fbb56e4c18d5c52ccb62f90d0741e4dcfc7", "transactionIndex": 38 }

I am not so sure why the transaction is being reverted. I believe it has nothing to do with the modifiers as I have successfully used the same method using metamask before. One thing I noticed about using the sendSignedTransaction() is the contractAddress property. The given contractAddress is not the contractAddress that I was trying to contact and the value also changes on each transaction. Is it really how it behaves and does it represent the contractAddress that you are going to access? I hope someone can enlighten me with this.

Contract Code

    function redeem(address _user, uint256 _amount)
    public
    hasStarted
    onlyOwner
    returns (bool)
{
    uint256 _balanceOf = balanceOf[_user];
    if (totalRemainingToken < _amount) return false;
    if (_balanceOf + _amount < _balanceOf) return false;
    totalRemainingToken -= _amount;
    balanceOf[_user] += _amount;
    emit Redeem(msg.sender, _user, _amount);
    return true;
}
9
  • Could you share the code of the smart contract you are calling?
    – Henk
    Sep 18, 2018 at 9:31
  • @Henk I have edited the question and included the method code.
    – vhie
    Sep 18, 2018 at 9:36
  • "One thing I noticed about using the sendSignedTransaction() is the contractAddress property." Are you sure you are using the address of the contract you deployed to the Ropsten testnet to instantiate newContractInstance in your JS code?
    – Henk
    Sep 18, 2018 at 9:40
  • 1
    I believe it has nothing to do with the modifiers - why don't you get rid of the modifiers and give it another go, just to verify this assumption of yours? Sep 18, 2018 at 9:45
  • @Henk Yes. I've used the balanceOf method of the contract using newContractInstance and was able to get the correct value.
    – vhie
    Sep 18, 2018 at 9:46

1 Answer 1

1

Your transaction is missing the "to" field.

{
  ...
  "status": false,
  "to": null,         <--------
  ..
}

Ethereum will interpret that transaction as a contract creation using your "data" as the contract bytecode, and it fails because it is not valid.

For it to work you have to set "to" to your contract address, in web3 v1.0 to obtain the contract address you can use newContractInstance.options.address

let rawTx = {
    to: newContractInstance.options.address,
    nonce: nonce,
    gasPrice: 10000000000,
    gasLimit: 3000000,
    data: data
}
1
  • this made it work. Thank you very much for the answer. I misinterpreted the '(optional) The destination address of the message' of the 'to' property for raw transactions, I thought it is only use when you are sending eth to another address.
    – vhie
    Sep 19, 2018 at 1:50

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.