Skip to content

Commit

Permalink
Ocean: poolpair graph (#2855)
Browse files Browse the repository at this point in the history
* sync_token_graph on initialize

* poolpairs_path

* get_pool_pair_info_cached

* compute_paths_between_tokens + all_simple_paths

* get_pool_pair_info_cached

* add list_paths api

* fix list_paths api

* fix all_simple_paths

* missing Response list_paths api

* temp disable list_pool_pairs_cached + fix get_pool_pair_info_cached

* camelCase TokenIdentifier + Arc for token_graph & tokens_to_swappable_tokens data model

* fix SwapPathPoolPair rename id to pool_pair_id, enable price_ratio

* fix PriceRatio typing

* sync_token_graph: execute first then wait in loop

* estimated_dex_fees_in_pct

* grab fab7f5f defichain-rpc

* rm extra api

* get_best_path

* use rust_decimal

* rm unuse import

* fix estimated_dex_fees_in_pct

* default 1

* mmv list_paths above of get_best_path

* ceil

* format num

* fix typo

* ceil

* id default String

* proper err for`from_token_id != to_token_id`

* fix EstimatedDexFeesInPct typing

* rm unuse comment

* rm tokens_to_swappable_tokens & wip get_swappable_tokens api

* fix get_swappable_tokens

* get_swappable_tokens done

* br

* use format_err.into()

* fix unwrap

* rm println

* sync_token_graph_if_empty

* repl if let else by unwrap_or_default

* rm ceil.. its bug

* rm clone

* refine recur
  • Loading branch information
canonbrother authored Apr 24, 2024
1 parent 4350428 commit 92e01b6
Show file tree
Hide file tree
Showing 9 changed files with 595 additions and 62 deletions.
8 changes: 6 additions & 2 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ 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", git = "https://github.com/defich/rust-defichain-rpc.git" }
defichain-rpc = { rev = "fab7f5f", git = "https://github.com/defich/rust-defichain-rpc.git" }

### Local crates
ain-cpp-imports = { path = "./ain-cpp-imports" }
Expand Down
2 changes: 2 additions & 0 deletions lib/ain-ocean/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ rust_decimal = { version = "1.34", features = ["serde", "serde-float", "serde-wi
rust_decimal_macros = "1.34"
clap = { version = "4.5.0", features = ["derive"] }
num_cpus.workspace = true
petgraph = { version = "0.6.4", features = ["serde-1"] }
parking_lot.workspace = true

[dev-dependencies]
tempdir.workspace = true
Expand Down
37 changes: 36 additions & 1 deletion lib/ain-ocean/src/api/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use std::sync::Arc;

use anyhow::format_err;
use cached::proc_macro::cached;
use defichain_rpc::{defichain_rpc_json::token::TokenInfo, TokenRPC};
use defichain_rpc::{
defichain_rpc_json::{
poolpair::{PoolPairInfo, PoolPairsResult},
token::TokenInfo
}, json::poolpair::PoolPairPagination, PoolPairRPC, TokenRPC};

use super::AppContext;
use crate::Result;
Expand All @@ -23,3 +27,34 @@ pub async fn get_token_cached(ctx: &Arc<AppContext>, symbol: &str) -> Result<(St
.ok_or(format_err!("Error getting token info"))?;
Ok(token)
}

#[cached(
result = true,
key = "String",
convert = r#"{ format!("getpoolpair{id}") }"#
)]
pub async fn get_pool_pair_info_cached(ctx: &Arc<AppContext>, id: String) -> Result<(String, PoolPairInfo)> {
let pool_pair = ctx
.client
.get_pool_pair(id, Some(true))
.await?
.0
.into_iter()
.next()
.ok_or(format_err!("Error getting pool pair info"))?;

Ok(pool_pair)
}

// #[cached(
// result = true,
// key = "String",
// convert = r#"{ format!("listpoolpairs") }"#
// )]
pub async fn list_pool_pairs_cached(ctx: &Arc<AppContext>) -> Result<PoolPairsResult> {
let pool_pairs = ctx
.client
.list_pool_pairs(Some(PoolPairPagination {start: 0, including_start: true, limit: 1000}), Some(true))
.await?;
Ok(pool_pairs)
}
10 changes: 10 additions & 0 deletions lib/ain-ocean/src/api/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use defichain_rpc::json::token::TokenInfo;
use rust_decimal::Decimal;
use rust_decimal_macros::dec;

use super::query::PaginationQuery;

Expand Down Expand Up @@ -30,6 +31,15 @@ pub fn parse_dat_symbol(symbol: &str) -> String {
}
}

pub fn format_number(v: Decimal) -> String {
if v == dec!(0) {
"0".to_string()
} else {
format!("{:.8}", v)
}
}


/// Finds the balance of a specified token symbol within a list of token strings.
///
/// This function iterates through a vector of token strings, where each string
Expand Down
8 changes: 7 additions & 1 deletion lib/ain-ocean/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ mod loan;
mod masternode;
// mod oracle;
mod poolpairs;
pub mod poolpairs_path;
// mod prices;
// mod rawtx;
mod cache;
mod common;
pub mod common;
mod path;
mod query;
mod response;
Expand Down Expand Up @@ -66,6 +67,11 @@ pub async fn ocean_router(
network,
});

let context_cloned = context.clone();
tokio::spawn(async move {
poolpairs_path::sync_token_graph(&context_cloned).await
});

Ok(Router::new().nest(
format!("/v0/{}", context.network).as_str(),
Router::new()
Expand Down
Loading

0 comments on commit 92e01b6

Please sign in to comment.