Interacting with Nucleus

In the previous chapter, we deployed a Nucleus with two interfaces:

#![allow(unused)]
fn main() {
#[post]
pub fn add_user(user: User) -> Result<u64, String> {
    // Construct the user's key
    let key = [&b"user:"[..], &user.id.to_be_bytes()[..]].concat();
    // Write data to storage
    storage::put(&key, &user.encode()).map_err(|e| e.to_string())?;
    Ok(user.id)
}
}
#![allow(unused)]
fn main() {
#[get]
pub fn get_user(id: u64) -> Result<Option<User>, String> {
    // Construct the user's key
    let key = [&b"user:"[..], &id.to_be_bytes()[..]].concat();
    // Retrieve data from storage
    let result = storage::get(&key).map_err(|e| e.to_string())?;
    // Decode the data into a User struct
    let user = result.map(|data| User::decode(&mut &data[..]).unwrap());
    Ok(user)
}
}

This chapter explains how to interact with this Nucleus.

Encoding and Decoding

We use Polkadot's Codec crate to encode and decode data within the Nucleus.

For different programming languages, the following libraries can be used for encoding:

For instance, consider the following structure:

#![allow(unused)]
fn main() {
pub struct User {
    pub id: u64,
    pub name: String,
}
}

With id = 123456 and name = "nucleus_1", the encoded data is:

40e2010000000000246e75636c6575735f31

To invoke the add_user interface via JSON-RPC:

curl --location 'https://rpc.beta.verisense.network' \
--header 'Content-Type: application/json' \
--data '
{
    "jsonrpc": "2.0",
    "method": "nucleus_post",
    "params": [
        "kGgGtCimpkywYrQ7yULt3pEZYwetW35NrupEfSyTavTPULXbV", // Nucleus ID
        "add_user", // Function name
        "40e2010000000000246e75636c6575735f31" // Encoded parameters
    ],
    "id": 1
}

Example response:

{"jsonrpc":"2.0","result":"0040e2010000000000","id":1}

To invoke the get_user interface via JSON-RPC:

curl --location 'https://rpc.beta.verisense.network' \
--header 'Content-Type: application/json' \
--data '
{
    "jsonrpc": "2.0",
    "method": "nucleus_get",
    "params": ["kGgGtCimpkywYrQ7yULt3pEZYwetW35NrupEfSyTavTPULXbV", "get_user", "40e2010000000000"],
    "id": 1
}'

Example response:

{"jsonrpc":"2.0","result":"000140e2010000000000246e75636c6575735f31","id":1}

Decoding the result yields:

#![allow(unused)]
fn main() {
Ok(Some(User { id: 123456, name: "nucleus_1" }))
}