Skip to content

Commit

Permalink
fix(get_exchange_by_liquidity): use the last amount of the path to de…
Browse files Browse the repository at this point in the history
…cide which exchange use to better liquidity
  • Loading branch information
willyrgf committed Oct 8, 2022
1 parent 8c150ca commit 3f36650
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ cargo run -- unwrap --network bsc --wallet test-wallet --amount 0.005
### approve spender for an asset

```bash
cargo run -- approve_spender -e pancake_swap_v2 -w test-wallet -a wbnb -v 10
cargo run -- approve -e pancake_swap_v2 -w test-wallet -a wbnb -v 10
```


#### To approve all assets
```bash
cargo run -- allowance --network polygon --wallet test-wallet |
grep ^\| |
grep -v Exchange |
awk -F '|' '{print $2 $3}' |
xargs -n 2 bash -c 'cargo run -- approve --exchange $0 -w test-wallet --asset $1 --amount 10000000'
```

---
Expand Down Expand Up @@ -101,6 +111,8 @@ cargo run -- withdraw --wallet test-wallet -t test-wallet2 -v 0.008 -a wbnb -n b
---

## TODO
- [ ] FIX: problem on select a better exchanged based on liquidity in some assets (like bal with quickswap vs sushi_swap)
- [ ] validate network and exchange in the build of the config
- [ ] use exitable main to return to the OS the exit code (like here https://github.com/firecracker-microvm/firecracker/blob/main/src/firecracker/src/main.rs#L406)
- [ ] refactor all the U256 calc to use numbigint in testable functions
- [ ] refactor of the config mods to be first class module (like asset)
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ async fn cmd_run(_args: &ArgMatches) {
let parking_asset = rebalancer_config.get_parking_asset();
let from_wallet = rebalancer_config.get_wallet();

// TODO: handle with the possibility to have just one asset.
// By example, using a rebalancer exit command and just have a parking asset.
// May a network default wrapped asset.
let input_asset = match asset_rebalancers
.clone()
.iter()
Expand Down
5 changes: 3 additions & 2 deletions src/config/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl Network {
.collect()
}

// TODO: add test to avoid the bugs of like in the choice of exchange
pub async fn get_exchange_by_liquidity(
&self,
input_asset: &Asset,
Expand All @@ -72,13 +73,13 @@ impl Network {
Some((h, t)) => {
let mut current_amount_out = {
let path = h.build_route_for(input_asset, output_asset).await;
h.get_amounts_out(amount_in, path).await
*h.get_amounts_out(amount_in, path).await.last().unwrap()
};

futures::future::join_all(t.iter().map(|e| async move {
let current_amount = {
let path = e.build_route_for(input_asset, output_asset).await;
e.get_amounts_out(amount_in, path).await
*e.get_amounts_out(amount_in, path).await.last().unwrap()
};
(Some(*e), current_amount)
}))
Expand Down
3 changes: 2 additions & 1 deletion src/quote/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use clap::{ArgMatches, Command};
pub fn generate() -> Command {
Command::new("quote")
.about("Get a quote for tokens to tokens")
.arg(clap::arg!(-e --"exchange" <pancake_swap_v2> "Exchange to use router").required(true))
.arg(clap::arg!(-n --"network" <bsc> "Network to use, ex (bsc, polygon)").required(true))
.arg(clap::arg!(-e --"exchange" <pancake_swap_v2> "Exchange to use router").required(false))
.arg(
clap::arg!(-a --"amount" <AMMOUNT> "Amount of TokenA to swap to TokenB")
.required(false),
Expand Down
40 changes: 26 additions & 14 deletions src/quote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@ pub mod cmd;

#[tracing::instrument(name = "run quote")]
async fn run(args: &ArgMatches) -> Result<(), anyhow::Error> {
let exchange = helpers::get_exchange(args)?;
let network = helpers::get_network(args)?;

let input_token = helpers::get_token_input_in_network_from_args(args, exchange.network_id())?;
let output_token = helpers::get_token_output_in_network_from_args(args, exchange.network_id())?;
let input_asset = helpers::get_token_input_in_network_from_args(args, network.get_name())?;
let output_asset = helpers::get_token_output_in_network_from_args(args, network.get_name())?;

let input_token_decimals = input_token.decimals().await?;
let output_token_decimals = output_token.decimals().await?;
let input_asset_decimals = input_asset.decimals().await?;
let output_asset_decimals = output_asset.decimals().await?;
let amount_in = helpers::get_amount(args, input_asset_decimals)?;

let amount_in = helpers::get_amount(args, input_token_decimals)?;
let slippage = helpers::get_slippage(args, output_token_decimals)?;
let exchange = match helpers::get_exchange(args) {
Ok(e) => e,
Err(_) => {
tracing::warn!("exchange not found in args, try to use the best liquidity exchange");

let asset_path = exchange.build_route_for(&input_token, &output_token).await;
network
.get_exchange_by_liquidity(&input_asset, &output_asset, amount_in)
.await
.unwrap()
}
};

let slippage = helpers::get_slippage(args, output_asset_decimals)?;

let asset_path = exchange.build_route_for(&input_asset, &output_asset).await;

let amount_min_out: U256 = exchange
.get_amounts_out(amount_in, asset_path.clone())
Expand All @@ -28,7 +40,7 @@ async fn run(args: &ArgMatches) -> Result<(), anyhow::Error> {
.into();

// TODO: move this calc for the new mod of U256
let slippage_amount = (amount_min_out * slippage) / U256::exp10(output_token_decimals.into());
let slippage_amount = (amount_min_out * slippage) / U256::exp10(output_asset_decimals.into());
let amount_out_slippage: U256 = amount_min_out - slippage_amount;

let mut table = Table::new();
Expand All @@ -42,11 +54,11 @@ async fn run(args: &ArgMatches) -> Result<(), anyhow::Error> {
]);
table.add_row(row![
exchange.name(),
input_token.name(),
utils::blockchain::display_amount_to_float(amount_in, input_token_decimals),
output_token.name(),
utils::blockchain::display_amount_to_float(amount_min_out, output_token_decimals),
utils::blockchain::display_amount_to_float(amount_out_slippage, output_token_decimals),
input_asset.name(),
utils::blockchain::display_amount_to_float(amount_in, input_asset_decimals),
output_asset.name(),
utils::blockchain::display_amount_to_float(amount_min_out, output_asset_decimals),
utils::blockchain::display_amount_to_float(amount_out_slippage, output_asset_decimals),
]);
table.printstd();

Expand Down

0 comments on commit 3f36650

Please sign in to comment.