-
Notifications
You must be signed in to change notification settings - Fork 83
/
Titano.sol
846 lines (691 loc) · 22.5 KB
/
Titano.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
pragma solidity ^0.7.4;
library SafeMathInt {
int256 private constant MIN_INT256 = int256(1) << 255;
int256 private constant MAX_INT256 = ~(int256(1) << 255);
function mul(int256 a, int256 b) internal pure returns (int256) {
int256 c = a * b;
require(c != MIN_INT256 || (a & MIN_INT256) != (b & MIN_INT256));
require((b == 0) || (c / b == a));
return c;
}
function div(int256 a, int256 b) internal pure returns (int256) {
require(b != -1 || a != MIN_INT256);
return a / b;
}
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
require((b >= 0 && c <= a) || (b < 0 && c > a));
return c;
}
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
require((b >= 0 && c >= a) || (b < 0 && c < a));
return c;
}
function abs(int256 a) internal pure returns (int256) {
require(a != MIN_INT256);
return a < 0 ? -a : a;
}
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender)
external
view
returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(
address from,
address to,
uint256 value
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
function div(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0);
return a % b;
}
}
interface InterfaceLP {
function sync() external;
}
library Roles {
struct Role {
mapping (address => bool) bearer;
}
/**
* @dev Give an account access to this role.
*/
function add(Role storage role, address account) internal {
require(!has(role, account), "Roles: account already has role");
role.bearer[account] = true;
}
/**
* @dev Remove an account's access to this role.
*/
function remove(Role storage role, address account) internal {
require(has(role, account), "Roles: account does not have role");
role.bearer[account] = false;
}
/**
* @dev Check if an account has this role.
* @return bool
*/
function has(Role storage role, address account) internal view returns (bool) {
require(account != address(0), "Roles: account is the zero address");
return role.bearer[account];
}
}
contract MinterRole {
using Roles for Roles.Role;
event MinterAdded(address indexed account);
event MinterRemoved(address indexed account);
Roles.Role private _minters;
constructor () {
_addMinter(msg.sender);
}
modifier onlyMinter() {
require(isMinter(msg.sender), "MinterRole: caller does not have the Minter role");
_;
}
function isMinter(address account) public view returns (bool) {
return _minters.has(account);
}
function renounceMinter() public {
_removeMinter(msg.sender);
}
function _addMinter(address account) internal {
_minters.add(account);
emit MinterAdded(account);
}
function _removeMinter(address account) internal {
_minters.remove(account);
emit MinterRemoved(account);
}
}
abstract contract ERC20Detailed is IERC20 {
string private _name;
string private _symbol;
uint8 private _decimals;
constructor(
string memory name,
string memory symbol,
uint8 decimals
) {
_name = name;
_symbol = symbol;
_decimals = decimals;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
}
interface IDEXRouter {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint256 amountADesired,
uint256 amountBDesired,
uint256 amountAMin,
uint256 amountBMin,
address to,
uint256 deadline
)
external
returns (
uint256 amountA,
uint256 amountB,
uint256 liquidity
);
function addLiquidityETH(
address token,
uint256 amountTokenDesired,
uint256 amountTokenMin,
uint256 amountETHMin,
address to,
uint256 deadline
)
external
payable
returns (
uint256 amountToken,
uint256 amountETH,
uint256 liquidity
);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external;
}
interface IDEXFactory {
function createPair(address tokenA, address tokenB)
external
returns (address pair);
}
contract Ownable {
address private _owner;
event OwnershipRenounced(address indexed previousOwner);
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
constructor() {
_owner = msg.sender;
}
function owner() public view returns (address) {
return _owner;
}
modifier onlyOwner() {
require(isOwner());
_;
}
function isOwner() public view returns (bool) {
return msg.sender == _owner;
}
function renounceOwnership() public onlyOwner {
emit OwnershipRenounced(_owner);
_owner = address(0);
}
function transferOwnership(address newOwner) public onlyOwner {
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal {
require(newOwner != address(0));
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
contract Titano is ERC20Detailed, Ownable, MinterRole {
using SafeMath for uint256;
using SafeMathInt for int256;
event LogRebase(uint256 indexed epoch, uint256 totalSupply);
InterfaceLP public pairContract;
bool public initialDistributionFinished;
mapping(address => bool) allowTransfer;
mapping(address => bool) _isFeeExempt;
modifier initialDistributionLock() {
require(
initialDistributionFinished ||
isOwner() ||
allowTransfer[msg.sender]
);
_;
}
modifier validRecipient(address to) {
require(to != address(0x0));
_;
}
uint256 private constant DECIMALS = 18;
uint256 private constant MAX_UINT256 = ~uint256(0);
uint256 private constant INITIAL_FRAGMENTS_SUPPLY = 4 * 10**9 * 10**DECIMALS;
uint256 public liquidityFee = 5;
uint256 public Treasury = 3;
uint256 public RiskFreeValue = 5;
uint256 public sellFee = 5;
uint256 public totalFee =
liquidityFee.add(Treasury).add(RiskFreeValue);
uint256 public feeDenominator = 100;
address DEAD = 0x000000000000000000000000000000000000dEaD;
address ZERO = 0x0000000000000000000000000000000000000000;
address public autoLiquidityReceiver;
address public TreasuryReceiver;
address public RiskFreeValueReceiver;
uint256 targetLiquidity = 50;
uint256 targetLiquidityDenominator = 100;
IDEXRouter public router;
address public pair;
bool public swapEnabled = true;
uint256 private gonSwapThreshold = (TOTAL_GONS * 10) / 10000;
bool inSwap;
modifier swapping() {
inSwap = true;
_;
inSwap = false;
}
uint256 private constant TOTAL_GONS =
MAX_UINT256 - (MAX_UINT256 % INITIAL_FRAGMENTS_SUPPLY);
uint256 private constant MAX_SUPPLY = ~uint128(0);
uint256 private _totalSupply;
uint256 private _gonsPerFragment;
mapping(address => uint256) private _gonBalances;
mapping(address => mapping(address => uint256)) private _allowedFragments;
mapping(address => bool) public blacklist;
constructor() ERC20Detailed("Titano", "TITANO", uint8(DECIMALS)) {
router = IDEXRouter(0x10ED43C718714eb63d5aA57B78B54704E256024E); //Sushi 0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506 // Cake 0x10ED43C718714eb63d5aA57B78B54704E256024E
pair = IDEXFactory(router.factory()).createPair(
router.WETH(),
address(this)
);
autoLiquidityReceiver = 0xfa1D544D46c7c50d7B7d7D2e85915F1b129a9386;
TreasuryReceiver = 0x4DD90D3cE962039A3c66d613207aC2d449dFa04F;
RiskFreeValueReceiver = 0x00dE99c90E8971D3E1c9cBA724381B537F6e88C1;
_allowedFragments[address(this)][address(router)] = uint256(-1);
pairContract = InterfaceLP(pair);
_totalSupply = INITIAL_FRAGMENTS_SUPPLY;
_gonBalances[TreasuryReceiver] = TOTAL_GONS;
_gonsPerFragment = TOTAL_GONS.div(_totalSupply);
initialDistributionFinished = false;
_isFeeExempt[TreasuryReceiver] = true;
_isFeeExempt[address(this)] = true;
_transferOwnership(TreasuryReceiver);
emit Transfer(address(0x0), TreasuryReceiver, _totalSupply);
}
function updateBlacklist(address _user, bool _flag) public onlyOwner{
blacklist[_user] = _flag;
}
function rebase(uint256 epoch, int256 supplyDelta)
external
onlyOwner
returns (uint256)
{
require(!inSwap, "Try again");
if (supplyDelta == 0) {
emit LogRebase(epoch, _totalSupply);
return _totalSupply;
}
if (supplyDelta < 0) {
_totalSupply = _totalSupply.sub(uint256(-supplyDelta));
} else {
_totalSupply = _totalSupply.add(uint256(supplyDelta));
}
if (_totalSupply > MAX_SUPPLY) {
_totalSupply = MAX_SUPPLY;
}
_gonsPerFragment = TOTAL_GONS.div(_totalSupply);
pairContract.sync();
emit LogRebase(epoch, _totalSupply);
return _totalSupply;
}
function totalSupply() external view override returns (uint256) {
return _totalSupply;
}
function transfer(address to, uint256 value)
external
override
validRecipient(to)
initialDistributionLock
returns (bool)
{
_transferFrom(msg.sender, to, value);
return true;
}
function setLP(address _address) external onlyOwner {
pairContract = InterfaceLP(_address);
_isFeeExempt[_address];
}
function allowance(address owner_, address spender)
external
view
override
returns (uint256)
{
return _allowedFragments[owner_][spender];
}
function balanceOf(address who) external view override returns (uint256) {
return _gonBalances[who].div(_gonsPerFragment);
}
function _basicTransfer(
address from,
address to,
uint256 amount
) internal returns (bool) {
uint256 gonAmount = amount.mul(_gonsPerFragment);
_gonBalances[from] = _gonBalances[from].sub(gonAmount);
_gonBalances[to] = _gonBalances[to].add(gonAmount);
return true;
}
function _transferFrom(
address sender,
address recipient,
uint256 amount
) internal returns (bool) {
require(!blacklist[sender] && !blacklist[recipient], 'in_blacklist');
if (inSwap) {
return _basicTransfer(sender, recipient, amount);
}
uint256 gonAmount = amount.mul(_gonsPerFragment);
if (shouldSwapBack()) {
swapBack();
}
_gonBalances[sender] = _gonBalances[sender].sub(gonAmount);
uint256 gonAmountReceived = shouldTakeFee(sender, recipient)
? takeFee(sender, recipient, gonAmount)
: gonAmount;
_gonBalances[recipient] = _gonBalances[recipient].add(
gonAmountReceived
);
emit Transfer(
sender,
recipient,
gonAmountReceived.div(_gonsPerFragment)
);
return true;
}
function transferFrom(
address from,
address to,
uint256 value
) external override validRecipient(to) returns (bool) {
if (_allowedFragments[from][msg.sender] != uint256(-1)) {
_allowedFragments[from][msg.sender] = _allowedFragments[from][
msg.sender
].sub(value, "Insufficient Allowance");
}
_transferFrom(from, to, value);
return true;
}
function swapBack() internal swapping {
uint256 dynamicLiquidityFee = isOverLiquified(
targetLiquidity,
targetLiquidityDenominator
)
? 0
: liquidityFee;
uint256 contractTokenBalance = _gonBalances[address(this)].div(
_gonsPerFragment
);
uint256 amountToLiquify = contractTokenBalance
.mul(dynamicLiquidityFee)
.div(totalFee)
.div(2);
uint256 amountToSwap = contractTokenBalance.sub(amountToLiquify);
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = router.WETH();
uint256 balanceBefore = address(this).balance;
router.swapExactTokensForETHSupportingFeeOnTransferTokens(
amountToSwap,
0,
path,
address(this),
block.timestamp
);
uint256 amountETH = address(this).balance.sub(balanceBefore);
uint256 totalETHFee = totalFee.sub(dynamicLiquidityFee.div(2));
uint256 amountETHLiquidity = amountETH
.mul(dynamicLiquidityFee)
.div(totalETHFee)
.div(2);
uint256 amountETHRiskFreeValue = amountETH.mul(RiskFreeValue).div(totalETHFee);
uint256 amountETHTreasury = amountETH.mul(Treasury).div(
totalETHFee
);
(bool success, ) = payable(TreasuryReceiver).call{
value: amountETHTreasury,
gas: 30000
}("");
(success, ) = payable(RiskFreeValueReceiver).call{
value: amountETHRiskFreeValue,
gas: 30000
}("");
success = false;
if (amountToLiquify > 0) {
router.addLiquidityETH{value: amountETHLiquidity}(
address(this),
amountToLiquify,
0,
0,
autoLiquidityReceiver,
block.timestamp
);
}
}
function takeFee(address sender, address recipient, uint256 gonAmount)
internal
returns (uint256)
{
uint256 _totalFee = totalFee;
if(recipient == pair) _totalFee = _totalFee.add(sellFee);
uint256 feeAmount = gonAmount.mul(_totalFee).div(feeDenominator);
_gonBalances[address(this)] = _gonBalances[address(this)].add(
feeAmount
);
emit Transfer(sender, address(this), feeAmount.div(_gonsPerFragment));
return gonAmount.sub(feeAmount);
}
function decreaseAllowance(address spender, uint256 subtractedValue)
external
initialDistributionLock
returns (bool)
{
uint256 oldValue = _allowedFragments[msg.sender][spender];
if (subtractedValue >= oldValue) {
_allowedFragments[msg.sender][spender] = 0;
} else {
_allowedFragments[msg.sender][spender] = oldValue.sub(
subtractedValue
);
}
emit Approval(
msg.sender,
spender,
_allowedFragments[msg.sender][spender]
);
return true;
}
function increaseAllowance(address spender, uint256 addedValue)
external
initialDistributionLock
returns (bool)
{
_allowedFragments[msg.sender][spender] = _allowedFragments[msg.sender][
spender
].add(addedValue);
emit Approval(
msg.sender,
spender,
_allowedFragments[msg.sender][spender]
);
return true;
}
function approve(address spender, uint256 value)
external
override
initialDistributionLock
returns (bool)
{
_allowedFragments[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function checkFeeExempt(address _addr) external view returns (bool) {
return _isFeeExempt[_addr];
}
function setInitialDistributionFinished() external onlyOwner {
initialDistributionFinished = true;
}
function enableTransfer(address _addr) external onlyOwner {
allowTransfer[_addr] = true;
}
function setFeeExempt(address _addr) external onlyOwner {
_isFeeExempt[_addr] = true;
}
function shouldTakeFee(address from, address to) internal view returns (bool) {
return (pair == from || pair == to) && (!_isFeeExempt[from]);
}
function mint(address recipient, uint256 amount) external onlyMinter {
_totalSupply = _totalSupply.add(uint256(amount));
if (_totalSupply > MAX_SUPPLY) {
_totalSupply = MAX_SUPPLY;
}
_gonsPerFragment = TOTAL_GONS.div(_totalSupply);
pairContract.sync();
_gonBalances[recipient] = _gonBalances[recipient].add(amount);
}
function setSwapBackSettings(
bool _enabled,
uint256 _num,
uint256 _denom
) external onlyOwner {
swapEnabled = _enabled;
gonSwapThreshold = TOTAL_GONS.div(_denom).mul(_num);
}
function shouldSwapBack() internal view returns (bool) {
return
msg.sender != pair &&
!inSwap &&
swapEnabled &&
_gonBalances[address(this)] >= gonSwapThreshold;
}
function getCirculatingSupply() public view returns (uint256) {
return
(TOTAL_GONS.sub(_gonBalances[DEAD]).sub(_gonBalances[ZERO])).div(
_gonsPerFragment
);
}
function setTargetLiquidity(uint256 target, uint256 accuracy) external onlyOwner {
targetLiquidity = target;
targetLiquidityDenominator = accuracy;
}
function addMinter(address account) public onlyOwner {
_addMinter(account);
}
function removeMinter(address account) public onlyOwner {
_removeMinter(account);
}
function isNotInSwap() external view returns (bool) {
return !inSwap;
}
function sendPresale(address[] calldata recipients, uint256[] calldata values)
external
onlyOwner
{
for (uint256 i = 0; i < recipients.length; i++) {
_transferFrom(msg.sender, recipients[i], values[i]);
}
}
function checkSwapThreshold() external view returns (uint256) {
return gonSwapThreshold.div(_gonsPerFragment);
}
function manualSync() external {
InterfaceLP(pair).sync();
}
function setFeeReceivers(
address _autoLiquidityReceiver,
address _TreasuryReceiver,
address _RiskFreeValueReceiver
) external onlyOwner {
autoLiquidityReceiver = _autoLiquidityReceiver;
TreasuryReceiver = _TreasuryReceiver;
RiskFreeValueReceiver = _RiskFreeValueReceiver;
}
function setFees(
uint256 _liquidityFee,
uint256 _RiskFreeValue,
uint256 _Treasury,
uint256 _sellFee,
uint256 _feeDenominator
) external onlyOwner {
liquidityFee = _liquidityFee;
RiskFreeValue = _RiskFreeValue;
Treasury = _Treasury;
sellFee = _sellFee;
totalFee = liquidityFee.add(Treasury).add(RiskFreeValue);
feeDenominator = _feeDenominator;
require(totalFee < feeDenominator / 4);
}
function clearStuckBalance(uint256 amountPercentage, address adr) external onlyOwner {
uint256 amountETH = address(this).balance;
payable(adr).transfer(
(amountETH * amountPercentage) / 100
);
}
function rescueToken(address tokenAddress, uint256 tokens)
public
onlyOwner
returns (bool success)
{
return ERC20Detailed(tokenAddress).transfer(msg.sender, tokens);
}
function transferToAddressETH(address payable recipient, uint256 amount)
private
{
recipient.transfer(amount);
}
function getLiquidityBacking(uint256 accuracy)
public
view
returns (uint256)
{
uint256 liquidityBalance = _gonBalances[pair].div(_gonsPerFragment);
return
accuracy.mul(liquidityBalance.mul(2)).div(getCirculatingSupply());
}
function isOverLiquified(uint256 target, uint256 accuracy)
public
view
returns (bool)
{
return getLiquidityBacking(accuracy) > target;
}
receive() external payable {}
}