Skip to content

Commit d14e4dd

Browse files
committed
feat(contract): added new contract
1 parent 1e40a42 commit d14e4dd

5 files changed

+200
-148
lines changed

contract/ILicenka.sol

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
pragma solidity >=0.8.0 <0.9.0;
22
// SPDX-License-Identifier: MIT License
33

4-
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5-
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
6-
74
interface ILicenka {
8-
function createLicence(address owner, string memory name, uint price, uint duration) external;
5+
function createLicense(address owner, string memory name, uint price, uint duration) external;
96

107
function subscribe(uint licenseId) external;
118

12-
function subscribeWeb2(address owner, uint hash, uint licenseId) external;
13-
149
function verifySubscription(address owner, uint licenseId) external view returns(bool);
15-
16-
function verifySubscriptionWeb2(address owner, uint hash, uint licenseId) external view returns(bool);
17-
18-
function getLicenses(address owner) external view returns(uint[] memory);
19-
20-
function getSubscriptions(address owner) external view returns(uint[] memory);
2110
}

contract/Licenka.sol

+17-111
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,21 @@ pragma solidity >=0.8.0 <0.9.0;
33

44
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
55
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
6-
import "./LicenkaPassword.sol";
76
import "./ILicenka.sol";
7+
import "./LicenkaLicense.sol";
8+
import "./LicenkaSubscription.sol";
89

910
/**
1011
* @dev Licenka is a contract that allows you to create a license.
1112
*/
12-
contract Licenka is ILicenka, licenkaPassword {
13+
contract Licenka is ILicenka, LicenkaLicense, LicenkaSubscription {
1314

1415
using SafeMath for uint;
1516

1617
IERC20 public token;
1718

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-
3119
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;
4421

4522
/**
4623
* @dev Check if the caller is the owner of the contract.
@@ -63,7 +40,7 @@ contract Licenka is ILicenka, licenkaPassword {
6340
*/
6441
function setFee(uint newFee) external isOwner(msg.sender) {
6542
require(0 <= newFee && newFee <= 100, "The fee must be between 0 and 100");
66-
percentageFee = newFee;
43+
fee = newFee;
6744
}
6845

6946
/**
@@ -74,7 +51,7 @@ contract Licenka is ILicenka, licenkaPassword {
7451
}
7552

7653
/**
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.
7855
*/
7956
function transferFunds(address dest, uint amount) external isOwner(msg.sender) {
8057
token.transfer(dest, amount);
@@ -83,98 +60,27 @@ contract Licenka is ILicenka, licenkaPassword {
8360
/**
8461
* @dev Create a new license.
8562
*/
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);
12565
}
12666

12767
/**
128-
* @dev Create a new license. With the api call
68+
* @dev Subscribe to a license.
12969
*/
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
13373

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);
14575

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));
15178
}
15279

15380
/**
154-
* @dev Verify if a wallet owns a license. With the api call
81+
* @dev Verify if a wallet is subscribed to a license.
15582
*/
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) {
15784
return _verifySubscription(owner, licenseId);
15885
}
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-
}
18086
}

contract/LicenkaLicense.sol

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
pragma solidity >=0.8.0 <0.9.0;
2+
// SPDX-License-Identifier: MIT License
3+
4+
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
5+
6+
/**
7+
* @dev Licenka is a contract that allows you to create a license.
8+
*/
9+
contract LicenkaLicense {
10+
11+
using SafeMath for uint;
12+
13+
struct License {
14+
address owner;
15+
string name;
16+
uint price;
17+
uint duration;
18+
}
19+
20+
event LicenseCreate(address indexed owner, string indexed name, uint price, uint duration);
21+
event LicensePause(uint indexed licenseId, bool indexed isPaused);
22+
23+
uint _nextLicenseId = 1;
24+
25+
mapping (uint => License) public licenses;
26+
mapping (uint => bool) _licensePaused;
27+
mapping (address => uint[]) _licenseOwner;
28+
29+
/**
30+
* @dev Modifier msg.sender musdt be the license owner.
31+
*/
32+
modifier licenseOwner(uint licenseId) {
33+
require(licenses[licenseId].owner == msg.sender, "You are not the license owner.");
34+
_;
35+
}
36+
37+
/**
38+
* @dev Modifier license must not be paused.
39+
*/
40+
modifier licenseUnpaused(uint licenseId) {
41+
require(_licensePaused[licenseId] == false, "The license is paused.");
42+
_;
43+
}
44+
45+
/**
46+
* @dev Modifier license must exist.
47+
*/
48+
modifier licenseExist(uint licenseId) {
49+
require(0 < licenseId && licenseId < _nextLicenseId, "The license doesn't exist.");
50+
_;
51+
}
52+
53+
/**
54+
* @dev Create a new license.
55+
*/
56+
function _mintLicense(address owner, string memory name, uint price, uint duration) internal {
57+
License memory license = License(owner, name, price, duration);
58+
licenses[_nextLicenseId] = license;
59+
_licenseOwner[owner].push(_nextLicenseId);
60+
_nextLicenseId++;
61+
62+
emit LicenseCreate(owner, name, price, duration);
63+
}
64+
65+
/**
66+
* @dev Set a license pause value to false or true.
67+
*/
68+
function setLicensePause(uint licenseId, bool isPaused) external licenseExist(licenseId) licenseOwner(licenseId) {
69+
_licensePaused[licenseId] = isPaused;
70+
71+
emit LicensePause(licenseId, isPaused);
72+
}
73+
74+
/**
75+
* @dev Get the licenses owned by a wallet.
76+
*/
77+
function getLicenses(address owner) external view returns(uint[] memory) {
78+
return _licenseOwner[owner];
79+
}
80+
}

contract/LicenkaPassword.sol

-25
This file was deleted.

0 commit comments

Comments
 (0)