Просмотр исходного кода

SolClient: send received token to the main account's address

ghassmo 4 лет назад
Родитель
Сommit
ae501a5905
1 измененных файлов с 19 добавлено и 39 удалено
  1. 19 39
      src/service/sol.rs

+ 19 - 39
src/service/sol.rs

@@ -7,7 +7,6 @@ use super::bridge::CoinClient;
 use async_trait::async_trait;
 
 use async_executor::Executor;
-use ed25519_dalek::SecretKey;
 use futures::{SinkExt, StreamExt};
 use log::*;
 use rand::rngs::OsRng;
@@ -41,8 +40,8 @@ struct SubscribeParams {
 pub struct SolClient {
     keypair: Keypair,
 
-    // subscription hashmap with pubkey and balance
-    subscriptions: Arc<Mutex<HashMap<String, u64>>>,
+    // subscription hashmap using pubkey as an index
+    subscriptions: Arc<Mutex<HashMap<String, (Vec<u8>, u64)>>>,
 
     // notify when get new update
     notify_channel: (
@@ -129,13 +128,17 @@ impl SolClient {
                     // TODO remove unwrap
                     let new_bal = n.params["result"]["value"]["lamports"].as_u64().unwrap();
                     let owner_pubkey = n.params["result"]["value"]["owner"].as_str().unwrap();
-                    let old_balance = self.subscriptions.lock().await[owner_pubkey];
+                    let (keypair, old_balance) =
+                        self.subscriptions.lock().await[owner_pubkey].clone();
 
                     if new_bal > old_balance {
                         let sub_id = n.params["subscription"].as_u64().unwrap();
                         let received_balance = new_bal - old_balance;
 
-                        // TODO Send the received coins to the main address
+                        let keypair: Keypair = deserialize(&keypair).expect("deserialize keypair");
+
+                        self.send_to_main_account(keypair)
+                            .expect("Send to main account");
 
                         self.notify_channel
                             .0
@@ -144,7 +147,7 @@ impl SolClient {
                                 received_balance,
                             ))
                             .await
-                            .expect(" send notify msg");
+                            .expect("send notify msg");
 
                         SolClient::unsubscribe(self.watch_channel.0.clone(), sub_id)
                             .await
@@ -168,15 +171,13 @@ impl SolClient {
         Ok(())
     }
 
-    async fn send_to_main_account(
-        &self,
-        keypair: Keypair,
-    ) -> Result<()> {
+    fn send_to_main_account(&self, keypair: Keypair) -> Result<()> {
         let rpc = RpcClient::new(RPC_SERVER.to_string());
-        
+
         let amount = rpc.get_balance(&keypair.pubkey()).unwrap();
 
-        let instruction = system_instruction::transfer(&keypair.pubkey(), &self.keypair.pubkey(), amount);
+        let instruction =
+            system_instruction::transfer(&keypair.pubkey(), &self.keypair.pubkey(), amount);
 
         let mut tx = Transaction::new_with_payer(&[instruction], Some(&keypair.pubkey()));
         let bhq = BlockhashQuery::default();
@@ -220,16 +221,16 @@ impl CoinClient for SolClient {
         let rpc = RpcClient::new(RPC_SERVER.to_string());
         let balance = rpc.get_balance(&keypair.pubkey()).unwrap();
 
-        self.subscriptions
-            .lock()
-            .await
-            .insert(keypair.pubkey().to_string(), balance);
+        self.subscriptions.lock().await.insert(
+            keypair.pubkey().to_string(),
+            (serialize(&keypair), balance),
+        );
 
         self.watch_channel.0.send(sub_msg).await?;
 
         let pubkey = serialize(&keypair.pubkey());
-        let private_key = serialize(keypair.secret());
-        Ok((pubkey, private_key))
+        let keypair = serialize(&keypair);
+        Ok((pubkey, keypair))
     }
 
     async fn send(&self, address: Vec<u8>, amount: u64) -> Result<()> {
@@ -249,7 +250,6 @@ impl CoinClient for SolClient {
             .expect("send transaction");
         Ok(())
     }
-
 }
 
 impl Encodable for Keypair {
@@ -292,26 +292,6 @@ impl Decodable for Pubkey {
     }
 }
 
-impl Encodable for SecretKey {
-    fn encode<S: std::io::Write>(&self, s: S) -> Result<usize> {
-        let key = self.to_bytes();
-        let len = key.encode(s)?;
-        Ok(len)
-    }
-}
-
-impl Decodable for SecretKey {
-    fn decode<D: std::io::Read>(mut d: D) -> Result<Self> {
-        let key: Vec<u8> = Decodable::decode(&mut d)?;
-        let key = SecretKey::from_bytes(key.as_slice()).map_err(|_| {
-            crate::Error::from(SolFailed::DecodeAndEncodeError(
-                "load secret key from slice".into(),
-            ))
-        })?;
-        Ok(key)
-    }
-}
-
 #[derive(Debug)]
 pub enum SolFailed {
     NotEnoughValue(u64),