Skip to main content

Client SDK

Once the smart contract is deployed on the WeilChain, developers would be interested in interacting with it and build exciting applications on top of it. This is realized through the Weilliptic Client SDK. We provide client SDK in TypeScript, Rust and Golang. Let's again take example of counter widl interface:

interface Counter {
query func get_count() -> uint;
mutate func increment()
}
warning

The steps are slightly different depending on the language you are using, so make sure to chose the right language now.

You have chosen Rust!

Now to generate client bindings from widl, use following command:

widl generate counter.widl client rust

which will generate a bindings file

bindings.rs
use serde::{Deserialize, Serialize};
use w_wasmutils::errors::WeilError;
use weil_wallet::{contract::ContractId, wallet::Wallet, WeilClient, WeilContractClient};

struct CounterClient {
client: WeilContractClient,
}

impl CounterClient {
pub fn new(contract_id: ContractId, wallet: Wallet) -> Result<Self, anyhow::Error> {
Ok(CounterClient {
client: WeilClient::new(wallet)?.to_contract_client(contract_id),
})
}

pub async fn get_count(&self) -> Result<u32, anyhow::Error> {
#[derive(Serialize)]
struct Args {
}

let args = Args { };

let resp = self.client.execute("get_count".to_string(), serde_json::to_string(&args).unwrap()).await?;

let txn_result = serde_json::from_str::<Result<String, WeilError>>(&resp.txn_result)?;
let result = txn_result?;
let result = serde_json::from_str::<u32>(&result)?;

Ok(result)
}

pub async fn increment(&self) -> Result<(), anyhow::Error> {
#[derive(Serialize)]
struct Args {
}

let args = Args { };

let resp = self.client.execute("increment".to_string(), serde_json::to_string(&args).unwrap()).await?;

let txn_result = serde_json::from_str::<Result<String, WeilError>>(&resp.txn_result)?;
let result = txn_result?;
let result = serde_json::from_str::<()>(&result)?;

Ok(result)
}

}

This CounterClient can be used to interact with the Counter smart contract with the provided ContractId.

So a very simple main program in rust would look like:


#[tokio::main]
async fn main() {
// put path of the private key here!
let private_key = PrivateKey::from_file("/root/.weilliptic/private_key.wc").unwrap();
let wallet = Wallet::new(private_key).unwrap();

// put your contract id here!
let contract_id = "0000000279b490f8823ec1ce7e3c6ff2600500afa0e58eb59a1572afd25d0d4d16eb7512"
.parse::<ContractId>()
.unwrap();

let client = CounterClient::new(contract_id, wallet).unwrap();

let count = client.get_count().await.unwrap();
println!("count: {:?}", count);

client.increment().await.unwrap();

let count = client.get_count().await.unwrap();
println!("count: {:?}", count);
}

Hence client SDK is a great way to programmatically interact with the smart contract and build cool decentralized application in your favourite language!