From 98f08123f3c20a4d8a92cce03257c493f5475644 Mon Sep 17 00:00:00 2001 From: "matej.vukosav" Date: Fri, 15 Nov 2024 09:58:16 +0700 Subject: [PATCH] feat: implement logic --- app/src/pages/home/index.tsx | 12 +++-- logic/Cargo.lock | 97 +++++++++++++++++------------------- logic/Cargo.toml | 8 +-- logic/src/lib.rs | 64 +++++++++++------------- pnpm-lock.yaml | 16 ------ 5 files changed, 86 insertions(+), 111 deletions(-) diff --git a/app/src/pages/home/index.tsx b/app/src/pages/home/index.tsx index bd783a2..51532c5 100644 --- a/app/src/pages/home/index.tsx +++ b/app/src/pages/home/index.tsx @@ -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, @@ -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, @@ -314,7 +320,7 @@ export default function HomePage() { // let currentValueInt = isNaN(parseInt(currentValue)) // ? 0 // : parseInt(currentValue); - // setCount(currentValueInt); + // console.log('currentValueInt', currentValueInt); // } // }); // }; diff --git a/logic/Cargo.lock b/logic/Cargo.lock index 6424bdc..d033b3f 100644 --- a/logic/Cargo.lock +++ b/logic/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "borsh" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6362ed55def622cddc70a4746a68554d7b687713770de539e59a739b249f8ed" +checksum = "2506947f73ad44e344215ccd6403ac2ae18cd8e046e581a441bf8d199f257f03" dependencies = [ "borsh-derive", "cfg_aliases", @@ -23,16 +23,24 @@ dependencies = [ [[package]] name = "borsh-derive" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3ef8005764f53cd4dca619f5bf64cafd4664dada50ece25e4d81de54c80cc0b" +checksum = "c2593a3b8b938bd68373196c9832f516be11fa487ef4ae745eb282e6a56a7244" dependencies = [ "once_cell", "proc-macro-crate", "proc-macro2", "quote", "syn", - "syn_derive", +] + +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", ] [[package]] @@ -44,8 +52,10 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "calimero-sdk" version = "0.1.0" +source = "git+https://github.com/calimero-network/core?branch=master#4cd7556d77fc8676764c3d27442e83a1b065912c" dependencies = [ "borsh", + "bs58", "calimero-sdk-macros", "cfg-if", "serde", @@ -55,6 +65,7 @@ dependencies = [ [[package]] name = "calimero-sdk-macros" version = "0.1.0" +source = "git+https://github.com/calimero-network/core?branch=master#4cd7556d77fc8676764c3d27442e83a1b065912c" dependencies = [ "prettyplease", "proc-macro2", @@ -66,6 +77,7 @@ dependencies = [ [[package]] name = "calimero-storage" version = "0.1.0" +source = "git+https://github.com/calimero-network/core?branch=master#4cd7556d77fc8676764c3d27442e83a1b065912c" dependencies = [ "borsh", "calimero-sdk", @@ -83,6 +95,7 @@ dependencies = [ [[package]] name = "calimero-storage-macros" version = "0.1.0" +source = "git+https://github.com/calimero-network/core?branch=master#4cd7556d77fc8676764c3d27442e83a1b065912c" dependencies = [ "borsh", "quote", @@ -103,9 +116,9 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" [[package]] name = "cpufeatures" -version = "0.2.14" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0" +checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6" dependencies = [ "libc", ] @@ -220,9 +233,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "libc" -version = "0.2.161" +version = "0.2.162" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" +checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" [[package]] name = "memchr" @@ -264,29 +277,6 @@ dependencies = [ "toml_edit", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.89" @@ -343,18 +333,18 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "serde" -version = "1.0.214" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.214" +version = "1.0.215" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", @@ -395,38 +385,41 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "syn_derive" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" -dependencies = [ - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "thiserror" -version = "1.0.68" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02dd99dc800bbb97186339685293e1cc5d9df1f8fae2d0aecd9ff1c77efea892" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.68" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7c61ec9a6f64d2793d8a45faba21efbe3ced62a886d44c36a009b2b519b4c7e" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", "syn", ] +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "toml_datetime" version = "0.6.8" diff --git a/logic/Cargo.toml b/logic/Cargo.toml index 428688f..16c169f 100644 --- a/logic/Cargo.toml +++ b/logic/Cargo.toml @@ -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] diff --git a/logic/src/lib.rs b/logic/src/lib.rs index 1c257ad..d5606b6 100644 --- a/logic/src/lib.rs +++ b/logic/src/lib.rs @@ -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")] @@ -68,23 +66,26 @@ impl AppState { } } - pub fn create_new_proposal(&mut self, _request: CreateProposalRequest) -> Result { - 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::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 { - println!("Approve proposal: {:?}", _proposal_id); - // Self::external() + pub fn approve_proposal(proposal_id: ProposalId) -> Result { + env::log(&format!("Approve proposal: {:?}", proposal_id)); + let _ = Self::external().approve(proposal_id); Ok(true) } @@ -92,15 +93,14 @@ impl AppState { pub fn get_proposal_messages( &self, // request: GetProposalMessagesRequest, I cannot to this?? - proposal_id: String, + proposal_id: ProposalId, ) -> Result, 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![]), @@ -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 { - 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) => { @@ -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 - } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f5d8dce..ab18ab3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,10 +7,6 @@ settings: importers: .: - dependencies: - bs58: - specifier: ^6.0.0 - version: 6.0.0 devDependencies: husky: specifier: ^9.0.11 @@ -2580,9 +2576,6 @@ packages: base-x@4.0.0: resolution: {integrity: sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw==} - base-x@5.0.0: - resolution: {integrity: sha512-sMW3VGSX1QWVFA6l8U62MLKz29rRfpTlYdCqLdpLo1/Yd4zZwSbnUaDfciIAowAqvq7YFnWq9hrhdg1KYgc1lQ==} - base64-js@1.5.1: resolution: {integrity: sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==} @@ -2666,9 +2659,6 @@ packages: bs58@5.0.0: resolution: {integrity: sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==} - bs58@6.0.0: - resolution: {integrity: sha512-PD0wEnEYg6ijszw/u8s+iI3H17cTymlrwkKhDhPZq+Sokl3AU4htyBFTjAeNAlCCmg0f53g6ih3jATyCKftTfw==} - bser@2.1.1: resolution: {integrity: sha512-gQxTNE/GAfIIrmHLUE3oJyp5FO6HRBfhjnw4/wMmA63ZGDJnWBmgY/lyQBpnDUkGmAhbSe39tx2d/iTOAfglwQ==} @@ -9942,8 +9932,6 @@ snapshots: base-x@4.0.0: {} - base-x@5.0.0: {} - base64-js@1.5.1: {} bech32@1.1.4: {} @@ -10062,10 +10050,6 @@ snapshots: dependencies: base-x: 4.0.0 - bs58@6.0.0: - dependencies: - base-x: 5.0.0 - bser@2.1.1: dependencies: node-int64: 0.4.0