Prechádzať zdrojové kódy

eth: implement EthFailed and EthResult

ghassmo 4 rokov pred
rodič
commit
8b48849c77
2 zmenil súbory, kde vykonal 91 pridanie a 17 odobranie
  1. 89 17
      src/service/eth.rs
  2. 2 0
      src/service/mod.rs

+ 89 - 17
src/service/eth.rs

@@ -194,7 +194,6 @@ impl EthClient {
 
 
     async fn handle_subscribe_request(
     async fn handle_subscribe_request(
         self: Arc<Self>,
         self: Arc<Self>,
-        private: String,
         addr: String,
         addr: String,
         drk_pub_key: jubjub::SubgroupPoint,
         drk_pub_key: jubjub::SubgroupPoint,
     ) -> Result<()> {
     ) -> Result<()> {
@@ -239,7 +238,6 @@ impl EthClient {
 
 
         let amnt = current_balance - prev_balance;
         let amnt = current_balance - prev_balance;
 
 
-
         send_notification
         send_notification
             .send(TokenNotification {
             .send(TokenNotification {
                 network: NetworkName::Ethereum,
                 network: NetworkName::Ethereum,
@@ -264,9 +262,11 @@ impl EthClient {
         }
         }
     }
     }
 
 
-    async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
+    async fn request(&self, r: jsonrpc::JsonRequest) -> EthResult<Value> {
         debug!(target: "ETH RPC", "--> {}", serde_json::to_string(&r)?);
         debug!(target: "ETH RPC", "--> {}", serde_json::to_string(&r)?);
-        let reply: JsonResult = match jsonrpc::send_unix_request(&self.socket_path, json!(r)).await
+        let reply: JsonResult = match jsonrpc::send_unix_request(&self.socket_path, json!(r))
+            .await
+            .map_err(EthFailed::from)
         {
         {
             Ok(v) => v,
             Ok(v) => v,
             Err(e) => return Err(e),
             Err(e) => return Err(e),
@@ -280,17 +280,17 @@ impl EthClient {
 
 
             JsonResult::Err(e) => {
             JsonResult::Err(e) => {
                 debug!(target: "ETH RPC", "<-- {}", serde_json::to_string(&e)?);
                 debug!(target: "ETH RPC", "<-- {}", serde_json::to_string(&e)?);
-                Err(Error::JsonRpcError(e.error.message.to_string()))
+                Err(EthFailed::RpcError(e.error.message.to_string()))
             }
             }
 
 
             JsonResult::Notif(n) => {
             JsonResult::Notif(n) => {
                 debug!(target: "ETH RPC", "<-- {}", serde_json::to_string(&n)?);
                 debug!(target: "ETH RPC", "<-- {}", serde_json::to_string(&n)?);
-                Err(Error::JsonRpcError("Unexpected reply".to_string()))
+                Err(EthFailed::RpcError("Unexpected reply".to_string()))
             }
             }
         }
         }
     }
     }
 
 
-    pub async fn import_privkey(&self, key: &str, passphrase: &str) -> Result<Value> {
+    pub async fn import_privkey(&self, key: &str, passphrase: &str) -> EthResult<Value> {
         let req = jsonrpc::request(json!("personal_importRawKey"), json!([key, passphrase]));
         let req = jsonrpc::request(json!("personal_importRawKey"), json!([key, passphrase]));
         Ok(self.request(req).await?)
         Ok(self.request(req).await?)
     }
     }
@@ -302,17 +302,17 @@ impl EthClient {
     }
     }
     */
     */
 
 
-    pub async fn block_number(&self) -> Result<Value> {
+    pub async fn block_number(&self) -> EthResult<Value> {
         let req = jsonrpc::request(json!("eth_blockNumber"), json!([]));
         let req = jsonrpc::request(json!("eth_blockNumber"), json!([]));
         Ok(self.request(req).await?)
         Ok(self.request(req).await?)
     }
     }
 
 
-    pub async fn get_eth_balance(&self, acc: &str, block: &str) -> Result<Value> {
+    pub async fn get_eth_balance(&self, acc: &str, block: &str) -> EthResult<Value> {
         let req = jsonrpc::request(json!("eth_getBalance"), json!([acc, block]));
         let req = jsonrpc::request(json!("eth_getBalance"), json!([acc, block]));
         Ok(self.request(req).await?)
         Ok(self.request(req).await?)
     }
     }
 
 
-    pub async fn get_erc20_balance(&self, acc: &str, mint: &str) -> Result<Value> {
+    pub async fn get_erc20_balance(&self, acc: &str, mint: &str) -> EthResult<Value> {
         let tx = EthTx::new(
         let tx = EthTx::new(
             acc,
             acc,
             mint,
             mint,
@@ -326,7 +326,7 @@ impl EthClient {
         Ok(self.request(req).await?)
         Ok(self.request(req).await?)
     }
     }
 
 
-    pub async fn get_current_balance(&self, acc: &str, _mint: Option<&str>) -> Result<BigUint> {
+    pub async fn get_current_balance(&self, acc: &str, _mint: Option<&str>) -> EthResult<BigUint> {
         // Latest known block, used to calculate present balance.
         // Latest known block, used to calculate present balance.
         let block = self.block_number().await?;
         let block = self.block_number().await?;
         let block = block.as_str().unwrap();
         let block = block.as_str().unwrap();
@@ -339,7 +339,7 @@ impl EthClient {
         Ok(balance)
         Ok(balance)
     }
     }
 
 
-    pub async fn send_transaction(&self, tx: &EthTx, passphrase: &str) -> Result<Value> {
+    pub async fn send_transaction(&self, tx: &EthTx, passphrase: &str) -> EthResult<Value> {
         let req = jsonrpc::request(json!("personal_sendTransaction"), json!([tx, passphrase]));
         let req = jsonrpc::request(json!("personal_sendTransaction"), json!([tx, passphrase]));
         Ok(self.request(req).await?)
         Ok(self.request(req).await?)
     }
     }
@@ -363,15 +363,14 @@ impl NetworkClient for EthClient {
             .unwrap()
             .unwrap()
             .to_string();
             .to_string();
 
 
-        let private = private_key.clone();
         let addr_cloned = addr.clone();
         let addr_cloned = addr.clone();
         executor
         executor
             .spawn(async move {
             .spawn(async move {
                 let result = self
                 let result = self
-                    .handle_subscribe_request(private, addr_cloned, drk_pub_key)
+                    .handle_subscribe_request(addr_cloned, drk_pub_key)
                     .await;
                     .await;
                 if let Err(e) = result {
                 if let Err(e) = result {
-                    error!(target: "SOL BRIDGE SUBSCRIPTION","{}", e.to_string());
+                    error!(target: "ETH BRIDGE SUBSCRIPTION","{}", e.to_string());
                 }
                 }
             })
             })
             .detach();
             .detach();
@@ -388,11 +387,22 @@ impl NetworkClient for EthClient {
         self: Arc<Self>,
         self: Arc<Self>,
         _private_key: Vec<u8>,
         _private_key: Vec<u8>,
         public_key: Vec<u8>,
         public_key: Vec<u8>,
-        _drk_pub_key: jubjub::SubgroupPoint,
+        drk_pub_key: jubjub::SubgroupPoint,
         _mint_address: Option<String>,
         _mint_address: Option<String>,
-        _executor: Arc<Executor<'_>>,
+        executor: Arc<Executor<'_>>,
     ) -> Result<String> {
     ) -> Result<String> {
         let public_key: String = deserialize(&public_key)?;
         let public_key: String = deserialize(&public_key)?;
+
+        let address = public_key.clone();
+        executor
+            .spawn(async move {
+                let result = self.handle_subscribe_request(address, drk_pub_key).await;
+                if let Err(e) = result {
+                    error!(target: "ETH BRIDGE SUBSCRIPTION","{}", e.to_string());
+                }
+            })
+            .detach();
+
         Ok(public_key)
         Ok(public_key)
     }
     }
 
 
@@ -410,6 +420,68 @@ impl NetworkClient for EthClient {
     }
     }
 }
 }
 
 
+#[derive(Debug)]
+pub enum EthFailed {
+    NotEnoughValue(u64),
+    MainAccountNotEnoughValue,
+    BadEthAddress(String),
+    DecodeAndEncodeError(String),
+    RpcError(String),
+    EthClientError(String),
+    MintIsNotValid(String),
+    JsonError(String),
+    ParseError(String),
+}
+
+impl std::error::Error for EthFailed {}
+
+impl std::fmt::Display for EthFailed {
+    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
+        match self {
+            EthFailed::NotEnoughValue(i) => {
+                write!(f, "There is no enough value {}", i)
+            }
+            EthFailed::MainAccountNotEnoughValue => {
+                write!(f, "Main Account Has no enough value")
+            }
+            EthFailed::BadEthAddress(ref err) => {
+                write!(f, "Bad Eth Address: {}", err)
+            }
+            EthFailed::DecodeAndEncodeError(ref err) => {
+                write!(f, "Decode and decode keys error: {}", err)
+            }
+            EthFailed::RpcError(i) => {
+                write!(f, "Rpc Error: {}", i)
+            }
+            EthFailed::ParseError(i) => {
+                write!(f, "Parse Error: {}", i)
+            }
+            EthFailed::MintIsNotValid(i) => {
+                write!(f, "Given mint is not valid: {}", i)
+            }
+            EthFailed::JsonError(i) => {
+                write!(f, "JsonError: {}", i)
+            }
+            EthFailed::EthClientError(i) => {
+                write!(f, "Eth client error: {}", i)
+            }
+        }
+    }
+}
+
+impl From<crate::error::Error> for EthFailed {
+    fn from(err: crate::error::Error) -> EthFailed {
+        EthFailed::EthClientError(err.to_string())
+    }
+}
+impl From<serde_json::Error> for EthFailed {
+    fn from(err: serde_json::Error) -> EthFailed {
+        EthFailed::JsonError(err.to_string())
+    }
+}
+
+pub type EthResult<T> = std::result::Result<T, EthFailed>;
+
 #[allow(unused_imports)]
 #[allow(unused_imports)]
 mod tests {
 mod tests {
     use super::*;
     use super::*;

+ 2 - 0
src/service/mod.rs

@@ -15,5 +15,7 @@ pub use sol::{SolClient, SolFailed, SolResult};
 
 
 #[cfg(feature = "eth")]
 #[cfg(feature = "eth")]
 pub mod eth;
 pub mod eth;
+#[cfg(feature = "eth")]
+pub use eth::{EthClient, EthFailed, EthResult};
 
 
 pub use gateway::{GatewayClient, GatewayService, GatewaySlabsSubscriber};
 pub use gateway::{GatewayClient, GatewayService, GatewaySlabsSubscriber};