使用说明
Agoric智能合约脚手架
一个模板项目,展示了构建具有编排流和持久状态的Agoric智能合约的最佳实践。
项目结构
src/
contract.js # Main contract entry point
flows.js # Orchestration flows
exos/
greeter-kit.js # Durable ExoClassKit example
utils/
helpers.js # Pure utility functions
zoe.js # Zoe-related utilities
typeGuards.js # Endo pattern shapes and interfaces
test/
contract.test.js # Contract integration tests
supports.js # Test setup utilities
exos/
greeter-kit.test.js # Unit tests for the kit
关键概念演示
1.合同结构
合同遵循编排模式:
export const contract = async (zcf, privateArgs, zone, tools) => {
// Contract setup using zone for durable state
// Returns { creatorFacet, publicFacet }
};
export const start = withOrchestration(contract);
2.ExoClassKit模式
演示如何创建具有多个方面的持久对象:
- 持有人:主要功能(问候)
- 邀请制造者:为基于能力的访问创建Zoe邀请
- 管理员:行政控制
export const prepareGreeterKit = (zone, powers) => {
return zone.exoClassKit('GreeterKit', GreeterKitI, initFn, {
holder: { /* methods */ },
invitationMakers: { /* methods */ },
admin: { /* methods */ },
});
};
3.类型防护装置和图案
使用Endo模式进行运行时类型验证:
export const GreeterKitI = harden({
holder: M.interface('GreeterHolder', {
greet: M.call(M.string()).returns(GreetingResultShape),
}),
// ...
});
4.编排流程
演示如何定义异步编排流:
export const makeLocalAccount = async (orch) => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
};
5.佐伊邀请
演示如何创建基于功能的访问邀请:
const greetHandler = (seat, { name }) => {
const result = mainGreeter.holder.greet(name);
seat.exit();
return result;
};
return zcf.makeInvitation(greetHandler, 'greet');
运行测试
# Run all tests
yarn test
# Run with coverage
yarn test:c8
# Run linting
yarn lint
# Fix linting issues
yarn lint-fix
按键模式
持久状态
状态必须被强化,数组不能就地突变:
// Wrong - will fail
state.history.push(name);
// Correct - replace the array
state.history = harden([...state.history, name]);
基于区域的Singleton
使用 zone.makeOnce 对于耐用的单身汉:
const mainGreeter = zone.makeOnce('MainGreeter', () =>
makeGreeterKit('main', greeting),
);
惰性邀请
使用惰性邀请作为已完成操作的收据:
const makeInertInvitation = defineInertInvitation(zcf, 'action performed');
文件责任
| 文件 | 目的 |
|---|---|
contract.js |
合约设置、方面定义、初始化 |
flows.js |
编排流功能 |
exos/greeter-kit.js |
耐用的exoClassKit,带有刻面 |
utils/helpers.js |
纯实用功能 |
utils/zoe.js |
Zoe相关实用程序 |
typeGuards.js |
类型验证模式 |
依赖项
@agoric/orchestration-跨链编排@agoric/zoe-智能合约框架@agoric/zone-持久状态管理@endo/patterns-运行时类型验证@endo/errors-错误处理实用程序
许可证
阿帕奇-2.0
