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
handle
function MUST accept exactly two arguments:state
andaction
- The
state
argument MUST be the current state of the contract. - The
action
argument MUST containcaller
field (i.e. Arweave wallet address) andinput
field. Theinput
field MUST contain thefunction
field and any additional data required to perform given operation. - The maximum size of the JSON.stringified representation of the
input
field MUST NOT exceed 2048 bytes. - The
handle
function 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
和action
state
参数必须是合约当前的状态(state)action
参数必须包含caller
字段(例如 Arweave 钱包地址) 和input
字段。input
字段必须包含function
字段,以及其它操作所需的附加数据。
开发过程中需要注意:
7. input
字段是字符串化的JSON对象,有体积限制,不能超过 2048 bytes(即2KB.)
8. handle
函数必须以以下几种方式结束:
1. 返回 { state: newState }
。这会更新合约的状态(state)。
2. 返回 { result: newResult }
3. 抛出错误 ContractError