Skip to content

Commit

Permalink
Liquidator: rebalancing - append sequence check before validating TX …
Browse files Browse the repository at this point in the history
…size
  • Loading branch information
farnyser committed Aug 10, 2024
1 parent 142609a commit 2aeaeb6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
45 changes: 27 additions & 18 deletions bin/liquidator/src/rebalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,16 @@ impl Rebalancer {
let results = futures::future::join_all(jobs).await;
let routes: Vec<_> = results.into_iter().filter_map(|v| v.ok()).collect_vec();

let seq_check_ix = self
.mango_client
.sequence_check_instruction(&self.mango_account_address, account)
.await?;

let best_route_res = self
.determine_best_swap_tx(routes, quote_mint, output_mint)
.determine_best_swap_tx(routes, quote_mint, output_mint, seq_check_ix.clone())
.await;

let (mut tx_builder, route) = match best_route_res {
let (tx_builder, route) = match best_route_res {
Ok(x) => x,
Err(e) => {
warn!("could not use simple routes because of {}, trying with an alternative one (if configured)", e);
Expand All @@ -216,17 +221,12 @@ impl Rebalancer {
},
quote_mint,
output_mint,
seq_check_ix,
)
.await?
}
};

let seq_check_ix = self
.mango_client
.sequence_check_instruction(&self.mango_account_address, account)
.await?;
tx_builder.append(seq_check_ix);

let sig = tx_builder
.send_and_confirm(&self.mango_client.client)
.await?;
Expand Down Expand Up @@ -273,11 +273,16 @@ impl Rebalancer {
let results = futures::future::join_all(jobs).await;
let routes: Vec<_> = results.into_iter().filter_map(|v| v.ok()).collect_vec();

let seq_check_ix = self
.mango_client
.sequence_check_instruction(&self.mango_account_address, account)
.await?;

let best_route_res = self
.determine_best_swap_tx(routes, input_mint, quote_mint)
.determine_best_swap_tx(routes, input_mint, quote_mint, seq_check_ix.clone())
.await;

let (mut tx_builder, route) = match best_route_res {
let (tx_builder, route) = match best_route_res {
Ok(x) => x,
Err(e) => {
warn!("could not use simple routes because of {}, trying with an alternative one (if configured)", e);
Expand All @@ -295,17 +300,12 @@ impl Rebalancer {
},
input_mint,
quote_mint,
seq_check_ix,
)
.await?
}
};

let seq_check_ix = self
.mango_client
.sequence_check_instruction(&self.mango_account_address, account)
.await?;
tx_builder.append(seq_check_ix);

let sig = tx_builder
.send_and_confirm(&self.mango_client.client)
.await?;
Expand Down Expand Up @@ -351,6 +351,7 @@ impl Rebalancer {
quote_fetcher: impl Fn(&u16) -> anyhow::Result<T>,
original_input_mint: Pubkey,
original_output_mint: Pubkey,
seq_check_ix: PreparedInstructions,
) -> anyhow::Result<(TransactionBuilder, swap::Quote)> {
let mut alt_jobs = vec![];
for in_token_index in &self.config.alternate_jupiter_route_tokens {
Expand All @@ -360,7 +361,12 @@ impl Rebalancer {
let alt_routes: Vec<_> = alt_results.into_iter().filter_map(|v| v.ok()).collect_vec();

let best_route = self
.determine_best_swap_tx(alt_routes, original_input_mint, original_output_mint)
.determine_best_swap_tx(
alt_routes,
original_input_mint,
original_output_mint,
seq_check_ix,
)
.await?;
Ok(best_route)
}
Expand All @@ -370,6 +376,7 @@ impl Rebalancer {
mut routes: Vec<swap::Quote>,
original_input_mint: Pubkey,
original_output_mint: Pubkey,
seq_check_ix: PreparedInstructions,
) -> anyhow::Result<(TransactionBuilder, swap::Quote)> {
let mut prices = HashMap::<Pubkey, I80F48>::new();
let mut get_or_fetch_price = |m| {
Expand Down Expand Up @@ -414,11 +421,13 @@ impl Rebalancer {
});

for route in routes {
let builder = self
let mut builder = self
.mango_client
.swap()
.prepare_swap_transaction(&route)
.await?;
builder.append(seq_check_ix.clone());

let tx_size = builder.transaction_size()?;
if tx_size.is_within_limit() {
return Ok((builder, route.clone()));
Expand Down
3 changes: 2 additions & 1 deletion lib/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,8 @@ impl MangoClient {
let open_orders = account.serum3_orders(market_index).unwrap().open_orders;

let ix = self.serum3_settle_funds_instruction(s3, base, quote, open_orders);
self.send_and_confirm_authority_tx(ix.to_instructions()).await
self.send_and_confirm_authority_tx(ix.to_instructions())
.await
}

pub fn serum3_settle_funds_instruction(
Expand Down

0 comments on commit 2aeaeb6

Please sign in to comment.