Skip to content

Commit 84569d6

Browse files
authored
add swap fee unit tests (#1713)
* add swap fee unit tests * remove linear inventory fee component
1 parent 6e6813a commit 84569d6

File tree

3 files changed

+92
-19
lines changed

3 files changed

+92
-19
lines changed

programs/drift/src/state/lp_pool.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -334,22 +334,6 @@ impl LPPool {
334334
Ok((lp_burn_amount, out_amount, lp_fee_to_charge, out_fee_amount))
335335
}
336336

337-
pub fn get_linear_fee_inventory(
338-
&self,
339-
notional_error: i128,
340-
trade_notional: i128,
341-
kappa_inventory: u64,
342-
) -> DriftResult<i128> {
343-
notional_error
344-
.safe_mul(-1_i128)?
345-
.safe_mul(trade_notional.signum() as i128)?
346-
.safe_mul(kappa_inventory.cast::<i128>()?)?
347-
.safe_mul(PERCENTAGE_PRECISION_I128)?
348-
.cast::<i128>()?
349-
.safe_div(self.last_aum.cast::<i128>()?)?
350-
.safe_div(QUOTE_PRECISION_I128)
351-
}
352-
353337
pub fn get_quadratic_fee_inventory(
354338
&self,
355339
gamma_covar: [[i128; 2]; 2],

programs/drift/src/state/lp_pool/tests.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,3 +1636,89 @@ mod swap_tests {
16361636
}
16371637
}
16381638
}
1639+
1640+
#[cfg(test)]
1641+
mod swap_fee_tests {
1642+
use crate::math::constants::{
1643+
PERCENTAGE_PRECISION_I64, PERCENTAGE_PRECISION_U64, QUOTE_PRECISION,
1644+
};
1645+
use crate::state::lp_pool::*;
1646+
1647+
#[test]
1648+
fn test_get_gamma_covar_matrix() {
1649+
// in = sol, out = btc
1650+
let covar_matrix = get_gamma_covar_matrix(
1651+
PERCENTAGE_PRECISION_I64,
1652+
2, // gamma sol
1653+
2, // gamma btc
1654+
4 * PERCENTAGE_PRECISION_U64 / 100, // vol sol
1655+
3 * PERCENTAGE_PRECISION_U64 / 100, // vol btc
1656+
)
1657+
.unwrap();
1658+
assert_eq!(covar_matrix, [[3200, 2400], [2400, 1800]]);
1659+
}
1660+
1661+
#[test]
1662+
fn test_lp_pool_get_linear_fee_execution() {
1663+
let lp_pool = LPPool {
1664+
last_aum: 10_000_000 * QUOTE_PRECISION, // $10,000,000
1665+
..LPPool::default()
1666+
};
1667+
1668+
let fee_execution_linear = lp_pool
1669+
.get_linear_fee_execution(
1670+
5_000_000 * QUOTE_PRECISION_I128,
1671+
1600, // 0.0016
1672+
2,
1673+
15_000_000 * QUOTE_PRECISION,
1674+
)
1675+
.unwrap();
1676+
1677+
assert_eq!(fee_execution_linear, 1066); // 10.667 bps
1678+
}
1679+
1680+
#[test]
1681+
fn test_lp_pool_get_quadratic_fee_execution() {
1682+
let lp_pool = LPPool {
1683+
last_aum: 10_000_000 * QUOTE_PRECISION, // $10,000,000
1684+
..LPPool::default()
1685+
};
1686+
1687+
let fee_execution_quadratic = lp_pool
1688+
.get_quadratic_fee_execution(
1689+
5_000_000 * QUOTE_PRECISION_I128,
1690+
1600, // 0.0016
1691+
2,
1692+
15_000_000 * QUOTE_PRECISION,
1693+
)
1694+
.unwrap();
1695+
1696+
assert_eq!(fee_execution_quadratic, 711); // 7.1 bps
1697+
}
1698+
1699+
#[test]
1700+
fn test_lp_pool_get_quadratic_fee_inventory() {
1701+
let lp_pool = LPPool {
1702+
last_aum: 10_000_000 * QUOTE_PRECISION, // $10,000,000
1703+
..LPPool::default()
1704+
};
1705+
1706+
let (fee_in, fee_out) = lp_pool
1707+
.get_quadratic_fee_inventory(
1708+
[[3200, 2400], [2400, 1800]],
1709+
[
1710+
1_000_000 * QUOTE_PRECISION_I128,
1711+
-500_000 * QUOTE_PRECISION_I128,
1712+
],
1713+
[
1714+
-4_000_000 * QUOTE_PRECISION_I128,
1715+
4_500_000 * QUOTE_PRECISION_I128,
1716+
],
1717+
5_000_000 * QUOTE_PRECISION_I128,
1718+
)
1719+
.unwrap();
1720+
1721+
assert_eq!(fee_in, 6 * PERCENTAGE_PRECISION_I128 / 100000); // 0.6 bps
1722+
assert_eq!(fee_out, -6 * PERCENTAGE_PRECISION_I128 / 100000); // -0.6 bps
1723+
}
1724+
}

tests/lpPool.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ describe('LP Pool', () => {
400400
swapFeeMax: new BN(2).mul(PERCENTAGE_PRECISION),
401401
oracleStalenessThreshold: new BN(400),
402402
costToTrade: 1,
403-
constituentDerivativeDepegThreshold: PERCENTAGE_PRECISION.divn(10).muln(9),
403+
constituentDerivativeDepegThreshold:
404+
PERCENTAGE_PRECISION.divn(10).muln(9),
404405
derivativeWeight: ZERO,
405406
volatility: new BN(10).mul(PERCENTAGE_PRECISION),
406407
constituentCorrelations: [ZERO],
@@ -703,7 +704,8 @@ describe('LP Pool', () => {
703704
oracleStalenessThreshold: new BN(400),
704705
costToTrade: 1,
705706
derivativeWeight: PERCENTAGE_PRECISION.divn(2),
706-
constituentDerivativeDepegThreshold: PERCENTAGE_PRECISION.divn(10).muln(9),
707+
constituentDerivativeDepegThreshold:
708+
PERCENTAGE_PRECISION.divn(10).muln(9),
707709
volatility: new BN(10).mul(PERCENTAGE_PRECISION),
708710
constituentCorrelations: [ZERO, PERCENTAGE_PRECISION.muln(87).divn(100)],
709711
constituentDerivativeIndex: 1,
@@ -1213,7 +1215,8 @@ describe('LP Pool', () => {
12131215
oracleStalenessThreshold: new BN(400),
12141216
costToTrade: 1,
12151217
derivativeWeight: PERCENTAGE_PRECISION.divn(4),
1216-
constituentDerivativeDepegThreshold: PERCENTAGE_PRECISION.divn(10).muln(9),
1218+
constituentDerivativeDepegThreshold:
1219+
PERCENTAGE_PRECISION.divn(10).muln(9),
12171220
volatility: new BN(10).mul(PERCENTAGE_PRECISION),
12181221
constituentCorrelations: [
12191222
ZERO,

0 commit comments

Comments
 (0)