Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ocean: poolpair graph #2855

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
e6c2a59
WIP
canonbrother Mar 8, 2024
57c90c0
sync_token_graph on initialize
canonbrother Mar 21, 2024
4b7857d
poolpairs_path
canonbrother Mar 22, 2024
4f3efd6
get_pool_pair_info_cached
canonbrother Apr 11, 2024
f418047
compute_paths_between_tokens + all_simple_paths
canonbrother Apr 11, 2024
284d1bc
get_pool_pair_info_cached
canonbrother Apr 11, 2024
7c8b7a0
add list_paths api
canonbrother Apr 11, 2024
19625a6
fix list_paths api
canonbrother Apr 12, 2024
a99a979
fix all_simple_paths
canonbrother Apr 18, 2024
eaa3256
missing Response list_paths api
canonbrother Apr 19, 2024
2071a2d
temp disable list_pool_pairs_cached + fix get_pool_pair_info_cached
canonbrother Apr 19, 2024
f727a13
camelCase TokenIdentifier + Arc for token_graph & tokens_to_swappable…
canonbrother Apr 19, 2024
11b5dd1
fix SwapPathPoolPair rename id to pool_pair_id, enable price_ratio
canonbrother Apr 19, 2024
8a44889
fix PriceRatio typing
canonbrother Apr 19, 2024
53bd59d
sync_token_graph: execute first then wait in loop
canonbrother Apr 19, 2024
6c01a2f
estimated_dex_fees_in_pct
canonbrother Apr 19, 2024
272683c
grab fab7f5f defichain-rpc
canonbrother Apr 19, 2024
b5a3a63
rm extra api
canonbrother Apr 19, 2024
02a04bb
get_best_path
canonbrother Apr 21, 2024
5958947
use rust_decimal
canonbrother Apr 21, 2024
e285a1b
rm unuse import
canonbrother Apr 21, 2024
5cb033b
fix estimated_dex_fees_in_pct
canonbrother Apr 21, 2024
058b3c5
default 1
canonbrother Apr 22, 2024
49bac87
mmv list_paths above of get_best_path
canonbrother Apr 22, 2024
14508c3
ceil
canonbrother Apr 22, 2024
7b7f88e
format num
canonbrother Apr 22, 2024
67fa551
fix typo
canonbrother Apr 22, 2024
1dc0715
ceil
canonbrother Apr 22, 2024
c20173c
id default String
canonbrother Apr 22, 2024
d532bd4
proper err for`from_token_id != to_token_id`
canonbrother Apr 22, 2024
27d852b
fix EstimatedDexFeesInPct typing
canonbrother Apr 22, 2024
170d80e
rm unuse comment
canonbrother Apr 22, 2024
3144f09
rm tokens_to_swappable_tokens & wip get_swappable_tokens api
canonbrother Apr 23, 2024
0a1959c
fix get_swappable_tokens
canonbrother Apr 23, 2024
7e2358f
get_swappable_tokens done
canonbrother Apr 23, 2024
b7588c9
br
canonbrother Apr 23, 2024
4d8d142
use format_err.into()
canonbrother Apr 24, 2024
a85d137
fix unwrap
canonbrother Apr 24, 2024
ad2cba6
rm println
canonbrother Apr 24, 2024
376c35e
sync_token_graph_if_empty
canonbrother Apr 24, 2024
8eadc61
repl if let else by unwrap_or_default
canonbrother Apr 24, 2024
7b4301d
rm ceil.. its bug
canonbrother Apr 24, 2024
8213d7a
rm clone
canonbrother Apr 24, 2024
d0522ed
refine recur
canonbrother Apr 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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