Expand description
§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 --forceUse 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/ --forceIn order to initialize a new project you can use:
cargo we new flipperThis 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 buildAs 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);
}Re-exports§
pub use wevm;
Modules§
Macros§
- base58
- Base58 string to bytes conversion
- blake2b256
- Hash an array of bytes using BLAKE2b-256 (https://en.wikipedia.org/wiki/BLAKE_%28hash_function%29)
- block
- Get block fields
- burn
- Burn the asset
- call_
contract - Call contract
- caller
- Get the ContractId of the calling contract
- cancel_
lease - Cancel the lease
- contains
- Checks if the string/binary is contained in a string/binary
- contains_
key - Checks if a record with this key exists
- drop
- Returns a string/binary without the first N characters/bytes
- drop_
right - Returns a string/binary without the last N characters/bytes
- equals
- Comparison data of Binary and String types
- fast_
hash - Fast hash
- get_
balance - Get the balance of tokens
- get_
storage - Get storage
- get_
tx_ payment - Get the
asset_idoramountof the attached payment in the transaction - get_
tx_ payments - Get the number of attached payments in the transaction
- index_
of - Returns the index of the first occurrence of the substring (if no occurrence is found, returns -1)
- issue
- Issue the asset
- join
- Joining data of Binary and String types
- keccak256
- Hash an array of bytes using Keccak-256 (https://keccak.team/files/Keccak-submission-3.pdf)
- last_
index_ of - Returns the index of the last occurrence of the substring (if no occurrence is found, returns -1)
- lease
- Leasing of funds
- parse_
bool - Converts a string representation of a boolean to an equivalent logical value
- parse_
int - Converts a string representation of a number to an equivalent integer number
- reissue
- Reissue the asset
- require
- Verify inputs and conditions
- secure_
hash - Secure hash
- set_
storage - Set storage
- sha256
- Hash an array of bytes using SHA-256 (https://en.wikipedia.org/wiki/SHA-2)
- sig_
verify - Signature verify
- take
- Returns the first N characters/bytes of the string/binary
- take_
right - Returns a string/binary without the last N characters/bytes
- to_
base58_ string - Bytes to Base58 string conversion
- to_
bytes - Converts an integer to a byte array
- to_int
- Converts a byte array to an integer number
- to_
string_ binary - Converts an binary to a string
- to_
string_ bool - Converts a logical value to a string
- to_
string_ int - Converts an integer to a string
- transfer
- Tokens transfer
- tx
- Get tx fields
Type Aliases§
- Binary
- Binary is a data type for byte array.
- Boolean
- Boolean is a boolean data type.
- Integer
- Integer is an integer data type.
- Payment
- Payment is a payment that can be attached when calling the function of another contract.
- String
- String is a string data type. Strings are UTF-8 encoded.