Skip to content

Commit

Permalink
fix get_swappable_tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Apr 23, 2024
1 parent b840a63 commit 0db9b0b
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions lib/ain-ocean/src/api/poolpairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,34 +420,42 @@ async fn list_pool_swaps_verbose(
#[serde(rename_all = "camelCase")]
struct AllSwappableTokensResponse {
from_token: TokenIdentifier,
swappable_tokens: HashSet<TokenIdentifier>,
swappable_tokens: Vec<TokenIdentifier>,
}

#[ocean_endpoint]
async fn get_swappable_tokens(
Path(token_id): Path<String>,
Extension(ctx): Extension<Arc<AppContext>>,
) -> Result<Response<AllSwappableTokensResponse>> {
let from_token = get_token_identifier(&ctx, &token_id).await?;
let mut swappable_tokens: HashSet<TokenIdentifier> = HashSet::new();
let graph = ctx.services.token_graph.lock();
let edges = graph.edges(token_id.parse::<u32>()?).collect::<Vec<_>>();
for edge in edges {
// swappable_tokens.insert(get_token_identifier(&ctx, &edge.0.to_string()).await?);
// swappable_tokens.insert(get_token_identifier(&ctx, &edge.1.to_string()).await?);
let mut token_ids: HashSet<u32> = HashSet::new();
{
let graph = &ctx.services.token_graph.lock();
let edges = graph.edges(token_id.parse::<u32>()?).collect::<Vec<_>>();
for edge in edges {
token_ids.insert(edge.0);
token_ids.insert(edge.1);
}
}

let mut swappable_tokens = Vec::new();
for id in token_ids.into_iter() {
let token = get_token_identifier(&ctx, &id.to_string()).await?;
swappable_tokens.push(token);
}

Ok(Response::new(AllSwappableTokensResponse{
from_token,
from_token: get_token_identifier(&ctx, &token_id).await?,
swappable_tokens,
}))
}

#[ocean_endpoint]
async fn list_paths(
Path((from_token_id, to_token_id)): Path<(String, String)>,
Path((token_id, to_token_id)): Path<(String, String)>,
Extension(ctx): Extension<Arc<AppContext>>,
) -> Result<Response<SwapPathsResponse>> {
let paths = get_all_swap_paths(&ctx, &from_token_id, &to_token_id).await?;
let paths = get_all_swap_paths(&ctx, &token_id, &to_token_id).await?;
Ok(Response::new(paths))
}

Expand Down

0 comments on commit 0db9b0b

Please sign in to comment.