From 9a6ce3ad45adccba9d03abb5e1cb8ef39808b4d9 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 26 Sep 2024 22:04:10 +0800 Subject: [PATCH] Ocean: cache pool utils (#3069) * cache pool utils * get_network_info_cached * guard --- lib/ain-ocean/src/api/cache.rs | 14 ++++++- lib/ain-ocean/src/api/pool_pair/service.rs | 43 +++++++++++++++++++--- lib/ain-ocean/src/api/stats/mod.rs | 12 +++--- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/lib/ain-ocean/src/api/cache.rs b/lib/ain-ocean/src/api/cache.rs index c0e5da89e4..10aa1c29af 100644 --- a/lib/ain-ocean/src/api/cache.rs +++ b/lib/ain-ocean/src/api/cache.rs @@ -6,9 +6,10 @@ use defichain_rpc::{ loan::LoanSchemeResult, poolpair::{PoolPairInfo, PoolPairPagination, PoolPairsResult}, token::{TokenInfo, TokenPagination, TokenResult}, + GetNetworkInfoResult, }, jsonrpc_async::error::{Error as JsonRpcError, RpcError}, - Error, LoanRPC, MasternodeRPC, PoolPairRPC, TokenRPC, + Error, LoanRPC, MasternodeRPC, PoolPairRPC, RpcApi, TokenRPC, }; use super::AppContext; @@ -159,3 +160,14 @@ pub async fn get_loan_scheme_cached(ctx: &Arc, id: String) -> Result let loan_scheme = ctx.client.get_loan_scheme(id).await?; Ok(loan_scheme) } + +#[cached( + result = true, + time = 600, + key = "String", + convert = r#"{ format!("getnetworkinfo") }"# +)] +pub async fn get_network_info_cached(ctx: &Arc) -> Result { + let info = ctx.client.get_network_info().await?; + Ok(info) +} diff --git a/lib/ain-ocean/src/api/pool_pair/service.rs b/lib/ain-ocean/src/api/pool_pair/service.rs index d400634270..a641748d98 100644 --- a/lib/ain-ocean/src/api/pool_pair/service.rs +++ b/lib/ain-ocean/src/api/pool_pair/service.rs @@ -2,6 +2,7 @@ use std::{collections::HashMap, str::FromStr, sync::Arc}; use ain_dftx::{deserialize, pool::CompositeSwap, DfTx, Stack}; use bitcoin::Txid; +use cached::proc_macro::cached; use defichain_rpc::{json::poolpair::PoolPairInfo, BlockchainRPC}; use rust_decimal::{prelude::FromPrimitive, Decimal}; use rust_decimal_macros::dec; @@ -54,6 +55,12 @@ pub struct PoolSwapFromTo { pub to: Option, } +#[cached( + result = true, + time = 600, + key = "String", + convert = r#"{ format!("getusdperdfi") }"# +)] pub async fn get_usd_per_dfi(ctx: &Arc) -> Result { let usdt = get_pool_pair_cached(ctx, "USDT-DFI".to_string()).await?; @@ -226,6 +233,12 @@ async fn get_yearly_custom_reward_usd(ctx: &Arc, p: &PoolPairInfo) - }) } +#[cached( + result = true, + time = 600, + key = "String", + convert = r#"{ format!("getdailydfireward") }"# +)] async fn get_daily_dfi_reward(ctx: &Arc) -> Result { let gov = get_gov_cached(ctx, "LP_DAILY_DFI_REWARD".to_string()).await?; @@ -247,10 +260,6 @@ async fn get_loan_token_splits(ctx: &Arc) -> Result, p: &PoolPairInfo) -> Result { - // if p.reward_pct.is_none() { - // return dec!(0) - // }; - let dfi_price_usd = get_usd_per_dfi(ctx).await?; let daily_dfi_reward = get_daily_dfi_reward(ctx).await?; @@ -297,7 +306,12 @@ async fn get_block_subsidy(eunos_height: u32, height: u32) -> Result { Ok(block_subsidy) } -// TODO(): cached +#[cached( + result = true, + time = 600, + key = "String", + convert = r#"{ format!("getloanemission") }"# +)] async fn get_loan_emission(ctx: &Arc) -> Result { let info = ctx.client.get_blockchain_info().await?; let eunos_height = info @@ -313,12 +327,17 @@ async fn get_loan_emission(ctx: &Arc) -> Result { async fn get_yearly_reward_loan_usd(ctx: &Arc, id: &String) -> Result { let splits = get_loan_token_splits(ctx).await?; - let value = splits.unwrap_or_default(); + let Some(value) = splits else { + return Ok(dec!(0)); + }; let split = value .as_object() .and_then(|obj| obj.get(id)) .and_then(serde_json::Value::as_f64) .unwrap_or_default(); + if split == 0.0 { + return Ok(dec!(0)); + } let split = Decimal::from_f64(split).context(DecimalConversionSnafu)?; let dfi_price_usd = get_usd_per_dfi(ctx).await?; @@ -397,6 +416,12 @@ async fn gather_amount( Ok(volume) } +#[cached( + result = true, + time = 900, // 15 mins + key = "String", + convert = r#"{ format!("getusdvolume{id}") }"# +)] pub async fn get_usd_volume(ctx: &Arc, id: &str) -> Result { let pool_id = id.parse::()?; Ok(PoolPairVolumeResponse { @@ -475,6 +500,12 @@ async fn get_pool_pair(ctx: &Arc, a: &str, b: &str) -> Result