Skip to content

Commit

Permalink
cache price. (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinyuchan authored Feb 25, 2024
1 parent a23b9b1 commit fc8affc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 deletions.
8 changes: 6 additions & 2 deletions explorer/src/service/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ impl Api {
ids: Query<String>,
vs_currencies: Query<String>,
) -> poem::Result<SimplePriceResponse> {
service::v1::price::simple_price(ids, vs_currencies).await
service::v1::price::simple_price(self, ids, vs_currencies)
.await
.map_err(handle_fetch_one_err)
}

#[oai(
Expand All @@ -499,7 +501,9 @@ impl Api {
interval: Query<Option<String>>,
days: Query<i32>,
) -> poem::Result<MarketChartResponse> {
service::v1::price::market_chart(id, vs_currency, interval, days).await
service::v1::price::market_chart(id, vs_currency, interval, days)
.await
.map_err(handle_fetch_one_err)
}

#[oai(path = "/address/count", method = "get", tag = "ApiTags::Address")]
Expand Down
74 changes: 52 additions & 22 deletions explorer/src/service/v1/price.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::service::api::Api;
use anyhow::Result;
use log::error;
use poem_openapi::param::{Path, Query};
use poem_openapi::types::Type;
use poem_openapi::{payload::Json, ApiResponse, Object};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sqlx::Row;

#[derive(ApiResponse)]
pub enum SimplePriceResponse {
Expand All @@ -20,7 +24,7 @@ pub enum SimplePriceResponse {
pub struct SimplePriceResult {
pub code: i32,
pub message: String,
pub data: SimplePrice,
pub data: Option<SimplePrice>,
}

#[derive(Serialize, Deserialize, Debug, Default, Object)]
Expand Down Expand Up @@ -49,14 +53,36 @@ pub enum MarketChartResponse {
pub struct MarketChartResult {
pub code: i32,
pub message: String,
pub data: FraMarketChart,
pub data: Option<FraMarketChart>,
}

pub async fn get_fra_price(api: &Api) -> Result<FraPrice> {
let mut conn = api.storage.lock().await.acquire().await?;
let row = sqlx::query("SELECT price FROM prices")
.fetch_one(&mut conn)
.await?;
let p: String = row.try_get("price")?;
let fra_price = FraPrice { usd: p.parse()? };
Ok(fra_price)
}

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

Ok(())
}

#[allow(clippy::let_unit_value)]
pub async fn simple_price(
api: &Api,
ids: Query<String>,
vs_currencies: Query<String>,
) -> poem::Result<SimplePriceResponse> {
) -> Result<SimplePriceResponse> {
if ids.is_empty() || vs_currencies.is_empty() {
return Ok(SimplePriceResponse::BadRequest);
}
Expand All @@ -67,29 +93,33 @@ pub async fn simple_price(
);
let resp1 = reqwest::get(url).await;
if let Err(e) = resp1 {
return Ok(SimplePriceResponse::InternalError(Json(
SimplePriceResult {
code: 500,
message: e.to_string(),
data: Default::default(),
},
)));
error!("Get FRA price: {}", e.to_string());
let fra_price = get_fra_price(api).await?;
return Ok(SimplePriceResponse::Ok(Json(SimplePriceResult {
code: 200,
message: "".to_string(),
data: Some(SimplePrice { findora: fra_price }),
})));
}

let resp2 = resp1.unwrap().json::<SimplePrice>().await;
if let Err(e) = resp2 {
return Ok(SimplePriceResponse::InternalError(Json(
SimplePriceResult {
code: 500,
message: e.to_string(),
data: Default::default(),
},
)));
error!("Parse FRA price: {}", e.to_string());
let fra_price = get_fra_price(api).await?;
return Ok(SimplePriceResponse::Ok(Json(SimplePriceResult {
code: 200,
message: "".to_string(),
data: Some(SimplePrice { findora: fra_price }),
})));
}

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

Ok(SimplePriceResponse::Ok(Json(SimplePriceResult {
code: 200,
message: "".to_string(),
data: resp2.unwrap(),
data: Some(SimplePrice { findora: fra_price }),
})))
}

Expand All @@ -106,7 +136,7 @@ pub async fn market_chart(
vs_currency: Query<String>,
interval: Query<Option<String>>,
days: Query<i32>,
) -> poem::Result<MarketChartResponse> {
) -> Result<MarketChartResponse> {
if id.is_empty() || vs_currency.is_empty() || days.is_empty() {
return Ok(MarketChartResponse::BadRequest);
}
Expand All @@ -124,7 +154,7 @@ pub async fn market_chart(
MarketChartResult {
code: 500,
message: e.to_string(),
data: Default::default(),
data: None,
},
)));
}
Expand All @@ -134,7 +164,7 @@ pub async fn market_chart(
MarketChartResult {
code: 500,
message: e.to_string(),
data: Default::default(),
data: None,
},
)));
}
Expand All @@ -145,6 +175,6 @@ pub async fn market_chart(
Ok(MarketChartResponse::Ok(Json(MarketChartResult {
code: 200,
message: "".to_string(),
data: fmc,
data: Some(fmc),
})))
}
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 @@ -179,3 +179,9 @@ create index n2e_sender_index on n2e(sender);
create index n2e_receiver_index on n2e(receiver);
create index n2e_time_index on n2e(timestamp);
create index n2e_height_index on n2e(height);

create table prices(
name varchar(8) not null,
price varchar(16) not null,
primary key (name)
);

0 comments on commit fc8affc

Please sign in to comment.