UNPKG

bufan-hardhat-strip-comments

Version:

Hardhat plugin to strip comments from contracts during verification with custom Dubai ASCII art headers

139 lines (104 loc) 4.48 kB
# hardhat-strip-comments-plugin Hardhat插件,用于在验证合约时去除注释并支持自定义头部样式注释。 ## 功能 - 去除合约代码中的所有注释 - 支持两种预定义的ASCII艺术头部样式:迪拜风格和标准风格 - 自动提取合约名称 - 允许添加自定义头部注释(例如版权声明) - 支持保留SPDX许可证标识符 - 可以作为独立任务运行或自动与verify任务集成 - 支持排除特定文件 ## 安装 ### 第1步:安装依赖 ```bash npm install strip-comments --save-dev ``` ### 第2步:复制插件代码 将本插件目录下的`index.js`文件复制到您的项目中,例如放在`plugins/hardhat-strip-comments-plugin.js`路径下。 ### 第3步:在Hardhat配置中导入插件 在您的`hardhat.config.js`文件中添加: ```javascript // 导入插件 require("./plugins/hardhat-strip-comments-plugin"); module.exports = { // ... 其他配置 ... // 插件配置 stripComments: { enabled: true, headerStyle: "dubai", // 可选值: "dubai" 或 "standard" authorInfo: { author: "VeloraChain", createdDate: "2025-05-11", purpose: "VeloraChain Blockchain Technology Token", description: "This is no ordinary code — it carries ambition, and marks a legend on-chain." }, excludedFiles: ["某些不需要处理的文件.sol"], preserveLicense: true }, // ... 其他配置 ... }; ``` ## 使用方法 ### 方法1:作为独立任务运行 您可以手动运行任务来处理合约文件: ```bash npx hardhat strip-comments --input contracts/MarsTokenOptimized.sol --output stripped-contracts ``` ### 方法2:与验证任务集成 插件会自动与`verify`任务集成。当您运行验证任务时,它会自动处理合约文件: ```bash npx hardhat verify --network bscmainnet 0x合约地址 "构造函数参数" ``` 或者指定合约名称和路径: ```bash npx hardhat verify --network bscmainnet --contract "contracts/MarsTokenOptimized.sol:MarsTokenOptimized" 0x合约地址 "构造函数参数" ``` ## 配置选项`hardhat.config.js`中,您可以设置以下选项: | 选项 | 类型 | 默认值 | 说明 | |------|------|--------|------| | `enabled` | Boolean | `true` | 是否启用插件 | | `headerStyle` | String | `"dubai"` | 头部样式,可选值: "dubai""standard" | | `customHeader` | String | `"// SPDX-License-Identifier: MIT"` | 自定义头部(如果不使用预定义样式) | | `excludedFiles` | Array | `[]` | 不处理的文件列表 | | `preserveLicense` | Boolean | `true` | 是否保留SPDX许可证标识符 | | `authorInfo.author` | String | `"VeloraChain"` | 作者信息 | | `authorInfo.createdDate` | String | `"2025-05-11"` | 创建日期 | | `authorInfo.purpose` | String | `"VeloraChain Blockchain Technology Token"` | 项目目的 | | `authorInfo.description` | String | `"This is no ordinary code — it carries ambition, and marks a legend on-chain."` | 项目描述 | ## 头部样式示例 ### 迪拜风格 (dubai) ``` // SPDX-License-Identifier: MIT /* 🌴🏜️ 𓆃 DUBAI SMART CONTRACT 𓆃 🏜️🌴 ──────────────────────────────── "Built beneath the sun, coded for fortune, destined for legend." Contract Name : YourContractName Author : VeloraChain Created Date : 2025-05-11 Purpose : VeloraChain Blockchain Technology Token Description : This is no ordinary code — it carries ambition, and marks a legend on-chain. */ ``` ### 标准风格 (standard) ``` // SPDX-License-Identifier: MIT /* ___ _ _ _ | _ \__ _ _| |_ (_)_ _ __ _ ___ __| |___ | _/ _` | | ' \| | ' \/ _` / -_)/ _` (_-< |_| \__,_|_|_||_|_|_||_\__, \___|\_\_,/__/ |___/ Contract Name : YourContractName Author : VeloraChain Created Date : 2025-05-11 Purpose : VeloraChain Blockchain Technology Token Description : This is no ordinary code — it carries ambition, and marks a legend on-chain. */ ``` ## 注意事项 - 处理后的文件会保存在输出目录(默认为`stripped-contracts`)中 - 该插件不会修改原始文件 - 验证时会自动使用处理后的无注释版本