-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtls.sol
36 lines (26 loc) · 1.03 KB
/
tls.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract TimeLockedSavings {
struct Deposit {
uint amount;
uint unlockTime;
}
mapping(address => Deposit) private deposits;
uint256 public penaltyPercentage = 60;
function deposit(uint lockTime) external payable {
require(msg.value > 0, "Must send Ether");
require(deposits[msg.sender].amount == 0, "Existing deposit found");
deposits[msg.sender] = Deposit(msg.value, block.timestamp + lockTime);
}
function withdraw() external {
Deposit storage userDeposit = deposits[msg.sender];
require(userDeposit.amount > 0, "No deposit found");
uint256 amountToWithdraw = userDeposit.amount;
// Apply penalty if withdrawn early
if (block.timestamp < userDeposit.unlockTime) {
amountToWithdraw -= (amountToWithdraw * penaltyPercentage) / 100;
}
userDeposit.amount = 0; // Reset deposit
payable(msg.sender).transfer(amountToWithdraw);
}
}