From 81a438a1058296f32555cb93563c13029b81ca22 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 18 Jun 2024 16:28:14 +0800 Subject: [PATCH 1/7] wip --- lib/Cargo.lock | 4 ++-- lib/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 7f1c521147..8e69f103bc 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -1531,7 +1531,7 @@ dependencies = [ [[package]] name = "defichain-rpc" version = "0.18.0" -source = "git+https://github.com/defich/rust-defichain-rpc.git#9a338f8d0ed5e837a67eb8c1aa04a9efc0c5d2ba" +source = "git+https://github.com/defich/rust-defichain-rpc.git?rev=8a507ef#8a507eff070e5607ac48b7399caa8bd46dd6cea2" dependencies = [ "async-trait", "defichain-rpc-json", @@ -1544,7 +1544,7 @@ dependencies = [ [[package]] name = "defichain-rpc-json" version = "0.18.0" -source = "git+https://github.com/defich/rust-defichain-rpc.git#9a338f8d0ed5e837a67eb8c1aa04a9efc0c5d2ba" +source = "git+https://github.com/defich/rust-defichain-rpc.git?rev=8a507ef#8a507eff070e5607ac48b7399caa8bd46dd6cea2" dependencies = [ "bitcoin", "serde", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index df3ddb4b24..a3b682fb6b 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -125,7 +125,7 @@ substrate-bn = "0.6" #### Ocean dependencies bitcoin = "0.31" cached = { version = "0.48", features = ["async"] } -defichain-rpc = { version = "0.18.0", git = "https://github.com/defich/rust-defichain-rpc.git" } +defichain-rpc = { version = "0.18.0", rev = "8a507ef", git = "https://github.com/defich/rust-defichain-rpc.git" } ### Local crates ain-cpp-imports = { path = "./ain-cpp-imports" } From 3ce9d6b8b19b5caac2ae4dc525bc3f40d18d385d Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 9 Jul 2024 22:56:23 +0800 Subject: [PATCH 2/7] add active price and fixed interval price id on struct LoanToken --- lib/ain-ocean/src/api/loan.rs | 90 +++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/lib/ain-ocean/src/api/loan.rs b/lib/ain-ocean/src/api/loan.rs index 7c8bb6fffd..f325c5281e 100644 --- a/lib/ain-ocean/src/api/loan.rs +++ b/lib/ain-ocean/src/api/loan.rs @@ -1,7 +1,7 @@ use std::{str::FromStr, sync::Arc}; use ain_macros::ocean_endpoint; -use anyhow::{format_err, Context}; +use anyhow::format_err; use axum::{routing::get, Extension, Router}; use bitcoin::{hashes::Hash, Txid}; use defichain_rpc::{ @@ -159,16 +159,14 @@ async fn get_collateral_token( ))) } -#[derive(Serialize)] +#[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct LoanToken { token_id: String, token: TokenData, - interest: f64, - // activate_after_block: u32, - // fixed_interval_price_id: String, - // TODO when indexing price - // activePrice?: ActivePrice + interest: String, + fixed_interval_price_id: String, + active_price: Option, } #[ocean_endpoint] @@ -181,10 +179,11 @@ async fn list_loan_token( struct FlattenToken { id: String, data: TokenInfo, + fixed_interval_price_id: String, interest: f64, } - let fut = tokens + let res = tokens .into_iter() .flat_map(|el| { el.token @@ -194,6 +193,7 @@ async fn list_loan_token( .map(|(id, data)| FlattenToken { id, data, + fixed_interval_price_id: el.fixed_interval_price_id, interest: el.interest, }) }) @@ -201,19 +201,34 @@ async fn list_loan_token( None => false, Some(v) => v != &token.id, }) - .map(|token| async move { + .map(|flatten_token| { + let fixed_interval_price_id = flatten_token.fixed_interval_price_id.clone(); + let parts = fixed_interval_price_id.split("/").collect::>(); + let [token, currency] = <[&str; 2]>::try_from(parts) + .map_err(|_| format_err!("Invalid fixed interval price id structure"))?; + + let repo = &ctx.services.oracle_price_active; + let key = repo + .by_key + .get(&(token.to_string(), currency.to_string()))?; + let active_price = if let Some(key) = key { + repo + .by_id + .get(&key)? + } else { + None + }; + let token = LoanToken { - token_id: token.id.clone(), - token: TokenData::from_with_id(token.id, token.data), - interest: token.interest, - // activate_after_block: todo!(), - // fixed_interval_price_id: todo!(), + token_id: flatten_token.id.clone(), + token: TokenData::from_with_id(flatten_token.id, flatten_token.data), + interest: format!("{:.2}", flatten_token.interest), + fixed_interval_price_id, + active_price, }; Ok::(token) }) - .collect::>(); - - let res = try_join_all(fut).await?; + .collect::>>()?; Ok(ApiPagedResponse::of(res, query.size, |loan_scheme| { loan_scheme.token_id.to_owned() @@ -225,20 +240,45 @@ async fn get_loan_token( Path(token_id): Path, Extension(ctx): Extension>, ) -> Result> { - let loan_token_result = ctx.client.get_loan_token(token_id).await?; - let res = loan_token_result + let loan_token_result = ctx.client.get_loan_token(token_id.clone()).await?; + let token = loan_token_result .token .0 .into_iter() .next() - .map(|(id, info)| LoanToken { - token_id: id.clone(), - token: TokenData::from_with_id(id, info), - interest: loan_token_result.interest, + .map(|(id, info)| { + let fixed_interval_price_id = loan_token_result.fixed_interval_price_id.clone(); + let parts = fixed_interval_price_id.split("/").collect::>(); + let [token, currency] = <[&str; 2]>::try_from(parts) + .map_err(|_| format_err!("Invalid fixed interval price id structure"))?; + + let repo = &ctx.services.oracle_price_active; + let key = repo + .by_key + .get(&(token.to_string(), currency.to_string()))?; + let active_price = if let Some(key) = key { + repo + .by_id + .get(&key)? + } else { + None + }; + + Ok::(LoanToken { + token_id: id.clone(), + token: TokenData::from_with_id(id, info), + interest: format!("{:.2}", loan_token_result.interest), + fixed_interval_price_id, + active_price, + }) }) - .context("Unable to find loan token")?; + .transpose()?; - Ok(Response::new(res)) + if token.is_none() { + return Err(format_err!("Token {:?} does not exist!", token_id).into()) + } + + Ok(Response::new(token.unwrap())) } #[ocean_endpoint] From 4205520cc2a140b8d09aa0018a4c9a10e51be62f Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 9 Jul 2024 23:11:08 +0800 Subject: [PATCH 3/7] sorted by creation_tx --- lib/ain-ocean/src/api/loan.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ain-ocean/src/api/loan.rs b/lib/ain-ocean/src/api/loan.rs index f325c5281e..c17f6df152 100644 --- a/lib/ain-ocean/src/api/loan.rs +++ b/lib/ain-ocean/src/api/loan.rs @@ -220,7 +220,7 @@ async fn list_loan_token( }; let token = LoanToken { - token_id: flatten_token.id.clone(), + token_id: flatten_token.data.creation_tx.clone(), token: TokenData::from_with_id(flatten_token.id, flatten_token.data), interest: format!("{:.2}", flatten_token.interest), fixed_interval_price_id, @@ -265,7 +265,7 @@ async fn get_loan_token( }; Ok::(LoanToken { - token_id: id.clone(), + token_id: info.creation_tx.clone(), token: TokenData::from_with_id(id, info), interest: format!("{:.2}", loan_token_result.interest), fixed_interval_price_id, From 028e7dc2214973412e769bd5d1eb1b6cfa9c8704 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 9 Jul 2024 23:13:41 +0800 Subject: [PATCH 4/7] clippy --- lib/ain-ocean/src/api/loan.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/ain-ocean/src/api/loan.rs b/lib/ain-ocean/src/api/loan.rs index c17f6df152..08268581cd 100644 --- a/lib/ain-ocean/src/api/loan.rs +++ b/lib/ain-ocean/src/api/loan.rs @@ -203,7 +203,7 @@ async fn list_loan_token( }) .map(|flatten_token| { let fixed_interval_price_id = flatten_token.fixed_interval_price_id.clone(); - let parts = fixed_interval_price_id.split("/").collect::>(); + let parts = fixed_interval_price_id.split('/').collect::>(); let [token, currency] = <[&str; 2]>::try_from(parts) .map_err(|_| format_err!("Invalid fixed interval price id structure"))?; @@ -248,7 +248,7 @@ async fn get_loan_token( .next() .map(|(id, info)| { let fixed_interval_price_id = loan_token_result.fixed_interval_price_id.clone(); - let parts = fixed_interval_price_id.split("/").collect::>(); + let parts = fixed_interval_price_id.split('/').collect::>(); let [token, currency] = <[&str; 2]>::try_from(parts) .map_err(|_| format_err!("Invalid fixed interval price id structure"))?; @@ -376,10 +376,7 @@ async fn map_vault_active( }) } -async fn map_vault_liquidation( - ctx: &Arc, - vault: VaultLiquidation, -) -> Result { +async fn map_vault_liquidation(ctx: &Arc, vault: VaultLiquidation) -> Result { let loan_scheme = get_loan_scheme_cached(ctx, vault.loan_scheme_id).await?; Ok(VaultLiquidatedResponse { batches: map_liquidation_batches(ctx, &vault.vault_id, vault.batches).await?, From 69255c01356f154f15762086c6f283f21c75cecd Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 9 Jul 2024 23:13:50 +0800 Subject: [PATCH 5/7] fmt --- lib/ain-ocean/src/api/loan.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/ain-ocean/src/api/loan.rs b/lib/ain-ocean/src/api/loan.rs index 08268581cd..26b7cc37f8 100644 --- a/lib/ain-ocean/src/api/loan.rs +++ b/lib/ain-ocean/src/api/loan.rs @@ -212,9 +212,7 @@ async fn list_loan_token( .by_key .get(&(token.to_string(), currency.to_string()))?; let active_price = if let Some(key) = key { - repo - .by_id - .get(&key)? + repo.by_id.get(&key)? } else { None }; @@ -257,9 +255,7 @@ async fn get_loan_token( .by_key .get(&(token.to_string(), currency.to_string()))?; let active_price = if let Some(key) = key { - repo - .by_id - .get(&key)? + repo.by_id.get(&key)? } else { None }; @@ -275,7 +271,7 @@ async fn get_loan_token( .transpose()?; if token.is_none() { - return Err(format_err!("Token {:?} does not exist!", token_id).into()) + return Err(format_err!("Token {:?} does not exist!", token_id).into()); } Ok(Response::new(token.unwrap())) @@ -376,7 +372,10 @@ async fn map_vault_active( }) } -async fn map_vault_liquidation(ctx: &Arc, vault: VaultLiquidation) -> Result { +async fn map_vault_liquidation( + ctx: &Arc, + vault: VaultLiquidation, +) -> Result { let loan_scheme = get_loan_scheme_cached(ctx, vault.loan_scheme_id).await?; Ok(VaultLiquidatedResponse { batches: map_liquidation_batches(ctx, &vault.vault_id, vault.batches).await?, From f6472b22f4f1d9df2de1544b69089c9bfdc22e4d Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 16 Jul 2024 14:04:31 +0800 Subject: [PATCH 6/7] rust-defichain-rpc main --- lib/Cargo.lock | 4 ++-- lib/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 8e69f103bc..7f1c521147 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -1531,7 +1531,7 @@ dependencies = [ [[package]] name = "defichain-rpc" version = "0.18.0" -source = "git+https://github.com/defich/rust-defichain-rpc.git?rev=8a507ef#8a507eff070e5607ac48b7399caa8bd46dd6cea2" +source = "git+https://github.com/defich/rust-defichain-rpc.git#9a338f8d0ed5e837a67eb8c1aa04a9efc0c5d2ba" dependencies = [ "async-trait", "defichain-rpc-json", @@ -1544,7 +1544,7 @@ dependencies = [ [[package]] name = "defichain-rpc-json" version = "0.18.0" -source = "git+https://github.com/defich/rust-defichain-rpc.git?rev=8a507ef#8a507eff070e5607ac48b7399caa8bd46dd6cea2" +source = "git+https://github.com/defich/rust-defichain-rpc.git#9a338f8d0ed5e837a67eb8c1aa04a9efc0c5d2ba" dependencies = [ "bitcoin", "serde", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index a3b682fb6b..df3ddb4b24 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -125,7 +125,7 @@ substrate-bn = "0.6" #### Ocean dependencies bitcoin = "0.31" cached = { version = "0.48", features = ["async"] } -defichain-rpc = { version = "0.18.0", rev = "8a507ef", git = "https://github.com/defich/rust-defichain-rpc.git" } +defichain-rpc = { version = "0.18.0", git = "https://github.com/defich/rust-defichain-rpc.git" } ### Local crates ain-cpp-imports = { path = "./ain-cpp-imports" } From efe2727a442936cdb3c768eaf5c2dbdbd257eb66 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Tue, 16 Jul 2024 15:34:07 +0800 Subject: [PATCH 7/7] fix fake_paginate --- lib/ain-ocean/src/api/loan.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ain-ocean/src/api/loan.rs b/lib/ain-ocean/src/api/loan.rs index 26b7cc37f8..7dbcff26c3 100644 --- a/lib/ain-ocean/src/api/loan.rs +++ b/lib/ain-ocean/src/api/loan.rs @@ -199,7 +199,7 @@ async fn list_loan_token( }) .fake_paginate(&query, |token| match &query.next { None => false, - Some(v) => v != &token.id, + Some(v) => v != &token.data.creation_tx, }) .map(|flatten_token| { let fixed_interval_price_id = flatten_token.fixed_interval_price_id.clone();