Crate we_cdk

source ·
Expand description

§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=<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);
}

Re-exports§

Modules§

Macros§

  • Base58 string to bytes conversion
  • Get block fields
  • Burn the asset
  • Call contract
  • Get the ContractId of the calling contract
  • Cancel the lease
  • Checks if the string/binary is contained in a string/binary
  • Checks if a record with this key exists
  • Returns a string/binary without the first N characters/bytes
  • Returns a string/binary without the last N characters/bytes
  • Comparison data of Binary and String types
  • Fast hash
  • Get the balance of tokens
  • Get storage
  • Get the asset_id or amount of the attached payment in the transaction
  • Get the number of attached payments in the transaction
  • Returns the index of the first occurrence of the substring (if no occurrence is found, returns -1)
  • Issue the asset
  • Joining data of Binary and String types
  • Returns the index of the last occurrence of the substring (if no occurrence is found, returns -1)
  • Leasing of funds
  • Converts a string representation of a boolean to an equivalent logical value
  • Converts a string representation of a number to an equivalent integer number
  • Reissue the asset
  • Verify inputs and conditions
  • Secure hash
  • Set storage
  • Signature verify
  • Returns the first N characters/bytes of the string/binary
  • Returns a string/binary without the last N characters/bytes
  • Bytes to Base58 string conversion
  • Converts an integer to a byte array
  • Converts a byte array to an integer number
  • Converts an binary to a string
  • Converts a logical value to a string
  • Converts an integer to a string
  • Tokens transfer
  • Get tx fields

Type Aliases§

  • Binary is a data type for byte array.
  • Boolean is a boolean data type.
  • Integer is an integer data type.
  • Payment is a payment that can be attached when calling the function of another contract.
  • String is a string data type. Strings are UTF-8 encoded.

Attribute Macros§

  • Marks function as a called function.
  • Marks trait as an interface to another contract.