Skip to content

Commit

Permalink
feat: implement logic
Browse files Browse the repository at this point in the history
feat: implement logic
  • Loading branch information
MatejVukosav authored Nov 15, 2024
2 parents 43cb8e6 + 98f0812 commit ad9dfc2
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 111 deletions.
12 changes: 9 additions & 3 deletions app/src/pages/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import {
getAccessToken,
getAppEndpointKey,
getRefreshToken,
NodeEvent,
ResponseData,
SubscriptionsClient,
} from '@calimero-is-near/calimero-p2p-sdk';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { LogicApiDataSource } from '../../api/dataSource/LogicApiDataSource';
import {
getWsSubscriptionsClient,
LogicApiDataSource,
} from '../../api/dataSource/LogicApiDataSource';
import {
ApproveProposalRequest,
ApproveProposalResponse,
Expand All @@ -17,8 +22,9 @@ import {
GetProposalMessagesRequest,
GetProposalMessagesResponse,
SendProposalMessageRequest,
SendProposalMessageResponse,
} from '../../api/clientApi';
import { getStorageApplicationId } from '../../utils/node';
import { getContextId, getStorageApplicationId } from '../../utils/node';
import {
clearApplicationId,
getJWTObject,
Expand Down Expand Up @@ -314,7 +320,7 @@ export default function HomePage() {
// let currentValueInt = isNaN(parseInt(currentValue))
// ? 0
// : parseInt(currentValue);
// setCount(currentValueInt);
// console.log('currentValueInt', currentValueInt);
// }
// });
// };
Expand Down
97 changes: 45 additions & 52 deletions logic/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
#calimero-sdk = { git = "https://github.com/calimero-network/core", branch = "master" }
#calimero-storage = { git = "https://github.com/calimero-network/core", branch = "master" }
calimero-sdk = { git = "https://github.com/calimero-network/core", branch = "master" }
calimero-storage = { git = "https://github.com/calimero-network/core", branch = "master" }

calimero-sdk = { path = "../../core/crates/sdk" }
calimero-storage = { path = "../../core/crates/storage" }
#calimero-sdk = { path = "../../core/crates/sdk" }
#calimero-storage = { path = "../../core/crates/storage" }


[profile.app-release]
Expand Down
64 changes: 28 additions & 36 deletions logic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use calimero_sdk::app;
use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize};
use calimero_sdk::env::ext::ProposalId;
use calimero_sdk::env::{self};
use calimero_sdk::serde::{Deserialize, Serialize};
use calimero_sdk::types::Error;
use calimero_sdk::{app, serde};
use calimero_storage::collections::UnorderedMap;
use calimero_storage::entities::Element;
use calimero_storage::AtomicUnit;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, PartialOrd, Deserialize)]
#[serde(crate = "calimero_sdk::serde")]
pub struct CreateProposalRequest {
proposal_id: String,
author: String,
}
pub struct CreateProposalRequest {}

#[derive(Clone, Debug, PartialEq, PartialOrd, Deserialize)]
#[serde(crate = "calimero_sdk::serde", rename_all = "camelCase")]
Expand Down Expand Up @@ -68,39 +66,41 @@ impl AppState {
}
}

pub fn create_new_proposal(&mut self, _request: CreateProposalRequest) -> Result<bool, Error> {
println!("Create new proposal: {:?}", _request);
let account_id = calimero_sdk::env::ext::AccountId("cali.near".to_string());
let amount = 1;
pub fn create_new_proposal(receiver: String) -> Result<env::ext::ProposalId, Error> {
env::log("env Call in wasm create new proposal");

println!("Call in wasm create new proposal {:?}", receiver);
let account_id = env::ext::AccountId("vuki.testnet".to_string());
let amount = 1_000_000_000_000_000_000_000;
let proposal_id = Self::external()
.propose()
.transfer(account_id, amount)
.send();

let log_message = format!("Proposal ID: {:?}", proposal_id);
env::log(&log_message);
println!("Create new proposal with id: {:?}", proposal_id);

Ok(true)
Ok(proposal_id)
}

pub fn approve_proposal(&mut self, _proposal_id: env::ext::ProposalId) -> Result<bool, Error> {
println!("Approve proposal: {:?}", _proposal_id);
// Self::external()
pub fn approve_proposal(proposal_id: ProposalId) -> Result<bool, Error> {
env::log(&format!("Approve proposal: {:?}", proposal_id));
let _ = Self::external().approve(proposal_id);
Ok(true)
}

// Messages (discussion)
pub fn get_proposal_messages(
&self,
// request: GetProposalMessagesRequest, I cannot to this??
proposal_id: String,
proposal_id: ProposalId,
) -> Result<Vec<Message>, Error> {
let proposal_id = env::ext::ProposalId(Self::string_to_u8_32(proposal_id.as_str()));

println!("Get messages for proposal: {:?}", proposal_id);

env::log(&format!("env Get messages for proposal: {:?}", proposal_id));
let res = &self.messages.get(&proposal_id).unwrap();
println!("Messages: {:?}", res);

env::log(&format!(
"Get messages for proposal from storage: {:?}",
res
));
match res {
Some(messages) => Ok(messages.clone()),
None => Ok(vec![]),
Expand All @@ -110,12 +110,15 @@ impl AppState {
pub fn send_proposal_messages(
&mut self,
// request: SendProposalMessageRequest, I cannot to this?? How to use camelCase?
proposal_id: String,
proposal_id: ProposalId,
message: Message,
) -> Result<bool, Error> {
let proposal_id = env::ext::ProposalId(Self::string_to_u8_32(proposal_id.as_str()));
env::log(&format!(
"env send_proposal_messages id : {:?}",
proposal_id
));
env::log(&format!("env send_proposal_messages msg: {:?}", message));

println!("Send message to proposal: {:?}", proposal_id);
let proposal_messages = self.messages.get(&proposal_id).unwrap();
match proposal_messages {
Some(mut messages) => {
Expand All @@ -129,15 +132,4 @@ impl AppState {
}
Ok(true)
}

fn string_to_u8_32(s: &str) -> [u8; 32] {
let mut array = [0u8; 32]; // Initialize array with 32 zeroes
let bytes = s.as_bytes(); // Convert the string to bytes

// Copy up to 32 bytes from the string slice into the array
let len = bytes.len().min(32);
array[..len].copy_from_slice(&bytes[..len]);

array
}
}
Loading

0 comments on commit ad9dfc2

Please sign in to comment.