forked from cryppadotta/dotta-license
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LicenseSale.sol
270 lines (237 loc) · 7.49 KB
/
LicenseSale.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
pragma solidity ^0.4.19;
import "./LicenseOwnership.sol";
import "./Affiliate/AffiliateProgram.sol";
contract LicenseSale is LicenseOwnership {
AffiliateProgram public affiliateProgram;
/**
* @notice We credit affiliates for renewals that occur within this time of
* original purchase. E.g. If this is set to 1 year, and someone subscribes to
* a monthly plan, the affiliate will receive credits for that whole year, as
* the user renews their plan
*/
uint256 public renewalsCreditAffiliatesFor = 1 years;
/** internal **/
function _performPurchase(
uint256 _productId,
uint256 _numCycles,
address _assignee,
uint256 _attributes,
address _affiliate)
internal returns (uint)
{
_purchaseOneUnitInStock(_productId);
return _createLicense(
_productId,
_numCycles,
_assignee,
_attributes,
_affiliate
);
}
function _createLicense(
uint256 _productId,
uint256 _numCycles,
address _assignee,
uint256 _attributes,
address _affiliate)
internal
returns (uint)
{
// You cannot create a subscription license with zero cycles
if(isSubscriptionProduct(_productId)) {
require(_numCycles != 0);
}
// Non-subscription products have an expiration time of 0, meaning "no-expiration"
uint256 expirationTime = isSubscriptionProduct(_productId) ?
now.add(intervalOf(_productId).mul(_numCycles)) : // solium-disable-line security/no-block-members
0;
License memory _license = License({
productId: _productId,
attributes: _attributes,
issuedTime: now, // solium-disable-line security/no-block-members
expirationTime: expirationTime,
affiliate: _affiliate
});
uint256 newLicenseId = licenses.push(_license) - 1; // solium-disable-line zeppelin/no-arithmetic-operations
LicenseIssued(
_assignee,
msg.sender,
newLicenseId,
_license.productId,
_license.attributes,
_license.issuedTime,
_license.expirationTime,
_license.affiliate);
_mint(_assignee, newLicenseId);
return newLicenseId;
}
function _handleAffiliate(
address _affiliate,
uint256 _productId,
uint256 _licenseId,
uint256 _purchaseAmount)
internal
{
uint256 affiliateCut = affiliateProgram.cutFor(
_affiliate,
_productId,
_licenseId,
_purchaseAmount);
if(affiliateCut > 0) {
require(affiliateCut < _purchaseAmount);
affiliateProgram.credit.value(affiliateCut)(_affiliate, _licenseId);
}
}
function _performRenewal(uint256 _tokenId, uint256 _numCycles) internal {
// You cannot renew a non-expiring license
// ... but in what scenario can this happen?
// require(licenses[_tokenId].expirationTime != 0);
uint256 productId = licenseProductId(_tokenId);
// If our expiration is in the future, renewing adds time to that future expiration
// If our expiration has passed already, then we use `now` as the base.
uint256 renewalBaseTime = Math.max256(now, licenses[_tokenId].expirationTime);
// We assume that the payment has been validated outside of this function
uint256 newExpirationTime = renewalBaseTime.add(intervalOf(productId).mul(_numCycles));
licenses[_tokenId].expirationTime = newExpirationTime;
LicenseRenewal(
ownerOf(_tokenId),
msg.sender,
_tokenId,
productId,
newExpirationTime
);
}
function _affiliateProgramIsActive() internal view returns (bool) {
return
affiliateProgram != address(0) &&
affiliateProgram.storeAddress() == address(this) &&
!affiliateProgram.paused();
}
/** executives **/
function setAffiliateProgramAddress(address _address) external onlyCEO {
AffiliateProgram candidateContract = AffiliateProgram(_address);
require(candidateContract.isAffiliateProgram());
affiliateProgram = candidateContract;
}
function setRenewalsCreditAffiliatesFor(uint256 _newTime) external onlyCEO {
renewalsCreditAffiliatesFor = _newTime;
}
function createPromotionalPurchase(
uint256 _productId,
uint256 _numCycles,
address _assignee,
uint256 _attributes
)
external
onlyCEOOrCOO
whenNotPaused
returns (uint256)
{
return _performPurchase(
_productId,
_numCycles,
_assignee,
_attributes,
address(0));
}
function createPromotionalRenewal(
uint256 _tokenId,
uint256 _numCycles
)
external
onlyCEOOrCOO
whenNotPaused
{
uint256 productId = licenseProductId(_tokenId);
_requireRenewableProduct(productId);
return _performRenewal(_tokenId, _numCycles);
}
/** anyone **/
/**
* @notice Makes a purchase of a product.
* @dev Requires that the value sent is exactly the price of the product
* @param _productId - the product to purchase
* @param _numCycles - the number of cycles being purchased. This number should be `1` for non-subscription products and the number of cycles for subscriptions.
* @param _assignee - the address to assign the purchase to (doesn't have to be msg.sender)
* @param _affiliate - the address to of the affiliate - use address(0) if none
*/
function purchase(
uint256 _productId,
uint256 _numCycles,
address _assignee,
address _affiliate
)
external
payable
whenNotPaused
returns (uint256)
{
require(_productId != 0);
require(_numCycles != 0);
require(_assignee != address(0));
// msg.value can be zero: free products are supported
// Don't bother dealing with excess payments. Ensure the price paid is
// accurate. No more, no less.
require(msg.value == costForProductCycles(_productId, _numCycles));
// Non-subscription products should send a _numCycle of 1 -- you can't buy a
// multiple quantity of a non-subscription product with this function
if(!isSubscriptionProduct(_productId)) {
require(_numCycles == 1);
}
// this can, of course, be gamed by malicious miners. But it's adequate for our application
// Feel free to add your own strategies for product attributes
// solium-disable-next-line security/no-block-members, zeppelin/no-arithmetic-operations
uint256 attributes = uint256(keccak256(block.blockhash(block.number-1)))^_productId^(uint256(_assignee));
uint256 licenseId = _performPurchase(
_productId,
_numCycles,
_assignee,
attributes,
_affiliate);
if(
priceOf(_productId) > 0 &&
_affiliate != address(0) &&
_affiliateProgramIsActive()
) {
_handleAffiliate(
_affiliate,
_productId,
licenseId,
msg.value);
}
return licenseId;
}
/**
* @notice Renews a subscription
*/
function renew(
uint256 _tokenId,
uint256 _numCycles
)
external
payable
whenNotPaused
{
require(_numCycles != 0);
require(ownerOf(_tokenId) != address(0));
uint256 productId = licenseProductId(_tokenId);
_requireRenewableProduct(productId);
// No excess payments. Ensure the price paid is exactly accurate. No more,
// no less.
uint256 renewalCost = costForProductCycles(productId, _numCycles);
require(msg.value == renewalCost);
_performRenewal(_tokenId, _numCycles);
if(
renewalCost > 0 &&
licenseAffiliate(_tokenId) != address(0) &&
_affiliateProgramIsActive() &&
licenseIssuedTime(_tokenId).add(renewalsCreditAffiliatesFor) > now
) {
_handleAffiliate(
licenseAffiliate(_tokenId),
productId,
_tokenId,
msg.value);
}
}
}