From 873d259bd261e9c90baef35edd6e502a3164cca7 Mon Sep 17 00:00:00 2001 From: lunar-mining Date: Wed, 29 Sep 2021 08:56:56 +0200 Subject: [PATCH] src/util: made TokenList struct called in darkfid::main() --- src/bin/darkfid.rs | 6 +++++- src/util.rs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/bin/darkfid.rs b/src/bin/darkfid.rs index cd29b870d..2d99197b1 100644 --- a/src/bin/darkfid.rs +++ b/src/bin/darkfid.rs @@ -17,7 +17,9 @@ use drk::{ rpcserver::{listen_and_serve, RequestHandler, RpcServerConfig}, }, serial::{deserialize, serialize}, - util::{expand_path, join_config_path, parse_network, parse_wrapped_token, search_id}, + util::{ + expand_path, join_config_path, parse_network, parse_wrapped_token, search_id, TokenList, + }, wallet::WalletDb, Result, }; @@ -349,6 +351,8 @@ impl Darkfid { #[async_std::main] async fn main() -> Result<()> { + let _tokens = TokenList::new()?; + let args = clap_app!(darkfid => (@arg CONFIG: -c --config +takes_value "Sets a custom config file") (@arg verbose: -v --verbose "Increase verbosity") diff --git a/src/util.rs b/src/util.rs index 08ceb460d..88eaa8cb6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,9 +6,22 @@ use crate::{ use log::debug; use sha2::{Digest, Sha256}; +use serde_json::Value; use std::path::{Path, PathBuf}; use std::str::FromStr; +pub struct TokenList { + tokenlist: Value, +} + +impl TokenList { + pub fn new() -> Result { + let file_contents = std::fs::read_to_string("token/solanatokenlist.json")?; + let tokenlist: serde_json::Value = serde_json::from_str(&file_contents)?; + Ok(Self { tokenlist }) + } +} + pub fn expand_path(path: &str) -> Result { let ret: PathBuf; @@ -39,7 +52,6 @@ pub fn join_config_path(file: &Path) -> Result { Ok(path) } - #[derive(Debug, PartialEq, Eq, Hash, Clone)] pub enum NetworkName { Solana, @@ -205,6 +217,25 @@ pub fn search_id(symbol: &str) -> Result { unreachable!(); } +// TODO: implement this + +//pub fn search_decimals(symbol: &str) -> Result { +// // TODO: FIXME +// let file_contents = std::fs::read_to_string("token/solanatokenlist.json")?; +// let tokenlist: serde_json::Value = serde_json::from_str(&file_contents)?; +// let tokens = tokenlist["tokens"] +// .as_array() +// .ok_or_else(|| 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)?; +// return Ok(address.to_string()); +// } +// } +// unreachable!(); +//} + #[cfg(test)] mod tests { use crate::serial::{deserialize, serialize};