{"content":{"title":"让 EVM 再次伟大，用智能合约保证 MEME 的安全","body":"# 让 EVM 再次伟大，用智能合约保证 MEME 的安全\r\n\r\n## 备注\r\n\r\n时间：2024 年 3 月 19 日\r\n\r\n作者：[33357](https://github.com/33357)\r\n\r\n## 正文\r\n\r\n最近 MEME 爆火，但都 2024 年了，为什么还在用给普通账号打钱的方式做 MEME？是嫌钱太多跑路太慢吗？用智能合约可以完成 MEME 从发行到上市的全部流程，而且不可能跑路。智能合约是一个伟大的发明，无脑 FUD 不可取，时间会证明 EVM 的伟大！\r\n\r\n### 起一个名字比如 AVAXMEME，设定期限为 3 天，目标额度为 1000 AVAX，完不成退款\r\n```javascript\r\n    // deadline 为 3 天后\r\n    uint256 public immutable deadline = block.timestamp + 3 days;\r\n    // 退款时间为 deadline 之后 1 小时\r\n    uint256 public immutable refundTime = block.timestamp + 3 days + 1 hours;\r\n    // 目标额度为 1000 AVAX\r\n    uint256 public immutable targetAmount = 1000 ether;\r\n    // LP 是否开启，默认为 false\r\n    bool public LPopen;\r\n    // WAVAX\r\n    IWETH WAVAX = IWETH(0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7);\r\n    // 创建 uniswapV2 的池子\r\n    address pair = IUniswapV2Factory(0x9e5A52f57b3038F1B8EeE45F28b3C1967e22799C).createPair(\r\n        address(this),\r\n        address(WAVAX)\r\n    );\r\n\r\n    // 全称为 AVAXMEME，符号为 AVME\r\n    constructor() ERC20(\"AVAXMEME\", \"AVME\") {}\r\n```\r\n\r\n### 接受 AVAX 转账必须在 1-100 之间，过期不候，冲 1 个 AVAX 送 10000 个 AVAXMEME，单个账户最高额度 100 AVAX\r\n```javascript\r\n    receive() external payable {\r\n        // 不能低于 1 AVAX\r\n        require(msg.value >= 1 ether, \"less than 1 AVAX\");\r\n        // 不能高于 100 AVAX\r\n        require(msg.value <= 100 ether, \"greater than 100 AVAX\");\r\n        // 要在截止日期之前\r\n        require(block.timestamp < deadline, \"deadline reached\");\r\n        // 1 个 AVAX 送 10000 个 AVAXMEME\r\n        _mint(msg.sender, 10000 * msg.value);\r\n        // 单个账户额度不能高于 100 AVAX\r\n        require(balanceOf(msg.sender) <= 10000 * 100 ether, \"reached limit of 100 AVAX\");\r\n    }\r\n```\r\n\r\n### LP 开启之前禁止转账\r\n```javascript\r\n    function _transfer(address from, address to, uint256 amount) override internal {\r\n        // LPopen 为 true 才能转账\r\n        require(LPopen, \"too early\");\r\n        super._transfer(from, to, amount);\r\n    }\r\n```\r\n\r\n### 退款时间到没有开启 LP，退回 AVAX，支持主动退款和批量退款\r\n```javascript\r\n    function _refund(address sender) internal {\r\n        // 需要在 refundTime 之后\r\n        require(block.timestamp >= refundTime, \"wait refundTime\");\r\n        // 不能在 LP 开启后\r\n        require(!LPopen, \"LP opened\");\r\n        uint256 balance = balanceOf(sender);\r\n        // 回收 AVAXMEME\r\n        _burn(sender, balance);\r\n        // 退回 AVAX\r\n        payable(sender).transfer(balance / 10000);\r\n    }\r\n\r\n    function refund() external {\r\n        // 给自己退款\r\n        _refund(msg.sender);\r\n    }\r\n\r\n    function batchRefund(address[] memory senderList) external {\r\n        for (uint256 i; i < senderList.length; i++) {\r\n            // 批量退款\r\n            _refund(senderList[i]);\r\n        }\r\n    }\r\n```\r\n\r\n### 截止日期之后额度达标可以开启 LP，添加池子流动性为所有 AVAX + 1/4 总量的 AVAXMEME，LP 全部燃烧\r\n```javascript\r\n    function openLP() external {\r\n        // deadline 之前不能开启 LP\r\n        require(block.timestamp >= deadline,\"wait deadline\");\r\n        // 不能在 LP 开启后\r\n        require(!LPopen, \"LP opened\");\r\n        uint256 amountWAVAX = address(this).balance;\r\n        // 达不到目标额度不能开启 LP\r\n        require(amountWAVAX >= targetAmount, \"target not reached\");\r\n        // AVAX 换成 WAVAX\r\n        WAVAX.deposit{value: amountWAVAX}();\r\n        // 添加池子流动性，所有 WAVAX + 1/4 总量的 AVAXMEME\r\n        WAVAX.transfer(pair, amountWAVAX);\r\n        _mint(pair, totalSupply() / 4);\r\n        // 燃烧 LP\r\n        IUniswapV2Pair(pair).mint(address(0));\r\n        // LPopen 变成 true\r\n        LPopen = true;\r\n    }\r\n```\r\n\r\n## 结语\r\n\r\n完整代码已经部署在 AVAX 地址 https://snowtrace.io/address/0x09515534BdB84dc2Fa79C7a537F00a60Ca4bd693 ，只要正确使用智能合约，MEME 就不可能跑路，对代码而不是某个人的信任会让 EVM 再次伟大。\r\n\r\n\r\n## 原文发布在 <https://github.com/33357/smartcontract-apps> 这是一个面向中文社区，分析市面上智能合约应用的架构与实现的仓库。欢迎关注开源知识项目！"},"author":{"user":"https://learnblockchain.cn/people/3877","address":"0x1f2479ee1b4aFE789e19D257D2D50810ac90fa59"},"history":null,"timestamp":1710852202,"version":1}