L3

Sophia 智能合约

Æternity 的函数式智能合约语言

语言特性
函数式编程

纯函数、不可变数据、模式匹配

静态类型

编译时类型检查,减少运行时错误

FATE VM

高效虚拟机,Gas 消耗低

原生集成

内置 Oracle、AENS、State Channel 支持

语法基础
Hello World 合约
@compiler >= 7

contract HelloWorld =
  // 状态定义
  record state = { greeting : string }
  
  // 初始化函数
  entrypoint init() = { greeting = "Hello, Æternity!" }
  
  // 只读函数
  entrypoint greet() : string = state.greeting
  
  // 修改状态的函数
  stateful entrypoint set_greeting(new_greeting : string) =
    put(state{ greeting = new_greeting })
基础类型
类型说明示例
int任意精度整数42
bool布尔值true / false
stringUTF-8 字符串"hello"
address账户/合约地址ak_...
hash256-bit 哈希#abc...
list(a)列表[1, 2, 3]
map(k, v)映射{["a"] = 1}
option(a)可选值Some(1) / None
AEX 标准接口
AEX-9 (同质化代币)

类似 ERC-20 的代币标准

entrypoint total_supply() : int
entrypoint balance(owner: address) : int
stateful entrypoint transfer(to: address, amount: int)
stateful entrypoint approve(spender: address, amount: int)
AEX-9 实战
AEX-141 (NFT)

非同质化代币标准

entrypoint owner_of(token_id: int) : address
entrypoint balance_of(owner: address) : int
stateful entrypoint transfer(to: address, token_id: int)
stateful entrypoint mint(to: address, metadata: string)
规范
学习路径
L3 Sophia (当前)