2023-07-03 05:09:11 -07:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::{Arc, RwLock};
|
|
|
|
|
|
|
|
use itertools::Itertools;
|
2023-12-20 06:25:48 -08:00
|
|
|
use mango_v4_client::error_tracking::ErrorTracking;
|
2023-07-11 23:38:38 -07:00
|
|
|
use tracing::*;
|
|
|
|
|
2023-07-03 05:09:11 -07:00
|
|
|
use mango_v4::state::TokenIndex;
|
2024-04-22 04:27:15 -07:00
|
|
|
use mango_v4_client::swap;
|
2023-08-24 07:45:01 -07:00
|
|
|
use mango_v4_client::MangoClient;
|
2023-07-03 05:09:11 -07:00
|
|
|
|
|
|
|
pub struct Config {
|
|
|
|
pub quote_index: TokenIndex,
|
2024-02-09 02:31:37 -08:00
|
|
|
|
|
|
|
/// Size in quote_index-token native tokens to quote.
|
2023-07-03 05:09:11 -07:00
|
|
|
pub quote_amount: u64,
|
2024-02-09 02:31:37 -08:00
|
|
|
|
2024-04-22 04:27:15 -07:00
|
|
|
pub jupiter_version: swap::Version,
|
2023-07-03 05:09:11 -07:00
|
|
|
}
|
|
|
|
|
2023-10-07 12:27:19 -07:00
|
|
|
#[derive(Clone)]
|
2023-07-03 05:09:11 -07:00
|
|
|
pub struct TokenSwapInfo {
|
2023-10-07 12:27:19 -07:00
|
|
|
pub last_update: std::time::SystemTime,
|
|
|
|
|
|
|
|
// in USDC per token, not the literal oracle price (which is in USD per token)
|
|
|
|
pub quote_per_token_oracle: f64,
|
|
|
|
pub quote_per_token_buy: f64,
|
|
|
|
pub quote_per_token_sell: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TokenSwapInfo {
|
2023-07-03 05:09:11 -07:00
|
|
|
/// multiplier to the oracle price for executing a buy, so 1.5 would mean buying 50% over oracle price
|
2023-10-07 12:27:19 -07:00
|
|
|
pub fn buy_over_oracle(&self) -> f64 {
|
|
|
|
self.quote_per_token_buy / self.quote_per_token_oracle
|
|
|
|
}
|
|
|
|
|
2023-07-03 05:09:11 -07:00
|
|
|
/// multiplier to the oracle price for executing a sell,
|
|
|
|
/// but with the price inverted, so values > 1 mean a worse deal than oracle price
|
2023-10-07 12:27:19 -07:00
|
|
|
pub fn sell_over_oracle(&self) -> f64 {
|
|
|
|
self.quote_per_token_oracle / self.quote_per_token_sell
|
|
|
|
}
|
2023-07-03 05:09:11 -07:00
|
|
|
}
|
|
|
|
|
2023-12-20 06:25:48 -08:00
|
|
|
struct TokenSwapInfoState {
|
|
|
|
swap_infos: HashMap<TokenIndex, TokenSwapInfo>,
|
|
|
|
errors: ErrorTracking<TokenIndex, &'static str>,
|
|
|
|
}
|
|
|
|
|
2023-07-03 05:09:11 -07:00
|
|
|
/// Track the buy/sell slippage for tokens
|
|
|
|
///
|
|
|
|
/// Needed to evaluate whether a token conditional swap premium might be good enough
|
|
|
|
/// without having to query each time.
|
|
|
|
pub struct TokenSwapInfoUpdater {
|
|
|
|
mango_client: Arc<MangoClient>,
|
2023-12-20 06:25:48 -08:00
|
|
|
state: RwLock<TokenSwapInfoState>,
|
2023-07-03 05:09:11 -07:00
|
|
|
config: Config,
|
|
|
|
}
|
|
|
|
|
2023-12-20 06:25:48 -08:00
|
|
|
const ERROR_TYPE: &'static str = "tsi";
|
|
|
|
|
2023-07-03 05:09:11 -07:00
|
|
|
impl TokenSwapInfoUpdater {
|
|
|
|
pub fn new(mango_client: Arc<MangoClient>, config: Config) -> Self {
|
|
|
|
Self {
|
|
|
|
mango_client,
|
2023-12-20 06:25:48 -08:00
|
|
|
state: RwLock::new(TokenSwapInfoState {
|
|
|
|
swap_infos: HashMap::new(),
|
|
|
|
errors: ErrorTracking::builder().build().unwrap(),
|
|
|
|
}),
|
2023-07-03 05:09:11 -07:00
|
|
|
config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mango_client(&self) -> &Arc<MangoClient> {
|
|
|
|
&self.mango_client
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&self, token_index: TokenIndex, slippage: TokenSwapInfo) {
|
2023-12-20 06:25:48 -08:00
|
|
|
let mut lock = self.state.write().unwrap();
|
|
|
|
lock.swap_infos.insert(token_index, slippage);
|
2023-07-03 05:09:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn swap_info(&self, token_index: TokenIndex) -> Option<TokenSwapInfo> {
|
2023-12-20 06:25:48 -08:00
|
|
|
let lock = self.state.read().unwrap();
|
|
|
|
lock.swap_infos.get(&token_index).cloned()
|
2023-07-03 05:09:11 -07:00
|
|
|
}
|
|
|
|
|
2024-04-22 04:27:15 -07:00
|
|
|
fn in_per_out_price(route: &swap::Quote) -> f64 {
|
2023-08-24 07:45:01 -07:00
|
|
|
let in_amount = route.in_amount as f64;
|
|
|
|
let out_amount = route.out_amount as f64;
|
2023-10-07 12:27:19 -07:00
|
|
|
in_amount / out_amount
|
2023-07-03 05:09:11 -07:00
|
|
|
}
|
|
|
|
|
2023-12-20 06:25:48 -08:00
|
|
|
pub async fn update_one(&self, token_index: TokenIndex) {
|
|
|
|
{
|
|
|
|
let lock = self.state.read().unwrap();
|
|
|
|
if lock
|
|
|
|
.errors
|
|
|
|
.had_too_many_errors(ERROR_TYPE, &token_index, std::time::Instant::now())
|
|
|
|
.is_some()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(err) = self.try_update_one(token_index).await {
|
|
|
|
let mut lock = self.state.write().unwrap();
|
|
|
|
lock.errors
|
|
|
|
.record(ERROR_TYPE, &token_index, err.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn try_update_one(&self, token_index: TokenIndex) -> anyhow::Result<()> {
|
2023-07-03 05:09:11 -07:00
|
|
|
// since we're only quoting, the slippage does not matter
|
|
|
|
let slippage = 100;
|
|
|
|
|
|
|
|
let quote_index = self.config.quote_index;
|
|
|
|
if token_index == quote_index {
|
2023-10-07 12:27:19 -07:00
|
|
|
self.update(
|
|
|
|
quote_index,
|
|
|
|
TokenSwapInfo {
|
|
|
|
last_update: std::time::SystemTime::now(),
|
|
|
|
quote_per_token_oracle: 1.0,
|
|
|
|
quote_per_token_buy: 1.0,
|
|
|
|
quote_per_token_sell: 1.0,
|
|
|
|
},
|
|
|
|
);
|
2023-07-03 05:09:11 -07:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-12-05 04:22:24 -08:00
|
|
|
let token_mint = self.mango_client.context.token(token_index).mint;
|
|
|
|
let quote_mint = self.mango_client.context.token(quote_index).mint;
|
2023-07-03 05:09:11 -07:00
|
|
|
|
|
|
|
// these prices are in USD, which doesn't exist on chain
|
|
|
|
let token_price = self
|
|
|
|
.mango_client
|
|
|
|
.bank_oracle_price(token_index)
|
|
|
|
.await?
|
|
|
|
.to_num::<f64>();
|
|
|
|
let quote_price = self
|
|
|
|
.mango_client
|
|
|
|
.bank_oracle_price(quote_index)
|
|
|
|
.await?
|
|
|
|
.to_num::<f64>();
|
|
|
|
|
|
|
|
// prices for the pair
|
2023-10-07 12:27:19 -07:00
|
|
|
let quote_per_token_oracle = token_price / quote_price;
|
|
|
|
let token_per_quote_oracle = quote_price / token_price;
|
2023-07-03 05:09:11 -07:00
|
|
|
|
2023-10-07 12:27:19 -07:00
|
|
|
let token_amount = (self.config.quote_amount as f64 * token_per_quote_oracle) as u64;
|
2023-08-24 07:45:01 -07:00
|
|
|
let sell_route = self
|
|
|
|
.mango_client
|
2024-04-22 04:27:15 -07:00
|
|
|
.swap()
|
2023-08-24 07:45:01 -07:00
|
|
|
.quote(
|
|
|
|
token_mint,
|
|
|
|
quote_mint,
|
|
|
|
token_amount,
|
|
|
|
slippage,
|
|
|
|
false,
|
|
|
|
self.config.jupiter_version,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let buy_route = self
|
|
|
|
.mango_client
|
2024-04-22 04:27:15 -07:00
|
|
|
.swap()
|
2023-08-24 07:45:01 -07:00
|
|
|
.quote(
|
|
|
|
quote_mint,
|
|
|
|
token_mint,
|
|
|
|
self.config.quote_amount,
|
|
|
|
slippage,
|
|
|
|
false,
|
|
|
|
self.config.jupiter_version,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2023-10-07 12:27:19 -07:00
|
|
|
let quote_per_token_buy = Self::in_per_out_price(&buy_route);
|
|
|
|
let token_per_quote_sell = Self::in_per_out_price(&sell_route);
|
2023-07-03 05:09:11 -07:00
|
|
|
|
|
|
|
self.update(
|
|
|
|
token_index,
|
|
|
|
TokenSwapInfo {
|
2023-10-07 12:27:19 -07:00
|
|
|
last_update: std::time::SystemTime::now(),
|
|
|
|
quote_per_token_oracle,
|
|
|
|
quote_per_token_buy,
|
|
|
|
quote_per_token_sell: 1.0 / token_per_quote_sell,
|
2023-07-03 05:09:11 -07:00
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_all(&self) {
|
2023-12-20 06:25:48 -08:00
|
|
|
{
|
|
|
|
let mut lock = self.state.write().unwrap();
|
|
|
|
lock.errors.update();
|
|
|
|
}
|
|
|
|
|
2023-07-03 05:09:11 -07:00
|
|
|
let mut tokens = self
|
|
|
|
.mango_client
|
|
|
|
.context
|
|
|
|
.token_indexes_by_name
|
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.collect_vec();
|
|
|
|
tokens.sort_by(|a, b| a.0.cmp(&b.0));
|
2023-12-20 06:25:48 -08:00
|
|
|
let lock = self.state.read().unwrap();
|
2023-07-03 05:09:11 -07:00
|
|
|
|
|
|
|
let mut msg = String::new();
|
|
|
|
for (token, token_index) in tokens {
|
2023-12-20 06:25:48 -08:00
|
|
|
let info = lock
|
|
|
|
.swap_infos
|
2023-07-03 05:09:11 -07:00
|
|
|
.get(&token_index)
|
|
|
|
.map(|info| {
|
|
|
|
format!(
|
2023-10-07 12:27:19 -07:00
|
|
|
"oracle {}, buy {}, sell {}",
|
|
|
|
info.quote_per_token_oracle,
|
|
|
|
info.quote_per_token_buy,
|
|
|
|
info.quote_per_token_sell
|
2023-07-03 05:09:11 -07:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| "no data".into());
|
|
|
|
msg.push_str(&format!("token {token}, {info}"));
|
|
|
|
}
|
2023-07-11 23:38:38 -07:00
|
|
|
trace!("swap infos:{}", msg);
|
2023-07-03 05:09:11 -07:00
|
|
|
}
|
|
|
|
}
|