5 个版本
| 0.4.1 | 2024年7月31日 |
|---|---|
| 0.4.0 | 2024年6月24日 |
| 0.3.2 | 2024年5月15日 |
| 0.3.1 | 2024年5月15日 |
| 0.3.0 | 2024年4月23日 |
2021 in 神奇豆子
148 每月下载量
41KB
709 行
we-cdk
Waves Enterprise 生态系统上开发 WASM 智能合约的工具包。
使用方法
编译智能合约的前提是已安装 Rust 和 Cargo。这里是安装指南。
要使用该合约,需要安装 cargo-we。这是一个 CLI 工具,可以帮助设置和管理 WASM 智能合约。
cargo install cargo-we --force
使用 --force 确保您更新到最新的 cargo-we 版本。
也可以从本项目的文件夹安装 CLI 工具。
cargo install --path ./crates/cargo-we/ --force
要初始化新项目,可以使用
cargo we new flipper
这将创建一个名为 flipper 的文件夹在您的工作目录中。该文件夹包含一个支架 Cargo.toml 和一个 lib.rs,它们都包含使用 WASM 智能合约所需的所有构建块。
lib.rs 包含我们的 hello world 合约 —— Flipper,我们将在下一节中解释。
要构建合约,只需在 flipper 文件夹中执行此命令
cargo we build
结果您将在合约的 target/we/flipper.wasm 文件夹中获取一个 target/we/flipper.wasm 文件和一个 flipper.json 文件。
创建与更新合约
使用 cargo-we 也可以直接向网络节点发送 CreateContract 和 UpdateContract 交易。
为此,创建一个包含对节点访问描述和要发送的交易描述的文件。
要发送交易,您只需执行以下命令
cargo we tx --send <path_json>
示例 JSON 文件
{
"nodeUrl": "https://:6862",
"apiKey": "we",
"transaction": {
"type" : 103,
"version" : 7,
"sender" : "3NA9hBGoVPfJVybremiFgWN8REi9oiDydEF",
"password": "",
"contractName": "flipper",
"params": [
{
"type": "boolean",
"key": "init_value",
"value": false
}
],
"isConfidential": false,
"fee" : 100000000,
"payments" : [ ],
"feeAssetId" : null,
"validationPolicy" : {
"type": "any"
},
"groupOwners" : [ ],
"groupParticipants" : [ ]
}
}
你好,世界!——翻转器
Flipper 合约是一个简单的合约,仅包含一个 bool 值。
它提供了翻转其值从 true 到 false(反之亦然)的方法。
下面是使用 CDK 编写的代码。
#![no_std]
#![no_main]
use we_cdk::*;
// Declaring a function available for calling.
// `#[action]` keyword is used for this purpose.
// _constructor mandatory method that is called during CreateContract Transaction.
#[action]
fn _constructor(init_value: Boolean) {
// Write the obtained value as an argument of the function into contract state.
set_storage!(boolean :: "value" => init_value);
}
#[action]
fn flip() {
// Read the value from the contract state.
let value: Boolean = get_storage!(boolean :: "value");
// Write the inverted value to the contract state.
set_storage!(boolean :: "value" => !value);
}
依赖项
~0.4–0.8MB
~20K SLoC