bom-contracts/contracts/token.sol

567 lines
17 KiB
Solidity
Raw Normal View History

2022-09-01 14:44:06 +00:00
// SPDX-License-Identifier: MIT OR Apache-2.0
2022-07-25 15:40:16 +00:00
pragma solidity ^0.8.15;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
2022-09-22 10:05:04 +00:00
import "./interfaces/IPancakeSwapPair.sol";
2022-07-25 15:40:16 +00:00
import "./mixins/SafeMathInt.sol";
import "./interfaces/ITreasury.sol";
import "./interfaces/INFT.sol";
contract BabiesOfMars is ERC20Upgradeable {
event LogRebase(uint256 indexed epoch, uint256 totalSupply);
string public _name = "BabiesOfMars";
string public _symbol = "BoM";
uint8 public _decimals = 5;
IPancakeSwapPair public pairContract;
mapping(address => bool) _isFeeExempt;
modifier validRecipient(address to) {
2022-09-22 10:05:04 +00:00
require(to != address(0x0), "!recipient");
2022-07-25 15:40:16 +00:00
_;
}
modifier onlyAdmin() {
2022-09-22 10:05:04 +00:00
require(treasury.isAdmin(msg.sender), "!admin");
2022-07-25 15:40:16 +00:00
_;
}
uint256 public constant DECIMALS = 5;
uint256 public constant MAX_UINT256 = ~uint256(0);
uint8 public constant RATE_DECIMALS = 7;
uint256 public feeDenominator = 10000;
address DEAD = 0x000000000000000000000000000000000000dEaD;
address ZERO = 0x0000000000000000000000000000000000000000;
address public autoLiquidityReceiver;
ITreasury public treasury;
address public redTrustWallet;
address public redFurnace;
address public pairAddress;
INFT public nftRewardPool;
bool public swapEnabled = true;
IPancakeSwapRouter public router;
IPancakeSwapPair public pair;
bool inSwap = false;
uint256 lastPrice;
uint256 defenderTimer;
2022-08-09 13:29:11 +00:00
uint256 accumulatedImpact;
2022-07-25 15:40:16 +00:00
bool rdStatus;
modifier swapping() {
inSwap = true;
_;
inSwap = false;
}
2022-09-22 10:05:04 +00:00
uint256 private constant INITIAL_FRAGMENTS_SUPPLY = 150_000 * 10**DECIMALS; // should be 50
2022-07-25 15:40:16 +00:00
uint256 private constant MAX_SUPPLY = 500_000 * 10**DECIMALS;
2022-09-22 10:05:04 +00:00
uint256 private constant TOTAL_GONS = MAX_UINT256 - (MAX_UINT256 % INITIAL_FRAGMENTS_SUPPLY);
2022-07-25 15:40:16 +00:00
bool public _autoRebase;
bool public _autoAddLiquidity;
uint256 public _initRebaseStartTime;
uint256 public _lastRebasedTime;
uint256 public _lastAddLiquidityTime;
uint256 public _totalSupply;
uint256 private _gonsPerFragment;
uint256 public rebaseInterval = 15 minutes;
mapping(address => uint256) private _gonBalances;
mapping(address => mapping(address => uint256)) private _allowedFragments;
mapping(address => bool) public blacklist;
function initialize(
address _router,
address _owner,
ITreasury _treasury,
address _redTrustWallet,
INFT _nftRewardPool,
2022-09-22 10:05:04 +00:00
address _redFurnace,
address _BUSD
2022-07-25 15:40:16 +00:00
) public initializer {
__ERC20_init(_name, _symbol);
router = IPancakeSwapRouter(_router);
2022-09-22 10:05:04 +00:00
address factoryAddress = router.factory();
IPancakeSwapFactory factory = IPancakeSwapFactory(factoryAddress);
2022-07-25 15:40:16 +00:00
pair = IPancakeSwapPair(
2022-09-22 10:05:04 +00:00
factory.createPair(
2022-07-25 15:40:16 +00:00
router.WETH(),
address(this)
)
);
2022-09-22 10:05:04 +00:00
IPancakeSwapPair(
factory.createPair(
_BUSD,
address(this)
)
);
2022-07-25 15:40:16 +00:00
autoLiquidityReceiver = DEAD;
treasury = _treasury;
redTrustWallet = _redTrustWallet;
redFurnace = _redFurnace;
nftRewardPool = _nftRewardPool;
_allowedFragments[address(this)][address(router)] = type(uint256).max;
pairContract = IPancakeSwapPair(pair);
_totalSupply = INITIAL_FRAGMENTS_SUPPLY;
2022-09-22 10:05:04 +00:00
_gonBalances[_owner] = TOTAL_GONS / 3;
_gonBalances[address(_nftRewardPool)] = TOTAL_GONS / 3; // TODO: remove after test
_gonBalances[0x62F650c0eE84E3a1998A2EEe042a12D9E9728843] = TOTAL_GONS / 3; // TODO: remove after test
_gonsPerFragment = TOTAL_GONS / _totalSupply;
2022-07-25 15:40:16 +00:00
_initRebaseStartTime = block.timestamp;
_lastRebasedTime = block.timestamp;
_autoRebase = false;
_autoAddLiquidity = true;
_isFeeExempt[_owner] = true;
_isFeeExempt[address(this)] = true;
_isFeeExempt[redTrustWallet] = true;
_isFeeExempt[address(nftRewardPool)] = true;
_isFeeExempt[address(treasury)] = true;
defenderTimer = block.timestamp;
emit Transfer(address(0x0), _owner, _totalSupply);
}
function rebase() internal {
if (inSwap) return;
uint256 rebaseRate;
uint256 deltaTimeFromInit = block.timestamp - _initRebaseStartTime;
uint256 deltaTime = block.timestamp - _lastRebasedTime;
2022-09-22 10:05:04 +00:00
uint256 times = deltaTime / rebaseInterval;
uint256 epoch = times * 15;
2022-07-25 15:40:16 +00:00
if (deltaTimeFromInit < (365 days)) {
rebaseRate = 2731;
} else if (deltaTimeFromInit >= (365 days)) {
rebaseRate = 211;
} else if (deltaTimeFromInit >= ((15 * 365 days) / 10)) {
rebaseRate = 14;
} else if (deltaTimeFromInit >= (7 * 365 days)) {
rebaseRate = 2;
}
for (uint256 i = 0; i < times; i++) {
2022-09-22 10:05:04 +00:00
_totalSupply = _totalSupply * (10**RATE_DECIMALS + rebaseRate) / 10**RATE_DECIMALS;
2022-07-25 15:40:16 +00:00
}
2022-09-22 10:05:04 +00:00
_gonsPerFragment = TOTAL_GONS / _totalSupply;
_lastRebasedTime += times * rebaseInterval;
2022-07-25 15:40:16 +00:00
pairContract.sync();
emit LogRebase(epoch, _totalSupply);
}
function transfer(address to, uint256 value)
public
override
validRecipient(to)
returns (bool)
{
_transferFrom(msg.sender, to, value);
return true;
}
function transferFrom(
address from,
address to,
uint256 value
) public override validRecipient(to) returns (bool) {
2022-09-22 10:05:04 +00:00
if (from != msg.sender && _allowedFragments[from][msg.sender] != type(uint256).max) {
require(value <= _allowedFragments[from][msg.sender], "Insufficient Allowance");
_allowedFragments[from][msg.sender] -= (value);
2022-07-25 15:40:16 +00:00
}
_transferFrom(from, to, value);
return true;
}
function _basicTransfer(
address from,
address to,
uint256 amount
) internal returns (bool) {
2022-09-22 10:05:04 +00:00
uint256 gonAmount = amount * _gonsPerFragment;
_gonBalances[from] -= gonAmount;
_gonBalances[to] += gonAmount;
2022-07-25 15:40:16 +00:00
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);
}
if (shouldRebase()) {
rebase();
}
if (shouldAddLiquidity()) {
addLiquidity();
}
2022-09-22 10:05:04 +00:00
uint256 gonAmount = amount * _gonsPerFragment;
_gonBalances[sender] -= gonAmount;
2022-07-25 15:40:16 +00:00
uint256 gonAmountReceived = shouldTakeFee(sender, recipient)
? takeFee(sender, recipient, gonAmount)
: gonAmount;
2022-09-22 10:05:04 +00:00
_gonBalances[recipient] += gonAmountReceived;
2022-07-25 15:40:16 +00:00
emit Transfer(
sender,
recipient,
2022-09-22 10:05:04 +00:00
gonAmountReceived / _gonsPerFragment
2022-07-25 15:40:16 +00:00
);
return true;
}
function takeFee(
address sender,
address recipient,
uint256 gonAmount
) internal returns (uint256) {
(uint256 impact, uint256 oldPrice, uint256 newPrice) = getImpact(
gonAmount
);
2022-08-09 13:29:11 +00:00
// deactivate defender if 1 hour window has passed
2022-07-25 15:40:16 +00:00
if (rdStatus == true) {
2022-08-09 13:29:11 +00:00
if (block.timestamp - defenderTimer > 1 hours) {
2022-07-25 15:40:16 +00:00
rdStatus = false;
2022-09-22 10:05:04 +00:00
defenderTimer = block.timestamp - (1);
2022-08-09 13:29:11 +00:00
accumulatedImpact = 1;
2022-07-25 15:40:16 +00:00
}
}
2022-08-19 13:34:53 +00:00
uint256 impactTax;
uint256 feeAmount;
2022-08-09 13:29:11 +00:00
// sell
2022-07-25 15:40:16 +00:00
if (recipient == address(pair)) {
2022-08-09 13:29:11 +00:00
if (block.timestamp - defenderTimer < 1 hours) {
// add impact to accumulator
2022-09-22 10:05:04 +00:00
accumulatedImpact += impact;
2022-08-09 13:29:11 +00:00
} else {
// window has passed, reset accumulator
accumulatedImpact = impact;
defenderTimer = block.timestamp;
}
require(accumulatedImpact <= 500, "price impact is too large");
// activate defender if accumulated impact is > 1%
if (accumulatedImpact > 100) {
rdStatus = true;
defenderTimer = block.timestamp;
} else {
impactTax = ((gonAmount * impact) / 1000) * 4;
}
if (rdStatus) {
2022-08-19 13:34:53 +00:00
feeAmount = distributeFees(500, 1000, 100 + 4 * impact, 0, 0, gonAmount);
2022-08-09 13:29:11 +00:00
} else {
2022-08-19 13:34:53 +00:00
feeAmount = distributeFees(400, 500, 500 + 4 * impact, 200, 300, gonAmount);
2022-08-09 13:29:11 +00:00
}
} else { // buy
if (rdStatus) {
2022-08-19 13:34:53 +00:00
feeAmount = distributeFees(200, 500, 300, 0, 0, gonAmount);
2022-08-09 13:29:11 +00:00
} else {
2022-08-19 13:34:53 +00:00
feeAmount = distributeFees(400, 500, 300, 200, 300, gonAmount);
2022-07-25 15:40:16 +00:00
}
}
2022-09-22 10:05:04 +00:00
return gonAmount - (feeAmount);
2022-08-19 13:34:53 +00:00
}
function distributeFees(uint256 liquidityFee, uint256 rtfFee, uint256 rtFee, uint256 rfFee, uint256 rewardFee, uint256 gonAmount) private returns (uint256) {
2022-09-22 10:05:04 +00:00
uint256 _totalFee = liquidityFee + rtfFee;
_totalFee += rtFee;
_totalFee += rfFee;
_totalFee += rewardFee;
uint256 feeAmount = gonAmount * _totalFee / feeDenominator;
_gonBalances[redFurnace] += gonAmount * rfFee / feeDenominator;
_gonBalances[address(treasury)] += gonAmount * rtFee / feeDenominator;
_gonBalances[redTrustWallet] += gonAmount * rtfFee / feeDenominator;
_gonBalances[autoLiquidityReceiver] += gonAmount * liquidityFee / feeDenominator;
2022-08-09 13:29:11 +00:00
approve(address(nftRewardPool), rewardFee);
nftRewardPool.raiseRewardPool(rewardFee);
2022-09-22 10:05:04 +00:00
emit Transfer(msg.sender, address(treasury), rtFee / _gonsPerFragment);
emit Transfer(msg.sender, redTrustWallet, rtfFee / _gonsPerFragment);
emit Transfer(msg.sender, redFurnace, rfFee / _gonsPerFragment);
emit Transfer(msg.sender, autoLiquidityReceiver, liquidityFee / _gonsPerFragment);
2022-08-09 13:29:11 +00:00
2022-08-19 13:34:53 +00:00
return feeAmount;
2022-07-25 15:40:16 +00:00
}
function getImpact(uint256 amount)
internal
view
returns (
uint256,
uint256,
uint256
)
{
uint256 price0 = pair.price0CumulativeLast();
(uint256 reserve0, uint256 reserve1, ) = pair.getReserves();
uint256 constProduct = reserve0 * reserve1;
uint256 new1Amount = reserve1 + amount;
uint256 new0Amount = constProduct / new1Amount;
uint256 amountTradedFor = reserve1 - new0Amount;
uint256 new0Price = amount / amountTradedFor;
return (((new0Price - price0) / price0) * 10000, price0, new0Price);
// return (amount*priceImpact/1000)*4;
}
function addLiquidity() internal swapping {
2022-09-22 10:05:04 +00:00
uint256 autoLiquidityAmount = _gonBalances[autoLiquidityReceiver] / (
2022-07-25 15:40:16 +00:00
_gonsPerFragment
);
2022-09-22 10:05:04 +00:00
_gonBalances[address(this)] += _gonBalances[autoLiquidityReceiver];
2022-07-25 15:40:16 +00:00
_gonBalances[autoLiquidityReceiver] = 0;
2022-09-22 10:05:04 +00:00
uint256 amountToLiquify = autoLiquidityAmount / 2;
uint256 amountToSwap = autoLiquidityAmount - amountToLiquify;
2022-07-25 15:40:16 +00:00
if (amountToSwap == 0) {
return;
}
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
);
2022-09-22 10:05:04 +00:00
uint256 amountETHLiquidity = address(this).balance - (balanceBefore);
2022-07-25 15:40:16 +00:00
if (amountToLiquify > 0 && amountETHLiquidity > 0) {
router.addLiquidityETH{value: amountETHLiquidity}(
address(this),
amountToLiquify,
0,
0,
DEAD,
block.timestamp
);
}
_lastAddLiquidityTime = block.timestamp;
}
function withdrawAllToTreasury() public swapping onlyAdmin {
2022-09-22 10:05:04 +00:00
uint256 amountToSwap = _gonBalances[address(this)] / (
2022-07-25 15:40:16 +00:00
_gonsPerFragment
);
require(
amountToSwap > 0,
"There is no token deposited in token contract"
);
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = router.WETH();
router.swapExactTokensForETHSupportingFeeOnTransferTokens(
amountToSwap,
0,
path,
address(treasury),
block.timestamp
);
}
function shouldTakeFee(address from, address to)
internal
view
returns (bool)
{
return
(address(pair) == from || address(pair) == to) &&
!_isFeeExempt[from] &&
!_isFeeExempt[to];
}
function shouldRebase() internal view returns (bool) {
return
_autoRebase &&
(_totalSupply < MAX_SUPPLY) &&
msg.sender != address(pair) &&
!inSwap &&
block.timestamp >= (_lastRebasedTime + rebaseInterval);
}
function shouldAddLiquidity() internal view returns (bool) {
return
_autoAddLiquidity &&
!inSwap &&
msg.sender != address(pair) &&
block.timestamp >= (_lastAddLiquidityTime + 48 hours);
}
function shouldSwapBack() internal view returns (bool) {
return !inSwap && msg.sender != address(pair);
}
function setAutoRebase(bool _flag) public onlyAdmin {
if (_flag) {
_autoRebase = _flag;
_lastRebasedTime = block.timestamp;
} else {
_autoRebase = _flag;
}
}
function setAutoAddLiquidity(bool _flag) public onlyAdmin {
if (_flag) {
_autoAddLiquidity = _flag;
_lastAddLiquidityTime = block.timestamp;
} else {
_autoAddLiquidity = _flag;
}
}
function allowance(address owner_, address spender)
public
view
override
returns (uint256)
{
return _allowedFragments[owner_][spender];
}
function decreaseAllowance(address spender, uint256 subtractedValue)
public
override
returns (bool)
{
uint256 oldValue = _allowedFragments[msg.sender][spender];
if (subtractedValue >= oldValue) {
_allowedFragments[msg.sender][spender] = 0;
} else {
2022-09-22 10:05:04 +00:00
_allowedFragments[msg.sender][spender] = oldValue - (
2022-07-25 15:40:16 +00:00
subtractedValue
);
}
emit Approval(
msg.sender,
spender,
_allowedFragments[msg.sender][spender]
);
return true;
}
function increaseAllowance(address spender, uint256 addedValue)
public
override
returns (bool)
{
2022-09-22 10:05:04 +00:00
_allowedFragments[msg.sender][spender] += addedValue;
2022-07-25 15:40:16 +00:00
emit Approval(
msg.sender,
spender,
_allowedFragments[msg.sender][spender]
);
return true;
}
function approve(address spender, uint256 value)
public
override
returns (bool)
{
_allowedFragments[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
function checkFeeExempt(address _addr) public view returns (bool) {
return _isFeeExempt[_addr];
}
function getCirculatingSupply() public view returns (uint256) {
2022-09-22 10:05:04 +00:00
return (TOTAL_GONS - _gonBalances[DEAD] - _gonBalances[ZERO]) / _gonsPerFragment;
2022-07-25 15:40:16 +00:00
}
function isNotInSwap() public view returns (bool) {
return !inSwap;
}
function manualSync() public {
IPancakeSwapPair(pair).sync();
}
function setFeeReceivers(
ITreasury _treasury,
address _redTrustWallet,
address _redFurnace
) public onlyAdmin {
treasury = _treasury;
redTrustWallet = _redTrustWallet;
redFurnace = _redFurnace;
}
function getLiquidityBacking(uint256 accuracy)
public
view
returns (uint256)
{
2022-09-22 10:05:04 +00:00
uint256 liquidityBalance = _gonBalances[address(pair)] / _gonsPerFragment;
2022-07-25 15:40:16 +00:00
return
2022-09-22 10:05:04 +00:00
accuracy * liquidityBalance * 2 / getCirculatingSupply();
2022-07-25 15:40:16 +00:00
}
function setWhitelist(address _addr) public onlyAdmin {
_isFeeExempt[_addr] = true;
}
function setBotBlacklist(address _botAddress, bool _flag) public onlyAdmin {
require(
isContract(_botAddress),
"only contract address, not allowed exteranlly owned account"
);
blacklist[_botAddress] = _flag;
}
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
function balanceOf(address who) public view override returns (uint256) {
2022-09-22 10:05:04 +00:00
return _gonBalances[who] / _gonsPerFragment;
2022-07-25 15:40:16 +00:00
}
function isContract(address addr) internal view returns (bool) {
uint256 size;
assembly {
size := extcodesize(addr)
}
return size > 0;
}
function decimals() public view override returns (uint8) {
return _decimals;
}
receive() external payable {}
}