Created
September 8, 2021 15:07
-
-
Save khofesh/d28745ee4cdff564066b2638878587f9 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.7.6+commit.7338295f.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.4; | |
contract MappingsStructExample { | |
struct Payment { | |
uint amount; | |
uint timestamp; | |
} | |
struct Balance { | |
uint totalBalance; | |
uint numPayments; | |
mapping(uint => Payment) payments; | |
} | |
mapping(address => Balance) public balanceReceived; | |
function getBalance() public view returns(uint) { | |
return address(this).balance; | |
} | |
function sendMoney() public payable { | |
balanceReceived[msg.sender].totalBalance += msg.value; | |
Payment memory payment = Payment(msg.value, block.timestamp); | |
balanceReceived[msg.sender].payments[balanceReceived[msg.sender].numPayments] = payment; | |
balanceReceived[msg.sender].numPayments++; | |
} | |
function withdrawMoney(address payable _to, uint _amount) public { | |
require(_amount <= balanceReceived[msg.sender].totalBalance, "not enough fund"); | |
balanceReceived[msg.sender].totalBalance -= _amount; | |
_to.transfer(_amount); | |
} | |
function withdrawAllMoney(address payable _to) public { | |
uint balanceToSend = balanceReceived[msg.sender].totalBalance; | |
balanceReceived[msg.sender].totalBalance = 0; | |
_to.transfer(balanceToSend); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment