Basic Smart Contract¶
Let's create a Smart Contract we start with again. It's slightly different than in the previous Lab, because I actually want to do something with it. By the end of the lab we have some sort of Payment Smart Contract can keep track of deposits and withdrawals in an easy way using mappings and structs.
Create the following file in Remix:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract MappingsStructExample {
function getBalance() public view returns(uint) {
return address(this).balance;
}
function sendMoney() public payable {
}
function withdrawAllMoney(address payable _to) public {
_to.transfer(address(this).balance);
}
}
The Smart Contract is very simplistic. It allows everyone to send Ether to the Smart Contract. But it also allows everyone to transfer all funds from the Smart Contract to any address. Not very secure - yet.
Let's play around with this Smart Contract and see how it works with depositing and withdrawing money, and how we can make it more secure.