Skip to content

Commit

Permalink
fix market price.
Browse files Browse the repository at this point in the history
  • Loading branch information
chinyuchan committed Feb 28, 2024
1 parent a15dccd commit 79f4aed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion explorer/src/service/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ impl Api {
interval: Query<Option<String>>,
days: Query<i32>,
) -> poem::Result<MarketChartResponse> {
service::v1::price::market_chart(id, vs_currency, interval, days)
service::v1::price::market_chart(self, id, vs_currency, interval, days)
.await
.map_err(handle_fetch_one_err)
}
Expand Down
63 changes: 44 additions & 19 deletions explorer/src/service/v1/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ pub async fn upsert_fra_price(api: &Api, price: &str) -> Result<()> {
Ok(())
}

pub async fn get_fra_market(api: &Api) -> Result<FraMarketChart> {
let mut conn = api.storage.lock().await.acquire().await?;
let row = sqlx::query("SELECT val FROM market")
.fetch_one(&mut conn)
.await?;
let val: Value = row.try_get("val")?;
let fmc: FraMarketChart = serde_json::from_value(val).unwrap();

Ok(fmc)
}

pub async fn upsert_fra_market(api: &Api, val: Value) -> Result<()> {
let mut conn = api.storage.lock().await.acquire().await?;
sqlx::query("INSERT INTO market VALUES($1,$2) ON CONFLICT(name) DO UPDATE SET val=$2")
.bind("fra")
.bind(val)
.execute(&mut conn)
.await
.unwrap();

Ok(())
}

#[allow(clippy::let_unit_value)]
pub async fn simple_price(
api: &Api,
Expand All @@ -102,7 +125,7 @@ pub async fn simple_price(
})));
}

let resp2 = resp1.unwrap().json::<SimplePrice>().await;
let resp2 = resp1?.json::<SimplePrice>().await;
if let Err(e) = resp2 {
error!("Parse FRA price: {}", e.to_string());
let fra_price = get_fra_price(api).await?;
Expand All @@ -113,7 +136,7 @@ pub async fn simple_price(
})));
}

let fra_price = resp2.unwrap().findora;
let fra_price = resp2?.findora;
upsert_fra_price(api, fra_price.usd.to_string().as_str()).await?;

Ok(SimplePriceResponse::Ok(Json(SimplePriceResult {
Expand All @@ -132,6 +155,7 @@ pub struct FraMarketChart {

#[allow(clippy::let_unit_value)]
pub async fn market_chart(
api: &Api,
id: Path<String>,
vs_currency: Query<String>,
interval: Query<Option<String>>,
Expand All @@ -150,27 +174,28 @@ pub async fn market_chart(
}
let resp1 = reqwest::get(url).await;
if let Err(e) = resp1 {
return Ok(MarketChartResponse::InternalError(Json(
MarketChartResult {
code: 500,
message: e.to_string(),
data: None,
},
)));
error!("Get FRA market error: {:?}", e);
let fmc = get_fra_market(api).await?;
return Ok(MarketChartResponse::Ok(Json(MarketChartResult {
code: 200,
message: "".to_string(),
data: Some(fmc),
})));
}
let resp2 = resp1.unwrap().text().await;
let resp2 = resp1?.json::<FraMarketChart>().await;
if let Err(e) = resp2 {
return Ok(MarketChartResponse::InternalError(Json(
MarketChartResult {
code: 500,
message: e.to_string(),
data: None,
},
)));
error!("Parse FRA market cap error: {:?}", e);
let fmc = get_fra_market(api).await?;
return Ok(MarketChartResponse::Ok(Json(MarketChartResult {
code: 200,
message: "".to_string(),
data: Some(fmc),
})));
}

let r = resp2.unwrap();
let fmc: FraMarketChart = serde_json::from_str(r.as_str()).unwrap();
let fmc = resp2?;
let v = serde_json::to_value(&fmc)?;
upsert_fra_market(api, v).await?;

Ok(MarketChartResponse::Ok(Json(MarketChartResult {
code: 200,
Expand Down
6 changes: 6 additions & 0 deletions module/migrations/20220218070358_create_tables.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,9 @@ create table prices(
price varchar(16) not null,
primary key (name)
);

create table market(
name varchar(8) not null,
val jsonb not null,
primary key (name)
);

0 comments on commit 79f4aed

Please sign in to comment.