0

I tried to write a simple contract (deployed on Virtual VM java script on Remix) that send a certain amount of money from an account to the other. I construct my receiver and sender address, and my send function as payable already as the error suggesting me to do, but it still throws me this error. Even omitting the require does not help as well. I do not understand what else is wrong. Any hints?

Thank you in advance!

Contract code :

pragma solidity >=0.4.0 <0.6.0; // Tested and compiled using 0.5.2

contract Workload {

    address payable public receiver;

    constructor(address payable _receiver) public payable {
        receiver = _receiver;
    }

    event Sent();

    // Get balance of a specified address
    function getBalance(address _from) public view returns (uint256) {
        return address(_from).balance;
    }

    // Get sender address
    function getSender() public view returns (address) {
        return address(msg.sender);
    }

    // Send money to receiver address
    function send(uint amount) public payable {
        address payable sender = msg.sender;
        require(amount <= sender.balance, "Insufficient balance.");
        sender.transfer(amount);;
        emit Sent();
    }
}
  • 1
    What d/o you mean when you say, 'send a certain amount of money'? Is money here is ETH – Prashant Prabhakar Singh Jan 20 at 12:15
0

I am not sure what are you trying to do in send function. Sending ETH back to sender? I guess you want to send ETH to receiver from sender when someone calls send(). If I got you correctly, this is the code you need.

pragma solidity >=0.4.0 <0.6.0; // Tested and compiled using 0.5.2

contract Workload {

address payable public receiver;

constructor(address payable _receiver) public {
    receiver = _receiver;
}

event Sent();

// Get balance of a specified address
function getBalance(address _from) public view returns (uint256) {
    return address(_from).balance;
}

// Get sender address
function getSender() public view returns (address){
    return address(msg.sender);
}

// Send money to receiver address
function send() public payable {
    require(msg.value > 0);
    address payable sender = msg.sender;
    require(msg.value <= sender.balance, "Insufficient balance.");
    receiver.transfer(msg.value);
    emit Sent();
}
}

PS: You need not to have amount in send function. All the ETH (in wei) will be available in msg.value of a payable function.

  • Exactly, I want to send ETH to receiver from an account (sender). I noticed my mistake where I used sender.transfer instead of receiver.transfer as you did. Another problem that I don't understand is that, why can't I specify the amount in the function(?) Thanks for the answer though, it works now :) – ArVe Jan 20 at 12:47
  • Because it will no sense. Say in amount field you enter 1 ETH but you send only 0.5 ETH as msg.value, so tx will fail. In all cases, msg.value has to be greater than equal to the amount. So you can only live with msg.value. If you send more ETH than amount, then remaining funds will be locked in smart contract. – Prashant Prabhakar Singh Jan 20 at 12:57
  • thank you for the explanation, it now makes a lot more sense to me :) – ArVe Jan 20 at 14:55

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.