@@ -3,44 +3,21 @@ pragma solidity >=0.8.0 <0.9.0;
3
3
4
4
import "@openzeppelin/contracts/token/ERC20/IERC20.sol " ;
5
5
import "@openzeppelin/contracts/utils/math/SafeMath.sol " ;
6
- import "./LicenkaPassword.sol " ;
7
6
import "./ILicenka.sol " ;
7
+ import "./LicenkaLicense.sol " ;
8
+ import "./LicenkaSubscription.sol " ;
8
9
9
10
/**
10
11
* @dev Licenka is a contract that allows you to create a license.
11
12
*/
12
- contract Licenka is ILicenka , licenkaPassword {
13
+ contract Licenka is ILicenka , LicenkaLicense , LicenkaSubscription {
13
14
14
15
using SafeMath for uint ;
15
16
16
17
IERC20 public token;
17
18
18
- struct License {
19
- string name;
20
- address owner;
21
- uint price;
22
- uint duration;
23
- }
24
-
25
- struct LicenseSubscribe {
26
- uint licenseId;
27
- uint validTime;
28
- bool isInfinite;
29
- }
30
-
31
19
address _owner;
32
- uint _nextLicenseId = 1 ;
33
- uint _nextLicenseSubscriptionId = 1 ;
34
- uint percentageFee = 3 ;
35
-
36
- //Licences
37
- mapping (uint => License) public licenses;
38
- mapping (address => uint []) _licenseOwner;
39
-
40
- //Subscriptions
41
- mapping (uint => LicenseSubscribe) public subscriptions;
42
- mapping (address => uint []) _subscriptionOwner;
43
- mapping (address => mapping (uint => uint )) _subcriptionIndex;
20
+ uint public fee = 3 ;
44
21
45
22
/**
46
23
* @dev Check if the caller is the owner of the contract.
@@ -63,7 +40,7 @@ contract Licenka is ILicenka, licenkaPassword {
63
40
*/
64
41
function setFee (uint newFee ) external isOwner (msg .sender ) {
65
42
require (0 <= newFee && newFee <= 100 , "The fee must be between 0 and 100 " );
66
- percentageFee = newFee;
43
+ fee = newFee;
67
44
}
68
45
69
46
/**
@@ -74,7 +51,7 @@ contract Licenka is ILicenka, licenkaPassword {
74
51
}
75
52
76
53
/**
77
- * @dev Transfert funds from the wallet to a given address. Only callable by the owner.
54
+ * @dev Transfert funds from the contract to a given address. Only callable by the owner.
78
55
*/
79
56
function transferFunds (address dest , uint amount ) external isOwner (msg .sender ) {
80
57
token.transfer (dest, amount);
@@ -83,98 +60,27 @@ contract Licenka is ILicenka, licenkaPassword {
83
60
/**
84
61
* @dev Create a new license.
85
62
*/
86
- function createLicence (address owner , string memory name , uint price , uint duration ) external {
87
- License memory license = License (name, owner, price, duration);
88
- licenses[_nextLicenseId] = license;
89
- _licenseOwner[owner].push (_nextLicenseId);
90
- _nextLicenseId++ ;
91
- }
92
-
93
-
94
- function _subscribe (address owner , uint licenseId ) internal {
95
- License memory license = licenses[licenseId];
96
- require (license.owner != address (0x0 ), "Wrong id number " ); //Test if license id is valid
97
- require (token.allowance (owner, address (this )) >= license.price, "Didn't approve enough fund for the license " ); //Test if the user has sufficient fund
98
-
99
- uint index = _subcriptionIndex[owner][licenseId];
100
- if (index == 0 ) { //if First time subscribing, add the licence to array
101
- subscriptions[_nextLicenseSubscriptionId] = LicenseSubscribe (licenseId, 0 , false );
102
- _subscriptionOwner[owner].push (_nextLicenseSubscriptionId);
103
- _subcriptionIndex[owner][licenseId] = _nextLicenseSubscriptionId;
104
- index = _nextLicenseSubscriptionId;
105
- _nextLicenseSubscriptionId++ ;
106
- }
107
-
108
- require (subscriptions[index].isInfinite == false , "Already subcribed " );
109
- LicenseSubscribe memory subscription_ = subscriptions[index];
110
- if (license.duration == 0 )
111
- subscriptions[index].isInfinite = true ;
112
- else {
113
- uint rootTimestamp = subscription_.validTime < block .timestamp ? block .timestamp : subscription_.validTime;
114
- subscriptions[index].validTime = rootTimestamp + license.duration;
115
- }
116
- token.transferFrom (owner, address (this ), license.price);
117
- token.transfer (license.owner, license.price.mul (100 - percentageFee).div (100 ));
118
- }
119
-
120
- /**
121
- * @dev Subscribe to a license. With your own wallet
122
- */
123
- function subscribe (uint licenseId ) external {
124
- _subscribe (msg .sender , licenseId);
63
+ function createLicense (address owner , string memory name , uint price , uint duration ) override external {
64
+ _mintLicense (owner, name, price, duration);
125
65
}
126
66
127
67
/**
128
- * @dev Create a new license. With the api call
68
+ * @dev Subscribe to a license.
129
69
*/
130
- function subscribeWeb2 ( address owner , uint hash , uint licenseId ) external isPasswordSet (owner) isPasswordMatch (owner, hash ) {
131
- _subscribe (owner, licenseId) ;
132
- }
70
+ function subscribe ( uint licenseId ) override external licenseExist (licenseId) licenseUnpaused (licenseId ) {
71
+ License memory license = licenses[ licenseId] ;
72
+ require (token. allowance ( msg . sender , address ( this )) >= license.price, " Didn't approve enough fund for the license " ); //Test if the user has sufficient fund
133
73
134
- function _verifySubscription (address owner , uint licenseId ) internal view returns (bool ) {
135
- if (_subcriptionIndex[owner][licenseId] == 0 )
136
- return false ;
137
- uint index = _subcriptionIndex[owner][licenseId];
138
- LicenseSubscribe memory subscription_ = subscriptions[index];
139
- if (subscription_.isInfinite == true )
140
- return true ;
141
- if (subscription_.validTime > block .timestamp )
142
- return true ;
143
- return false ;
144
- }
74
+ _subscribe (msg .sender , licenseId, license.duration);
145
75
146
- /**
147
- * @dev Verify if a wallet owns a license.
148
- */
149
- function verifySubscription (address owner , uint licenseId ) external view returns (bool ) {
150
- return _verifySubscription (owner, licenseId);
76
+ token.transferFrom (msg .sender , address (this ), license.price);
77
+ token.transfer (license.owner, license.price.mul (100 - fee).div (100 ));
151
78
}
152
79
153
80
/**
154
- * @dev Verify if a wallet owns a license. With the api call
81
+ * @dev Verify if a wallet is subscribed to a license.
155
82
*/
156
- function verifySubscriptionWeb2 (address owner , uint hash , uint licenseId ) external view isPasswordSet (owner) isPasswordMatch (owner, hash ) returns (bool ) {
83
+ function verifySubscription (address owner , uint licenseId ) override external view licenseExist (licenseId ) returns (bool ) {
157
84
return _verifySubscription (owner, licenseId);
158
85
}
159
-
160
- /**
161
- * @dev Get all owned licenses by a wallet.
162
- */
163
- function getLicenses (address owner ) external view returns (uint [] memory ) {
164
- return _licenseOwner[owner];
165
- }
166
-
167
- /**
168
- * @dev Get all owned subscriptions by a wallet.
169
- */
170
- function getSubscriptions (address owner ) external view returns (uint [] memory ) {
171
- return _subscriptionOwner[owner];
172
- }
173
-
174
- /**
175
- * @dev Get a wallet subscription for a given license.
176
- */
177
- function getSubscriptionIdForLicense (address owner , uint licenseId ) external view returns (uint ) {
178
- return _subcriptionIndex[owner][licenseId];
179
- }
180
86
}
0 commit comments