-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 33 additions & 3 deletions
36
backend/canisters/exchange_bot/impl/src/commands/sub_tasks/get_quotes.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,44 @@ | ||
use crate::commands::CommandSubTaskResult; | ||
use crate::swap_client::SwapClient; | ||
use exchange_bot_canister::ExchangeId; | ||
use futures::stream::FuturesUnordered; | ||
use futures::StreamExt; | ||
use ledger_utils::format_crypto_amount; | ||
use std::future::ready; | ||
|
||
pub(crate) async fn get_quote(client: &dyn SwapClient, amount: u128, output_token_decimals: u8) -> CommandSubTaskResult<u128> { | ||
pub(crate) async fn get_quotes<C: FnMut(ExchangeId, CommandSubTaskResult<u128>)>( | ||
clients: Vec<Box<dyn SwapClient>>, | ||
amount: u128, | ||
output_token_decimals: u8, | ||
mut callback: C, | ||
) { | ||
let futures = FuturesUnordered::new(); | ||
for client in clients { | ||
futures.push(get_quote(client, amount, output_token_decimals)); | ||
} | ||
|
||
futures | ||
.for_each(|(exchange_id, result)| { | ||
callback(exchange_id, result); | ||
ready(()) | ||
}) | ||
.await; | ||
} | ||
|
||
async fn get_quote( | ||
client: Box<dyn SwapClient>, | ||
amount: u128, | ||
output_token_decimals: u8, | ||
) -> (ExchangeId, CommandSubTaskResult<u128>) { | ||
let exchange_id = client.exchange_id(); | ||
let response = client.quote(amount).await; | ||
|
||
match response { | ||
let result = match response { | ||
Ok(amount_out) => { | ||
CommandSubTaskResult::Complete(amount_out, Some(format_crypto_amount(amount_out, output_token_decimals))) | ||
} | ||
Err(error) => CommandSubTaskResult::Failed(format!("{error:?}")), | ||
} | ||
}; | ||
|
||
(exchange_id, result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters