mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
tokens: Make TokenList generic.
This commit is contained in:
@@ -29,7 +29,7 @@ use drk::{
|
||||
state::ProgramState,
|
||||
util::{
|
||||
assign_id, decode_base10, encode_base10, expand_path, join_config_path, DrkTokenList,
|
||||
NetworkName, SolTokenList,
|
||||
NetworkName, TokenList,
|
||||
},
|
||||
wallet::WalletDb,
|
||||
Error, Result,
|
||||
@@ -70,7 +70,7 @@ impl RequestHandler for Darkfid {
|
||||
struct Darkfid {
|
||||
client: Arc<Mutex<Client>>,
|
||||
state: Arc<Mutex<State>>,
|
||||
sol_tokenlist: SolTokenList,
|
||||
sol_tokenlist: TokenList,
|
||||
drk_tokenlist: DrkTokenList,
|
||||
cashiers: Vec<Cashier>,
|
||||
}
|
||||
@@ -81,7 +81,7 @@ impl Darkfid {
|
||||
state: Arc<Mutex<State>>,
|
||||
cashiers: Vec<Cashier>,
|
||||
) -> Result<Self> {
|
||||
let sol_tokenlist = SolTokenList::new()?;
|
||||
let sol_tokenlist = TokenList::new(include_bytes!("../../token/solana_token_list.json"))?;
|
||||
let drk_tokenlist = DrkTokenList::new(sol_tokenlist.clone())?;
|
||||
|
||||
Ok(Self {
|
||||
|
||||
@@ -6,4 +6,4 @@ pub mod token_list;
|
||||
pub use net_name::NetworkName;
|
||||
pub use parse::{assign_id, decode_base10, encode_base10, generate_id};
|
||||
pub use path::{expand_path, join_config_path};
|
||||
pub use token_list::{DrkTokenList, SolTokenList};
|
||||
pub use token_list::{DrkTokenList, TokenList};
|
||||
|
||||
@@ -6,7 +6,7 @@ use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::{
|
||||
serial::{deserialize, serialize},
|
||||
util::{NetworkName, SolTokenList},
|
||||
util::{NetworkName, TokenList},
|
||||
Error, Result,
|
||||
};
|
||||
|
||||
@@ -43,7 +43,7 @@ pub fn generate_id(tkn_str: &str, network: &NetworkName) -> Result<jubjub::Fr> {
|
||||
Ok(token_id)
|
||||
}
|
||||
|
||||
pub fn assign_id(network: &str, token: &str, _tokenlist: &SolTokenList) -> Result<String> {
|
||||
pub fn assign_id(network: &str, token: &str, _tokenlist: &TokenList) -> Result<String> {
|
||||
let token = token.to_lowercase();
|
||||
let _token = token.as_str();
|
||||
match NetworkName::from_str(network)? {
|
||||
@@ -66,7 +66,7 @@ pub fn assign_id(network: &str, token: &str, _tokenlist: &SolTokenList) -> Resul
|
||||
}
|
||||
}
|
||||
|
||||
pub fn symbol_to_id(token: &str, tokenlist: &SolTokenList) -> Result<String> {
|
||||
pub fn symbol_to_id(token: &str, tokenlist: &TokenList) -> Result<String> {
|
||||
let vec: Vec<char> = token.chars().collect();
|
||||
let mut counter = 0;
|
||||
for c in vec {
|
||||
|
||||
@@ -8,19 +8,17 @@ use crate::{
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SolTokenList {
|
||||
pub struct TokenList {
|
||||
tokens: Vec<Value>,
|
||||
}
|
||||
|
||||
impl SolTokenList {
|
||||
pub fn new() -> Result<Self> {
|
||||
let file_contents = include_bytes!("../../token/solanatokenlist.json");
|
||||
let sol_tokenlist: Value = serde_json::from_slice(file_contents)?;
|
||||
let tokens = sol_tokenlist["tokens"]
|
||||
impl TokenList {
|
||||
pub fn new(data: &[u8]) -> Result<Self> {
|
||||
let tokenlist: Value = serde_json::from_slice(data)?;
|
||||
let tokens = tokenlist["tokens"]
|
||||
.as_array()
|
||||
.ok_or(Error::TokenParseError)?
|
||||
.clone();
|
||||
|
||||
Ok(Self { tokens })
|
||||
}
|
||||
|
||||
@@ -28,12 +26,7 @@ impl SolTokenList {
|
||||
let mut symbols: Vec<String> = Vec::new();
|
||||
for item in self.tokens.iter() {
|
||||
let symbol = item["symbol"].as_str().unwrap();
|
||||
// FIXME: Change Sollet BTC to SBTC?
|
||||
if symbol == "BTC" {
|
||||
symbols.push("SBTC".to_string());
|
||||
} else {
|
||||
symbols.push(symbol.to_string());
|
||||
}
|
||||
symbols.push(symbol.to_string());
|
||||
}
|
||||
Ok(symbols)
|
||||
}
|
||||
@@ -68,7 +61,7 @@ pub struct DrkTokenList {
|
||||
}
|
||||
|
||||
impl DrkTokenList {
|
||||
pub fn new(sol_list: SolTokenList) -> Result<Self> {
|
||||
pub fn new(sol_list: TokenList) -> Result<Self> {
|
||||
let sol_symbols = sol_list.get_symbols()?;
|
||||
|
||||
let mut tokens: HashMap<String, jubjub::Fr> = sol_symbols
|
||||
@@ -84,7 +77,7 @@ impl DrkTokenList {
|
||||
Ok(Self { tokens })
|
||||
}
|
||||
|
||||
fn generate_hash_pair(sol_list: &SolTokenList, symbol: &str) -> Result<(String, jubjub::Fr)> {
|
||||
fn generate_hash_pair(sol_list: &TokenList, symbol: &str) -> Result<(String, jubjub::Fr)> {
|
||||
if let Some(token_id) = &sol_list.search_id(symbol)? {
|
||||
Ok((
|
||||
symbol.to_string(),
|
||||
@@ -110,10 +103,10 @@ impl DrkTokenList {
|
||||
#[allow(unused_imports)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::util::{DrkTokenList, SolTokenList};
|
||||
use crate::util::{DrkTokenList, TokenList};
|
||||
use crate::Result;
|
||||
|
||||
fn _get_tokens() -> Result<SolTokenList> {
|
||||
fn _get_tokens() -> Result<TokenList> {
|
||||
let file_contents = include_bytes!("../../token/solanatokenlisttest.json");
|
||||
let sol_tokenlist: Value = serde_json::from_slice(file_contents)?;
|
||||
|
||||
@@ -122,7 +115,7 @@ mod tests {
|
||||
.ok_or(Error::TokenParseError)?
|
||||
.clone();
|
||||
|
||||
let sol_tokenlist = SolTokenList { tokens };
|
||||
let sol_tokenlist = TokenList { tokens };
|
||||
Ok(sol_tokenlist)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user