Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save glaksmono/46372f5f0596fa199e92e0129ef7544d to your computer and use it in GitHub Desktop.
Save glaksmono/46372f5f0596fa199e92e0129ef7544d 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.6.0+commit.26b70077.js&optimize=false&runs=200&gist=
pragma solidity ^0.5.13;
contract MappingStructExample {
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, now);
balanceReceived[msg.sender].payments[balanceReceived[msg.sender].numPayments] = payment;
balanceReceived[msg.sender].numPayments++;
}
function withdrawMoney(address payable _to, uint _amount) public {
require(balanceReceived[msg.sender].totalBalance >= _amount, "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