-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW6day4.sol
More file actions
38 lines (26 loc) · 1.12 KB
/
W6day4.sol
File metadata and controls
38 lines (26 loc) · 1.12 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
type WalletId is address;
type DepositAmount is uint256;
type WithdrawAmount is uint256;
contract UserDefine{
mapping(WalletId => DepositAmount) public user;
error DepositSomething();
function deposit(WalletId depositor) public payable {
require(msg.value > 0, DepositSomething());
DepositAmount current = user[depositor];
uint256 updated = DepositAmount.unwrap(current) + msg.value;
user[depositor] = DepositAmount.wrap(updated);
}
function withdraw(WalletId depositor, WithdrawAmount amt) public {
uint256 withdrawValue = WithdrawAmount.unwrap(amt);
require(withdrawValue > 0, DepositSomething());
DepositAmount current = user[depositor];
uint256 currentValue = DepositAmount.unwrap(current);
require(currentValue >= withdrawValue, "Insufficient balance");
uint256 updated = currentValue - withdrawValue;
user[depositor] = DepositAmount.wrap(updated);
(bool success, ) = msg.sender.call{value: withdrawValue}("");
require(success, "Transfer failed");
}
}