//SPDX-License-Identifier: UNLICENSED pragma solidity >=0.6;// this is just to show you the functionalities of transfer, balance, address and payablecontract example{ function move_money(uint x, address payable a) public{ a.transfer(x); //this transfers x wei from this contract to address a // attention that money is uint (unsigned int, so no negative money) } function tell_contract_address() view public returns(address){ return address(this); // is the address of this contract } function tell_balance() view public returns(uint){ return (address(this)).balance; // is the balance of the contract in wei } function receive_money( ) public payable{ // a function needs to be payable to receive money // clearly here insert some code that you need // the person who is sending money is msg.sender and the amount is msg.value } receive() external payable{ // in previous versions of Solidity it was instead function() external payable{ // a function without money is called RECEIVE or FALLBACK (there is a slight difference) and // is used whenever somebody sends money to the smart contract without activating any function // the person who is sending money is msg.sender and the amount is msg.value // clearly here insert some code that you need } }// this instead is the decentralized unstoppable bankcontract bank{ mapping(address => uint) private situation; // it is the bank's accounting system // this is like a Python dictionary, address is the key and uint is the value // pay attention that this situation is only the bank accounting system. Do not confuse it with // the address balance that you may get with
.balance which is instead the ether in Metamask receive() external payable{ situation[msg.sender]=situation[msg.sender]+msg.value; // this is identical to Python dictionary, we update our bank's ledger } function deposit() payable public{ situation[msg.sender]=situation[msg.sender]+msg.value; } function deposit_on_another(address x) payable public{ situation[x]=situation[x]+msg.value; } function withdraw(uint q) public{ // if(situation[msg.sender]>=q){ // situation[msg.sender]=situation[msg.sender]-q; // (payable(msg.sender)).transfer(q); // } // if(situation[msg.sender]>=q){ // this is a better alternative to an IF. If require fails, the entire function fails and any ether send is returned require(situation[msg.sender]>=q); situation[msg.sender]=situation[msg.sender]-q; (payable(msg.sender)).transfer(q); } function internal_transfer(uint quantity,address receiver) public{ require(situation[msg.sender]>=quantity); situation[msg.sender]=situation[msg.sender]-quantity; situation[receiver]=situation[receiver]+quantity; } function tell_balance() view public returns(uint){ // if situation is private this function is necessary to get to know your own's balance return(situation[msg.sender]); } receive() payable external{ situation[msg.sender]=situation[msg.sender]+msg.value; }}