¥
立即购买

智能合约注释生成专家

39 浏览
2 试用
0 购买
Dec 1, 2025更新

本提示词专为区块链开发场景设计,能够为智能合约函数生成专业、准确的技术注释。通过深度分析合约代码逻辑和业务功能,自动生成结构清晰、内容完整的注释文档,涵盖函数功能、参数说明、返回值解释等关键要素,帮助开发者更好地理解和维护智能合约代码,提升代码可读性和团队协作效率。

以下为合约中各函数的专业技术注释(标准格式),基于代码实际逻辑分析撰写。

函数:transfer(address to, uint256 amount)

  • 函数功能概述:
    • 将调用者(msg.sender)的指定数量代币转账给目标地址to,并触发Transfer事件。
  • 参数说明:
    • to (address):接收代币的地址,必须为非零地址。
    • amount (uint256):转账数量(单位为代币最小计量单位)。
  • 返回值解释:
    • bool:恒为true,表示执行成功。
  • 业务逻辑:
    • 调用内部函数_transfer(msg.sender, to, amount)执行余额扣减与增加。
    • 不修改授权额度_allowances,也不影响totalSupply。
    • 成功后由内部函数触发Transfer事件。
  • 注意事项:
    • 若to为零地址则回退(错误信息:"zero address")。
    • 若调用者余额不足则回退(错误信息:"insufficient balance")。
    • 支持自转账(from == to),余额净效为0,但仍会发出Transfer事件。
    • 由于内部使用unchecked进行算术操作,加法存在理论溢出风险(见_transfer说明),应确保不会出现极端溢出情形。
    • 函数不包含手续费、黑名单、暂停等控制逻辑。

函数:approve(address spender, uint256 amount)

  • 函数功能概述:
    • 设置/覆盖调用者(msg.sender)授予spender的可用代币额度为amount,并触发Approval事件。
  • 参数说明:
    • spender (address):被授权可代扣代币的地址。
    • amount (uint256):授权额度。
  • 返回值解释:
    • bool:恒为true,表示执行成功。
  • 业务逻辑:
    • 直接写入_allowances[msg.sender][spender] = amount。
    • 触发Approval(msg.sender, spender, amount)事件。
  • 注意事项:
    • 未限制spender为零地址;对零地址授权通常无实际用途,但不会回退。
    • 存在经典的ERC20“非原子修改授权”前置交易风险:从非零额度直接修改为另一非零额度可能被前置交易利用。建议先将额度设为0,再设置新额度;或提供increase/decreaseAllowance(本合约未提供)。
    • 不校验当前余额,授权与实际余额无关。

函数:transferFrom(address from, address to, uint256 amount)

  • 函数功能概述:
    • 在授权机制下,由调用者(msg.sender)从from地址向to地址转账amount,成功后扣减调用者对from的授权额度。
  • 参数说明:
    • from (address):被代扣的地址(代币持有人)。
    • to (address):接收代币的地址,必须为非零地址。
    • amount (uint256):转账数量。
  • 返回值解释:
    • bool:恒为true,表示执行成功。
  • 业务逻辑:
    • 读取当前授权currentAllowance = _allowances[from][msg.sender]。
    • 要求currentAllowance >= amount,否则回退(错误信息:"allowance too low")。
    • 扣减授权:_allowances[from][msg.sender] = currentAllowance - amount(在require保证下安全)。
    • 调用内部函数_transfer(from, to, amount)进行余额变更与事件触发。
  • 注意事项:
    • 不触发授权变更(Approval)事件来同步扣减后的授权额度,这与部分实现(会在transferFrom中发Approval事件)不同,可能影响依赖事件追踪授权的索引服务。
    • 未实现“无限授权”(如type(uint256).max时不扣减)的优化逻辑,本实现会始终扣减额度。
    • 未限制from为零地址;若零地址存在余额且已授权,则可被转出(通常生产级ERC20会避免零地址持有余额)。
    • 受_transfer中unchecked算术的潜在溢出注意(见_transfer说明)。
    • to为零地址将回退("zero address");from余额不足将回退("insufficient balance")。

函数:balanceOf(address account)

  • 函数功能概述:
    • 查询指定账户的当前代币余额。
  • 参数说明:
    • account (address):被查询的账户地址。
  • 返回值解释:
    • uint256:account地址的当前余额。
  • 业务逻辑:
    • 直接返回_balances[account],为视图函数,不修改状态。
  • 注意事项:
    • 支持查询任意地址(包括零地址)的余额。
    • 返回值未与totalSupply进行任何一致性校验(本合约片段未展示铸造/销毁等逻辑)。

函数:_transfer(address from, address to, uint256 amount) [internal]

  • 函数功能概述:
    • 执行内部转账的核心逻辑:校验、更新余额并触发Transfer事件。
  • 参数说明:
    • from (address):扣款地址。
    • to (address):收款地址,必须为非零地址。
    • amount (uint256):转账数量。
  • 返回值解释:
    • 无返回值。
  • 业务逻辑:
    • 校验to != address(0),否则回退("zero address")。
    • 读取from当前余额bal = _balances[from]。
    • 要求bal >= amount,否则回退("insufficient balance")。
    • 在unchecked块中执行:
      • _balances[from] = bal - amount;
      • _balances[to] += amount;
    • 触发Transfer(from, to, amount)事件。
  • 注意事项:
    • 算术在unchecked块中执行:在已校验余额充足的前提下,from侧不会下溢;但to侧的加法存在理论上溢出风险(若to余额接近uint256上限)。通常应通过总量不超过uint256上限且无异常铸造来保证安全,或避免对加法使用unchecked。
    • 未限制from为零地址;若零地址存在余额,则可被转出。
    • 允许自转账(from == to),净余额不变但仍会触发事件。
    • 不修改totalSupply,也未包含钩子(如before/after transfer)、暂停、黑名单等控制逻辑。

Function: deposit(uint64 lockSeconds) external payable returns (uint256 id)

  • Function Overview:

    • Accepts ETH and creates a time-locked deposit for the caller. The deposit is identified by a per-user index (id) and becomes withdrawable after the specified lock duration.
  • Parameters:

    • lockSeconds (uint64): Number of seconds to lock the deposited ETH. A value of 0 means the deposit is immediately withdrawable.
  • Return Values:

    • id (uint256): The zero-based index of this deposit within deposits[msg.sender]. It is unique per user but not globally unique.
  • Business Logic:

    • Validates that msg.value > 0; otherwise reverts with "no value".
    • Computes unlockAt = block.timestamp + lockSeconds and stores:
      • amount: uint128(msg.value)
      • unlockAt: uint64(block.timestamp + lockSeconds)
      • withdrawn: false
    • Appends the new record to deposits[msg.sender] and returns its index.
    • Emits Deposited(user=msg.sender, id, amount=msg.value, unlockAt).
  • Notes and Constraints:

    • The amount is stored as uint128. Extremely large deposits that do not fit into 128 bits would be truncated in Solidity; while practically unreachable on mainnet due to supply limits, callers should assume the system expects typical ETH-sized values.
    • unlockAt is stored as uint64. The sum block.timestamp + lockSeconds is cast to uint64; theoretical wrap-around would require astronomically large values.
    • The lock uses block.timestamp, which is subject to minor miner/validator manipulation; this is generally acceptable for timelocks but should be noted.
    • There is no upper bound or minimum on lockSeconds enforced by the contract; application-level validation can be added if needed.
    • The id is local to each user (deposits[msg.sender]); it is not a global counter.

Function: withdraw(uint256 id, address payable to) external nonReentrant

  • Function Overview:

    • Withdraws the caller’s matured deposit identified by id and sends the ETH to the specified recipient address.
  • Parameters:

    • id (uint256): Index of the caller’s deposit in deposits[msg.sender]. If out of bounds, the call reverts due to array indexing.
    • to (address payable): Recipient address for the withdrawn ETH. Can be any payable address.
  • Return Values:

    • None.
  • Business Logic:

    • Loads the deposit at deposits[msg.sender][id]; out-of-range indices revert automatically.
    • Requires the deposit has not been withdrawn; otherwise reverts with "already withdrawn".
    • Requires the current timestamp >= unlockAt; otherwise reverts with "still locked".
    • Marks the deposit as withdrawn and zeroes the stored amount (checks-effects-interactions).
    • Performs a low-level call to to with the withdrawn amount: (bool ok, ) = to.call{value: amount}("");
    • Reverts with "send failed" if the transfer fails (the state changes are rolled back on failure).
    • Emits Withdrawn(user=msg.sender, id, amount, to).
  • Notes and Constraints:

    • Reentrancy:
      • The function is protected by the nonReentrant modifier, and state is updated before the external call, adhering to CEI. This mitigates reentrancy even though .call forwards all remaining gas to the recipient.
    • Recipient address:
      • to can be any address, including contracts and address(0). Sending to address(0) is allowed and effectively burns the funds; callers should ensure the correctness of the recipient address.
    • Access control:
      • Only the depositor (msg.sender) can withdraw their own deposits; one cannot withdraw deposits belonging to other addresses.
    • Partial withdrawals:
      • Not supported. Each deposit is all-or-nothing; once withdrawn, it is marked as withdrawn and the amount is set to 0.
    • Error behavior:
      • If the external call fails, the entire transaction reverts and the deposit remains unchanged (not marked withdrawn, amount not zeroed) due to atomicity of state changes on revert.

以下为合约中两个函数的专业技术注释(简洁格式):

函数:castVote(uint256 id, bool support, uint128 weight)

  • 函数功能概述:
    • 记录投票:根据调用者传入的支持/反对标志,将对应提案的赞成票或反对票权重累加。
  • 参数说明:
    • id (uint256):提案标识,对应 proposals 映射的键。若该 id 尚未初始化,将使用默认值 Proposal 结构(proposer=address(0)、startBlock=0、endBlock=0、票数为0)。
    • support (bool):投票倾向;true 为赞成票,false 为反对票。
    • weight (uint128):本次投票的权重,将直接累加到对应的票数字段。
  • 返回值解释:
    • 无返回值。
  • 业务逻辑:
    • 若 support 为 true,则将 weight 累加到 proposals[id].forVotes;否则累加到 proposals[id].againstVotes。
    • 不做任何权限、时间窗口或提案有效性检查。
  • 注意事项:
    • 无时间校验:未使用 Proposal 的 startBlock/endBlock,任何区块均可投票(包括投票期外)。
    • 无重复投票防护:未记录投票人或投票状态;同一地址可多次调用累积权重。
    • 权重来源未校验:weight 完全由外部传入,未与代币余额/快照/委托等机制绑定;需要上层逻辑确保权重的正确性与唯一性。
    • 溢出与回退:forVotes/againstVotes 为 uint128;Solidity 0.8+ 下若累加超过 uint128 上限将导致交易回退。
    • 提案存在性:对于未初始化的 id 也可累计票数,可能导致“空提案”被后续结算;需在外部流程中规范提案创建与校验。

函数:finalizeProposal(uint256 id)

  • 函数功能概述:
    • 结算提案:在投票结束后,根据总投票数与法定人数(quorum)判断提案是否通过,标记为已执行并触发事件。
  • 参数说明:
    • id (uint256):提案标识,对应 proposals 映射的键。
  • 返回值解释:
    • 无返回值。通过事件 Finalized(id, passed) 对外公布结算结果。
  • 业务逻辑:
    • 读取存储中的 Proposal p = proposals[id]。
    • 要求当前区块号大于 p.endBlock;要求 p.executed 为 false。
    • 计算 total = uint256(p.forVotes) + uint256(p.againstVotes)。
    • 计算法定人数是否达成:quorum = (total * 100) >= (totalVotingPower * quorumPercent)。
    • 判定是否通过:passed = quorum && (p.forVotes > p.againstVotes)。
    • 将 p.executed 置为 true。
    • 若通过,预留执行提案相关动作的位置(代码占位)。
    • 触发事件 Finalized(id, passed)。
  • 注意事项:
    • 起止时间校验不完整:仅检查结束块 endBlock,未检查 startBlock;配合 castVote 的“无时间限制”,可能出现未开始即投票或 endBlock=0 时可立即结算的情况。
    • 提案存在性:对未初始化的提案(默认值,endBlock=0)在区块号>0时可被结算;应在 finalize 前校验 proposer、元数据或专门的存在标记。
    • 法定人数参数约定:quorumPercent 视为百分比(建议范围 0–100),但合约未强制限制;若设置>100,将提高达标门槛;若 totalVotingPower=0,则 quorum 恒为真,可能导致任意票数即可达标。
    • 票数比较策略:使用严格大于号(forVotes > againstVotes),平票视为未通过。
    • 溢出与安全:forVotes/againstVotes 为 uint128,提升为 uint256 后参与运算;total*100 在当前票数范围内安全。若 totalVotingPower 与 quorumPercent 极端偏大,右侧乘法可能接近上限,建议在设置时施加范围约束或改用更稳健的比例计算。
    • 交互顺序与重入:先标记 executed 再执行外部动作(占位),遵循检查-效果-交互模式。实际接入外部调用时建议加入重入保护与失败处理策略。
    • 事件与可观测性:仅在 finalize 时触发事件;castVote 无事件,可能影响离线索引与审计。建议为投票行为增加事件以提升可追溯性。

示例详情

解决的问题

把零散的智能合约代码,快速转化为“可读、可审、可协作”的专业注释。通过一条高质量提示词,让 AI 以合约注释专家的视角工作:自动识别函数意图、提炼业务逻辑、补充参数与返回值说明、标注安全与约束要点,并统一注释风格与语言。适用于新功能开发、遗留代码补文档、审计前梳理、开源仓库规范化与团队交接等场景,帮助团队在更短时间内产出更高质量的注释,提升代码评审效率、降低沟通成本,推动审计与上线进度。使用方式简单:粘贴合约代码,选择注释语言与风格,即刻生成可直接粘贴入库的标准化注释,预期节省大量文档人力并显著提升协作体验。

适用用户

区块链后端开发工程师

在提交合约前快速补齐函数注释与参数说明,明确返回含义与边界条件,缩短评审周期并减少反复修改

智能合约审计工程师

审计前自动梳理函数意图、业务约束与潜在风险点,形成清晰注释基础,提升沟通效率与审计报告质量

开源项目维护者

为新PR与合并代码生成统一风格注释,多语言输出便于全球贡献者理解,显著提升项目可读性与贡献门槛

特征总结

一键为合约函数生成结构化注释,覆盖功能概述、参数含义、返回解释,团队快速对齐理解
自动识别业务逻辑并用通俗表述展现流程,帮助新人上手与评审讨论更聚焦
内置安全注意提示,自动标注边界条件与潜在风险,减少上线事故与审计返工
支持多语言注释与风格定制,满足开源与内部代码库的不同规范需求
针对旧代码一键补文档,智能回溯变量与调用关系,提升维护与交接效率
生成可复制的标准段落模板,方便团队统一注释口径,降低跨协作沟通成本
适配主流Solidity写法与常见合约模式,自动补充必要说明,提升可读性
与代码审查流程结合,评审时即时补注释与风险点记录,提升合规透明度

如何使用购买的提示词模板

1. 直接在外部 Chat 应用中使用

将模板生成的提示词复制粘贴到您常用的 Chat 应用(如 ChatGPT、Claude 等),即可直接对话使用,无需额外开发。适合个人快速体验和轻量使用场景。

2. 发布为 API 接口调用

把提示词模板转化为 API,您的程序可任意修改模板参数,通过接口直接调用,轻松实现自动化与批量处理。适合开发者集成与业务系统嵌入。

3. 在 MCP Client 中配置使用

在 MCP client 中配置对应的 server 地址,让您的 AI 应用自动调用提示词模板。适合高级用户和团队协作,让提示词在不同 AI 工具间无缝衔接。

AI 提示词价格
¥20.00元
先用后买,用好了再付款,超安全!

您购买后可以获得什么

获得完整提示词模板
- 共 500 tokens
- 3 个可调节参数
{ 合约代码 } { 注释语言 } { 注释风格 }
获得社区贡献内容的使用权
- 精选社区优质案例,助您快速上手提示词
使用提示词兑换券,低至 ¥ 9.9
了解兑换券 →
限时半价

不要错过!

半价获取高级提示词-优惠即将到期

17
:
23
小时
:
59
分钟
:
59