一、说明
该合约代码主要演示了合约部署后其中5%用于添加流动性,进入底池,并实现自动锁仓的操作。剩余95%通过swap交易产出。该模式的合约主要有以下优点:
1、所有代币自合约部署完成后直接进入合约地址,无持币用户
2、5%用于添加流动性,直接在合约中构造addliquidity接口,完成流动性的添加
3、剩余 95%依赖在dex中的交易产出,按照特定的产出规则,进入指定账号钱包地址
4、该方式添加流动性可以避免代币被抢开盘软件或者夹子软件操纵价格行情。
【附注20230125】
该类型的合约模式通常适用于交易挖矿相关的场景,即用户交易触发代币的挖矿产出,产出到指定的地址用于代币分红,兑换ustd,回购,铸造NFT等相关的外围生态建设用途。
并且该模式合约通常配合dapp出现,很少单独为了交易挖矿而部署该模式的合约,并且大部分的业务逻辑是在dapp中去实现的。合约仅仅是实现基本的代币产出相关的基础功能。产出后的代币利用,分配基本都控制在dapp中。
二、合约代码实现
1、IERC20接口文件
interface IERC20 { /** * @dev Emitted when `value` tokens are moved from one account (`from`) to * another (`to`). * * Note that `value` may be zero. */ event Transfer(address indexed from, address indexed to, uint256 value); /** * @dev Emitted when the allowance of a `spender` for an `owner` is set by * a call to {approve}. `value` is the new allowance. */ event Approval(address indexed owner, address indexed spender, uint256 value); /** * @dev Returns the amount of tokens in existence. */ function totalSupply() external view returns (uint256); /** * @dev Returns the amount of tokens owned by `account`. */ function balanceOf(address account) external view returns (uint256); /** * @dev Moves `amount` tokens from the caller's account to `to`. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transfer(address to, uint256 amount) external returns (bool); /** * @dev Returns the remaining number of tokens that `spender` will be * allowed to spend on behalf of `owner` through {transferFrom}. This is * zero by default. * * This value changes when {approve} or {transferFrom} are called. */ function allowance(address owner, address spender) external view returns (uint256); /** * @dev Sets `amount` as the allowance of `spender` over the caller's tokens. * * Returns a boolean value indicating whether the operation succeeded. * * IMPORTANT: Beware that changing an allowance with this method brings the risk * that someone may use both the old and the new allowance by unfortunate * transaction ordering. One possible solution to mitigate this race * condition is to first reduce the spender's allowance to 0 and set the * desired value afterwards: * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 * * Emits an {Approval} event. */ function approve(address spender, uint256 amount) external returns (bool); /** * @dev Moves `amount` tokens from `from` to `to` using the * allowance mechanism. `amount` is then deducted from the caller's * allowance. * * Returns a boolean value indicating whether the operation succeeded. * * Emits a {Transfer} event. */ function transferFrom( address from, address to, uint256 amount ) external returns (bool); }
2、元数据文件
interface IERC20Metadata is IERC20 { /** * @dev Returns the name of the token. */ function name() external view returns (string memory); /** * @dev Returns the symbol of the token. */ function symbol() external view returns (string memory); /** * @dev Returns the decimals places of the token. */ function decimals() external view returns (uint8); }
abstract contract Context { function _msgSender() internal view virtual returns (address) { return msg.sender; } function _msgData() internal view virtual returns (bytes calldata) { return msg.data; } }
4、IERC20接口标准实现类
contract ERC20 is Context, IERC20, IERC20Metadata { mapping(address => uint256) private _balances; mapping(address => mapping(address => uint256)) private _allowances; uint256 private _totalSupply; string private _name; string private _symbol; /** * @dev Sets the values for {name} and {symbol}. * * The default value of {decimals} is 18. To select a different value for * {decimals} you should overload it. * * All two of these values are immutable: they can only be set once during * construction. */ constructor(string memory name_, string memory symbol_) { _name = name_; _symbol = symbol_; } /** * @dev Returns the name of the token. */ function name() public view virtual override returns (string memory) { return _name; } /** * @dev Returns the symbol of the token, usually a shorter version of the * name. */ function symbol() public view virtual override returns (string memory) { return _symbol; } /** * @dev Returns the number of decimals used to get its user representation. * For example, if `decimals` equals `2`, a balance of `505` tokens should * be displayed to a user as `5.05` (`505 / 10 ** 2`). * * Tokens usually opt for a value of 18, imitating the relationship between * Ether and Wei. This is the value {ERC20} uses, unless this function is * overridden; * * NOTE: This information is only used for _display_ purposes: it in * no way affects any of the arithmetic of the contract, including * {IERC20-balanceOf} and {IERC20-transfer}. */ function decimals() public view virtual override returns (uint8) { return 18; } /** * @dev See {IERC20-totalSupply}. */ function totalSupply() public view virtual override returns (uint256) { return _totalSupply; } /** * @dev See {IERC20-balanceOf}. */ function balanceOf(address account) public view virtual override returns (uint256) { return _balances[account]; } /** * @dev See {IERC20-transfer}. * * Requirements: * * - `to` cannot be the zero address. * - the caller must have a balance of at least `amount`. */ function transfer(address to, uint256 amount) public virtual override returns (bool) { address owner = _msgSender(); _transfer(owner, to, amount); return true; } /** * @dev See {IERC20-allowance}. */ function allowance(address owner, address spender) public view virtual override returns (uint256) { return _allowances[owner][spender]; } /** * @dev See {IERC20-approve}. * * NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on * `transferFrom`. This is semantically equivalent to an infinite approval. * * Requirements: * * - `spender` cannot be the zero address. */ function approve(address spender, uint256 amount) public virtual override returns (bool) { address owner = _msgSender(); _approve(owner, spender, amount); return true; } /** * @dev See {IERC20-transferFrom}. * * Emits an {Approval} event indicating the updated allowance. This is not * required by the EIP. See the note at the beginning of {ERC20}. * * NOTE: Does not update the allowance if the current allowance * is the maximum `uint256`. * * Requirements: * * - `from` and `to` cannot be the zero address. * - `from` must have a balance of at least `amount`. * - the caller must have allowance for ``from``'s tokens of at least * `amount`. */ function transferFrom( address from, address to, uint256 amount ) public virtual override returns (bool) { address spender = _msgSender(); _spendAllowance(from, spender, amount); _transfer(from, to, amount); return true; } /** * @dev Atomically increases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. */ function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) { address owner = _msgSender(); _approve(owner, spender, allowance(owner, spender) + addedValue); return true; } /** * @dev Atomically decreases the allowance granted to `spender` by the caller. * * This is an alternative to {approve} that can be used as a mitigation for * problems described in {IERC20-approve}. * * Emits an {Approval} event indicating the updated allowance. * * Requirements: * * - `spender` cannot be the zero address. * - `spender` must have allowance for the caller of at least * `subtractedValue`. */ function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) { address owner = _msgSender(); uint256 currentAllowance = allowance(owner, spender); require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero"); unchecked { _approve(owner, spender, currentAllowance - subtractedValue); } return true; } /** * @dev Moves `amount` of tokens from `sender` to `recipient`. * * This internal function is equivalent to {transfer}, and can be used to * e.g. implement automatic token fees, slashing mechanisms, etc. * * Emits a {Transfer} event. * * Requirements: * * - `from` cannot be the zero address. * - `to` cannot be the zero address. * - `from` must have a balance of at least `amount`. */ function _transfer( address from, address to, uint256 amount ) internal virtual { require(from != address(0), "ERC20: transfer from the zero address"); require(to != address(0), "ERC20: transfer to the zero address"); _beforeTokenTransfer(from, to, amount); uint256 fromBalance = _balances[from]; require(fromBalance >= amount, "ERC20: transfer amount exceeds balance"); unchecked { _balances[from] = fromBalance - amount; } _balances[to] += amount; emit Transfer(from, to, amount); _afterTokenTransfer(from, to, amount); } /** @dev Creates `amount` tokens and assigns them to `account`, increasing * the total supply. * * Emits a {Transfer} event with `from` set to the zero address. * * Requirements: * * - `account` cannot be the zero address. */ function _mint(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: mint to the zero address"); _beforeTokenTransfer(address(0), account, amount); _totalSupply += amount; _balances[account] += amount; emit Transfer(address(0), account, amount); _afterTokenTransfer(address(0), account, amount); } /** * @dev Destroys `amount` tokens from `account`, reducing the * total supply. * * Emits a {Transfer} event with `to` set to the zero address. * * Requirements: * * - `account` cannot be the zero address. * - `account` must have at least `amount` tokens. */ function _burn(address account, uint256 amount) internal virtual { require(account != address(0), "ERC20: burn from the zero address"); _beforeTokenTransfer(account, address(0), amount); uint256 accountBalance = _balances[account]; require(accountBalance >= amount, "ERC20: burn amount exceeds balance"); unchecked { _balances[account] = accountBalance - amount; } _totalSupply -= amount; emit Transfer(account, address(0), amount); _afterTokenTransfer(account, address(0), amount); } /** * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens. * * This internal function is equivalent to `approve`, and can be used to * e.g. set automatic allowances for certain subsystems, etc. * * Emits an {Approval} event. * * Requirements: * * - `owner` cannot be the zero address. * - `spender` cannot be the zero address. */ function _approve( address owner, address spender, uint256 amount ) internal virtual { require(owner != address(0), "ERC20: approve from the zero address"); require(spender != address(0), "ERC20: approve to the zero address"); _allowances[owner][spender] = amount; emit Approval(owner, spender, amount); } /** * @dev Updates `owner` s allowance for `spender` based on spent `amount`. * * Does not update the allowance amount in case of infinite allowance. * Revert if not enough allowance is available. * * Might emit an {Approval} event. */ function _spendAllowance( address owner, address spender, uint256 amount ) internal virtual { uint256 currentAllowance = allowance(owner, spender); if (currentAllowance != type(uint256).max) { require(currentAllowance >= amount, "ERC20: insufficient allowance"); unchecked { _approve(owner, spender, currentAllowance - amount); } } } /** * @dev Hook that is called before any transfer of tokens. This includes * minting and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens * will be transferred to `to`. * - when `from` is zero, `amount` tokens will be minted for `to`. * - when `to` is zero, `amount` of ``from``'s tokens will be burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _beforeTokenTransfer( address from, address to, uint256 amount ) internal virtual {} /** * @dev Hook that is called after any transfer of tokens. This includes * minting and burning. * * Calling conditions: * * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens * has been transferred to `to`. * - when `from` is zero, `amount` tokens have been minted for `to`. * - when `to` is zero, `amount` of ``from``'s tokens have been burned. * - `from` and `to` are never both zero. * * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks]. */ function _afterTokenTransfer( address from, address to, uint256 amount ) internal virtual {} }
5、主合约文件
pragma solidity =0.8.4; // import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; // import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; // import "@openzeppelin/contracts/access/Ownable.sol"; // import "contracts/interfaces/IUniswapV2Factory.sol"; // import "contracts/interfaces/IUniswapV2Router02.sol"; // import "contracts/interfaces/IUniswapV2Pair.sol"; // import "contracts/libs/SafeMath.sol"; contract CCDS is ERC20, Ownable { using SafeMath for uint256; bool private swapping; uint256 public startTime; bool public zeroVaultPeriod = false; IUniswapV2Router02 public uniswapV2Router; address public uniswapV2Pair; address public usdtAddr; uint256 public constant MAX_SUPPLY = 150000000 * 10**18; uint256 public constant LP_SUPPLY = 15000000 * 10**18; uint256 public constant VAULT_SUPPLY = 135000000 * 10**18; uint256 public constant LIMIT_AMOUNT = 100 * 10**18; uint256 public constant swapTokensAtAmount = 15 * 10 ** 18; uint256 private constant ONE_DAY = 18 hours; uint256 private constant THREE_DAYS = 24 hours; uint256 private constant ONE_YEAR = 365 days; address public vaultWallet; address public lpRewardsWallet; address public rewardsWallet; address public marketingWallet; address public creatorWallet; address public technologyWallet; address public supportRewardWallet; uint256 public burnFee = 20; uint256 public supportFee = 20; uint256 public rewardsFee = 15; uint256 public liquidityFee = 20; uint256 public marketingFee = 5; uint256 public creatorFee = 5; uint256 public technologyFee = 5; uint256 public totalFees = 50; uint256 public totalBurnedToken; uint256 public distributedToken; uint256 public stabilizeToken; uint256 public currentFeeTokens; mapping(address => bool) private _isExcludedFromFees; mapping(address => bool) public automatedMarketMakerPairs; mapping(address => uint256) public buyedTokens; event ExcludeFromFees(address indexed account, bool isExcluded); event ExcludeMultipleAccountsFromFees(address[] accounts, bool isExcluded); event SetAutomatedMarketMakerPair(address indexed pair, bool indexed value); event DistributeToken(address indexed wallet, uint256 amount, uint256 timestamp); constructor( string memory name_, string memory symbol_, address router_, address usdtAddr_, address[7] memory wallets ) payable ERC20(name_, symbol_) { vaultWallet = wallets[0]; lpRewardsWallet = wallets[1]; rewardsWallet = wallets[2]; marketingWallet = wallets[3]; creatorWallet = wallets[4]; technologyWallet = wallets[5]; supportRewardWallet = wallets[6]; usdtAddr = usdtAddr_; IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(router_); address _uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()) .createPair(address(this), usdtAddr); uniswapV2Router = _uniswapV2Router; uniswapV2Pair = _uniswapV2Pair; _setAutomatedMarketMakerPair(_uniswapV2Pair, true); excludeFromFees(owner(), true); excludeFromFees(address(this), true); excludeFromFees(vaultWallet, true); excludeFromFees(lpRewardsWallet, true); excludeFromFees(rewardsWallet, true); excludeFromFees(marketingWallet, true); excludeFromFees(creatorWallet, true); excludeFromFees(technologyWallet, true); excludeFromFees(supportRewardWallet, true); _mint(address(this), MAX_SUPPLY); } receive() external payable {} function addInitLiquidity(uint256 amount) external payable onlyOwner { require(startTime == 0, "only once"); startTime = block.timestamp; _approve(address(this), address(uniswapV2Router), LP_SUPPLY); IERC20(usdtAddr).approve(address(uniswapV2Router), amount); uniswapV2Router.addLiquidity( address(this), usdtAddr, LP_SUPPLY, amount, 0, 0, address(0), block.timestamp ); } function burn(uint256 amount) public { _burn(_msgSender(), amount); totalBurnedToken = totalBurnedToken.add(amount); } function excludeFromFees(address account, bool excluded) public onlyOwner { require( _isExcludedFromFees[account] != excluded, "Account is already the value of 'excluded'" ); _isExcludedFromFees[account] = excluded; emit ExcludeFromFees(account, excluded); } function excludeMultipleAccountsFromFees( address[] calldata accounts, bool excluded ) public onlyOwner { for (uint256 i = 0; i < accounts.length; i++) { _isExcludedFromFees[accounts[i]] = excluded; } emit ExcludeMultipleAccountsFromFees(accounts, excluded); } function setAutomatedMarketMakerPair(address pair, bool value) public onlyOwner { require( pair != uniswapV2Pair, "The PancakeSwap pair cannot be removed from automatedMarketMakerPairs" ); _setAutomatedMarketMakerPair(pair, value); } function _setAutomatedMarketMakerPair(address pair, bool value) private { require( automatedMarketMakerPairs[pair] != value, "Automated market maker pair is already set to that value" ); automatedMarketMakerPairs[pair] = value; emit SetAutomatedMarketMakerPair(pair, value); } function isExcludedFromFees(address account) public view returns (bool) { return _isExcludedFromFees[account]; } function distributeToken() external { require(totalBurnedToken > 10000000 * 10**18, "Less than ten million"); require(!zeroVaultPeriod, "Vault is zero"); uint256 duration = block.timestamp.sub(startTime); require(duration > THREE_DAYS, "Not distributed period"); uint256 remainder = VAULT_SUPPLY.sub(distributedToken); if(duration > ONE_YEAR) { super._burn(address(this), remainder); totalBurnedToken = totalBurnedToken.add(remainder); zeroVaultPeriod = true; }else { uint256 amount = totalBurnedToken.sub(LP_SUPPLY - stabilizeToken).sub(distributedToken); if(amount > 0) { distributedToken = distributedToken.add(amount); if(distributedToken >= VAULT_SUPPLY) { distributedToken = VAULT_SUPPLY; zeroVaultPeriod = true; } uint256 burnAmount = amount.mul(15).div(100); totalBurnedToken = totalBurnedToken.add(burnAmount); super._burn(address(this), burnAmount); amount = amount.sub(burnAmount); if(amount > remainder) { amount = remainder; } super._transfer(address(this), vaultWallet, amount); emit DistributeToken(vaultWallet, amount, block.timestamp); } } } function _transfer( address from, address to, uint256 amount ) internal override { require(from != address(0), "ERC20: transfer from the zero address"); require(to != address(0), "ERC20: transfer to the zero address"); if (amount == 0) { super._transfer(from, to, 0); return; } bool canSwap = currentFeeTokens > swapTokensAtAmount; if ( canSwap && !swapping && !automatedMarketMakerPairs[from] && !_isExcludedFromFees[from] ) { swapping = true; swapAndSendToWalletsFee(currentFeeTokens); currentFeeTokens = 0; swapping = false; } bool takeFee = !swapping; if (_isExcludedFromFees[from] || _isExcludedFromFees[to]) { takeFee = false; } if(takeFee) { if(automatedMarketMakerPairs[from] || automatedMarketMakerPairs[to]) { swapTokenAndFees(from, to, amount); } else if(!zeroVaultPeriod) { transferTokenAndFees(from, to, amount); }else{ super._transfer(from, to, amount); } } else { super._transfer(from, to, amount); } } function swapTokenAndFees( address from, address to, uint256 amount ) internal { uint256 burnAmount = 0; uint256 duration = block.timestamp.sub(startTime); if(duration < ONE_DAY){ if (automatedMarketMakerPairs[from]) { burnAmount = amount.mul(50).div(100); amount = amount.sub(burnAmount); buyedTokens[to] = buyedTokens[to].add(amount); require(buyedTokens[to] <= LIMIT_AMOUNT, "exceeds limit"); } else if(automatedMarketMakerPairs[to]) { burnAmount = amount.mul(7).div(100); amount = amount.sub(burnAmount); } } else if(duration < THREE_DAYS) { burnAmount = amount.mul(7).div(100); amount = amount.sub(burnAmount); if (automatedMarketMakerPairs[from]) { buyedTokens[to] = buyedTokens[to].add(amount); require(buyedTokens[to] <= LIMIT_AMOUNT, "exceeds limit"); } } else { if(stabilizeToken == 0) { if(totalBurnedToken < 1000 * 10**22) { stabilizeToken = 500 * 10**22; } else { stabilizeToken = LP_SUPPLY.sub(totalBurnedToken); } } if(zeroVaultPeriod) { burnAmount = amount.mul(2).div(1000); uint256 fees = amount.mul(18).div(1000); amount = amount.sub(burnAmount).sub(fees); super._transfer(from, supportRewardWallet, fees); } else if(totalBurnedToken < 1000 * 10**22) { burnAmount = amount.mul(7).div(100); amount = amount.sub(burnAmount); } else { burnAmount = amount.mul(burnFee).div(1000); uint256 fees = amount.mul(totalFees).div(1000); currentFeeTokens = currentFeeTokens.add(fees); amount = amount.sub(burnAmount).sub(fees); super._transfer(from, address(this), fees); } } super._burn(from, burnAmount); super._transfer(from, to, amount); totalBurnedToken = totalBurnedToken.add(burnAmount); } function transferTokenAndFees( address from, address to, uint256 amount ) internal { uint256 burnAmount = amount.mul(10).div(100); amount = amount.sub(burnAmount); super._burn(from, burnAmount); super._transfer(from, to, amount); totalBurnedToken = totalBurnedToken.add(burnAmount); } function swapAndSendToWalletsFee(uint256 tokenAmount) private { uint256 lpRewardsTokens = tokenAmount.mul(liquidityFee).div(totalFees); swapTokensForUSDT(lpRewardsTokens, lpRewardsWallet); uint256 marketingTokens = tokenAmount.mul(marketingFee).div(totalFees); swapTokensForUSDT(marketingTokens, marketingWallet); uint256 creatorTokens = tokenAmount.mul(creatorFee).div(totalFees); swapTokensForUSDT(creatorTokens, creatorWallet); uint256 technologyTokens = tokenAmount.mul(technologyFee).div(totalFees); swapTokensForUSDT(technologyTokens, technologyWallet); uint256 rewardsTokens = tokenAmount.mul(rewardsFee).div(totalFees); swapTokensForUSDT(rewardsTokens, rewardsWallet); } function swapTokensForUSDT(uint256 tokenAmount, address to) private { address[] memory path = new address[](2); path[0] = address(this); path[1] = usdtAddr; _approve(address(this), address(uniswapV2Router), tokenAmount); uniswapV2Router.swapExactTokensForTokensSupportingFeeOnTransferTokens( tokenAmount, 0, path, to, block.timestamp ); } function addLiquidity(uint256 tokenAmount, uint256 usdtAmount) private { _approve(address(this), address(uniswapV2Router), tokenAmount); IERC20(usdtAddr).approve(address(uniswapV2Router), usdtAmount); uniswapV2Router.addLiquidity( address(this), usdtAddr, tokenAmount, usdtAmount, 0, 0, address(0), block.timestamp ); } function getBlockTime() external view returns(uint256) { return block.timestamp; } }
参考合约地址:
至此,完成5%代币自动进入底池,95%依赖交易产出的合约代码实现。
pdf+视频币安智能链BSC发币教程及多模式组合合约源代码下载:
币安智能链BSC发币(合约部署、开源、锁仓、LP、参数配置、开发、故障处理、工具使用)教程下载:
多模式(燃烧、回流指定营销地址、分红本币及任意币种,邀请推广八代收益,LP加池分红、交易分红、复利分红、NFT分红、自动筑池、动态手续费、定时开盘、回购)组合合约源代码下载:
pdf+视频币安智能链BSC发币教程及多模式组合合约源代码下载地址:
添加VX或者telegram获取全程线上免费指导
评论前必须登录!
注册