Skip to content

Example - Hello Contract

We will go through the steps of creating a simple smart contract with the help of pchain-sdk.

Firstly, we have to prepare the Cargo.toml, which specifies the name, version, and year of edition of the smart contract. We need to use pchain-sdk for this smart contract development, so remember to add pchain-sdk = { version = "LATEST_VERSION"} under dependencies.

Cargo.toml

[package]
name = "hello_contract"
version = "0.4.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
pchain-sdk = { git = "https://github.com/parallelchain-io/pchain-sdk.git" }

After preparing Cargo.toml, we can now start preparing the smart contract.

#[contract] defines basic struct as a programming model of a contract. Fields are data representations of contract storage.

#[contract_methods] defines impl for the contract struct.

#[call] macro applies to impl methods for the contract method call. Methods declared under this macro are callable by Transaction Command - Call.

In the file, we have added a method called hello(). In this method, the SDK provided function pchain_sdk::log() logs the information as a key-value pair and appends it to the Logs in a transaction receipt.

lib.rs

use pchain_sdk::{
    contract, contract_methods, call, storage, log, 
};

#[contract]
struct HelloContract {}

#[contract_methods] 
impl HelloContract {

    #[call]
    fn hello() {
        pchain_sdk::log(
            "topic: Hello".as_bytes(), 
            "Hello, Contract".as_bytes()
        );
    }
}

Next, we can add two other methods, which illustrate how we can set and get values from the storage by using the SDK provided functions in crate pchain_sdk::storage.

#[call]
fn hello_set_many() {
    for i in 1..10{
        let key = format!("hello-key-{}", i);
        let value = vec![0_u8; 1024*10]; //10KB
        storage::set(key.as_bytes(), &value);
    }
}

#[call]
fn hello_read_many() {
    for i in 1..10{
        let key = format!("hello-key-{}", i);
        let value = storage::get(key.as_bytes());
        if value.is_some(){
            log(
                "topic: Hello read".as_bytes(), 
                format!("key: {}, len: {}", key, value.unwrap().len()).as_bytes()
            );
        }
    }
}