-
Notifications
You must be signed in to change notification settings - Fork 1
/
Subscription-Payment.sol
288 lines (228 loc) · 10.1 KB
/
Subscription-Payment.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
//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 {
uint256 _paidTime;
uint256 _salePrice;
address _tokenForPayment;
uint numOfDay;
}
struct UserDataStruct {
address _userAddress;
uint256 _userId;
uint256 _beginTime;
uint256 _expiryTime;
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;
struct SaleStatsStruct {
mapping(address => uint256) _totalSalePerPaymentToken;
}
SaleStatsStruct private saleStats;
// beneficiary of payment
address private _newBeneficiary;
address private _beneficiary;
uint256 private _beneficiaryActiveTime;
address private _worker;
uint256 private subscriptionPrice = 24e18;//24 * 10^18 tokens per day
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);
}
//Declare an Event
event SetSubscriptionPrice(
address indexed caller,
uint 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];
}
function getSaleStatsPerToken(IERC20 tokenPaymentAddress) external view returns (uint256)
{
return saleStats._totalSalePerPaymentToken[address(tokenPaymentAddress)];
}
//Declare an Event
event ExtendedSubscription(
address indexed caller,
uint256 indexed newExpiryTime
);
function extendSubscription(uint numOfDay)
external
{
require(
block.timestamp > getActiveTime(),
"ActiveTime: You can't extend subscription before the active time."
);
if(_beneficiary != _newBeneficiary && block.timestamp > _beneficiaryActiveTime) _beneficiary = _newBeneficiary;
if(walletUserMap[msg.sender]._userId != 0){
uint subscriptionTimeLeft = (walletUserMap[msg.sender]._expiryTime - block.timestamp) / 86400;
if(subscriptionTimeLeft > 0){
require(numOfDay >= _minSubscriptionDay && (numOfDay + subscriptionTimeLeft) <= _maxSubscriptionDay, "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[msg.sender]._expiryTime < block.timestamp){
walletUserMap[msg.sender]._expiryTime = block.timestamp + numOfDay * 86400;
}else{
walletUserMap[msg.sender]._expiryTime = walletUserMap[msg.sender]._expiryTime + numOfDay * 86400;
}
paymentToken().safeTransferFrom(msg.sender, beneficiary(), numOfDay * getSubscriptionPrice());
saleStats._totalSalePerPaymentToken[address(_tokenForPayment)] += numOfDay * getSubscriptionPrice();
walletUserMap[msg.sender]._saleHistory.push(SaleStruct(
block.timestamp,
getSubscriptionPrice(),
address(paymentToken()),
numOfDay
));
}
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());
saleStats._totalSalePerPaymentToken[address(_tokenForPayment)] += numOfDay * getSubscriptionPrice();
_userIds.increment();
uint256 userId = _userIds.current();
walletUserMap[msg.sender]._userAddress = msg.sender;
walletUserMap[msg.sender]._userId = userId;
walletUserMap[msg.sender]._beginTime = block.timestamp;
walletUserMap[msg.sender]._expiryTime = block.timestamp + numOfDay * 86400;
walletUserMap[msg.sender]._saleHistory.push(SaleStruct(
block.timestamp,
getSubscriptionPrice(),
address(paymentToken()),
numOfDay
));
idUserMap[userId] = msg.sender;
}
//Emit an event
emit ExtendedSubscription(msg.sender, walletUserMap[msg.sender]._expiryTime);
}
}