Dastan-glitch 3 лет назад
Родитель
Сommit
4e4ae9914e
4 измененных файлов с 163 добавлено и 58 удалено
  1. 43 18
      bin/dao-cli/src/main.rs
  2. 49 0
      bin/dao-cli/src/rpc.rs
  3. 7 40
      bin/daod/src/main.rs
  4. 64 0
      bin/daod/src/rpc.rs

+ 43 - 18
bin/dao-cli/src/main.rs

@@ -1,16 +1,24 @@
 use clap::{IntoApp, Parser, Subcommand};
 use clap::{IntoApp, Parser, Subcommand};
-use serde_json::{json, Value};
 use url::Url;
 use url::Url;
 
 
-use darkfi::{
-    rpc::{client::RpcClient, jsonrpc::JsonRequest},
-    Result,
-};
+use darkfi::{rpc::client::RpcClient, Result};
+
+mod rpc;
 
 
 #[derive(Subcommand)]
 #[derive(Subcommand)]
 pub enum CliDaoSubCommands {
 pub enum CliDaoSubCommands {
-    /// Say hello to the RPC
-    Hello {},
+    /// Initialize DAO
+    Init {},
+    /// Create DAO
+    Create {},
+    /// Airdrop tokens
+    Airdrop {},
+    /// Propose
+    Propose {},
+    /// Vote
+    Vote {},
+    /// Execute
+    Exec {},
 }
 }
 
 
 /// DAO cli
 /// DAO cli
@@ -24,25 +32,42 @@ pub struct CliDao {
     #[clap(subcommand)]
     #[clap(subcommand)]
     pub command: Option<CliDaoSubCommands>,
     pub command: Option<CliDaoSubCommands>,
 }
 }
+
 pub struct Rpc {
 pub struct Rpc {
     client: RpcClient,
     client: RpcClient,
 }
 }
 
 
-impl Rpc {
-    // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42}
-    // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42}
-    async fn say_hello(&self) -> Result<Value> {
-        let req = JsonRequest::new("say_hello", json!([]));
-        self.client.request(req).await
-    }
-}
-
 async fn start(options: CliDao) -> Result<()> {
 async fn start(options: CliDao) -> Result<()> {
     let rpc_addr = "tcp://127.0.0.1:7777";
     let rpc_addr = "tcp://127.0.0.1:7777";
     let client = Rpc { client: RpcClient::new(Url::parse(rpc_addr)?).await? };
     let client = Rpc { client: RpcClient::new(Url::parse(rpc_addr)?).await? };
     match options.command {
     match options.command {
-        Some(CliDaoSubCommands::Hello {}) => {
-            let reply = client.say_hello().await?;
+        Some(CliDaoSubCommands::Init {}) => {
+            let reply = client.init().await?;
+            println!("Server replied: {}", &reply.to_string());
+            return Ok(())
+        }
+        Some(CliDaoSubCommands::Create {}) => {
+            let reply = client.create().await?;
+            println!("Server replied: {}", &reply.to_string());
+            return Ok(())
+        }
+        Some(CliDaoSubCommands::Airdrop {}) => {
+            let reply = client.airdrop().await?;
+            println!("Server replied: {}", &reply.to_string());
+            return Ok(())
+        }
+        Some(CliDaoSubCommands::Propose {}) => {
+            let reply = client.propose().await?;
+            println!("Server replied: {}", &reply.to_string());
+            return Ok(())
+        }
+        Some(CliDaoSubCommands::Vote {}) => {
+            let reply = client.vote().await?;
+            println!("Server replied: {}", &reply.to_string());
+            return Ok(())
+        }
+        Some(CliDaoSubCommands::Exec {}) => {
+            let reply = client.exec().await?;
             println!("Server replied: {}", &reply.to_string());
             println!("Server replied: {}", &reply.to_string());
             return Ok(())
             return Ok(())
         }
         }

+ 49 - 0
bin/dao-cli/src/rpc.rs

@@ -0,0 +1,49 @@
+use serde_json::{json, Value};
+
+use darkfi::{rpc::jsonrpc::JsonRequest, Result};
+
+use crate::Rpc;
+
+impl Rpc {
+    // --> {"jsonrpc": "2.0", "method": "init", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "initializing...", "id": 42}
+    pub async fn init(&self) -> Result<Value> {
+        let req = JsonRequest::new("init", json!([]));
+        self.client.request(req).await
+    }
+
+    // --> {"jsonrpc": "2.0", "method": "create", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "creating dao...", "id": 42}
+    pub async fn create(&self) -> Result<Value> {
+        let req = JsonRequest::new("create", json!([]));
+        self.client.request(req).await
+    }
+
+    // --> {"jsonrpc": "2.0", "method": "airdrop", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "airdropping tokens...", "id": 42}
+    pub async fn airdrop(&self) -> Result<Value> {
+        let req = JsonRequest::new("airdrop", json!([]));
+        self.client.request(req).await
+    }
+
+    // --> {"jsonrpc": "2.0", "method": "propose", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "creating proposal...", "id": 42}
+    pub async fn propose(&self) -> Result<Value> {
+        let req = JsonRequest::new("propose", json!([]));
+        self.client.request(req).await
+    }
+
+    // --> {"jsonrpc": "2.0", "method": "vote", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "voting...", "id": 42}
+    pub async fn vote(&self) -> Result<Value> {
+        let req = JsonRequest::new("vote", json!([]));
+        self.client.request(req).await
+    }
+
+    // --> {"jsonrpc": "2.0", "method": "exec", "params": [], "id": 42}
+    // <-- {"jsonrpc": "2.0", "result": "executing...", "id": 42}
+    pub async fn exec(&self) -> Result<Value> {
+        let req = JsonRequest::new("exec", json!([]));
+        self.client.request(req).await
+    }
+}

+ 7 - 40
bin/daod/src/main.rs

@@ -1,29 +1,22 @@
 use std::sync::Arc;
 use std::sync::Arc;
 
 
-use async_trait::async_trait;
-use log::debug;
-use serde_json::{json, Value};
 use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
 use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
 use url::Url;
 use url::Url;
 
 
-use darkfi::{
-    rpc::{
-        jsonrpc::{ErrorCode::*, JsonError, JsonRequest, JsonResponse, JsonResult},
-        server::{listen_and_serve, RequestHandler},
-    },
-    Result,
-};
+use darkfi::{rpc::server::listen_and_serve, Result};
 
 
 mod dao_contract;
 mod dao_contract;
 mod example_contract;
 mod example_contract;
 mod money_contract;
 mod money_contract;
+mod rpc;
 
 
 mod demo;
 mod demo;
 mod note;
 mod note;
 
 
-use crate::demo::demo;
+use crate::rpc::JsonRpcInterface;
+// use crate::demo::demo;
 
 
-async fn _start() -> Result<()> {
+async fn start() -> Result<()> {
     let rpc_addr = Url::parse("tcp://127.0.0.1:7777")?;
     let rpc_addr = Url::parse("tcp://127.0.0.1:7777")?;
     let rpc_interface = Arc::new(JsonRpcInterface {});
     let rpc_interface = Arc::new(JsonRpcInterface {});
 
 
@@ -31,32 +24,6 @@ async fn _start() -> Result<()> {
     Ok(())
     Ok(())
 }
 }
 
 
-struct JsonRpcInterface {}
-
-#[async_trait]
-impl RequestHandler for JsonRpcInterface {
-    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
-        if req.params.as_array().is_none() {
-            return JsonError::new(InvalidParams, None, req.id).into()
-        }
-
-        debug!(target: "RPC", "--> {}", serde_json::to_string(&req).unwrap());
-
-        match req.method.as_str() {
-            Some("say_hello") => return self.say_hello(req.id, req.params).await,
-            Some(_) | None => return JsonError::new(MethodNotFound, None, req.id).into(),
-        }
-    }
-}
-
-impl JsonRpcInterface {
-    // --> {"method": "say_hello", "params": []}
-    // <-- {"result": "hello world"}
-    async fn say_hello(&self, id: Value, _params: Value) -> JsonResult {
-        JsonResponse::new(json!("hello world"), id).into()
-    }
-}
-
 #[async_std::main]
 #[async_std::main]
 async fn main() -> Result<()> {
 async fn main() -> Result<()> {
     TermLogger::init(
     TermLogger::init(
@@ -66,7 +33,7 @@ async fn main() -> Result<()> {
         ColorChoice::Auto,
         ColorChoice::Auto,
     )?;
     )?;
 
 
-    //start().await?;
-    demo().await.unwrap();
+    start().await?;
+    // demo().await.unwrap();
     Ok(())
     Ok(())
 }
 }

+ 64 - 0
bin/daod/src/rpc.rs

@@ -0,0 +1,64 @@
+use async_trait::async_trait;
+use log::debug;
+use serde_json::{json, Value};
+
+use darkfi::rpc::{
+    jsonrpc::{ErrorCode::*, JsonError, JsonRequest, JsonResponse, JsonResult},
+    server::RequestHandler,
+};
+
+pub struct JsonRpcInterface {}
+
+#[async_trait]
+impl RequestHandler for JsonRpcInterface {
+    async fn handle_request(&self, req: JsonRequest) -> JsonResult {
+        if req.params.as_array().is_none() {
+            return JsonError::new(InvalidParams, None, req.id).into()
+        }
+
+        debug!(target: "RPC", "--> {}", serde_json::to_string(&req).unwrap());
+
+        match req.method.as_str() {
+            Some("init") => return self.init(req.id, req.params).await,
+            Some("create") => return self.create_dao(req.id, req.params).await,
+            Some("airdrop") => return self.airdrop_tokens(req.id, req.params).await,
+            Some("propose") => return self.create_proposal(req.id, req.params).await,
+            Some("vote") => return self.vote(req.id, req.params).await,
+            Some("exec") => return self.execute(req.id, req.params).await,
+            Some(_) | None => return JsonError::new(MethodNotFound, None, req.id).into(),
+        }
+    }
+}
+
+impl JsonRpcInterface {
+    // --> {"method": "init", "params": []}
+    // <-- {"result": "initializeing..."}
+    async fn init(&self, id: Value, _params: Value) -> JsonResult {
+        JsonResponse::new(json!("initializeing..."), id).into()
+    }
+    // --> {"method": "create", "params": []}
+    // <-- {"result": "creating dao..."}
+    async fn create_dao(&self, id: Value, _params: Value) -> JsonResult {
+        JsonResponse::new(json!("creating dao..."), id).into()
+    }
+    // --> {"method": "airdrop_tokens", "params": []}
+    // <-- {"result": "airdropping tokens..."}
+    async fn airdrop_tokens(&self, id: Value, _params: Value) -> JsonResult {
+        JsonResponse::new(json!("airdropping tokens..."), id).into()
+    }
+    // --> {"method": "create_proposal", "params": []}
+    // <-- {"result": "creating proposal..."}
+    async fn create_proposal(&self, id: Value, _params: Value) -> JsonResult {
+        JsonResponse::new(json!("creating proposal..."), id).into()
+    }
+    // --> {"method": "vote", "params": []}
+    // <-- {"result": "voting..."}
+    async fn vote(&self, id: Value, _params: Value) -> JsonResult {
+        JsonResponse::new(json!("voting..."), id).into()
+    }
+    // --> {"method": "execute", "params": []}
+    // <-- {"result": "executing..."}
+    async fn execute(&self, id: Value, _params: Value) -> JsonResult {
+        JsonResponse::new(json!("executing..."), id).into()
+    }
+}