we-contract-proc-macro

Crates.iowe-contract-proc-macro
lib.rswe-contract-proc-macro
version0.3.2
sourcesrc
created_at2024-04-23 13:06:33.289455
updated_at2024-07-31 04:01:21.398538
descriptionProcedural macros for generating code for WASM contracts
homepagehttps://github.com/waves-enterprise/we-cdk
repositoryhttps://github.com/waves-enterprise/we-cdk
max_upload_size
id1217524
size13,100
DEADBLACKCLOVER (deadblackclover)

documentation

README

we-cdk

we-cdk crates.io cargo-we crates.io

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>

Example JSON file:

{
    "nodeUrl": "http://localhost: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" : [ ]
    }
}

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);
}
Commit count: 107

cargo fmt