-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
64 lines (54 loc) · 1.96 KB
/
Copy pathlib.rs
File metadata and controls
64 lines (54 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use borsh::{io::Error, BorshDeserialize, BorshSerialize};
use sdk::FullStateRevert;
use serde::{Deserialize, Serialize};
impl sdk::ZkContract for Counter {
/// Entry point of the contract's logic
fn execute(&mut self, contract_input: &sdk::Calldata) -> sdk::RunResult {
// Parse contract inputs
let (action, ctx) = sdk::utils::parse_raw_calldata::<CounterAction>(contract_input)?;
// Execute the contract logic
match action {
CounterAction::Increment => self.value += 1,
}
// program_output might be used to give feedback to the user
let program_output = format!("new value: {}", self.value);
Ok((program_output.into(), ctx, vec![]))
}
/// Commit the state of the contract
fn commit(&self) -> sdk::StateCommitment {
sdk::StateCommitment(borsh::to_vec(self).expect("Failed to encode Balances"))
}
}
/// The action represents the different operations that can be done on the contract
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub enum CounterAction {
Increment,
}
/// The state of the contract, in this example it is fully serialized on-chain
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone)]
pub struct Counter {
pub value: u32,
}
impl FullStateRevert for Counter {}
/// Utils function for the host
impl Counter {
pub fn as_bytes(&self) -> Result<Vec<u8>, Error> {
borsh::to_vec(self)
}
}
/// Utils function for the host
impl CounterAction {
pub fn as_blob(&self, contract_name: &str) -> sdk::Blob {
sdk::Blob {
contract_name: contract_name.into(),
data: sdk::BlobData(borsh::to_vec(self).expect("failed to encode BlobData")),
}
}
}
impl From<sdk::StateCommitment> for Counter {
fn from(state: sdk::StateCommitment) -> Self {
borsh::from_slice(&state.0)
.map_err(|_| "Could not decode hyllar state".to_string())
.unwrap()
}
}