-
Notifications
You must be signed in to change notification settings - Fork 0
/
phoenix.rs
416 lines (369 loc) · 14.6 KB
/
phoenix.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use anchor_lang::{declare_id, ToAccountMetas};
use anyhow::{anyhow, ensure, Context, Result};
use jupiter::{accounts::PhoenixSwap, Side};
use phoenix::{
program::{load_with_dispatch, MarketHeader},
quantities::WrapperU64,
state::markets::{Ladder, LadderOrder},
};
use rust_decimal::{prelude::FromPrimitive, Decimal};
use rust_decimal_macros::dec;
use std::{collections::HashMap, mem::size_of};
use crate::{
amm::{try_get_account_data, AccountMap},
amms::amm::{Amm, KeyedAccount, Quote, QuoteParams, SwapAndAccountMetas, SwapParams},
};
use solana_sdk::{pubkey::Pubkey, sysvar};
use jupiter::jupiter_override::Swap;
declare_id!("PhoeNiXZ8ByJGLkxNfZRnkUfjvmuYqLR89jjFHGqdXY");
const BPS_TO_PCT: Decimal = dec!(10_000);
#[derive(Clone, Debug)]
pub struct PhoenixAmm {
/// The pubkey of the market account
market_key: Pubkey,
/// The pubkey of the base mint
base_mint: Pubkey,
/// The pubkey of the quote mint
quote_mint: Pubkey,
/// Only here for convenience
base_decimals: u32,
/// Only here for convenience
quote_decimals: u32,
/// The size of a base lot in base atoms
base_lot_size: u64,
/// The size of a quote lot in quote atoms
quote_lot_size: u64,
/// The number of a base lot in a base unit
base_lots_per_base_unit: u64,
/// The number of a quote lots per base unit in a tick (tick_size)
tick_size_in_quote_lots_per_base_unit_per_tick: u64,
/// Taker fee basis points
taker_fee_bps: u64,
/// Fee pct
fee_pct: Decimal,
/// The state of the orderbook (L2)
ladder: Option<Ladder>,
}
impl PhoenixAmm {
pub fn from_keyed_account(keyed_account: &KeyedAccount) -> Result<Self> {
let (header_bytes, bytes) = keyed_account
.account
.data
.split_at(size_of::<MarketHeader>());
let header: &MarketHeader = bytemuck::try_from_bytes(header_bytes)
.map_err(|e| anyhow!("Error getting market header. Error: {:?}", e))?;
let market = load_with_dispatch(&header.market_size_params, bytes)
.map_err(|e| anyhow!("Failed to load market. Error {:?}", e))?;
let taker_fee_bps = market.inner.get_taker_fee_bps();
let fee_pct =
PhoenixAmm::compute_fee_pct(taker_fee_bps).context("Cannot compute fee pct")?;
Ok(Self {
market_key: keyed_account.key,
base_mint: header.base_params.mint_key,
quote_mint: header.quote_params.mint_key,
base_decimals: header.base_params.decimals,
quote_decimals: header.quote_params.decimals,
taker_fee_bps,
fee_pct,
base_lot_size: header.get_base_lot_size().as_u64(),
quote_lot_size: header.get_quote_lot_size().as_u64(),
base_lots_per_base_unit: market.inner.get_base_lots_per_base_unit().as_u64(),
tick_size_in_quote_lots_per_base_unit_per_tick: header
.get_tick_size_in_quote_atoms_per_base_unit()
.as_u64()
/ header.get_quote_lot_size().as_u64(),
ladder: None,
})
}
fn compute_fee_pct(taker_fee_bps: u64) -> Option<Decimal> {
Decimal::from_u64(taker_fee_bps)?.checked_div(BPS_TO_PCT)
}
fn compute_decimal_div(a: u64, b: u64) -> Option<Decimal> {
Decimal::from_u64(a)?.checked_div(Decimal::from_u64(b)?)
}
fn compute_price_impact_pct(price: Decimal, best_price: Decimal) -> Option<Decimal> {
best_price.checked_sub(price)?.checked_div(best_price)
}
pub fn get_base_decimals(&self) -> u32 {
self.base_decimals
}
pub fn get_quote_decimals(&self) -> u32 {
self.quote_decimals
}
}
impl Amm for PhoenixAmm {
fn label(&self) -> String {
"Phoenix".into()
}
fn program_id(&self) -> Pubkey {
self::id()
}
fn key(&self) -> Pubkey {
self.market_key
}
fn get_reserve_mints(&self) -> Vec<Pubkey> {
vec![self.base_mint, self.quote_mint]
}
fn get_accounts_to_update(&self) -> Vec<Pubkey> {
vec![self.market_key, sysvar::clock::ID]
}
fn update(&mut self, account_map: &AccountMap) -> Result<()> {
let market_account_data = try_get_account_data(account_map, &self.market_key)?;
let sysvar_clock_data = try_get_account_data(account_map, &sysvar::clock::ID)?;
let clock: sysvar::clock::Clock = bincode::deserialize(sysvar_clock_data)?;
let (header_bytes, bytes) = market_account_data.split_at(size_of::<MarketHeader>());
let header: &MarketHeader = bytemuck::try_from_bytes(header_bytes)
.map_err(|e| anyhow!("Error getting market header. Error: {:?}", e))?;
let market = load_with_dispatch(&header.market_size_params, bytes)
.map_err(|e| anyhow!("Failed to load market. Error {:?}", e))?;
self.ladder = Some(market.inner.get_ladder_with_expiration(
u64::MAX,
Some(clock.slot),
Some(clock.unix_timestamp as u64),
));
Ok(())
}
fn quote(&self, quote_params: &QuoteParams) -> Result<Quote> {
let mut out_amount = 0;
let mut in_amount = 0;
let mut not_enough_liquidity = false;
let mut best_price: Option<Decimal> = None;
let ladder = self
.ladder
.as_ref()
.context("Market has not been updated")?;
if quote_params.input_mint == self.base_mint {
let mut base_lot_budget = quote_params
.in_amount
.checked_div(self.base_lot_size)
.context("division failed")?;
let initial_base_lot_budget = base_lot_budget;
for LadderOrder {
price_in_ticks,
size_in_base_lots,
} in ladder.bids.iter()
{
if base_lot_budget == 0 {
break;
}
let base_lots = size_in_base_lots.min(&base_lot_budget);
let filled_amount = price_in_ticks
.checked_mul(*base_lots)
.context("multiply overflow")?
.checked_mul(self.tick_size_in_quote_lots_per_base_unit_per_tick)
.context("multiply overflow")?
.checked_mul(self.quote_lot_size)
.context("multiply overflow")?
.checked_div(self.base_lots_per_base_unit)
.context("division failed")?;
if best_price.is_none() {
let in_amount_for_level = base_lots
.checked_mul(self.base_lot_size)
.context("multiply overflow")?;
best_price = Some(
PhoenixAmm::compute_decimal_div(filled_amount, in_amount_for_level)
.context("Cannot compute best price")?,
);
}
out_amount += filled_amount;
base_lot_budget = base_lot_budget.saturating_sub(*base_lots);
}
in_amount = (initial_base_lot_budget - base_lot_budget)
.checked_mul(self.base_lot_size)
.context("multiply overflow")?;
if base_lot_budget > 0 {
not_enough_liquidity = true;
}
} else {
let mut quote_lot_budget = quote_params
.in_amount
.checked_div(self.quote_lot_size)
.context("division failed")?;
let initial_quote_lot_budget = quote_lot_budget;
for LadderOrder {
price_in_ticks,
size_in_base_lots,
} in ladder.asks.iter()
{
if quote_lot_budget == 0 {
break;
}
let purchasable_base_lots = quote_lot_budget
.checked_mul(self.base_lots_per_base_unit)
.context("multiple overflow")?
.checked_div(self.tick_size_in_quote_lots_per_base_unit_per_tick)
.context("division failed")?
.checked_div(*price_in_ticks)
.context("division failed")?;
let base_lots: u64;
let quote_lots: u64;
if size_in_base_lots > &purchasable_base_lots {
base_lots = purchasable_base_lots;
quote_lots = quote_lot_budget;
} else {
base_lots = *size_in_base_lots;
quote_lots = price_in_ticks
.checked_mul(base_lots)
.context("multiple overflow")?
.checked_mul(self.tick_size_in_quote_lots_per_base_unit_per_tick)
.context("multiple overflow")?
.checked_div(self.base_lots_per_base_unit)
.context("division failed")?;
}
let filled_amount = base_lots
.checked_mul(self.base_lot_size)
.context("multiple overflow")?;
if best_price.is_none() {
let in_amount_for_level = quote_lots
.checked_mul(self.quote_lot_size)
.context("multiple overflow")?;
best_price = Some(
PhoenixAmm::compute_decimal_div(filled_amount, in_amount_for_level)
.context("Cannot compute price impact")?,
)
}
out_amount += filled_amount;
quote_lot_budget = quote_lot_budget.saturating_sub(quote_lots);
}
in_amount = (initial_quote_lot_budget - quote_lot_budget)
.checked_div(self.quote_lot_size)
.context("division failed")?;
if quote_lot_budget > 0 {
not_enough_liquidity = true;
}
};
// Not 100% accurate, but it's a reasonable enough approximation
let out_amount_after_fees = out_amount
.checked_mul(10_000 - self.taker_fee_bps)
.context("multiply overflow")?
.checked_div(10_000)
.context("division failed")?;
let fee_amount = out_amount - out_amount_after_fees;
let price_impact_pct = if quote_params.in_amount > 0 {
if let Some(best_price) = best_price {
let price = PhoenixAmm::compute_decimal_div(out_amount, quote_params.in_amount)
.context("Cannot compute price")?;
PhoenixAmm::compute_price_impact_pct(price, best_price)
.context("Cannot compute price impact")?
} else {
dec!(1)
}
} else {
dec!(1)
};
Ok(Quote {
not_enough_liquidity,
in_amount,
out_amount: out_amount_after_fees,
fee_amount,
fee_mint: quote_params.output_mint, // Technically quote_mint but fee is estimated on the output amount
fee_pct: self.fee_pct,
price_impact_pct,
..Quote::default()
})
}
fn get_swap_leg_and_account_metas(
&self,
swap_params: &SwapParams,
) -> Result<SwapAndAccountMetas> {
let SwapParams {
destination_mint,
source_mint,
user_destination_token_account,
user_source_token_account,
user_transfer_authority,
..
} = swap_params;
let log_authority = Pubkey::find_program_address(&["log".as_ref()], &ID).0;
let (side, base_account, quote_account) = if source_mint == &self.base_mint {
ensure!(destination_mint == &self.quote_mint, "Invalid quote mint");
(
Side::Ask,
*user_source_token_account,
*user_destination_token_account,
)
} else {
ensure!(destination_mint == &self.base_mint, "Invalid quote mint");
(
Side::Bid,
*user_destination_token_account,
*user_source_token_account,
)
};
let base_vault = Pubkey::find_program_address(
&[b"vault", self.market_key.as_ref(), self.base_mint.as_ref()],
&ID,
)
.0;
let quote_vault = Pubkey::find_program_address(
&[b"vault", self.market_key.as_ref(), self.quote_mint.as_ref()],
&ID,
)
.0;
let account_metas = PhoenixSwap {
swap_program: ID,
market: self.key(),
log_authority,
trader: *user_transfer_authority,
base_account,
quote_account,
base_vault,
quote_vault,
token_program: spl_token::ID,
}
.to_account_metas(None);
Ok(SwapAndAccountMetas {
swap: Swap::Phoenix { side },
account_metas,
})
}
fn clone_amm(&self) -> Box<dyn Amm + Send + Sync> {
Box::new(self.clone())
}
}
#[test]
fn test_jupiter_phoenix_integration() {
use crate::test_harness::AmmTestHarness;
use solana_sdk::pubkey;
use solana_sdk::pubkey::Pubkey;
const SOL_USDC_MARKET: Pubkey = pubkey!("14CAwu3LiBBk5fcHGdTsFyVxDwvpgFiSfDwgPJxECcE5");
let test_harness = AmmTestHarness::new();
let keyed_account = test_harness.get_keyed_account(SOL_USDC_MARKET).unwrap();
let mut phoenix_amm: PhoenixAmm = PhoenixAmm::from_keyed_account(&keyed_account).unwrap();
test_harness.update_amm(&mut phoenix_amm);
let in_amount = 1_000_000_000_000;
println!(
"Getting quote for selling {} SOL",
in_amount as f64 / 10.0_f64.powf(phoenix_amm.get_base_decimals() as f64)
);
let quote = phoenix_amm
.quote(&QuoteParams {
/// 1 SOL
in_amount,
input_mint: phoenix_amm.base_mint,
output_mint: phoenix_amm.quote_mint,
})
.unwrap();
let Quote { out_amount, .. } = quote;
println!(
"Quote result: {:?}",
out_amount as f64 / 10.0_f64.powf(phoenix_amm.get_quote_decimals() as f64)
);
let in_amount = out_amount;
println!(
"Getting quote for buying SOL with {} USDC",
in_amount as f64 / 10.0_f64.powf(phoenix_amm.get_quote_decimals() as f64)
);
let quote = phoenix_amm
.quote(&QuoteParams {
in_amount,
input_mint: phoenix_amm.quote_mint,
output_mint: phoenix_amm.base_mint,
})
.unwrap();
let Quote { out_amount, .. } = quote;
println!(
"Quote result: {:?}",
out_amount as f64 / 10.0_f64.powf(phoenix_amm.get_base_decimals() as f64)
);
}