From 02ad07e01ff521c93ddfa8269e8fb6fb5b5c480c Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Thu, 7 Oct 2021 11:32:24 +0200 Subject: [PATCH] tokenlist: symbol_from_id --- src/util/token_list.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/util/token_list.rs b/src/util/token_list.rs index f099d812d..83d770e66 100644 --- a/src/util/token_list.rs +++ b/src/util/token_list.rs @@ -43,21 +43,6 @@ impl SolTokenList { Ok(None) } - // pub fn search_all_id(&self, symbol: &str) -> Result> { - // let tokens = self.sol_tokenlist["tokens"] - // .as_array() - // .ok_or(Error::TokenParseError)?; - // let mut ids = Vec::new(); - // for item in tokens { - // if item["symbol"] == symbol.to_uppercase() { - // let address = item["address"].clone(); - // let address = address.as_str().ok_or(Error::TokenParseError)?; - // ids.push(address.to_string()); - // } - // } - // return Ok(ids); - // } - pub fn search_decimal(&self, symbol: &str) -> Result> { for item in self.tokens.iter() { if item["symbol"] == symbol.to_uppercase() { @@ -77,7 +62,6 @@ pub struct DrkTokenList { impl DrkTokenList { pub fn new(sol_list: SolTokenList) -> Result { - // get symbols let sol_symbols = sol_list.clone().get_symbols()?; let tokens: HashMap = sol_symbols @@ -98,6 +82,13 @@ impl DrkTokenList { Err(Error::NotSupportedToken) } } + + pub fn symbol_from_id(self, id: jubjub::Fr) -> Result> { + Ok(self + .tokens + .iter() + .find_map(|(key, &val)| if val == id { Some(key.clone()) } else { None })) + } } #[allow(unused_imports)]