3 releases

new 0.0.4 Jan 15, 2025
0.0.3 Jan 14, 2025
0.0.2 Dec 17, 2024
0.0.1 Dec 17, 2024

#50 in Magic Beans

Download history 129/week @ 2024-12-11 107/week @ 2024-12-18 3/week @ 2024-12-25 68/week @ 2025-01-08

307 downloads per month

MIT license

21KB
427 lines

Verisense Core SDK

The core sdk for developing Verisense nucleus. Check Docs for more details.

NOTE: This is a preview version for developers; breaking changes may occur in the future.


lib.rs:

The core sdk for developing nucleus on Verisense. NOTE: This crate is currently under heavy development and is not stable yet. We release it just for testing and collecting feedback.

Examples

use parity_scale_codec::{Decode, Encode};
use vrs_core_sdk::{get, post, storage};

#[derive(Debug, Decode, Encode)]
pub struct User {
    pub id: u64,
    pub name: String,
}

#[post]
pub fn add_user(user: User) -> Result<u64, String> {
    let max_id_key = [&b"user:"[..], &u64::MAX.to_be_bytes()[..]].concat();
    let max_id = match storage::search(&max_id_key, storage::Direction::Reverse)
        .map_err(|e| e.to_string())?
    {
        Some((id, _)) => u64::from_be_bytes(id[5..].try_into().unwrap()) + 1,
        None => 1u64,
    };
    let key = [&b"user:"[..], &max_id.to_be_bytes()[..]].concat();
    storage::put(&key, user.encode()).map_err(|e| e.to_string())?;
    Ok(max_id)
}

#[get]
pub fn get_user(id: u64) -> Result<Option<User>, String> {
    let key = [&b"user:"[..], &id.to_be_bytes()[..]].concat();
    let r = storage::get(&key).map_err(|e| e.to_string())?;
    let user = r.map(|d| User::decode(&mut &d[..]).unwrap());
    Ok(user)
}

Dependencies

~15MB
~303K SLoC