-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserBalanceModifiers.sol
89 lines (70 loc) · 2.63 KB
/
UserBalanceModifiers.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
// Custom errors
error NotaDepositorAlready();
error AmountTooSmall();
error InsufficientBalance();
contract UserBalanceModifier {
address payable public owner;
uint private constant FEE = 10;
struct UserDetail {
string userName;
uint256 userAge;
}
// Mapping of address to Struct
mapping(address => UserDetail) public usersDetail;
//mapping of address to uint256
mapping(address => uint256) public balances;
constructor() {
owner = payable(msg.sender);
}
// functions that saves the amount a user is depositing into a mapping
function deposit(uint256 amount) public {
balances[msg.sender] += amount;
}
// function that searches for user balance inside the mapping and returns the balance of who calls the contract.
function checkBalance() public view returns (uint) {
return balances[msg.sender];
}
// function that saves the details of the user calling the smart contract into defined struct
function setUserDetails(string calldata name, uint256 age) public {
usersDetail[msg.sender] = UserDetail({userName: name, userAge: age});
}
// function retrieves and returns the details saved for the user calling the contract.
function getUserDetail() public view returns (string memory, uint256) {
UserDetail memory userDetail = usersDetail[msg.sender];
return (userDetail.userName, userDetail.userAge);
}
modifier onlyOwner() {
require(msg.sender == owner, "Only Owner Allowed");
_;
}
// function that allows only the owner to withdraw funds
function withdrawFund() public payable onlyOwner {
// get the amount of Ether stored in this contract
uint amount = address(this).balance;
require(balances[msg.sender] >= amount, "Insufficient balance");
// Owner can receive Ether since the address of owner is payable
(bool success, ) = owner.call{value: amount}("");
require(success, "Funds Withdrawal Failed");
balances[msg.sender] -= amount;
}
modifier onlyDepositor() {
if (balances[msg.sender] <= 0) {
revert NotaDepositorAlready();
}
_;
}
modifier validateAmount(uint256 _amount) {
if (_amount <= FEE) {
revert AmountTooSmall();
}
_;
}
// function that allows only users that have deposited, to increase their balance on the mapping
function addFund(
uint256 _amount
) public onlyDepositor validateAmount(_amount) {
balances[msg.sender] += _amount;
}
}