-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1st version
More file actions
214 lines (181 loc) · 6.11 KB
/
Copy path1st version
File metadata and controls
214 lines (181 loc) · 6.11 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
pragma solidity 0.8.9;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount)
external
returns (bool);
function allowance(address owner, address spender)
external
view
returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
pragma solidity 0.8.9;
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
constructor() {
_transferOwnership(_msgSender());
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(
newOwner != address(0),
"Ownable: new owner is the zero address"
);
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
library SafeERC20 {
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
require(token.transfer(to, value), "SafeERC20 Transfer Failed");
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
require(token.transferFrom(from, to, value), "SafeERC20 TransferFrom failed");
}
}
pragma solidity 0.8.9;
contract TokenClaimV3 is Ownable {
using SafeERC20 for IERC20;
string public name;
IERC20 public ERC20Interface;
mapping(address => mapping(uint256 => uint256)) public unlockTime;
mapping(address => mapping(uint256 => mapping(address => uint256))) userTokenClaimPerPhase;
event RewardAdded(address indexed token, uint256 phase, uint256 amount);
event Claimed(address indexed user, address indexed token, uint256 amount);
modifier _hasAllowance(
address allower,
uint256 amount,
address token
) {
// Make sure the allower has provided the right allowance.
require(token != address(0), "Zero token address");
ERC20Interface = IERC20(token);
uint256 ourAllowance = ERC20Interface.allowance(allower, address(this));
require(amount <= ourAllowance, "Make sure to add enough allowance");
_;
}
function updateUserTokens(
address tokenAddress,
uint256 totalReward,
uint256 phaseNo,
uint256 release,
address[] memory users,
uint256[] memory tokenValues
)
external
_hasAllowance(msg.sender, totalReward, tokenAddress)
onlyOwner
returns (bool)
{
require(totalReward > 0 && users.length > 0, "Invalid data");
require(users.length == tokenValues.length, "Invalid user data");
require(release > block.timestamp, "Invalid release time");
if (unlockTime[tokenAddress][phaseNo] > 0) {
require(
block.timestamp < unlockTime[tokenAddress][phaseNo],
"Phase already started"
);
}
unlockTime[tokenAddress][phaseNo] = release;
uint256 rewardCheck = totalReward;
for (uint256 i = 0; i < users.length; i++) {
userTokenClaimPerPhase[tokenAddress][phaseNo][users[i]] =
userTokenClaimPerPhase[tokenAddress][phaseNo][users[i]] +
tokenValues[i];
unchecked {
rewardCheck = rewardCheck - tokenValues[i];
}
}
require(rewardCheck == 0, "Incorrect reward values");
ERC20Interface = IERC20(tokenAddress);
ERC20Interface.safeTransferFrom(msg.sender, address(this), totalReward);
emit RewardAdded(tokenAddress, phaseNo, totalReward);
return true;
}
function claimMultiple(address tokenAddress, uint256[] calldata phaseNo) external returns(bool) {
for(uint i; i < phaseNo.length; i++) {
require(claim(tokenAddress, phaseNo[i]));
}
return true;
}
function getUserPhaseTokenClaim(
address tokenAddress,
uint256 phaseNo,
address user
) external view returns (uint256) {
return userTokenClaimPerPhase[tokenAddress][phaseNo][user];
}
function claim(address tokenAddress, uint256 phaseNo)
public
returns (bool)
{
require(
unlockTime[tokenAddress][phaseNo] < block.timestamp,
"Wait for unlock time"
);
uint256 amount = userTokenClaimPerPhase[tokenAddress][phaseNo][
msg.sender
];
require(
amount > 0,
"No claimable tokens available for user in this phase"
);
delete userTokenClaimPerPhase[tokenAddress][phaseNo][msg.sender];
ERC20Interface = IERC20(tokenAddress);
require(
ERC20Interface.balanceOf(address(this)) >= amount,
"No tokens available in the contract"
);
ERC20Interface.safeTransfer(msg.sender, amount);
emit Claimed(msg.sender, tokenAddress, amount);
return true;
}
}