Skip to content

Commit

Permalink
feat: add message methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MatejVukosav committed Nov 5, 2024
1 parent 7bb3df1 commit 7f0ebd6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 48 deletions.
97 changes: 50 additions & 47 deletions apps/blockchain/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use calimero_sdk::borsh::BorshSerialize;
use calimero_sdk::app;
use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize};
use calimero_sdk::env::{self};
use calimero_sdk::types::Error;
use calimero_sdk::{app, borsh};
use calimero_storage::collections::UnorderedMap;
use calimero_storage::entities::Element;
use calimero_storage::AtomicUnit;
use serde::{Deserialize, Serialize};

#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)]
#[borsh(crate = "calimero_sdk::borsh")]
#[type_id(1)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Deserialize)]
#[serde(crate = "calimero_sdk::serde")]
pub struct CreateProposalRequest {
proposal_id: String,
author: Option<String>,
// status: ProposalStatus,
#[storage]
storage: Element,
}

#[app::event]
pub enum Event {
ProposalCreated(),
}

#[app::state(emits = Event)]
Expand All @@ -25,28 +28,20 @@ pub struct AppState {
#[storage]
storage: Element,

messages: Vec<Message>,
// messages: Vec<env::ext::ProposalId>,
// messages: Map<String, Vec<Message>>,
messages: UnorderedMap<env::ext::ProposalId, Vec<Message>>,
}

#[app::event]
pub enum Event {
ProposalCreated(),
}

// #[derive(AtomicUnit, BorshDeserialize, BorshSerialize, Default, Serialize)]
#[derive(AtomicUnit, BorshSerialize)]
#[type_id(1)]
#[derive(
Clone, Debug, PartialEq, PartialOrd, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
#[borsh(crate = "calimero_sdk::borsh")]
#[serde(crate = "calimero_sdk::serde")]
pub struct Message {
id: String,
proposal_id: String,
author: String,
text: String,
created_at: String,

#[storage]
storage: Element,
}

#[app::logic]
Expand All @@ -56,49 +51,57 @@ impl AppState {
AppState {
count: 0,
storage: Element::root(),
messages: Vec::new(),
messages: UnorderedMap::new().unwrap(),
}
}

pub fn create_new_proposal(&mut self, _request: CreateProposalRequest) -> Result<bool, Error> {
// let proposal_id = Blockchain::create_proposal("transfer", "xabi.near", 999999);
// let enhanced_proposal = Proposal {
// proposal_id,
// title,
// description
// };
// storage.save(enhanced_proposal)

let account_id = env::ext::AccountId("cali.near".to_string());
let amount = 1;
let proposal_id = Self::external()
.propose()
.transfer(account_id, amount)
.send();

println!("Proposal ID: {:?}", proposal_id);
println!("Create new proposal with id: {:?}", proposal_id);

Ok(true)
}

pub fn approve_proposal(&mut self, _proposal_id: String) -> Result<bool, Error> {
// let proposal = storage.get_proposal(proposal_id);
// let vote = Vote {
// proposal_id,
// voter_id: "xabi.near",
// vote_type: VoteType::Accept(),
// voted_at: 1234567890,
// };
// storage.save(vote)
pub fn approve_proposal(&mut self, _proposal_id: env::ext::ProposalId) -> Result<bool, Error> {
// Self::external()
Ok(true)
}

// Messages (discussion)
pub fn get_proposal_messages(&self, _proposal_id: String) -> Result<&Vec<Message>, Error> {
let res = &self.messages;
Ok(res)
pub fn get_proposal_messages(
&self,
proposal_id: env::ext::ProposalId,
) -> Result<Vec<Message>, Error> {
let res = &self.messages.get(&proposal_id).unwrap();

match res {
Some(messages) => Ok(messages.clone()),
None => Ok(vec![]),
}
}

pub fn send_message(
&mut self,
proposal_id: env::ext::ProposalId,
message: Message,
) -> Result<bool, Error> {
let proposal_messages = self.messages.get(&proposal_id).unwrap();
match proposal_messages {
Some(mut messages) => {
messages.push(message);
self.messages.insert(proposal_id, messages)?;
}
None => {
let messages = vec![message];
self.messages.insert(proposal_id, messages)?;
}
}
Ok(true)
}
// pub fn send_message(proposal_id: String, message: Message) -> bool {
// true
// }
}
21 changes: 20 additions & 1 deletion crates/sdk/src/env/ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use borsh::{to_vec as to_borsh_vec, to_vec, BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

use super::{expected_boolean, expected_register, panic_str, read_register, DATA_REGISTER};
use crate::sys;
Expand Down Expand Up @@ -126,9 +127,27 @@ impl External {
}

/// Unique identifier for a proposal.
#[derive(BorshDeserialize, BorshSerialize, Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(
BorshDeserialize,
BorshSerialize,
Clone,
Copy,
Debug,
Eq,
Ord,
PartialEq,
PartialOrd,
Serialize,
Deserialize,
)]
pub struct ProposalId(pub [u8; 32]);

impl AsRef<[u8]> for ProposalId {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

#[doc(hidden)]
pub unsafe fn fetch(
url: &str,
Expand Down

0 comments on commit 7f0ebd6

Please sign in to comment.