The Smart Contract¶
Let's start with this basic Smart Contract. As mentioned before, it's written in Solidity 0.6, not the latest version, because of the integer roll-over example that we will demonstrate.
//SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
contract ExceptionExample {
mapping(address => uint) public balanceReceived;
function receiveMoney() public payable {
balanceReceived[msg.sender] += msg.value;
}
function withdrawMoney(address payable _to, uint _amount) public {
if(_amount <= balanceReceived[msg.sender]) {
balanceReceived[msg.sender] -= _amount;
_to.transfer(_amount);
}
}
}
Add this Smart Contract to Remix. The correct Solidity Compiler Version should be selected automatically, but double check, to make sure everything is in order:
Okay, what does the Smart Contract do? It's a simplistic wallet contract.
- You can send Ether to the Smart Contract (via
receiveMoney
). - You can withdraw Ether via
withdrawMoney
- The Balance of your account is stored in the balanceReceived mapping.
So far so good. Pay attention to the withdrawMoney function. There is a if-else control structure. If you don't have enough balance, then simply nothing happens. That is not ideal, there is no user-feedback.
Let's give it a try!