Troubleshooting Transaction Reverts in Remix for Solidity

address.transferremixsolidity

Here is my contract

pragma solidity ^0.4.14;
contract Sample {

  address private receiver;
  uint public amount;

  function pay(address _receiver, uint _amount) payable public returns (bool) {
    receiver = _receiver;
    amount =_amount;
    receiver.transfer(amount);
    return true;
  }
}

The error i am getting is,

transact to browser/ballot.sol:Sample.pay errored: VM error: revert.
revert The transaction has been reverted to the initial state. Debug
the transaction to get more information.

Best Answer

Problem: You are trying to send Ethers with the command receiver.transfer(amount); and specifying the amount as a parameter. Since your contract do not have enough ether balance it reverts the transaction.

Solution: You need to have enough ethers in your contract or you should send ethers along with the function call to solve the problem.

Change your smart contract as follows if you need to send _amount to _receiver from sender's address

pragma solidity ^0.4.14;
contract Sample {

  address private receiver;
  uint public amount;

  function pay(address _receiver) payable public returns (bool) {
    receiver = _receiver;
    receiver.transfer(msg.value);
    return true;
  }
}

And your caller function as follows

sampleContract.pay(_receiver, {value : <value in wei>});
Related Topic