You've overlooked a key principle.
The contract is a full participant. It has a balance. You've disregarded msg.value
which is the amount of money received. So, by default, the change stays where it is, in the contract. 2 went in, 1 came out.
You can find the missing funds with a convenience function.
function myBalance() public view returns(uint) {
return address(this).balance;
}
Keep in mind this wouldn't be appropriate for production code. It's unnecessary given that anyone anywhere can inspect any address balance without help from the contract. Still, it's convenient for explorations in Remix.
You could make a betterForwarder()
that accepts a recipient
argument and forwards all funds received, or even returnTheChange()
if you want to achieve the expected behavior. This is not production-ready code. For illustration only.
pragma solidity 0.5.1;
contract Test {
function send() payable public {
address payable recipient = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C;
recipient.transfer(1000000000000000000); // send 1 Ether
}
function myBalance() public view returns(uint) {
return address(this).balance;
}
function betterForwarder(address payable recipient) public payable {
recipient.transfer(msg.value);
}
function returnTheChange(address payable recipient) public payable {
require(msg.value >= 1 ether);
recipient.transfer(1 ether);
if(msg.value > 1 ether) msg.sender.transfer(msg.value - 1 ether);
}
}
Hope it helps.