1

I have Solidity contract that is meant to send ether to someone. Unfortunately it's giving me a cryptic error message after I confirm the transaction in MetaMask.

I'm testing on a local ethereum network running on ganache and the project was built using a react truffle box.

Contract:

pragma solidity ^0.4.24;

contract Sender {
    uint256 public balance;

    function send(address _receiver) payable {
        msg.sender.transfer(msg.value);
    }

    function getBalance() public returns (uint) {
        return address(this).balance;
    }

}

JavaScript

userInputHandler = (event) => {
    this.setState({amountToPay: event.target.value})

    const contract = require('truffle-contract')
    const sender = contract(Sender);
    sender.setProvider(this.state.web3.currentProvider);

    this.state.web3.eth.getAccounts((error, accounts) => {
        sender.deployed().then((instance) => {

            console.log("Seems to explode after here" );
            return instance.sendTransaction({
                from: accounts[0],
                to: accounts[4],
                value: this.state.web3.toWei(this.state.amountToPay, "ether"),
                gas: "220000"}).then((result) => {

                console.log("result: " + result);
        })
    })
})

};

ErrorMessage

inpage.js:1
MetaMask - RPC Error: Error: Error: [ethjs-rpc] rpc error with payload
{
"id": 8476063269707,
"jsonrpc": "2.0",
"params": ["0xf87182011a85174876e80083035b609406c1939f6cbb68d42333f140cae815cc36d341b0880de0b6b3a764000080822d46a011d708c9d53a5e7e9195c32fab4f6f3cad259887436eadd521d93ac853541455a0176061761d6d14c063eb2b0c359457dec982d72d52f19bdf33fa8f5be20ed939"],
"method": "eth_sendRawTransaction"
} Error: VM Exception while processing transaction: revert

Uncaught (in promise) Error: Error: Error: [ethjs-rpc] rpc error with payload  Error: VM Exception while processing transaction: revert

Solution send seems to be some kind of reserved word or function that was getting overridden. I changed the function name to sendMoney and all suggestions from @Jaime which fixed my problems.

3

The error is that your contract can not receive ether because it doesn't have a fallback function. Just add this to it:

function () public payable{
}

This will allows your contract to accept ether.

Another thing is that in your send function you have:

msg.sender.transfer(msg.value);

but should be

 _receiver.transfer(msg.value);

Also, is not clear what are you trying to do in your js code, are you trying to send ether to the contract, or are you trying to use the 'send' function?

In the first case (sending ether to the contract use):

web3.eth.sendTransaction({....})

In the second case (trying to use the send function in your contract), having your contract instance as 'contractInstance' you can do something like this:

var account = web3.eth.accounts[0]; 
var mycontract = web3.eth.contract(ABI);
var contractInstance = mycontract.at('contractaddress...')
contractInstance.send('0x...',{'value':web3.toWei(myvalue,'ether')} function(err,res){
        if(err){
            console.log('transaction failed')
        }
    })

hope this helps

  • 1
    Please see my update as also in your js code is not clear what you are trying to do. I added an explanation if you want to send ether to the contract, or if you want to use the 'send' function of your contract. – Jaime Sep 3 '18 at 15:54
  • Thanks again. I was trying to create a contract that let me send ether to another address but went around in circles trying different things after I got the obscure error. – Kai Sep 3 '18 at 19:24
  • With your second example how would you add the msg.value I seem to be getting some insanely high number if I attempt to call it like this: instance.send('0xf17f52151EbEF6C7334FAD080c5704D77216b732', {value: this.state.web3.toWei(1, 'ether')}, function(err,res){ if(err){ console.log('transaction failed'); console.log(err); } console.log(res); }) – Kai Sep 4 '18 at 8:36
  • what is the insanely high value?, as you are setting 1 ether, that will be 100000000000000000 wei. – Jaime Sep 4 '18 at 8:39
  • with the above code MetaMask opens and gives an error about insufficient funds and thinks I'm trying to send: $3.945581176514035e+32 ♦ 1.378706120803003e+30 – Kai Sep 4 '18 at 8:51

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.