0

I am new to solidity and trying to develop a small smart contract. I want to send ether from the sender account to a given account. But I always get this error "VM error: revert. revert The transaction has been reverted to the initial state. Reason provided by the contract: "Insufficient balance". Debug the transaction to get more information."

pragma solidity >=0.5.0.0 <0.9.0;

contract SimpleTranaction {
    address  payable public receiver =  0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;

    function sendTo(uint amount)   public  payable  {
      
        require(msg.sender.balance < amount, "Insufficient balance");
        receiver.transfer(amount); 
    }
}

I use Remix IDE. The sender account has an enough balance enter image description here

What could be the reason for this? Am I doing something theoretically wrong? If so what is the best approach to do this?

0

First you really should not use such a big range of pragma. ">=0.8.0" should be fine for the moment.

require(msg.sender.balance < amount, "Insufficient balance");

Checks if the balance of the sending Externally Owned Account (EOA) is smaller than "amount".

If you change the condition to ">=" in runs through.

pragma solidity >=0.8.0;
contract SimpleTransaction {
address payable receiver = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);

function sendTo(uint256 amount)   external  payable  {
    require(msg.sender.balance >= amount, "Error because balance");
    receiver.transfer(amount); 
}

}

2
  • toothleth_byte thanks for your response. I changed the code as you proposed. But I get the same error. What I dont understand is when I call receiver.transfer(amount) is it transfer from EOA to the receiver or from the contract address to the receiver? . When I enter the same amount into the Value field of Remix, the transaction was successfully completed. – Rama Feb 3 at 19:33
  • address.transfer tries to send ether from the Smart Contract Owned Account to the receiver address. That said, you use a very big range of pragmas, I will change my answer a little bit with a working example. – toothleth_byte Feb 3 at 19:42

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.