-
Notifications
You must be signed in to change notification settings - Fork 1
/
Subscription-Payment-V2-Updated.sol
359 lines (285 loc) · 12.4 KB
/
Subscription-Payment-V2-Updated.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//Contract based on [https://docs.openzeppelin.com/contracts/3.x/erc721](https://docs.openzeppelin.com/contracts/3.x/erc721)
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract SubscriptionPayment is Ownable {
using SafeERC20 for IERC20;
using Counters for Counters.Counter;
Counters.Counter private _userIds;
uint private constant _minSubscriptionDay = 1;
uint private _maxSubscriptionDay = 30;
struct SaleStruct {
address _payer;
uint256 _paidTime;
uint256 _salePrice;
address _tokenForPayment;
uint numOfDay;
string _profileURI;
}
struct UserDataStruct {
address _userAddress;
uint256 _userId;
uint256 _beginTime;
uint256 _expiryTime;
string _profileURI;
SaleStruct[] _saleHistory;
}
mapping(address => UserDataStruct) private walletUserMap;
mapping(uint256 => address) private idUserMap;
uint256 private _activeTime;
// ERC20 basic payment token contracts being held
IERC20 private _tokenForPayment;
// beneficiary of payment
address private _newBeneficiary;
address private _beneficiary;
uint256 private _beneficiaryActiveTime;
address private _worker;
// forever locker address
address private _foreverLocker;
uint256 private subscriptionPrice = 24e18;//24 tokens per day
uint256 private burnRate = 1e18;//1% setable
constructor (IERC20 token_) {
_tokenForPayment = token_;
_activeTime = block.timestamp + 24 hours;
_beneficiary = address(this);
_newBeneficiary = address(this);
_beneficiaryActiveTime = block.timestamp;
_worker = msg.sender;
}
function getWorker() external onlyOwner view returns (address) {
return _worker;
}
//Declare an Event
event UpdateWorker(
address indexed caller,
address indexed worker
);
function updateWorker(address newWorker_) external onlyOwner {
require(
newWorker_ != address(0),
"New worker is the zero address"
);
_worker = newWorker_;
emit UpdateWorker(msg.sender, newWorker_);
}
function getActiveTime() public view returns (uint256)
{
return _activeTime;
}
function getMaxSubscriptionTime() external view returns (uint256)
{
return _maxSubscriptionDay;
}
//Declare an Event
event SetMaxSubscriptionDay(
address indexed caller,
uint indexed maxSubscriptionDay
);
function setMaxSubscriptionDay(uint maxSubscriptionDay_) external onlyOwner {
require(maxSubscriptionDay_ >= _minSubscriptionDay, "Subscription: Can't be less than min subscription time!");
_maxSubscriptionDay = maxSubscriptionDay_;
emit SetMaxSubscriptionDay(msg.sender, _maxSubscriptionDay);
}
/**
* @return the beneficiary of the tokens.
*/
function beneficiary() public view virtual returns (address) {
return _beneficiary;
}
//Declare an Event
event SetBeneficiary(
address indexed caller,
address indexed beneficiary_,
uint256 indexed activeTime
);
function setBeneficiary( address beneficiary_) external onlyOwner {
require(
beneficiary_ != address(0),
"New beneficiary is the zero address"
);
_newBeneficiary = beneficiary_;
_beneficiaryActiveTime = block.timestamp + 24 hours;
emit SetBeneficiary(msg.sender, beneficiary_, _beneficiaryActiveTime);
}
function getForeverLocker() public view virtual returns (address) {
return _foreverLocker;
}
//Declare an Event
event SetForeverLocker(
address indexed caller,
address indexed locker_,
uint256 indexed activeTime
);
function setForeverLocker( address locker_) external onlyOwner {
require(
locker_ != address(0),
"Zero Address Issue"
);
_foreverLocker = locker_;
_beneficiaryActiveTime = block.timestamp + 24 hours;
emit SetForeverLocker(msg.sender, locker_, _beneficiaryActiveTime);
}
//Declare an Event
event SetSubscriptionPrice(
address indexed caller,
uint256 indexed newSubscriptionPrice
);
function setSubscriptionPrice(uint256 newSubscriptionPrice) external onlyOwner {
subscriptionPrice = newSubscriptionPrice;
emit SetSubscriptionPrice(msg.sender, newSubscriptionPrice);
}
function getSubscriptionPrice() public view virtual returns (uint256) {
return subscriptionPrice;
}
function getCurrentUserId() external view virtual returns (uint256) {
return _userIds.current();
}
/**
* @return the token being held.
*/
function paymentToken() public view virtual returns (IERC20) {
return _tokenForPayment;
}
//Declare an Event
event SetTokenForPayment(
address indexed caller,
IERC20 indexed token_
);
function setTokenForPayment(IERC20 token_) external onlyOwner {
_tokenForPayment = token_;
emit SetTokenForPayment(msg.sender, token_);
}
function balanceTokenForPayment() external view virtual returns (uint256) {
return paymentToken().balanceOf(address(this));
}
//Declare an Event
event WithdrawTokenForPayment(
address indexed caller,
address indexed beneficiary_,
uint256 indexed balance_
);
function withdrawTokenForPayment() external {
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
require(msg.sender == _worker || msg.sender == owner(), "Invalid caller!");
uint256 currentBalance = paymentToken().balanceOf(address(this));
//paymentToken().safeTransferFrom(address(this), beneficiary(), currentBalance);
paymentToken().safeTransfer(beneficiary(), currentBalance);
emit WithdrawTokenForPayment(address(this), beneficiary(), currentBalance);
}
function getUserExpiryTimeById(uint256 userId) external view virtual returns (uint256) {
return walletUserMap[idUserMap[userId]]._expiryTime;
}
function getUserExpiryTimeByAddress(address walletAddress) external view virtual returns (uint256) {
return walletUserMap[walletAddress]._expiryTime;
}
function getUserAddressById(uint256 userId) external view virtual returns (address) {
return idUserMap[userId];
}
function getUserIdByAddress(address walletAddress) external view virtual returns (uint256) {
return walletUserMap[walletAddress]._userId;
}
function getUserDataById(uint256 userId) external view virtual returns (UserDataStruct memory) {
return walletUserMap[idUserMap[userId]];
}
function getUserDataByAddress(address walletAddress) external view virtual returns (UserDataStruct memory) {
return walletUserMap[walletAddress];
}
//Declare an Event
event ExtendedSubscription(
address indexed caller,
uint256 indexed newExpiryTime
);
function extendSubscription(uint numOfDay, address walletAddress)
external
{
require(
block.timestamp > getActiveTime(),
"ActiveTime: You can't extend subscription before the active time."
);
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
if(walletAddress==address(0)){
walletAddress = msg.sender;
}// check if the address is not set)
//if(walletUserMap[msg.sender]._userId != 0){
if(walletUserMap[walletAddress]._userId != 0){
//uint subscriptionTimeLeft = (walletUserMap[msg.sender]._expiryTime - block.timestamp) / 86400;
uint subscriptionTimeLeft = 0;
if(walletUserMap[walletAddress]._expiryTime > block.timestamp) subscriptionTimeLeft = walletUserMap[walletAddress]._expiryTime - block.timestamp;
if(subscriptionTimeLeft > 0){
require(numOfDay >= _minSubscriptionDay && (numOfDay * 86400 + subscriptionTimeLeft) <= _maxSubscriptionDay * 86400, "Subscription: Can't be out of min and max subscription time!");
}
require(paymentToken().balanceOf(msg.sender) >= numOfDay * getSubscriptionPrice(), "Can't pay subscription fee!");
if(walletUserMap[walletAddress]._expiryTime < block.timestamp){
walletUserMap[walletAddress]._expiryTime = block.timestamp + numOfDay * 86400;
}else{
walletUserMap[walletAddress]._expiryTime = walletUserMap[walletAddress]._expiryTime + numOfDay * 86400;
}
//paymentToken().safeTransferFrom(msg.sender, beneficiary(), numOfDay * getSubscriptionPrice());
paymentToken().safeTransferFrom(msg.sender, beneficiary(), (1e20 - burnRate ) * numOfDay * getSubscriptionPrice() / 1e20);
//paymentToken().safeTransferFrom(msg.sender, address(0), burnRate * numOfDay * getSubscriptionPrice()/ 1e20);
paymentToken().safeTransferFrom(msg.sender, _foreverLocker, burnRate * numOfDay * getSubscriptionPrice()/ 1e20);
walletUserMap[walletAddress]._saleHistory.push(SaleStruct(
msg.sender,
block.timestamp,
getSubscriptionPrice(),
address(paymentToken()),
numOfDay,
walletUserMap[walletAddress]._profileURI
));
}
else{
require(numOfDay >= _minSubscriptionDay && numOfDay <= _maxSubscriptionDay, "Subscription: Can't be out of min and max subscription time!");
require(paymentToken().balanceOf(msg.sender) >= numOfDay * getSubscriptionPrice(), "Can't pay subscription fee!");
//paymentToken().safeTransferFrom(msg.sender, beneficiary(), numOfDay * getSubscriptionPrice());
paymentToken().safeTransferFrom(msg.sender, beneficiary(), (1e20 - burnRate ) * numOfDay * getSubscriptionPrice() / 1e20);
//paymentToken().safeTransferFrom(msg.sender, address(0), burnRate * numOfDay * getSubscriptionPrice()/ 1e20);
paymentToken().safeTransferFrom(msg.sender, _foreverLocker, burnRate * numOfDay * getSubscriptionPrice()/ 1e20);
_userIds.increment();
uint256 userId = _userIds.current();
walletUserMap[walletAddress]._userAddress = walletAddress;
walletUserMap[walletAddress]._userId = userId;
walletUserMap[walletAddress]._beginTime = block.timestamp;
walletUserMap[walletAddress]._expiryTime = block.timestamp + numOfDay * 86400;
walletUserMap[walletAddress]._saleHistory.push(SaleStruct(
msg.sender,
block.timestamp,
getSubscriptionPrice(),
address(paymentToken()),
numOfDay,
walletUserMap[walletAddress]._profileURI
));
idUserMap[userId] = walletAddress;
}
//Emit an event
emit ExtendedSubscription(walletAddress, walletUserMap[walletAddress]._expiryTime);
}
//Declare an Event
event SetProfileURI(
address indexed caller,
string indexed profileURI_
);
function setProfileURI(string memory profileURI_) external
{
//require(bytes(profileURI_).length > 0, "profileURI: Can't be blank!");
walletUserMap[msg.sender]._profileURI = profileURI_;
emit SetProfileURI(msg.sender, profileURI_);
}
function getProfileURI() external view returns (string memory)
{
return walletUserMap[msg.sender]._profileURI;
}
//Declare an Event
event SetBurnRate(
address indexed caller,
uint256 indexed burnRate
);
function setBurnRate(uint256 burnRate_) external onlyOwner {
burnRate = burnRate_;
emit SetBurnRate(msg.sender, burnRate_);
}
function getBurnRate() public view virtual returns (uint256) {
return burnRate;
}
}