-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefiV5Token.sol
523 lines (459 loc) · 17.6 KB
/
DefiV5Token.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// SPDX-License-Identifier: GPL-3.0
/**
CryptoCoin - Crypto for all
*/
pragma solidity 0.8.17;
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { LibCommon } from "./lib/LibCommon.sol";
import { ReflectiveV2ERC20 } from "./ReflectiveV2ERC20.sol";
/// @title A Defi Token implementation with extended functionalities
/// @notice Implements ERC20 standards with additional features like tax and deflation
contract DefiV5Token is ReflectiveV2ERC20, Ownable {
// Constants
uint256 private constant MAX_BPS_AMOUNT = 10_000;
uint256 private constant MAX_ALLOWED_BPS = 2_000;
uint256 public constant MAX_EXCLUSION_LIMIT = 100;
string public constant VERSION = "defi_v_5";
string public constant CONTRACT_NAME = "DefiV5Token";
bytes32 public constant CONTRACT_HASH = 0xddecc1d01f92981b651f672b2a13ca3eca6fa3f183716ce665dd89b48821c2f8;
// State Variables
string public initialDocumentUri;
string public documentUri;
uint256 public immutable initialSupply;
uint256 public immutable initialMaxTokenAmountPerAddress;
uint256 public maxTokenAmountPerAddress;
uint256 public maxTotalSupply;
mapping(address => bool) public isFeesAndLimitsExcluded;
address[] public feesAndLimitsExcluded;
/// @notice Configuration properties for the ERC20 token
struct ERC20ConfigProps {
bool _isMintable;
bool _isBurnable;
bool _isDocumentAllowed;
bool _isMaxAmountOfTokensSet;
bool _isMaxSupplySet;
bool _isTaxable;
bool _isDeflationary;
bool _isReflective;
}
ERC20ConfigProps private configProps;
address public immutable initialTokenOwner;
uint8 private immutable _decimals;
address public taxAddress;
uint256 public taxBPS;
uint256 public deflationBPS;
// Events
event DocumentUriSet(string newDocUri);
event MaxTokenAmountPerSet(uint256 newMaxTokenAmount);
event TaxConfigSet(address indexed _taxAddress, uint256 indexed _taxBPS);
event DeflationConfigSet(uint256 indexed _deflationBPS);
event ReflectionConfigSet(uint256 indexed _feeBPS);
event ExcludeFromFeesAndLimits(address indexed account);
event IncludedInFeesAndLimits(address indexed account);
// Custom Errors
error InvalidMaxTokenAmount(uint256 maxTokenAmount);
error InvalidDecimals(uint8 decimals);
error MaxTokenAmountPerAddrLtPrevious();
error DestBalanceExceedsMaxAllowed(address addr);
error DocumentUriNotAllowed();
error MaxTokenAmountNotAllowed();
error TokenIsNotTaxable();
error TokenIsNotDeflationary();
error InvalidTotalBPS(uint256 bps);
error InvalidReflectiveConfig();
error InvalidMaxSupplyConfig();
error TotalSupplyExceedsMaxAllowedAmount();
error AlreadyExcludedFromFeesAndLimits();
error AlreadyIncludedInFeesAndLimits();
error ReachedMaxExclusionLimit();
/// @notice Constructor to initialize the DeFi token
/// @param name_ Name of the token
/// @param symbol_ Symbol of the token
/// @param initialSupplyToSet Initial supply of tokens
/// @param decimalsToSet Number of decimals for the token
/// @param tokenOwner Address of the initial token owner
/// @param customConfigProps Configuration properties for the token
/// @param newDocumentUri URI for the document associated with the token
/// @param _taxAddress Address where tax will be sent
/// @param bpsParams array of BPS values in this order:
/// taxBPS = bpsParams[0],
/// deflationBPS = bpsParams[1],
/// rewardFeeBPS = bpsParams[2],
/// @param amountParams array of amounts for amount specific config:
/// maxTokenAmount = amountParams[0], Maximum token amount per address
/// maxSupplyAmount = amountParams[1], Maximum token token supply amount
constructor(
string memory name_,
string memory symbol_,
uint256 initialSupplyToSet,
uint8 decimalsToSet,
address tokenOwner,
ERC20ConfigProps memory customConfigProps,
string memory newDocumentUri,
address _taxAddress,
uint256[3] memory bpsParams,
uint256[2] memory amountParams
)
ReflectiveV2ERC20(
name_,
symbol_,
tokenOwner,
initialSupplyToSet,
decimalsToSet,
initialSupplyToSet != 0 ? bpsParams[2] : 0,
customConfigProps._isReflective
)
{
// reflection feature can't be used in combination with burning/minting/deflation
// or reflection config is invalid if no reflection BPS amount is provided
if (
(customConfigProps._isReflective &&
(customConfigProps._isBurnable ||
customConfigProps._isMintable ||
customConfigProps._isDeflationary)) ||
(!customConfigProps._isReflective && bpsParams[2] != 0)
) {
revert InvalidReflectiveConfig();
}
if (customConfigProps._isMaxAmountOfTokensSet) {
if (amountParams[0] == 0) {
revert InvalidMaxTokenAmount(amountParams[0]);
}
}
if (decimalsToSet > 18) {
revert InvalidDecimals(decimalsToSet);
}
if (
customConfigProps._isMaxSupplySet &&
(!customConfigProps._isMintable || (totalSupply() > amountParams[1]))
) {
revert InvalidMaxSupplyConfig();
}
bpsInitChecks(customConfigProps, bpsParams, _taxAddress);
LibCommon.validateAddress(tokenOwner);
taxAddress = _taxAddress;
taxBPS = bpsParams[0];
deflationBPS = bpsParams[1];
initialSupply = initialSupplyToSet;
initialMaxTokenAmountPerAddress = amountParams[0];
initialDocumentUri = newDocumentUri;
initialTokenOwner = tokenOwner;
_decimals = decimalsToSet;
configProps = customConfigProps;
documentUri = newDocumentUri;
maxTokenAmountPerAddress = amountParams[0];
maxTotalSupply = amountParams[1];
if (tokenOwner != msg.sender) {
transferOwnership(tokenOwner);
}
}
function bpsInitChecks(
ERC20ConfigProps memory customConfigProps,
uint256[3] memory bpsParams,
address _taxAddress
) private pure {
uint256 totalBPS = 0;
if (customConfigProps._isTaxable) {
LibCommon.validateAddress(_taxAddress);
totalBPS += bpsParams[0];
}
if (customConfigProps._isDeflationary) {
totalBPS += bpsParams[1];
}
if (customConfigProps._isReflective) {
totalBPS += bpsParams[2];
}
if (totalBPS > MAX_ALLOWED_BPS) {
revert InvalidTotalBPS(totalBPS);
}
}
// Public and External Functions
function getFeesAndLimitsExclusionList() external view returns (address[] memory) {
return feesAndLimitsExcluded;
}
/// @notice Checks if the token is mintable
/// @return True if the token can be minted
function isMintable() public view returns (bool) {
return configProps._isMintable;
}
/// @notice Checks if the token is burnable
/// @return True if the token can be burned
function isBurnable() public view returns (bool) {
return configProps._isBurnable;
}
function getRewardsExclusionList() public view returns (address[] memory) {
return rewardsExcluded;
}
/// @notice Checks if the maximum amount of tokens per address is set
/// @return True if there is a maximum limit for token amount per address
function isMaxAmountOfTokensSet() public view returns (bool) {
return configProps._isMaxAmountOfTokensSet;
}
/// @notice Checks if the maximum amount of token supply is set
/// @return True if there is a maximum limit for token supply
function isMaxSupplySet() public view returns (bool) {
return configProps._isMaxSupplySet;
}
/// @notice Checks if setting a document URI is allowed
/// @return True if setting a document URI is allowed
function isDocumentUriAllowed() public view returns (bool) {
return configProps._isDocumentAllowed;
}
/// @notice Returns the number of decimals used for the token
/// @return The number of decimals
function decimals() public view virtual override returns (uint8) {
return _decimals;
}
/// @notice Checks if the token is taxable
/// @return True if the token has tax applied on transfers
function isTaxable() public view returns (bool) {
return configProps._isTaxable;
}
/// @notice Checks if the token is deflationary
/// @return True if the token has deflation applied on transfers
function isDeflationary() public view returns (bool) {
return configProps._isDeflationary;
}
/// @notice Checks if the token is reflective
/// @return True if the token has reflection (ie. holder rewards) applied on transfers
function isReflective() public view returns (bool) {
return configProps._isReflective;
}
/// @notice Sets a new document URI
/// @dev Can only be called by the contract owner
/// @param newDocumentUri The new URI to be set
function setDocumentUri(string memory newDocumentUri) external onlyOwner {
if (!isDocumentUriAllowed()) {
revert DocumentUriNotAllowed();
}
documentUri = newDocumentUri;
emit DocumentUriSet(newDocumentUri);
}
/// @notice Sets a new maximum token amount per address
/// @dev Can only be called by the contract owner
/// @param newMaxTokenAmount The new maximum token amount per address
function setMaxTokenAmountPerAddress(
uint256 newMaxTokenAmount
) external onlyOwner {
if (!isMaxAmountOfTokensSet()) {
revert MaxTokenAmountNotAllowed();
}
if (newMaxTokenAmount <= maxTokenAmountPerAddress) {
revert MaxTokenAmountPerAddrLtPrevious();
}
maxTokenAmountPerAddress = newMaxTokenAmount;
emit MaxTokenAmountPerSet(newMaxTokenAmount);
}
/// @notice Sets a new reflection fee
/// @dev Can only be called by the contract owner
/// @param _feeBPS The reflection fee in basis points
function setReflectionConfig(uint256 _feeBPS) external onlyOwner {
if (!isReflective()) {
revert TokenIsNotReflective();
}
super._setReflectionFee(_feeBPS);
emit ReflectionConfigSet(_feeBPS);
}
/// @notice Sets a new tax configuration
/// @dev Can only be called by the contract owner
/// @param _taxAddress The address where tax will be sent
/// @param _taxBPS The tax rate in basis points
function setTaxConfig(
address _taxAddress,
uint256 _taxBPS
) external onlyOwner {
if (!isTaxable()) {
revert TokenIsNotTaxable();
}
uint256 totalBPS = deflationBPS + tFeeBPS + _taxBPS;
if (totalBPS > MAX_ALLOWED_BPS) {
revert InvalidTotalBPS(totalBPS);
}
LibCommon.validateAddress(_taxAddress);
taxAddress = _taxAddress;
taxBPS = _taxBPS;
emit TaxConfigSet(_taxAddress, _taxBPS);
}
/// @notice Sets a new deflation configuration
/// @dev Can only be called by the contract owner
/// @param _deflationBPS The deflation rate in basis points
function setDeflationConfig(uint256 _deflationBPS) external onlyOwner {
if (!isDeflationary()) {
revert TokenIsNotDeflationary();
}
uint256 totalBPS = deflationBPS + tFeeBPS + _deflationBPS;
if (totalBPS > MAX_ALLOWED_BPS) {
revert InvalidTotalBPS(totalBPS);
}
deflationBPS = _deflationBPS;
emit DeflationConfigSet(_deflationBPS);
}
/// @notice Transfers tokens to a specified address
/// @dev Overrides the ERC20 transfer function with added tax and deflation logic
/// @param to The address to transfer tokens to
/// @param amount The amount of tokens to be transferred
/// @return True if the transfer was successful
function transfer(
address to,
uint256 amount
) public virtual override returns (bool) {
uint256 taxAmount = _taxAmount(msg.sender, to, amount);
uint256 deflationAmount = _deflationAmount(msg.sender, to, amount);
uint256 amountToTransfer = amount - taxAmount - deflationAmount;
if (isMaxAmountOfTokensSet() && !isFeesAndLimitsExcluded[to]) {
if (balanceOf(to) + amountToTransfer > maxTokenAmountPerAddress) {
revert DestBalanceExceedsMaxAllowed(to);
}
}
if (taxAmount != 0) {
_transferNonReflectedTax(msg.sender, taxAddress, taxAmount);
}
if (deflationAmount != 0) {
_burn(msg.sender, deflationAmount);
}
return super.transfer(to, amountToTransfer);
}
/// @notice Transfers tokens from one address to another
/// @dev Overrides the ERC20 transferFrom function with added tax and deflation logic
/// @param from The address which you want to send tokens from
/// @param to The address which you want to transfer to
/// @param amount The amount of tokens to be transferred
/// @return True if the transfer was successful
function transferFrom(
address from,
address to,
uint256 amount
) public virtual override returns (bool) {
uint256 taxAmount = _taxAmount(from, to, amount);
uint256 deflationAmount = _deflationAmount(from, to ,amount);
uint256 amountToTransfer = amount - taxAmount - deflationAmount;
if (isMaxAmountOfTokensSet() && !isFeesAndLimitsExcluded[to]) {
if (balanceOf(to) + amountToTransfer > maxTokenAmountPerAddress) {
revert DestBalanceExceedsMaxAllowed(to);
}
}
if (taxAmount != 0) {
_transferNonReflectedTax(from, taxAddress, taxAmount);
}
if (deflationAmount != 0) {
_burn(from, deflationAmount);
}
return super.transferFrom(from, to, amountToTransfer);
}
/// @notice Mints new tokens to a specified address
/// @dev Can only be called by the contract owner and if minting is enabled
/// @param to The address to mint tokens to
/// @param amount The amount of tokens to mint
function mint(address to, uint256 amount) external onlyOwner {
if (!isMintable()) {
revert MintingNotEnabled();
}
if (isMaxAmountOfTokensSet() && !isFeesAndLimitsExcluded[to]) {
if (balanceOf(to) + amount > maxTokenAmountPerAddress) {
revert DestBalanceExceedsMaxAllowed(to);
}
}
if (isMaxSupplySet()) {
if (totalSupply() + amount > maxTotalSupply) {
revert TotalSupplyExceedsMaxAllowedAmount();
}
}
super._mint(to, amount);
}
/// @notice Burns a specific amount of tokens
/// @dev Can only be called by the contract owner and if burning is enabled
/// @param amount The amount of tokens to be burned
function burn(uint256 amount) external onlyOwner {
if (!isBurnable()) {
revert BurningNotEnabled();
}
_burn(msg.sender, amount);
}
/// @notice Renounces ownership of the contract
/// @dev Leaves the contract without an owner, disabling any functions that require the owner's authorization
function renounceOwnership() public override onlyOwner {
super.renounceOwnership();
}
/// @notice Transfers ownership of the contract to a new account
/// @dev Can only be called by the current owner
/// @param newOwner The address of the new owner
function transferOwnership(address newOwner) public override onlyOwner {
super.transferOwnership(newOwner);
}
/// @notice method for adding a new account to the exclusion list
/// @param account account to add to the exclusion list
/// @dev only callable by owner
function excludeFromFeesAndLimits(
address account
) external onlyOwner {
if (isFeesAndLimitsExcluded[account]) {
revert AlreadyExcludedFromFeesAndLimits();
}
if (feesAndLimitsExcluded.length >= MAX_EXCLUSION_LIMIT) {
revert ReachedMaxExclusionLimit();
}
isFeesAndLimitsExcluded[account] = true;
feesAndLimitsExcluded.push(account);
emit ExcludeFromFeesAndLimits(account);
}
/// @notice method for adding a new account to the reflection exclusion list
/// @param account account to add to the exclusion list
/// @dev only callable by owner
function excludeFromRewards(address account) external onlyOwner {
super._excludeFromRewards(account);
}
/// @notice method for removing an account from the fees exclusion list
/// @param account account to remove from the exclusion list
/// @dev only callable by owner
function includeInFeesAndLimits(address account) external onlyOwner() {
if (!isFeesAndLimitsExcluded[account]) {
revert AlreadyIncludedInFeesAndLimits();
}
for (uint256 i = 0; i < feesAndLimitsExcluded.length; i++) {
if (feesAndLimitsExcluded[i] == account) {
feesAndLimitsExcluded[i] = feesAndLimitsExcluded[feesAndLimitsExcluded.length - 1];
isFeesAndLimitsExcluded[account] = false;
feesAndLimitsExcluded.pop();
emit IncludedInFeesAndLimits(account);
break;
}
}
}
/// @notice method for removing an account from the reflection exclusion list
/// @param account account to remove from the exclusion list
/// @dev only callable by owner
function includeInRewards(address account) external onlyOwner() {
super._includeInRewards(account);
}
// Internal Functions
/// @notice Calculates the tax amount for a transfer
/// @param sender The address initiating the transfer
/// @param recipient The address receiving the transfer
/// @param amount The amount of tokens being transferred
/// @return taxAmount The calculated tax amount
function _taxAmount(
address sender,
address recipient,
uint256 amount
) internal view returns (uint256 taxAmount) {
taxAmount = 0;
if (taxBPS != 0 && sender != taxAddress && !isFeesAndLimitsExcluded[sender] && !isFeesAndLimitsExcluded[recipient]) {
taxAmount = (amount * taxBPS) / MAX_BPS_AMOUNT;
}
}
/// @notice Calculates the deflation amount for a transfer
/// @param sender The address receiving the transfer
/// @param recipient The address receiving the transfer
/// @param amount The amount of tokens being transferred
/// @return deflationAmount The calculated deflation amount
function _deflationAmount(
address sender,
address recipient,
uint256 amount
) internal view returns (uint256 deflationAmount) {
deflationAmount = 0;
if (deflationBPS != 0 && !isFeesAndLimitsExcluded[sender] && !isFeesAndLimitsExcluded[recipient]) {
deflationAmount = (amount * deflationBPS) / MAX_BPS_AMOUNT;
}
}
}