Liquidator: reload account between rebalance steps

This commit is contained in:
Christian Kamm 2022-08-09 18:02:36 +02:00
parent e8479e2e7c
commit 7fcee1af96
1 changed files with 21 additions and 15 deletions

View File

@ -72,14 +72,13 @@ pub fn zero_all_non_quote(
.collect::<anyhow::Result<HashMap<TokenIndex, TokenState>>>()?; .collect::<anyhow::Result<HashMap<TokenIndex, TokenState>>>()?;
log::trace!("account tokens: {:?}", tokens); log::trace!("account tokens: {:?}", tokens);
let mut txsigs = vec![];
for (token_index, token_state) in tokens { for (token_index, token_state) in tokens {
let token = mango_client.context.token(token_index); let token = mango_client.context.token(token_index);
if token_index == quote_token.token_index { if token_index == quote_token.token_index {
continue; continue;
} }
if token_state.native_position > 0 { let maybe_txsig = if token_state.native_position > 0 {
let amount = token_state.native_position; let amount = token_state.native_position;
let txsig = mango_client.jupiter_swap( let txsig = mango_client.jupiter_swap(
token.mint_info.mint, token.mint_info.mint,
@ -95,7 +94,7 @@ pub fn zero_all_non_quote(
quote_token.name, quote_token.name,
txsig txsig
); );
txsigs.push(txsig); Some(txsig)
} else if token_state.native_position < 0 { } else if token_state.native_position < 0 {
let amount = (-token_state.native_position).ceil(); let amount = (-token_state.native_position).ceil();
let txsig = mango_client.jupiter_swap( let txsig = mango_client.jupiter_swap(
@ -112,19 +111,26 @@ pub fn zero_all_non_quote(
quote_token.name, quote_token.name,
txsig txsig
); );
txsigs.push(txsig); Some(txsig)
} } else {
} None
};
let max_slot = account_fetcher.transaction_max_slot(&txsigs)?; // The swaps aim to close token positions on the account. That means sending
if let Err(e) = account_fetcher.refresh_accounts_via_rpc_until_slot( // a second swap would fail due to including the wrong set of health accounts.
&[*mango_account_address], if let Some(txsig) = maybe_txsig {
max_slot, let max_slot = account_fetcher.transaction_max_slot(&[txsig])?;
config.refresh_timeout, if let Err(e) = account_fetcher.refresh_accounts_via_rpc_until_slot(
) { &[*mango_account_address],
// If we don't get fresh data, maybe the tx landed on a fork? max_slot,
// Rebalance is technically still ok. config.refresh_timeout,
log::info!("could not refresh account data: {}", e); ) {
// If we don't get fresh data, maybe the tx landed on a fork?
// Rebalance is technically still ok.
log::info!("could not refresh account data: {}", e);
return Ok(());
}
}
} }
Ok(()) Ok(())