-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyMappingStruct.sol
44 lines (35 loc) · 1.39 KB
/
MyMappingStruct.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
contract MyMappingStruct {
struct Transaction {
uint amount;
uint timestamp;
}
struct Balance {
uint totalBalance;
uint numDeposits;
mapping(uint => Transaction) deposits;
uint numWithdrawals;
mapping(uint => Transaction) withdrawals;
}
mapping(address => Balance) public balanceReceived;
function getBalance(address _addr) public view returns(uint) {
return balanceReceived[_addr].totalBalance;
}
function depositMoney() public payable {
balanceReceived[msg.sender].totalBalance += msg.value;
Transaction memory deposit = Transaction(msg.value, block.timestamp);
balanceReceived[msg.sender].deposits[balanceReceived[msg.sender].numDeposits] = deposit;
balanceReceived[msg.sender].numDeposits++;
}
function withdrawMoney(address payable _to, uint _amount) public payable {
// reduce balance by the amount of withdraw
balanceReceived[msg.sender].totalBalance -= _amount;
// record new withdrawal
Transaction memory withdrawal = Transaction(msg.value, block.timestamp);
balanceReceived[msg.sender].withdrawals[balanceReceived[msg.sender].numWithdrawals] = withdrawal;
balanceReceived[msg.sender].numWithdrawals++;
// transfer amount
_to.transfer(_amount);
}
}