-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDailyTrack.sol
More file actions
71 lines (63 loc) · 2.16 KB
/
Copy pathDailyTrack.sol
File metadata and controls
71 lines (63 loc) · 2.16 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
66
67
68
69
70
71
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract DailyTrack {
IERC20 public rewardToken;
struct User {
uint256 lastLogin;
uint256 streak;
}
mapping(address => User) public users;
uint256 public dailyReward;
address public owner;
event Login(address indexed user, uint256 streak, uint256 reward);
modifier onlyOwner() {
require(
msg.sender == owner,
"Only the contract owner can perform this action"
);
_;
}
constructor(address _rewardToken, uint256 _dailyReward) {
owner = msg.sender;
rewardToken = IERC20(_rewardToken);
dailyReward = _dailyReward;
}
function dailyLogin() public {
User storage user = users[msg.sender];
uint256 currentTime = block.timestamp;
if (user.lastLogin > 0 && (currentTime - user.lastLogin) < 1 days) {
revert("You can login only once per day");
}
if (
user.lastLogin > 0 &&
(currentTime - user.lastLogin) >= 1 days &&
(currentTime - user.lastLogin) < 2 days
) {
user.streak += 1;
} else if (
user.lastLogin > 0 && (currentTime - user.lastLogin) > 2 days
) {
user.streak = 1;
} else {
user.streak = 1;
}
user.lastLogin = currentTime;
require(
rewardToken.balanceOf(address(this)) >= dailyReward,
"Not enough tokens in Contract"
);
rewardToken.transfer(msg.sender, dailyReward);
emit Login(msg.sender, user.streak, dailyReward);
}
function setDailyReward(uint256 _dailyReward) public onlyOwner {
dailyReward = _dailyReward;
}
function withdrawToken(uint256 amount) public onlyOwner {
rewardToken.transfer(owner, amount);
}
function depositTokens(uint256 amount) public {
require(amount > 0, "Amount must be greater than zero");
rewardToken.transferFrom(msg.sender, address(this), amount);
}
}