0

In this answer, @pabloruiz55 provides the following code for sending ether.

SmartContract.sol
function transferToOwner() payable returns (bool) {
    //Sender should forward at least 1 ether.
    require(msg.value > 1 ether);
    uint amountToSend = 1 ether;
    uint change = msg.value - amountToSend; // Do something with the change. Return it to sender, maybe?
    // If you don't do anything with the change, it will be stuck on this contract
    owner.transfer(1 ether);
    return true;
}

What would be the code for?

// Do something with the change. Return it to sender, maybe?
1

Just take the amount of change and transfer it to the msg.sender. The code to return the Ether to the sender is (just add this line):

msg.sender.transfer(change)

Hope this helps.

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.