-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifiers.sol
More file actions
65 lines (59 loc) · 2.57 KB
/
Copy pathModifiers.sol
File metadata and controls
65 lines (59 loc) · 2.57 KB
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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract Modifiers {
/*structs---------------------------------------------------------------------------------------------
The contract should now contain the following:
setUserDetails(string calldata name, uint256 age) this function accepts 2 arguments that represent the details
of the user calling the smart contract and it saves them into a defined struct,
getUserDetail() this function retrieves and returns the details saved for the user calling the contract.*/
struct User{
string name;
uint256 age;
}
mapping(address => User) public users;
function setUserDetails(string calldata _name, uint256 _age) public {
users[msg.sender] = User(_name,_age);
}
function getUserDetail() public view returns(User memory _userInfo){
return users[msg.sender];
}
/*modifiers---------------------------------------------------------------------------------------------
Create a deposit function that allows anybody to send funds. Store the user and the amount in a mapping as the previous task.
Add a withdraw function and create a modifier that only allows the owner of the contract to withdraw the funds.
Add an addFund function and create a modifier that only allows users that have deposited using the deposit function, to increase their balance on the mapping.
The function should accept the amount to be added and update the mapping to have the new balance
Hint: if their balance is zero on the mapping, it should revert
Hint: theMapping[userId] = theMapping[userId] + _amount;
Create a modifier that accepts a value(uint256 _amount):Create a private constant variable called Fee
In the modifier check if the value(_amount) it accepts is less than the Fee, revert with a custom error AmountToSmall()
Add it to the addFund function
Hint: addFund(uint256 _amount)*/
uint fee = 0.05 ether;
address public owner;
mapping(address => uint256) public balances;
modifier onlyOwner(){
require(msg.sender == owner,"Not the owner!");
_;
}
modifier onlyUsers(){
require(balances[msg.sender] > 0,"You don't have a balance here!");
_;
}
modifier amountTooSmall(uint _amount){
require(_amount > fee,"Not enough money to pay for the fee!");
_;
}
function setOwner(address _owner) public{
owner = _owner;
}
function deposit(uint256 _amount) public{
balances[msg.sender] = _amount;
}
function addFund(uint256 _amount) public onlyUsers amountTooSmall(_amount){
balances[msg.sender] += _amount;
}
function withdrawFunds() public view onlyOwner{
uint _amount = address(this).balance;
//sent balance to owner
}
}