Skip to content

Commit

Permalink
Add asset type and token_id param
Browse files Browse the repository at this point in the history
  • Loading branch information
gemcoder21 committed Sep 6, 2023
1 parent 0427ba0 commit 4438e66
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 9 deletions.
2 changes: 1 addition & 1 deletion deamon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub async fn main() {
Err(err) => { println!("update prices cache error: {}", err) }
}

let result = tokenlist_client.update_versions().await;
let result = tokenlist_client.update().await;
match result {
Ok(count) => { println!("update tokenlist versions: {}", count) }
Err(err) => { println!("update tokenlist versions error: {}", err) }
Expand Down
11 changes: 7 additions & 4 deletions deamon/src/tokenlist_updater.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::error::Error;
use storage::database::DatabaseClient;
use storage::{database::DatabaseClient, models::Asset};
use api_connector::AssetsClient;

pub struct Client<'a> {
Expand All @@ -16,11 +16,14 @@ impl<'a> Client<'a> {
}
}

pub async fn update_versions(&mut self) -> Result<usize, Box<dyn Error>> {
pub async fn update(&mut self) -> Result<usize, Box<dyn Error>> {
let lists = self.database.get_tokenlists()?;
for list in &lists {
let version = self.assets_client.get_tokenlist(list.chain.as_str()).await?.version;
let _ = self.database.set_tokenlist_version(list.clone().chain, version);
let tokenlist = self.assets_client.get_tokenlist(list.chain.as_str()).await?;
let _ = self.database.set_tokenlist_version(list.clone().chain, tokenlist.version);

let assets = tokenlist.assets.into_iter().map(|x| Asset::from_primitive(x)).collect();
let _ = self.database.add_assets(assets);
}
Ok(lists.len())
}
Expand Down
27 changes: 27 additions & 0 deletions primitives/src/asset_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@ pub enum AssetType {
TRC20,
}

impl AssetType {
pub fn from_str(s: &str) -> Option<Self> {
match s {
"NATIVE" => Some(Self::NATIVE),
"ERC20" => Some(Self::ERC20),
"BEP2" => Some(Self::BEP2),
"BEP20" => Some(Self::BEP20),
"SPL" => Some(Self::SPL),
"ARBITRUM" => Some(Self::ARBITRUM),
"TRC20" => Some(Self::TRC20),
_ => None,
}
}

pub fn as_str(&self) -> &str {
match self {
Self::NATIVE => "NATIVE",
Self::ERC20 => "ERC20",
Self::BEP2 => "BEP2",
Self::BEP20 => "BEP20",
Self::SPL => "SPL",
Self::ARBITRUM => "ARBITRUM",
Self::TRC20 => "TRC20",
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[typeshare(swift = "Equatable, Codable, CaseIterable")]
#[serde(rename_all = "UPPERCASE")]
Expand Down
8 changes: 8 additions & 0 deletions storage/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ impl DatabaseClient {
.load(&mut self.connection)
}

pub fn add_assets(&mut self, _assets: Vec<Asset>) -> Result<usize, diesel::result::Error> {
use crate::schema::assets::dsl::*;
diesel::insert_into(assets)
.values(&_assets)
.on_conflict_do_nothing()
.execute(&mut self.connection)
}

pub fn migrations(&mut self) {
self.connection.run_pending_migrations(MIGRATIONS).unwrap();
}
Expand Down
10 changes: 6 additions & 4 deletions storage/src/migrations/2023-09-05-205905_assets/up.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CREATE TABLE assets (
id VARCHAR(128) PRIMARY KEY,
chain VARCHAR(16) NOT NULL,
token_id VARCHAR(128),
asset_type VARCHAR(16) NOT NULL,
name VARCHAR(64) NOT NULL,
symbol VARCHAR(16) NOT NULL,
decimals INTEGER NOT NULL,
Expand All @@ -11,7 +13,7 @@ CREATE TABLE assets (

SELECT diesel_manage_updated_at('assets');

INSERT INTO "assets" ("id", "chain", "name", "symbol", "decimals") VALUES
('bitcoin', 'bitcoin', 'Bitcoin', 'BTC', 8),
('ethereum', 'ethereum', 'Ethereum', 'ETH', 18),
('binance', 'binance', 'BNB Chain', 'BNB', 8);
INSERT INTO "assets" ("id", "chain", "asset_type", "name", "symbol", "decimals") VALUES
('bitcoin', 'bitcoin', 'NATIVE', 'Bitcoin', 'BTC', 8),
('ethereum', 'ethereum', 'NATIVE', 'Ethereum', 'ETH', 18),
('binance', 'binance', 'NATIVE', 'BNB Chain', 'BNB', 8);
28 changes: 28 additions & 0 deletions storage/src/models/asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use diesel::prelude::*;
use primitives::{tokenlist::TokenListAsset, chain::Chain, asset_type::AssetType, asset_id::AssetId};
use serde::{Deserialize, Serialize};

#[derive(Debug, Queryable, Selectable, Serialize, Deserialize, Insertable, AsChangeset, Clone)]
Expand All @@ -7,7 +8,34 @@ use serde::{Deserialize, Serialize};
pub struct Asset {
pub id: String,
pub chain: String,
pub token_id: Option<String>,
pub name: String,
pub symbol: String,
pub asset_type: String,
pub decimals: i32,
}

impl Asset {
pub fn as_primitive(&self) -> TokenListAsset {
TokenListAsset{
chain: Chain::new(&self.chain).unwrap(),
token_id: self.token_id.clone().unwrap_or_default(),
name: self.name.clone(),
symbol: self.symbol.clone(),
asset_type: AssetType::from_str(&self.asset_type).unwrap(),
decimals: self.decimals
}
}

pub fn from_primitive(asset: TokenListAsset) -> Self {
Self {
id: AssetId {chain: asset.chain, token_id: asset.token_id.clone().into() }.to_string(),
chain: asset.chain.as_str().to_string(),
token_id: Some(asset.token_id),
name: asset.name,
symbol: asset.symbol,
asset_type: asset.asset_type.as_str().to_string(),
decimals: asset.decimals
}
}
}
4 changes: 4 additions & 0 deletions storage/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ diesel::table! {
id -> Varchar,
#[max_length = 16]
chain -> Varchar,
#[max_length = 128]
token_id -> Nullable<Varchar>,
#[max_length = 16]
asset_type -> Varchar,
#[max_length = 64]
name -> Varchar,
#[max_length = 16]
Expand Down

0 comments on commit 4438e66

Please sign in to comment.