#smart-contracts #cargo #setup #tool #ecosystem #toolkit #waves

app cargo-we

Setup and deployment tool for developing WASM based smart-contracts

1 unstable release

0.2.1 Apr 23, 2024

#2789 in Magic Beans

Download history 120/week @ 2024-04-22

120 downloads per month

MIT license

22KB
497 lines

we-cdk

Toolkit for development WASM smart-contracts on Waves Enterprise ecosystem.

Usage

A prerequisite for compiling smart contracts is to have Rust and Cargo installed. Here's an installation guide.

To work with the contract it is necessary to install cargo-we. It's a CLI tool which helps set up and manage WASM smart contracts.

cargo install cargo-we --force

Use the --force to ensure you are updated to the most recent cargo-we version.

And there is also an option to install the CLI tool from this project folder.

cargo install --path ./crates/cargo-we/ --force

In order to initialize a new project you can use:

cargo we new flipper

This will create a folder flipper in your work directory. The folder contains a scaffold Cargo.toml and a lib.rs, which both contain the necessary building blocks for using WASM smart-contract.

The lib.rs contains our hello world contract ‒ the Flipper, which we explain in the next section.

In order to build the contract just execute this command in the flipper folder:

cargo we build

As a result you'll get a target/we/flipper.wasm file and a flipper.json file in the target/we folder of your contract.

Create & Update contract

With cargo-we can also send CreateContract and UpdateContract transactions directly to a network node.

To do this, create a file with a description of access to the node and the transaction to be sent.

To send a transaction, all you need to do is execute the command:

cargo we tx --send --path_json=<path_json>

Example JSON file:

{
    "nodeUrl": "http://localhost:6862",
    "apiKey": "we",
    "transaction": {
        "contractId": "FofPw6DRQVhYVsYQzsFEWDBrJpS1GVFLS7vGHajuQUtD",
        "fee" : 100000000,
        "payments" : [ ],
        "groupOwners" : [ ],
        "type" : 107,
        "version" : 6,
        "groupParticipants" : [ ],
        "sender" : "3NgoqC9HdDfTYJtKjcW2v3LeTdd2uyhimVo",
        "feeAssetId" : null,
        "contractName" : "name",
        "validationPolicy" : {
            "type": "any"
        }
    }
}

Hello, World! ‒ The Flipper

The Flipper contract is a simple contract containing only a single bool value.

It provides method flip its value from true to false (and vice versa).

Below you can see the code using 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);
}

Dependencies

~11–24MB
~387K SLoC