aggstam 3 лет назад
Родитель
Сommit
4292006959
2 измененных файлов с 19 добавлено и 9 удалено
  1. 5 9
      bin/drk/src/main.rs
  2. 14 0
      bin/drk/src/wallet_money.rs

+ 5 - 9
bin/drk/src/main.rs

@@ -609,9 +609,8 @@ async fn main() -> Result<()> {
 
         Subcmd::Airdrop { faucet_endpoint, amount, token, address } => {
             let amount = f64::from_str(&amount).with_context(|| "Invalid amount")?;
-            let token_id = TokenId::try_from(token.as_str()).with_context(|| "Invalid Token ID")?;
-
             let drk = Drk::new(args.endpoint).await?;
+            let token_id = drk.get_token(token).await.with_context(|| "Invalid Token ID")?;
 
             let address = match address {
                 Some(v) => PublicKey::from_str(v.as_str()).with_context(|| "Invalid address")?,
@@ -632,10 +631,9 @@ async fn main() -> Result<()> {
 
         Subcmd::Transfer { amount, token, recipient, dao, dao_bulla } => {
             let _ = f64::from_str(&amount).with_context(|| "Invalid amount")?;
-            let token_id = TokenId::try_from(token.as_str()).with_context(|| "Invalid Token ID")?;
             let rcpt = PublicKey::from_str(&recipient).with_context(|| "Invalid recipient")?;
-
             let drk = Drk::new(args.endpoint).await?;
+            let token_id = drk.get_token(token).await.with_context(|| "Invalid Token ID")?;
 
             let tx = drk
                 .transfer(&amount, token_id, rcpt, dao, dao_bulla)
@@ -786,8 +784,9 @@ async fn main() -> Result<()> {
                 let approval_ratio_base = 100_u64;
                 let approval_ratio_quot = (approval_ratio * approval_ratio_base as f64) as u64;
 
+                let drk = Drk::new(args.endpoint).await?;
                 let gov_token_id =
-                    TokenId::try_from(gov_token_id.as_str()).with_context(|| "Invalid Token ID")?;
+                    drk.get_token(gov_token_id).await.with_context(|| "Invalid Token ID")?;
 
                 let secret_key = SecretKey::random(&mut OsRng);
                 let bulla_blind = pallas::Base::random(&mut OsRng);
@@ -886,12 +885,9 @@ async fn main() -> Result<()> {
             DaoSubcmd::Propose { dao_id, recipient, amount, token_id } => {
                 let _ = f64::from_str(&amount).with_context(|| "Invalid amount")?;
                 let amount = decode_base10(&amount, 8, true)?;
-
                 let rcpt = PublicKey::from_str(&recipient).with_context(|| "Invalid recipient")?;
-                let token_id =
-                    TokenId::try_from(token_id.as_str()).with_context(|| "Invalid Token ID")?;
-
                 let drk = Drk::new(args.endpoint).await?;
+                let token_id = drk.get_token(token_id).await.with_context(|| "Invalid Token ID")?;
 
                 let tx = drk
                     .dao_propose(dao_id, rcpt, amount, token_id)

+ 14 - 0
bin/drk/src/wallet_money.rs

@@ -736,6 +736,20 @@ impl Drk {
         Ok(map)
     }
 
+    /// Retrieve token by provided string.
+    /// Input string represents either an alias or a token id.
+    pub async fn get_token(&self, input: String) -> Result<TokenId> {
+        // Check if input is an alias(max 5 characters)
+        if input.chars().count() <= 5 {
+            let aliases = self.get_aliases(Some(input.clone()), None).await?;
+            if let Some(token_id) = aliases.get(&input) {
+                return Ok(token_id.clone())
+            }
+        }
+        // Else parse input
+        Ok(TokenId::try_from(input.as_str())?)
+    }
+
     /// Create an alias record for provided Token ID
     pub async fn remove_alias(&self, alias: String) -> Result<()> {
         eprintln!("Removing alias: {}", alias);