-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPYXToken.sol
More file actions
executable file
·352 lines (302 loc) · 10 KB
/
PYXToken.sol
File metadata and controls
executable file
·352 lines (302 loc) · 10 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import '@openzeppelin/contracts/access/AccessControl.sol';
import '@openzeppelin/contracts/math/SafeMath.sol';
import './interfaces/IPYXToken.sol';
import './interfaces/IPYXStaking.sol';
/** Main token of Pyxis.network */
contract PYXToken is IPYXToken, IERC20, AccessControl {
using SafeMath for uint256;
using EnumerableSet for EnumerableSet.AddressSet;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
event AddSellingFees(
address indexed origin,
address indexed recipient,
address caller,
address sender,
uint256 indexed amount,
uint256 time
);
event SetSellFees(address indexed caller, uint256 indexed value);
event UpdateAddress(
bytes32 indexed setting,
address indexed newValue,
address indexed caller
);
mapping(address => uint256) public override balanceOf;
mapping(address => mapping(address => uint256)) public override allowance;
uint256 public override totalSupply;
string public constant name = 'PYXIS';
string public constant symbol = 'PYX';
uint8 public constant decimals = 18;
uint256 public SELL_FEES; // 3
IPYXStaking public PYX_STAKING;
EnumerableSet.AddressSet private recipientContractAddresses;
EnumerableSet.AddressSet private senderContractAddresses;
/* additional constants */
bytes32 public constant MINTER_ROLE = keccak256('MINTER_ROLE'); // only smart contracts
bytes32 public constant SETTER_ROLE = keccak256('SETTER_ROLE'); // renounce after init
bytes32 public constant SETTINGS_MANAGER_ROLE =
keccak256('SETTINGS_MANAGER_ROLE'); // need this to be able to extend the ecosystem.
/* additional modifiers */
modifier onlyMinter() {
require(
hasRole(MINTER_ROLE, msg.sender),
'PYXToken: Caller is not a minter'
);
_;
}
modifier onlySetter() {
require(
hasRole(SETTER_ROLE, msg.sender),
'PYXToken: Caller is not a setter'
);
_;
}
modifier onlySettingsManager() {
require(
hasRole(SETTINGS_MANAGER_ROLE, msg.sender),
'PYXToken: Caller is not a settings manager'
);
_;
}
constructor() public {
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
_setupRole(SETTINGS_MANAGER_ROLE, msg.sender);
_setupRole(SETTER_ROLE, msg.sender);
}
/* additional methods */
function init(
uint256 _sellFees,
uint256 _liquidityAmount,
address _recipient,
address _pyxStaking,
address[] calldata _minterAccounts
) external onlySetter {
SELL_FEES = _sellFees;
PYX_STAKING = IPYXStaking(_pyxStaking);
/* only smart contracts can mint the token */
for (uint256 idx = 0; idx < _minterAccounts.length; idx = idx.add(1)) {
_setupRole(MINTER_ROLE, _minterAccounts[idx]);
}
// liquidity amount(eth unit) to the recipient to add the liquidity
_mint(_recipient, _liquidityAmount.mul(1e18));
// revoke setter role
renounceRole(SETTER_ROLE, msg.sender);
}
/** only smart contracts can mint the token */
function mint(address _to, uint256 _amount) external override onlyMinter {
_mint(_to, _amount);
}
/** only smart contracts can burn the token */
function burn(address _from, uint256 _amount) external override onlyMinter {
_burn(_from, _amount);
}
function getBalanceOf(address _account)
external
view
override
returns (uint256)
{
return balanceOf[_account];
}
/** settings */
function setSellFees(uint256 _fees) external onlySettingsManager {
SELL_FEES = _fees;
emit SetSellFees(msg.sender, _fees);
}
function addRecipientContractAddress(address account)
external
onlySettingsManager
{
recipientContractAddresses.add(account);
emit UpdateAddress('addRecipientContractAddress', account, msg.sender);
}
function removeRecipientContractAddress(address account)
external
onlySettingsManager
{
recipientContractAddresses.remove(account);
emit UpdateAddress(
'removeRecipientContractAddress',
account,
msg.sender
);
}
function getRecipientContractAddressCount()
external
view
returns (uint256)
{
return recipientContractAddresses.length();
}
function getRecipientContractAddress(uint256 idx)
external
view
returns (address)
{
return recipientContractAddresses.at(idx);
}
function addSenderContractAddress(address account)
external
onlySettingsManager
{
senderContractAddresses.add(account);
emit UpdateAddress('addSenderContractAddress', account, msg.sender);
}
function removeSenderContractAddress(address account)
external
onlySettingsManager
{
senderContractAddresses.remove(account);
emit UpdateAddress('removeSenderContractAddress', account, msg.sender);
}
function getSenderContractAddressCount() external view returns (uint256) {
return senderContractAddresses.length();
}
function getSenderContractAddress(uint256 idx)
external
view
returns (address)
{
return senderContractAddresses.at(idx);
}
/* modified methods */
function _transfer(
address sender,
address recipient,
uint256 amount
) internal virtual {
require(sender != address(0), 'ERC20: transfer from the zero address');
require(recipient != address(0), 'ERC20: transfer to the zero address');
balanceOf[sender] = balanceOf[sender].sub(
amount,
'ERC20: transfer amount exceeds balance'
);
address originAddress = tx.origin;
// buy order - recipient is the same as a person who creates the transaction.
// * exclude smart contract addresses in our system - e.g., auto staking contract
if (
SELL_FEES == 0 ||
originAddress == recipient ||
recipientContractAddresses.contains(recipient) ||
senderContractAddresses.contains(sender)
) {
balanceOf[recipient] = balanceOf[recipient].add(amount);
}
// sell order - person who starts the transaction send it to someone else.
else {
uint256 feesAmount = amount.mul(SELL_FEES).div(100);
balanceOf[recipient] = balanceOf[recipient].add(
amount.sub(feesAmount)
);
// fees amount will be locked in the interest pool, so we burn it here
totalSupply = totalSupply.sub(feesAmount);
// half of the fees will be added to the staking reward pool
// another half will be burned
PYX_STAKING.contractAddPYXToPool(feesAmount.div(2));
emit AddSellingFees(
originAddress,
recipient,
_msgSender(),
sender,
feesAmount,
block.timestamp
);
}
emit Transfer(sender, recipient, amount);
}
/* default methods */
function transfer(address recipient, uint256 amount)
public
virtual
override
returns (bool)
{
_transfer(_msgSender(), recipient, amount);
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
_approve(
sender,
_msgSender(),
allowance[sender][_msgSender()].sub(
amount,
'ERC20: transfer amount exceeds allowance'
)
);
return true;
}
function approve(address spender, uint256 amount)
public
virtual
override
returns (bool)
{
_approve(_msgSender(), spender, amount);
return true;
}
function increaseAllowance(address spender, uint256 addedValue)
public
virtual
returns (bool)
{
_approve(
_msgSender(),
spender,
allowance[_msgSender()][spender].add(addedValue)
);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue)
public
virtual
returns (bool)
{
_approve(
_msgSender(),
spender,
allowance[_msgSender()][spender].sub(
subtractedValue,
'ERC20: decreased allowance below zero'
)
);
return true;
}
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), 'ERC20: mint to the zero address');
totalSupply = totalSupply.add(amount);
balanceOf[account] = balanceOf[account].add(amount);
emit Transfer(address(0), account, amount);
}
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), 'ERC20: burn from the zero address');
balanceOf[account] = balanceOf[account].sub(
amount,
'ERC20: burn amount exceeds balance'
);
totalSupply = totalSupply.sub(amount);
emit Transfer(account, address(0), amount);
}
function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), 'ERC20: approve from the zero address');
require(spender != address(0), 'ERC20: approve to the zero address');
allowance[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
}