Arweave 智能合约代码的规范
目录
先是引用。然后是翻译。
以下英文引用自 warp docs
Contract source code specification
- A contract source code MAY be written in ES module format.
- A contract source MUST contain function (sync or async) named
handle. - A contract source MAY use IIFE bundling format.
- The
handlefunction MUST accept exactly two arguments:stateandaction - The
stateargument MUST be the current state of the contract. - The
actionargument MUST containcallerfield (i.e. Arweave wallet address) andinputfield. Theinputfield MUST contain thefunctionfield and any additional data required to perform given operation. - The maximum size of the JSON.stringified representation of the
inputfield MUST NOT exceed 2048 bytes. - The
handlefunction MUST terminate by either:- returning
{ state: newState }- this will cause the contract state to be updated - returning
{ result: newResult } - throwing
ContractError
- returning
以下是我的翻译:
合约源码规范
- 用 Javascript ES 模块规范写合约源码
- 合约代码必须包含一个名称
handle的函数(同步或异步) - 可以使用 IIFE (Immediately Invoked Function Expression,意为立即调用的函数表达式)
handle函数必须接收且仅接收两个传入参数:state和actionstate参数必须是合约当前的状态(state)action参数必须包含caller字段(例如 Arweave 钱包地址) 和input字段。input字段必须包含function字段,以及其它操作所需的附加数据。
开发过程中需要注意:
7. input 字段是字符串化的JSON对象,有体积限制,不能超过 2048 bytes(即2KB.)
8. handle 函数必须以以下几种方式结束:
1. 返回 { state: newState }。这会更新合约的状态(state)。
2. 返回 { result: newResult }
3. 抛出错误 ContractError