← Back to blog

Multi-mode Token Contract Analysis (Referral, Burn, Marketing Wallet)

Walkthrough of a multi-mode ERC20 contract with referral tiers, trading tax, burn, marketing allocation, and owner-configurable parameters.

Summary

Analysis of a multi-mode smart contract covering multi-level referral rewards, buy/sell tax, burn, and related mechanics, with detailed code walkthrough. Full source is open on GitHub. To earn more token dividends, users must distribute or transfer tokens to more wallets and ensure those wallets participate in trading. Referral depth and reward ratios are adjustable until ownership is renounced or locked. The owner can renounce, transfer ownership before renouncing, and tune tax rates, marketing wallet, and more while admin rights remain. Whitelisted addresses skip trading tax. `initPoolAddress` restricts which wallets may seed the first liquidity pool.

GitHub

Modes

Total supply — set via constructor parameter at deploy time.

Buy/sell swap tax — fee deducted on trades; can fund burn, referral rewards, marketing, auto-liquidity, etc.

Burn — e.g. 1% of each trade (configurable) sent to burn, with an optional cap (`_burnMax`).

Auto liquidity — e.g. 1% of each trade added to the pool or a designated holder to deepen liquidity.

Marketing — e.g. 1% routed to a marketing wallet for promotion.

Direct referral rewards — holders above a minimum balance earn a share of buy/sell fees from direct referrals.

Indirect referral rewards — second-level referrers with sufficient balance also earn fees.

3rd–8th generation rewards — deeper referral tiers up to eight levels, each gated by minimum balance.

Code analysis

Key variables

Parameters are modular and owner-configurable until admin rights are renounced; after renounce, settings are immutable for fairness and security.

uint256 public _buyLiquidityFee = 10;//买入流动池手续费
uint256 public _sellLiquidityFee = 10;//卖出流动池手续费
uint256 public _buyBurnFee = 10;//费用燃烧
uint256 public _sellBurnFee = 10;

uint256 public _sellMarketFee = 10;//市场手续费,打入营销账户
uint256 public _buyMarketFee = 10;//市场手续费,打入营销账户
uint256 public _shareFee = 10;//推广分佣费用
uint[] internal shareConfig = [4,2,1,1,1,1,0,0];//推荐人分成比例配置
uint256 shareRank=6;//max is 8//推荐人分成层级
uint256 public holdMinBalance = 500* 10**9;//持仓最小数量,大于等于该数量才能享受推广分红
uint256 public totalBuyFee = 40;//买入总手续费
uint256 public totalSellFee = 40;//卖出总手续费
address public marketAddress;//营销账户地址
mapping(address => bool) public initPoolAddress;//有权初始化池子地址
 //黑洞地址
address constant public deadAddress = address(0x000000000000000000000000000000000000dEaD);
mapping (address => address) public _recommendMapping;//推荐绑定的关系
//添加池子的最小数量
uint256 public numTokensSellToAddToLiquidity = 50 * 10**9;
uint256 public _burnMax = 90000*10**9;//燃烧的最大数

IUniswapV2Router02 public immutable uniswapV2Router;//uniswap路由
address public  uniswapV2Pair;//UniswapV2Pair地址
address public wbnb;
mapping(address => bool) public ammPairs;//存储交易对
address public holder;//该地址用于接收发行的代币

Constructor

Initializes parameters, creates the LP via `IUniswapV2Router02` / `IUniswapV2Factory`, whitelists pool-init addresses in `initPoolAddress`, mints total supply to `holder`, and emits `Transfer(address(0), holder, _tTotal)`.

constructor (
        string memory name_,string memory symbol_,uint256  totalSupply_,
        uint256 minimumTokenBalanceForDividends_,uint256 burnMax_) {
    _name=name_;
    _symbol=symbol_;
    _tTotal=totalSupply_*10**9;
    numTokensSellToAddToLiquidity=minimumTokenBalanceForDividends_*10**9;
    _burnMax=burnMax_*10**9;
    shareRank=6;
    _shareFee=10;
     wbnb = address(0x55d398326f99059fF775485246999027B3197955);//0x7ef95a0FEE0Dd31b22626fA2e10Ee6A223F8a684);//usdt wbnb shib
     holder = address(0x38E77e49D404334Bb01d00cf93FF3Ab3d9ef98B2);// owner
     marketAddress = address(0x697efA0D06bd418a8bd8F24354A1E76bfd3Ee82C);
     _buyLiquidityFee=10;
     _sellLiquidityFee=10;
    _buyBurnFee=10;
    _sellBurnFee=10;
    _buyMarketFee=20;
    _sellMarketFee=20;
    shareRank=6;
    totalBuyFee=_shareFee;
    totalBuyFee=totalBuyFee.add(_buyLiquidityFee).add(_buyBurnFee).add(_buyMarketFee);
    totalSellFee=_shareFee;
    totalSellFee=totalSellFee.add(_sellLiquidityFee).add(_sellBurnFee).add(_sellMarketFee);
    _recommendMapping[deadAddress] = address(0xdeaddead);
    _recommendMapping[holder] = deadAddress;
    _recommendMapping[marketAddress] = deadAddress;
    _tOwned[holder] = _tTotal;
     IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(address(0x10ED43C718714eb63d5aA57B78B54704E256024E));
     uniswapV2Router = _uniswapV2Router;
    address _uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
        .createPair(address(this), wbnb);
    _allowances[address(this)][address(_uniswapV2Router)] = MAX;
    IERC20(wbnb).approve(address(_uniswapV2Router), MAX);
    uniswapV2Pair = _uniswapV2Pair;
    ammPairs[uniswapV2Pair] = true;
    _isExcludedFromFee[holder] = true;
    _isExcludedFromFee[address(this)] = true;
    _owner = msg.sender;
    initPoolAddress[address(0xad6C294BDE2f5829FC42072AEB19DB9A7Cc48cA9)] = true;
    initPoolAddress[address(0xad6C294BDE2f5829FC42072AEB19DB9A7Cc48cA9)] = true;
    _tokenDistributor = new TokenDistributor(wbnb);
    emit Transfer(address(0), holder, _tTotal);
}

Referral binding

Binds upline/downline on first buy or token receipt.

//绑定推荐关系 主动买入和转账
function addRelationEx(address recommer,address user) internal {
    if( 
        recommer != user 
        && _recommendMapping[user] == address(0x0) 
        && _recommendMapping[recommer] != address(0x0) ){
            _recommendMapping[user] = recommer;
    }       
}

Auto add liquidity

Swap fees accumulated on the contract trigger `swapAndLiquify` once balance reaches `numTokensSellToAddToLiquidity`. Half the tokens are swapped for the pair asset (e.g. USDT/BNB), then `addLiquidity` deepens the pool.

function swapAndLiquify(uint256 contractTokenBalance) private  lockTheSwap{
        
uint256 half = contractTokenBalance.div(2);
uint256 otherHalf = contractTokenBalance.sub(half);
IERC20 baseToken = IERC20(wbnb);
uint256 initialBalance = baseToken.balanceOf(address(_tokenDistributor));
swapTokensForBaseToken(half,address(_tokenDistributor)); 
uint256 newBalance = baseToken.balanceOf(address(_tokenDistributor)).sub(initialBalance);
baseToken.transferFrom(address(_tokenDistributor), address(this), newBalance);

addLiquidityBaseToken(otherHalf, newBalance);
}

Transfer hook

Main entry: detect swaps, bind referrals, auto-liquify when thresholds hit, compute burn/referral/marketing/liquidity fees, gate initial pool adds via `initPoolAddress`, then transfer net amounts.

function _transfer(
        address from,
        address to,
        uint256 amount
    ) private {
    require(from != address(0), "ERC20: transfer from the zero address");
    require(amount > 0, "Transfer amount must be greater than zero");
    uint256 balance = balanceOf(from);
    require(balance > amount, "balanceNotEnough");
    if( !_isContract(to) //钱包地址、不是合约地址
        && _recommendMapping[to] == address(0) ){//未绑定推荐人
        if( ammPairs[from]  ){//是否是交易对
            addRelationEx(holder,to);//买入时,如果未绑定推荐人,绑定holder
        }else{
            addRelationEx(from,to);//转账时,如果未绑定推荐人,绑定from
        }
    }

    uint256 contractTokenBalance = balanceOf(address(this));
    if( 
        contractTokenBalance >= numTokensSellToAddToLiquidity //判断是否达到添加流动池的数量
        && !inSwapAndLiquify //是否正在添加流动池
        && !ammPairs[from] //判断是否是交易对
        && IERC20(uniswapV2Pair).totalSupply() > numLPSellToAddToLiquidity ){//判断流动池的总数量是否大于最小添加流动池的数量
        swapAndLiquify(contractTokenBalance);
    }
    bool takeFee = true;
    if( _isExcludedFromFee[from] || _isExcludedFromFee[to]){//判断是否是免手续费账户
        takeFee = false;
    }

    Param memory param;
    param.tTransferAmount = amount;
    if( takeFee ){
       param.takeFee = true;
       if( ammPairs[from]){//判断是否是交易对,如果是交易对代表是买入
           _getBuyParam(amount,param);//获得买入参数
           param.user = to;
       }
       if( ammPairs[to]){//判断是否是交易对,如果是交易对代表是卖出
            _getSellParam(amount,param);//获得卖出参数
           param.user = from;
       }
       /*if( !ammPairs[from] && !ammPairs[to]){
            _getTransferParam(amount,param);
       }*/
    }
    if( ammPairs[to]){//判断是否是交易对,如果是交易对代表是卖出或者添加流动池
        if( IERC20(uniswapV2Pair).totalSupply() == 0 ){
            require(initPoolAddress[from],"not init pool address");
        }
    }
    _tokenTransfer(from,to,amount,param);//参数from是发起交易的地址
}

Summary

Referral tiers, burn, deflation, marketing wallet splits, and related modes are implemented in full; source is on GitHub. Referral rewards pay in the native token — to pay USDT, WBNB, DOGE, SHIB, etc., configure LP pairs and swap before distribution (extra gas). Renouncing ownership locks all parameters permanently.