Arweave 智能合约代码的规范

目录

先是引用。然后是翻译。

以下英文引用自 warp docs

  1. A contract source code MAY be written in ES module format.
  2. A contract source MUST contain function (sync or async) named handle.
  3. A contract source MAY use IIFE bundling format.
  4. The handle function MUST accept exactly two arguments: state and action
  5. The state argument MUST be the current state of the contract.
  6. The action argument MUST contain caller field (i.e. Arweave wallet address) and input field. The input field MUST contain the function field and any additional data required to perform given operation.
  7. The maximum size of the JSON.stringified representation of the input field MUST NOT exceed 2048 bytes.
  8. The handle function MUST terminate by either:
    1. returning { state: newState } - this will cause the contract state to be updated
    2. returning { result: newResult }
    3. throwing ContractError

以下是我的翻译:

  1. 用 Javascript ES 模块规范写合约源码
  2. 合约代码必须包含一个名称 handle 的函数(同步或异步)
  3. 可以使用 IIFE (Immediately Invoked Function Expression,意为立即调用的函数表达式)
  4. handle 函数必须接收且仅接收两个传入参数:stateaction
  5. state 参数必须是合约当前的状态(state)
  6. action 参数必须包含 caller 字段(例如 Arweave 钱包地址) 和 input 字段。input 字段必须包含 function 字段,以及其它操作所需的附加数据。

开发过程中需要注意:
7. input 字段是字符串化的JSON对象,有体积限制,不能超过 2048 bytes(即2KB.)
8. handle 函数必须以以下几种方式结束:
1. 返回 { state: newState }。这会更新合约的状态(state)。
2. 返回 { result: newResult }
3. 抛出错误 ContractError