@ calimero-network/abi-codegen (中文(简体) )
一个用于 Calimero 应用的 WASM-ABI v1 解析器和代码生成器 .
安装
npm install @calimero-network/abi-codegen
使用量
国 际
# Basic usage
npx calimero-abi-codegen -i abi.json -o src
# With custom client name
npx calimero-abi-codegen -i abi.json -o src --client-name MyClient
# Derive the client name from a file path (e.g. the .wasm binary)
npx calimero-abi-codegen -i abi.json -o src --name-from kv_store.wasm
# Point generated imports at a different runtime package
npx calimero-abi-codegen -i abi.json -o src --import-path @calimero-network/mero-js
# Validate ABI manifest only (no code generation)
npx calimero-abi-codegen --validate -i abi.json
旗帜 : -i, --input <file> (默认) abi.json, (中文(简体) ), -o, --output <dir> (默认) src, (中文(简体) ),
--client-name <Name>, (中文(简体) ), --name-from <path>, (中文(简体) ), --import-path <path> (默认)
@calimero-network/mero-react, (中文(简体) ), --target <ts|swift|kotlin> (默认) ts, (中文(简体) ),
--package <name> (只有考特林语), --validate, (中文(简体) ),
-h, --help中的全部细节
CLI 参考。 。 。 .
程序化
import { loadAbiManifestFromFile } from '@calimero-network/abi-codegen/parse';
import { generateClient } from '@calimero-network/abi-codegen/generate/client';
import fs from 'node:fs';
const manifest = loadAbiManifestFromFile('./abi.json');
const clientContent = generateClient(manifest, 'MyClient');
fs.writeFileSync('src/generated/MyClient.ts', clientContent);
generateClient 它的输出包括类型定义和
客户类一起。 见
方案 API 参考
用于整个地表(parseAbiManifest 和命名公用事业
内 generate/emit) (中文(简体) ).
生成的文件
CLI写道: 单人 文档, {ClientName}.ts (或 .swift 编号 : .kt 联 合 国
,输入输出目录.
类型定义和客户类生活在一个文件中——有
无单独的 types.ts 模块.
斯威夫特和克特林客户端
--target swift 财务报告和审定财务报表 --target kotlin 为移动应用程序创建本地客户端
而不是类型脚本 。 每个都是自成一体的文件,没有SDK
依赖关系:类型、客户端和a ContextContractCalling 运输
app在自己的节点或中继连接上执行.
# Writes Sources/Generated/ForumClient.swift
npx calimero-abi-codegen -i logic/res/abi.json -o Sources/Generated \
--target swift --client-name ForumClient
# Writes app/src/main/kotlin/network/calimero/forum/ForumClient.kt
npx calimero-abi-codegen -i logic/res/abi.json \
-o app/src/main/kotlin/network/calimero/forum \
--target kotlin --package network.calimero.forum --client-name ForumClient
--target 实值 ts (违约,不变), swift 或者说 kotlin· ; --package 设置
Kotlin 文件软件包( 无) package 线条没有它. --import-path 适用于
ts 只有这个.
生成的内容 :
- 挥发数字 :
Codable, Sendable, Equatable每类ABI型的钢筋和铝, (单位:千美元)struct {ClientName}一个async throws每个方法的函数. - 科特林语Name: kotlinx. 序列化
@Serializable数据类,enum类 和封存的阶级class {ClientName}一个suspend函数数 方法。 属性为骆驼Case@SerialName带着电线的名字. 需要一个org.jetbrains.kotlin.plugin.serialization插件和标签kotlinx-serialization-json。 。 。 .
运输方式是每条路线一个呼叫:
public protocol ContextContractCalling: Sendable {
func query(method: String, argsJSON: Data) async throws -> Data
func execute(method: String, argsJSON: Data) async throws -> Data
}
interface ContextContractCalling {
suspend fun query(method: String, argsJson: String): String
suspend fun execute(method: String, argsJson: String): String
}
ABI的方法 intent 实值 read_only 是一个视图,然后转到 query; 每一个
其他方法( 以mutating, (中文(简体) ), unspecified,或无意) execute作为
节点治疗. 每个函数的医生评论都这么说.
线形是 TypeScript 客户端的:整数为 JSON 数字,字节为
数字数组数组([UInt8] 编号 : List<UByte>),tuples是数组
(单位:千美元)ContractTuple2<A, B>,.),而enums遵循了serde的标记(bare name,
{ "Variant": payload },内部/相邻标记,或无标记. 映射密钥
这不是字符串键入 String因为JSON的钥匙是。 一个区别:
左侧的可选参数 nil编号 :null 在参数对象中省略
以 null;合同读取缺失 Option 作为 None。 。 。 .
每个 Swift 模块或 Kotlin 软件包生成一个客户端: 传输和 每个生成的文件都声明帮助者类型.
有本地的SDK,通过 --transport-import 用于使用
ContextContractCalling 所以SDK的契约就是运输
没有适配器 : --transport-import MeroKit (宽)或
--transport-import com.calimero.mero.app.ContextContractCalling (科特林语).
let forum = ForumClient(transport: contract.bound(to: contextId))
实例
见 代码示例 用于完整的反应示例.
发展
npm install
npm run build
npm run test