| Crates.io | fvm-std |
| lib.rs | fvm-std |
| version | 1.0.0 |
| created_at | 2023-02-07 03:12:36.656346+00 |
| updated_at | 2023-02-07 03:12:36.656346+00 |
| description | tool for user to write contract which will be run in hyperchain with rust |
| homepage | |
| repository | https://github.com/hyperchain/hyperchain |
| max_upload_size | |
| id | 778459 |
| size | 76,668 |
fvm-std is a crate that supply base tool for developer to write contract with RUST more convenient
Here some simple types that we provide for developers to write contract easier.
H256 is a 32-length bytes, in order to express block hash. There are methods implement
fn as_ref(&self) -> &H256
pub const fn new(val: [u8; 32]) -> Self
pub fn to_hex_string(&self) -> String
H160 is a 20-length bytes, in order to express address, there are methods implement
fn as_ref(&self) -> &H160
pub const fn new(val: [u8; 20]) -> Self
Address is a alias for H160, also a method implement
pub fn to_hex_string(&self) -> String
Notice: to_hex_string is different to to_string, the latter will only print part of the content, if it is too long
level for developer to use with event log. more details see in section log
// CRITICAL ERROR WARNING NOTICE INFO DEBUG
pub enum LogLevel {
CRITICAL,
ERROR,
WARNING,
NOTICE,
INFO,
DEBUG,
}
we have supplied several tool macros method for developer to print log in contract, include
critical!(), error!(), warning!(), notice!(), info!(), debug!()
pub fn add(&mut self) -> u64 {
info!("info {}", "hello");
warning!("warning {}", "hello");
notice!("notice {}", "hello");
1
}
supply event for developer used in contract.
#[derive(Debug, Serialize)]
pub struct Event<T> where T: Serialize {
address: Address,
data: T,
topics: Vec<H256>,
}
demo
#[derive(Debug, Serialize)]
struct Temp {
amount: u64,
}
#[storage]
pub struct SetHash {
map: HyperMap<String, String>,
}
#[contract]
impl SetHash {
fn new() -> Self {
Self { map: HyperMap::new() }
}
pub fn set_hash(&mut self, key: String, value: String) {
let temp = Temp { amount: 1 };
let ev = Event::new(temp, "set_hash".to_string(), vec!["test".to_string()]);
ev.emit();
self.map.insert(key, value);
}
pub fn get_hash(&mut self, key: String) -> &String {
self.map.get(&key).unwrap()
}
}