-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathquote.rs
More file actions
306 lines (273 loc) · 9.3 KB
/
Copy pathquote.rs
File metadata and controls
306 lines (273 loc) · 9.3 KB
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
use anyhow::{anyhow, ensure, Context, Result};
use cp_amm::{
params::swap::TradeDirection,
state::{fee::FeeMode, Pool, SwapResult},
ActivationType,
};
use spl_token_2022::extension::{
transfer_fee::{TransferFee, TransferFeeConfig},
BaseStateWithExtensions, StateWithExtensions,
};
use spl_token_2022::state::Mint;
/// Off-chain quote for a potential swap.
#[derive(Debug, PartialEq)]
pub struct Quote {
/// Resulting pool math prior to Token-2022 transfer fees being applied.
pub swap_result: SwapResult,
/// Amount that effectively reaches the pool after deducting input transfer fees.
pub effective_amount_in: u64,
/// Portion of the provided amount collected as input transfer fee.
pub input_transfer_fee: u64,
/// Amount the taker receives after output transfer fees are withheld.
pub effective_amount_out: u64,
/// Portion of the pool's output withheld as transfer fee.
pub output_transfer_fee: u64,
}
/// Transfer-fee metadata extracted for each pool token mint.
#[derive(Clone, Debug, PartialEq)]
pub struct MintTransferFees {
token_a: Option<TransferFee>,
token_b: Option<TransferFee>,
token_a_known: bool,
token_b_known: bool,
}
impl Default for MintTransferFees {
fn default() -> Self {
Self {
token_a: None,
token_b: None,
token_a_known: false,
token_b_known: false,
}
}
}
impl MintTransferFees {
/// Create a transfer-fee bundle when both token mints have already been inspected.
pub fn new(token_a: Option<TransferFee>, token_b: Option<TransferFee>) -> Self {
Self {
token_a,
token_b,
token_a_known: true,
token_b_known: true,
}
}
pub fn input_fee(&self, a_to_b: bool) -> Option<&TransferFee> {
if a_to_b {
self.token_a.as_ref()
} else {
self.token_b.as_ref()
}
}
pub fn output_fee(&self, a_to_b: bool) -> Option<&TransferFee> {
if a_to_b {
self.token_b.as_ref()
} else {
self.token_a.as_ref()
}
}
fn input_known(&self, a_to_b: bool) -> bool {
if a_to_b {
self.token_a_known
} else {
self.token_b_known
}
}
fn output_known(&self, a_to_b: bool) -> bool {
if a_to_b {
self.token_b_known
} else {
self.token_a_known
}
}
/// Construct transfer-fee metadata by parsing on-chain mint accounts.
pub fn from_pool_mints(
pool: &Pool,
current_epoch: u64,
token_a_mint_account: Option<&[u8]>,
token_b_mint_account: Option<&[u8]>,
) -> Result<Self> {
let token_a_flag = TokenProgramFlag::try_from(pool.token_a_flag)?;
let token_b_flag = TokenProgramFlag::try_from(pool.token_b_flag)?;
let (token_a, token_a_known) = match token_a_flag {
TokenProgramFlag::TokenProgram => (None, true),
TokenProgramFlag::TokenProgram2022 => {
let mint_data = token_a_mint_account
.context("token A mint account data required for Token-2022 pools")?;
(load_transfer_fee(mint_data, current_epoch)?, true)
}
};
let (token_b, token_b_known) = match token_b_flag {
TokenProgramFlag::TokenProgram => (None, true),
TokenProgramFlag::TokenProgram2022 => {
let mint_data = token_b_mint_account
.context("token B mint account data required for Token-2022 pools")?;
(load_transfer_fee(mint_data, current_epoch)?, true)
}
};
Ok(Self {
token_a,
token_b,
token_a_known,
token_b_known,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum TokenProgramFlag {
TokenProgram,
TokenProgram2022,
}
impl TryFrom<u8> for TokenProgramFlag {
type Error = anyhow::Error;
fn try_from(value: u8) -> Result<Self> {
match value {
0 => Ok(Self::TokenProgram),
1 => Ok(Self::TokenProgram2022),
_ => Err(anyhow!("unknown token program flag: {value}")),
}
}
}
/// Compute an off-chain quote, mirroring on-chain transfer-fee behaviour.
///
/// The `transfer_fees` argument should be populated with [`MintTransferFees::from_pool_mints`]
/// or another source that reflects the current epoch's Token-2022 transfer rules. Quotes for
/// Token-2022 pools error out when the relevant mint data has not been provided, preventing
/// callers from accidentally ignoring the additional fees enforced on-chain.
pub fn get_quote(
pool: &Pool,
current_timestamp: u64,
current_slot: u64,
actual_amount_in: u64,
a_to_b: bool,
has_referral: bool,
transfer_fees: &MintTransferFees,
) -> Result<Quote> {
ensure!(actual_amount_in > 0, "amount is zero");
let activation_type =
ActivationType::try_from(pool.activation_type).context("invalid activation type")?;
let current_point = match activation_type {
ActivationType::Slot => current_slot,
ActivationType::Timestamp => current_timestamp,
};
ensure!(
pool.pool_status == 0 && pool.activation_point <= current_point,
"Swap is disabled",
);
let trade_direction = if a_to_b {
TradeDirection::AtoB
} else {
TradeDirection::BtoA
};
let (input_fee, output_fee) = transfer_fees_for_direction(pool, transfer_fees, trade_direction)?;
let TransferFeeAmount {
amount: effective_amount_in,
transfer_fee: input_transfer_fee,
} = calculate_transfer_fee_excluded_amount(input_fee, actual_amount_in)?;
ensure!(effective_amount_in > 0, "amount is zero after transfer fees");
let fee_mode = &FeeMode::get_fee_mode(pool.collect_fee_mode, trade_direction, has_referral)?;
let swap_result = pool.get_swap_result(
effective_amount_in,
fee_mode,
trade_direction,
current_point,
)?;
let TransferFeeAmount {
amount: effective_amount_out,
transfer_fee: output_transfer_fee,
} = calculate_transfer_fee_excluded_amount(output_fee, swap_result.output_amount)?;
Ok(Quote {
swap_result,
effective_amount_in,
input_transfer_fee,
effective_amount_out,
output_transfer_fee,
})
}
fn transfer_fees_for_direction<'a>(
pool: &Pool,
transfer_fees: &'a MintTransferFees,
trade_direction: TradeDirection,
) -> Result<(Option<&'a TransferFee>, Option<&'a TransferFee>)> {
match trade_direction {
TradeDirection::AtoB => Ok((
require_transfer_fee(
TokenProgramFlag::try_from(pool.token_a_flag)?,
transfer_fees.input_known(true),
transfer_fees.input_fee(true),
"token A",
)?,
require_transfer_fee(
TokenProgramFlag::try_from(pool.token_b_flag)?,
transfer_fees.output_known(true),
transfer_fees.output_fee(true),
"token B",
)?,
)),
TradeDirection::BtoA => Ok((
require_transfer_fee(
TokenProgramFlag::try_from(pool.token_b_flag)?,
transfer_fees.input_known(false),
transfer_fees.input_fee(false),
"token B",
)?,
require_transfer_fee(
TokenProgramFlag::try_from(pool.token_a_flag)?,
transfer_fees.output_known(false),
transfer_fees.output_fee(false),
"token A",
)?,
)),
}
}
fn require_transfer_fee<'a>(
flag: TokenProgramFlag,
is_known: bool,
transfer_fee: Option<&'a TransferFee>,
label: &str,
) -> Result<Option<&'a TransferFee>> {
match flag {
TokenProgramFlag::TokenProgram => Ok(None),
TokenProgramFlag::TokenProgram2022 => {
ensure!(
is_known,
"missing transfer-fee data for {label} Token-2022 mint"
);
Ok(transfer_fee)
}
}
}
#[derive(Debug, PartialEq)]
struct TransferFeeAmount {
amount: u64,
transfer_fee: u64,
}
fn calculate_transfer_fee_excluded_amount(
transfer_fee: Option<&TransferFee>,
transfer_fee_included_amount: u64,
) -> Result<TransferFeeAmount> {
if let Some(transfer_fee) = transfer_fee {
let transfer_fee_amount = transfer_fee
.calculate_fee(transfer_fee_included_amount)
.ok_or_else(|| anyhow!("transfer fee calculation overflow"))?;
let transfer_fee_excluded_amount = transfer_fee_included_amount
.checked_sub(transfer_fee_amount)
.ok_or_else(|| anyhow!("transfer fee exceeds provided amount"))?;
Ok(TransferFeeAmount {
amount: transfer_fee_excluded_amount,
transfer_fee: transfer_fee_amount,
})
} else {
Ok(TransferFeeAmount {
amount: transfer_fee_included_amount,
transfer_fee: 0,
})
}
}
fn load_transfer_fee(mint_account_data: &[u8], epoch: u64) -> Result<Option<TransferFee>> {
let mint = StateWithExtensions::<Mint>::unpack(mint_account_data)?;
if let Ok(config) = mint.get_extension::<TransferFeeConfig>() {
Ok(Some(config.get_epoch_fee(epoch).clone()))
} else {
Ok(None)
}
}