-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathReferral.sol
More file actions
135 lines (116 loc) · 4.5 KB
/
Referral.sol
File metadata and controls
135 lines (116 loc) · 4.5 KB
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
/* Copyright (C) 2020 PlotX.io
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ */
pragma solidity 0.5.7;
import "./PlotXToken.sol";
import "./external/openzeppelin-solidity/math/SafeMath.sol";
import "./external/openzeppelin-solidity/token/ERC20/ERC20.sol";
interface IbLOTToken {
function mint(address account, uint256 amount) external returns (bool);
}
contract Referral {
using SafeMath for uint256;
IbLOTToken bLotToken;
PlotXToken public plotToken;
address public owner;
address public signer;
uint public endDate;
uint public remainingbudget;
uint public referralAmount;
/// @dev mapping to maintain if user have claimed or not
mapping(address => bool) public userClaimed;
/**
* @dev modifier that allows only the owner to execute the function
*/
modifier onlyOwner() {
require(owner == msg.sender, "Not owner");
_;
}
/**
* @dev Constructor
* @param _plotToken The address of plot token
* @param _bLotToken The address of BLot token
* @param _endDate user can claim thier allocated amounts before this time.
* @param _budget total amount of BLot to be minted
*/
constructor(address _plotToken, address _bLotToken, address _signer, uint _endDate, uint _budget, uint _referralAmount) public
{
require(_plotToken != address(0),"Cannot be null address");
require(_bLotToken != address(0),"Cannot be null address");
require(_signer != address(0),"Cannot be null address");
require(_referralAmount != 0,"Cannot be zero referral amount");
require(_budget >= _referralAmount,"Cannot be less than referral amount");
require(_endDate > now,"End date cannot be past time");
plotToken = PlotXToken(_plotToken);
bLotToken = IbLOTToken(_bLotToken);
owner = msg.sender;
signer = _signer;
endDate = _endDate;
remainingbudget = _budget;
referralAmount = _referralAmount;
plotToken.approve(address(bLotToken), _budget);
}
/**
* @dev Allows owner to take back left over plot token after end date.
*/
function takeLeftOverPlot() external onlyOwner {
require(endDate <= now, "Callable only after end date");
plotToken.transfer(owner, plotToken.balanceOf(address(this)));
}
/**
* @dev Allows owner to end the referral program and take back left over plot token.
*/
function endReferralCampaign() external onlyOwner {
endDate = now;
plotToken.transfer(owner, plotToken.balanceOf(address(this)));
}
/**
* @dev Allows users to claim their allocated tokens.
* user should claim before end date.
*/
function claim(uint8 v, bytes32 r, bytes32 s) external {
require(endDate > now, "Callable only before end date");
require(!userClaimed[msg.sender], "Already claimed");
bytes memory hash = abi.encode(msg.sender);
require(isValidSignature(hash, v, r, s));
userClaimed[msg.sender] = true;
bLotToken.mint(msg.sender, referralAmount);
}
/**
* @dev Verifies signature.
* @param hash order hash
* @param v argument from vrs hash.
* @param r argument from vrs hash.
* @param s argument from vrs hash.
*/
function isValidSignature(bytes memory hash, uint8 v, bytes32 r, bytes32 s) public view returns(bool) {
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHash = keccak256(abi.encodePacked(prefix, hash));
address _signer = ecrecover(prefixedHash, v, r, s);
return (_signer == signer);
}
/**
* @dev Allows owner to transfer ownership to other address.
* @param _newOwner new owner address
*/
function tranferOwnership(address _newOwner) external onlyOwner {
require(_newOwner != address(0), "Can not be null address");
owner = _newOwner;
}
/**
* @dev Allows owner to update the signer address.
* @param _newSigner new signer address
*/
function updateSigner(address _newSigner) external onlyOwner {
require(_newSigner != address(0), "Can not be null address");
signer = _newSigner;
}
}