Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use borsh::{io::Error, BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

Expand All @@ -10,12 +12,16 @@ impl HyleContract for Counter {
let (action, ctx) = sdk::utils::parse_raw_contract_input::<CounterAction>(contract_input)?;

// Execute the contract logic
match action {
CounterAction::Increment => self.value += 1,
}
let value = match action {
CounterAction::Increment => self
.values
.entry(contract_input.identity.0.clone())
.and_modify(|v| *v += 1)
.or_insert(1),
};

// program_output might be used to give feedback to the user
let program_output = format!("new value: {}", self.value);
let program_output = format!("new value: {}", value);
Ok((program_output, ctx, vec![]))
}
}
Expand All @@ -29,7 +35,7 @@ pub enum CounterAction {
/// 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,
pub values: BTreeMap<String, u32>,
}

/// Utils function for the host
Expand Down
9 changes: 7 additions & 2 deletions host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct Cli {
#[command(subcommand)]
command: Commands,

#[arg(long, default_value = "bob")]
pub username: String,

#[arg(long, default_value = "http://localhost:4321")]
pub host: String,

Expand All @@ -44,12 +47,14 @@ async fn main() -> Result<()> {
let prover = Risc0Prover::new(GUEST_ELF);

// This dummy example doesn't uses identities. But there are required fields & validation.
let identity = format!("none.{}", contract_name);
let identity = format!("{}.{}", cli.username, contract_name);

match cli.command {
Commands::RegisterContract {} => {
// Build initial state of contract
let initial_state = Counter { value: 0 };
let initial_state = Counter {
values: Default::default(),
};

// Send the transaction to register the contract
let res = client
Expand Down