Hey, I'm Richard. I can help answer any question about Buyback Token with Fees you might have. Ask away!
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/proxy/Clones.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "./IUniswapV2Factory.sol";
import "./IUniswapV2Router02.sol";
import "./IERC20Extended.sol";
import "./Auth.sol";
import "./DividendDistributor.sol";
import "./BaseToken.sol";
contract BuybackBabyToken is IERC20Extended, Auth, BaseToken {
using SafeMath for uint256;
using Address for address;
using Address for address payable;
uint256 public constant VERSION = 2;
address private constant DEAD = address(0xdead);
address private constant ZERO = address(0);
uint8 private constant _decimals = 9;
string private _name;
string private _symbol;
uint256 private _totalSupply;
address public rewardToken;
IUniswapV2Router02 public router;
address public pair;
address public marketingFeeReceiver;
uint256 public liquidityFee; // default: 200
uint256 public buybackFee; // default: 300
uint256 public reflectionFee; // default: 800
uint256 public marketingFee; // default: 100
uint256 public totalFee;
uint256 public feeDenominator; // default: 10000
uint256 public targetLiquidity; // default: 25
uint256 public targetLiquidityDenominator; // default: 100
uint256 public buybackMultiplierNumerator; // default: 200
uint256 public buybackMultiplierDenominator; // default: 100
uint256 public buybackMultiplierTriggeredAt;
uint256 public buybackMultiplierLength; // default: 30 mins
bool public autoBuybackEnabled;
uint256 public autoBuybackCap;
uint256 public autoBuybackAccumulator;
uint256 public autoBuybackAmount;
uint256 public autoBuybackBlockPeriod;
uint256 public autoBuybackBlockLast;
DividendDistributor public distributor;
uint256 public distributorGas;
bool public swapEnabled;
uint256 public swapThreshold;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
mapping(address => bool) public buyBacker;
mapping(address => bool) public isFeeExempt;
mapping(address => bool) public isDividendExempt;
event AutoLiquify(uint256 amountBNB, uint256 amountBOG);
event BuybackMultiplierActive(uint256 duration);
bool inSwap;
modifier swapping() {
inSwap = true;
_;
inSwap = false;
}
modifier onlyBuybacker() {
require(buyBacker[msg.sender] == true, "Not a buybacker");
_;
}
constructor(
string memory name_,
string memory symbol_,
uint256 totalSupply_,
address rewardToken_,
address router_,
uint256[5] memory feeSettings_,
address serviceFeeReceiver_,
uint256 serviceFee_
) payable Auth(msg.sender) {
_name = name_;
_symbol = symbol_;
_totalSupply = totalSupply_;
rewardToken = rewardToken_;
router = IUniswapV2Router02(router_);
pair = IUniswapV2Factory(router.factory()).createPair(
address(this),
router.WETH()
);
distributor = new DividendDistributor(rewardToken_, router_);
_initializeFees(feeSettings_);
_initializeLiquidityBuyBack();
distributorGas = 500_000;
swapEnabled = true;
swapThreshold = _totalSupply / 1000; // 0.1%
isFeeExempt[msg.sender] = true;
isDividendExempt[pair] = true;
isDividendExempt[address(this)] = true;
isDividendExempt[DEAD] = true;
buyBacker[msg.sender] = true;
marketingFeeReceiver = msg.sender;
require(
!marketingFeeReceiver.isContract(),
"Marketing wallet cannot be a contract"
);
_allowances[address(this)][address(router)] = _totalSupply;
_allowances[address(this)][address(pair)] = _totalSupply;
_balances[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
emit TokenCreated(
msg.sender,
address(this),
TokenType.buybackBaby,
VERSION
);
payable(serviceFeeReceiver_).transfer(serviceFee_);
}
function _initializeFees(uint256[5] memory feeSettings_) internal {
_setFees(
feeSettings_[0], // liquidityFee
feeSettings_[1], // buybackFee
feeSettings_[2], // reflectionFee
feeSettings_[3], // marketingFee
feeSettings_[4] // feeDenominator
);
}
function _initializeLiquidityBuyBack() internal {
targetLiquidity = 25;
targetLiquidityDenominator = 100;
buybackMultiplierNumerator = 200;
buybackMultiplierDenominator = 100;
buybackMultiplierLength = 30 minutes;
}
receive() external payable {}
function totalSupply() external view override returns (uint256) {
return _totalSupply;
}
function decimals() external pure override returns (uint8) {
return _decimals;
}
function symbol() external view override returns (string memory) {
return _symbol;
}
function name() external view override returns (string memory) {
return _name;
}
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
function allowance(address holder, address spender)
external
view
override
returns (uint256)
{
return _allowances[holder][spender];
}
function approve(address spender, uint256 amount)
public
override
returns (bool)
{
_allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function approveMax(address spender) external returns (bool) {
return approve(spender, _totalSupply);
}
function transfer(address recipient, uint256 amount)
external
override
returns (bool)
{
return _transferFrom(msg.sender, recipient, amount);
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) external override returns (bool) {
if (_allowances[sender][msg.sender] != _totalSupply) {
_allowances[sender][msg.sender] = _allowances[sender][msg.sender]
.sub(amount, "Insufficient Allowance");
}
return _transferFrom(sender, recipient, amount);
}
function _transferFrom(
address sender,
address recipient,
uint256 amount
) internal returns (bool) {
if (inSwap) {
return _basicTransfer(sender, recipient, amount);
}
if (shouldSwapBack()) {
swapBack();
}
if (shouldAutoBuyback()) {
triggerAutoBuyback();
}
_balances[sender] = _balances[sender].sub(
amount,
"Insufficient Balance"
);
uint256 amountReceived = shouldTakeFee(sender)
? takeFee(sender, recipient, amount)
: amount;
_balances[recipient] = _balances[recipient].add(amountReceived);
if (!isDividendExempt[sender]) {
try distributor.setShare(sender, _balances[sender]) {} catch {}
}
if (!isDividendExempt[recipient]) {
try
distributor.setShare(recipient, _balances[recipient])
{} catch {}
}
try distributor.process(distributorGas) {} catch {}
emit Transfer(sender, recipient, amountReceived);
return true;
}
function _basicTransfer(
address sender,
address recipient,
uint256 amount
) internal returns (bool) {
_balances[sender] = _balances[sender].sub(
amount,
"Insufficient Balance"
);
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
return true;
}
function shouldTakeFee(address sender) internal view returns (bool) {
return !isFeeExempt[sender] && totalFee > 0;
}
function getTotalFee(bool selling) public view returns (uint256) {
if (selling) {
return getMultipliedFee();
}
return totalFee;
}
function getMultipliedFee() public view returns (uint256) {
if (
buybackMultiplierTriggeredAt.add(buybackMultiplierLength) >
block.timestamp
) {
uint256 remainingTime = buybackMultiplierTriggeredAt
.add(buybackMultiplierLength)
.sub(block.timestamp);
uint256 feeIncrease = totalFee
.mul(buybackMultiplierNumerator)
.div(buybackMultiplierDenominator)
.sub(totalFee);
uint256 increasedFee = totalFee.add(
feeIncrease.mul(remainingTime).div(buybackMultiplierLength)
);
return
increasedFee > feeDenominator / 4
? feeDenominator / 4
: increasedFee;
}
return totalFee;
}
function takeFee(
address sender,
address receiver,
uint256 amount
) internal returns (uint256) {
uint256 feeAmount = amount.mul(getTotalFee(receiver == pair)).div(
feeDenominator
);
_balances[address(this)] = _balances[address(this)].add(feeAmount);
emit Transfer(sender, address(this), feeAmount);
return amount.sub(feeAmount);
}
function shouldSwapBack() internal view returns (bool) {
return
msg.sender != pair &&
!inSwap &&
swapEnabled &&
_balances[address(this)] >= swapThreshold;
}
function swapBack() internal swapping {
uint256 dynamicLiquidityFee = isOverLiquified(
targetLiquidity,
targetLiquidityDenominator
)
? 0
: liquidityFee;
uint256 amountToLiquify;
if (totalFee > 0) {
amountToLiquify = swapThreshold
.mul(dynamicLiquidityFee)
.div(totalFee)
.div(2);
}
uint256 amountToSwap = swapThreshold.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 amountBNB = address(this).balance.sub(balanceBefore);
uint256 totalBNBFee = totalFee.sub(dynamicLiquidityFee.div(2));
uint256 amountBNBLiquidity;
if (totalBNBFee > 0) {
amountBNBLiquidity = amountBNB
.mul(dynamicLiquidityFee)
.div(totalBNBFee)
.div(2);
uint256 amountBNBReflection = amountBNB.mul(reflectionFee).div(
totalBNBFee
);
uint256 amountBNBMarketing = amountBNB.mul(marketingFee).div(
totalBNBFee
);
try distributor.deposit{value: amountBNBReflection}() {} catch {}
payable(marketingFeeReceiver).transfer(amountBNBMarketing);
}
if (amountToLiquify > 0) {
router.addLiquidityETH{value: amountBNBLiquidity}(
address(this),
amountToLiquify,
0,
0,
DEAD,
block.timestamp
);
emit AutoLiquify(amountBNBLiquidity, amountToLiquify);
}
}
function shouldAutoBuyback() internal view returns (bool) {
return
msg.sender != pair &&
!inSwap &&
autoBuybackEnabled &&
autoBuybackBlockLast + autoBuybackBlockPeriod <= block.number && // After N blocks from last buyback
address(this).balance >= autoBuybackAmount;
}
function triggerZeusBuyback(uint256 amount, bool triggerBuybackMultiplier)
external
authorized
{
buyTokens(amount, DEAD);
if (triggerBuybackMultiplier) {
buybackMultiplierTriggeredAt = block.timestamp;
emit BuybackMultiplierActive(buybackMultiplierLength);
}
}
function clearBuybackMultiplier() external authorized {
buybackMultiplierTriggeredAt = 0;
}
function triggerAutoBuyback() internal {
buyTokens(autoBuybackAmount, DEAD);
autoBuybackBlockLast = block.number;
autoBuybackAccumulator = autoBuybackAccumulator.add(autoBuybackAmount);
if (autoBuybackAccumulator > autoBuybackCap) {
autoBuybackEnabled = false;
}
}
function buyTokens(uint256 amount, address to) internal swapping {
address[] memory path = new address[](2);
path[0] = router.WETH();
path[1] = address(this);
router.swapExactETHForTokensSupportingFeeOnTransferTokens{
value: amount
}(0, path, to, block.timestamp);
}
function setAutoBuybackSettings(
bool _enabled,
uint256 _cap,
uint256 _amount,
uint256 _period
) external authorized {
require(_period > 0, "Period must be greater than 0");
autoBuybackEnabled = _enabled;
autoBuybackCap = _cap;
autoBuybackAccumulator = 0;
autoBuybackAmount = _amount;
autoBuybackBlockPeriod = _period;
autoBuybackBlockLast = block.number;
}
function setBuybackMultiplierSettings(
uint256 numerator,
uint256 denominator,
uint256 length
) external authorized {
require(length <= 2 hours, "Length must be less than 2 hours");
require(numerator / denominator <= 2 && numerator > denominator);
buybackMultiplierNumerator = numerator;
buybackMultiplierDenominator = denominator;
buybackMultiplierLength = length;
}
function setIsDividendExempt(address holder, bool exempt)
external
authorized
{
require(holder != address(this) && holder != pair);
isDividendExempt[holder] = exempt;
if (exempt) {
distributor.setShare(holder, 0);
} else {
distributor.setShare(holder, _balances[holder]);
}
}
function setIsFeeExempt(address holder) external authorized {
isFeeExempt[holder] = true;
}
function setBuyBacker(address acc, bool add) external authorized {
buyBacker[acc] = add;
}
function setFees(
uint256 _liquidityFee,
uint256 _buybackFee,
uint256 _reflectionFee,
uint256 _marketingFee,
uint256 _feeDenominator
) public authorized {
_setFees(
_liquidityFee,
_buybackFee,
_reflectionFee,
_marketingFee,
_feeDenominator
);
}
function _setFees(
uint256 _liquidityFee,
uint256 _buybackFee,
uint256 _reflectionFee,
uint256 _marketingFee,
uint256 _feeDenominator
) internal {
liquidityFee = _liquidityFee;
buybackFee = _buybackFee;
reflectionFee = _reflectionFee;
marketingFee = _marketingFee;
totalFee = _liquidityFee.add(_buybackFee).add(_reflectionFee).add(
_marketingFee
);
feeDenominator = _feeDenominator;
require(
totalFee <= feeDenominator / 4,
"Total fee should not be greater than 1/4 of fee denominator"
);
}
function setFeeReceivers(address _marketingFeeReceiver)
external
authorized
{
require(
_marketingFeeReceiver != marketingFeeReceiver,
"Marketing wallet is already that address"
);
require(
!_marketingFeeReceiver.isContract(),
"Marketing wallet cannot be a contract"
);
marketingFeeReceiver = _marketingFeeReceiver;
}
function setSwapBackSettings(bool _enabled, uint256 _amount)
external
authorized
{
require(
_enabled && _amount >= _totalSupply / 100_000,
"Swapback amount should be at least 0.001% of total supply"
);
swapEnabled = _enabled;
swapThreshold = _amount;
}
function setTargetLiquidity(uint256 _target, uint256 _denominator)
external
authorized
{
require(_denominator > 0, "Denominator must be greater than 0");
targetLiquidity = _target;
targetLiquidityDenominator = _denominator;
}
function setDistributionCriteria(
uint256 _minPeriod,
uint256 _minDistribution
) external onlyOwner {
distributor.setDistributionCriteria(_minPeriod, _minDistribution);
}
function setDistributorSettings(uint256 gas) external authorized {
require(
gas >= 200_000 && gas <= 500_000,
"gasForProcessing must be between 200,000 and 500,000"
);
distributorGas = gas;
}
function getCirculatingSupply() public view returns (uint256) {
return _totalSupply.sub(balanceOf(DEAD)).sub(balanceOf(ZERO));
}
function getLiquidityBacking(uint256 accuracy)
public
view
returns (uint256)
{
return accuracy.mul(balanceOf(pair).mul(2)).div(getCirculatingSupply());
}
function isOverLiquified(uint256 target, uint256 accuracy)
public
view
returns (bool)
{
return getLiquidityBacking(accuracy) > target;
}
}
DeployOpen InDownload