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

Fix amount_in_apo in util.rs.

And some small lints/cleanups.
parazyd 4 лет назад
Родитель
Сommit
a45f9d3144
3 измененных файлов с 17 добавлено и 20 удалено
  1. 7 7
      src/util.rs
  2. 7 10
      src/wallet/cashierdb.rs
  3. 3 3
      src/wallet/walletdb.rs

+ 7 - 7
src/util.rs

@@ -167,14 +167,14 @@ pub fn parse_params(network: &str, token: &str, amount: u64) -> Result<(String,
             "solana" | "sol" => {
                 let token_id = "So11111111111111111111111111111111111111112";
                 let decimals = 9;
-                let amount_in_apo: u64 = amount * 10 ^ decimals;
+                let amount_in_apo = amount * u64::pow(10, decimals as u32);
                 Ok((token_id.to_string(), amount_in_apo))
             }
             tkn => {
                 let token_id = symbol_to_id(tkn)?;
                 let decimals = search_decimal(tkn)?;
-                let amount_in_apo: u64 = amount * 10 ^ decimals;
-                Ok((token_id.to_string(), amount_in_apo))
+                let amount_in_apo = amount * u64::pow(10, decimals as u32);
+                Ok((token_id, amount_in_apo))
             }
         },
         NetworkName::Bitcoin => Err(Error::NetworkParseError),
@@ -202,11 +202,11 @@ pub fn search_id(symbol: &str) -> Result<String> {
     let tokenlist: serde_json::Value = serde_json::from_str(&file_contents)?;
     let tokens = tokenlist["tokens"]
         .as_array()
-        .ok_or_else(|| Error::TokenParseError)?;
+        .ok_or(Error::TokenParseError)?;
     for item in tokens {
         if item["symbol"] == symbol.to_uppercase() {
             let address = item["address"].clone();
-            let address = address.as_str().ok_or_else(|| Error::TokenParseError)?;
+            let address = address.as_str().ok_or(Error::TokenParseError)?;
             return Ok(address.to_string());
         }
     }
@@ -219,11 +219,11 @@ pub fn search_decimal(symbol: &str) -> Result<u64> {
     let tokenlist: serde_json::Value = serde_json::from_str(&file_contents)?;
     let tokens = tokenlist["tokens"]
         .as_array()
-        .ok_or_else(|| Error::TokenParseError)?;
+        .ok_or(Error::TokenParseError)?;
     for item in tokens {
         if item["symbol"] == symbol.to_uppercase() {
             let decimals = item["decimals"].clone();
-            let decimals = decimals.as_u64().ok_or_else(|| Error::TokenParseError)?;
+            let decimals = decimals.as_u64().ok_or(Error::TokenParseError)?;
             return Ok(decimals);
         }
     }

+ 7 - 10
src/wallet/cashierdb.rs

@@ -1,7 +1,7 @@
 use super::{Keypair, WalletApi};
 use crate::client::ClientFailed;
-use crate::{Error, Result};
 use crate::util::NetworkName;
+use crate::{Error, Result};
 
 use async_std::sync::{Arc, Mutex};
 use log::*;
@@ -37,7 +37,7 @@ impl CashierDb {
     }
 
     pub async fn init_db(&self) -> Result<()> {
-        if *self.initialized.lock().await == false {
+        if !*self.initialized.lock().await {
             if !self.password.trim().is_empty() {
                 let contents = include_str!("../../sql/cashier.sql");
                 let conn = Connection::open(&self.path)?;
@@ -88,10 +88,7 @@ impl CashierDb {
         Ok(())
     }
 
-    pub fn get_main_keys(
-        &self,
-        network: &NetworkName,
-    ) -> Result<Vec<(Vec<u8>, Vec<u8>)>> {
+    pub fn get_main_keys(&self, network: &NetworkName) -> Result<Vec<(Vec<u8>, Vec<u8>)>> {
         debug!(target: "CASHIERDB", "Get main keys");
         // open connection
         let conn = Connection::open(&self.path)?;
@@ -105,10 +102,10 @@ impl CashierDb {
             FROM main_keypairs
             WHERE network = :network ;",
         )?;
-        let keys_iter = stmt.query_map::<(Vec<u8>, Vec<u8>), _, _>(
-            &[(":network", &network)],
-            |row| Ok((row.get(0)?, row.get(1)?)),
-        )?;
+        let keys_iter = stmt
+            .query_map::<(Vec<u8>, Vec<u8>), _, _>(&[(":network", &network)], |row| {
+                Ok((row.get(0)?, row.get(1)?))
+            })?;
 
         let mut keys = vec![];
 

+ 3 - 3
src/wallet/walletdb.rs

@@ -49,7 +49,7 @@ impl WalletDb {
     }
 
     pub async fn init_db(&self) -> Result<()> {
-        if *self.initialized.lock().await == false {
+        if !*self.initialized.lock().await {
             if !self.password.trim().is_empty() {
                 let contents = include_str!("../../sql/schema.sql");
                 let conn = Connection::open(&self.path)?;
@@ -77,12 +77,12 @@ impl WalletDb {
         conn.pragma_update(None, "key", &self.password)?;
         let mut stmt = conn.prepare("SELECT * FROM keys WHERE key_id > :id")?;
         let key_check = stmt.exists(&[(":id", &"0")])?;
-        if key_check == false {
+        if !key_check {
             let secret: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
             let public = zcash_primitives::constants::SPENDING_KEY_GENERATOR * secret;
             let pubkey = serial::serialize(&public);
             let privkey = serial::serialize(&secret);
-            self.put_keypair(pubkey.clone(), privkey.clone())?;
+            self.put_keypair(pubkey, privkey)?;
         } else {
             debug!(target: "WALLETDB", "Keys already exist.");
             return Err(Error::from(ClientFailed::KeyExists));