Skip to content

Commit 5061baf

Browse files
committed
feat: rust lst price
1 parent 748e443 commit 5061baf

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333

3434
### Changed
3535

36+
- improve stake pool price
3637
- `createCustomizablePermissionlessConstantProductPool` function `customizableParams` now require `feeCurve`. See [test](./ts-client/src/amm/tests/initializeCustomizablePermissionlessConstantProductPool.test.ts) example.
3738
- `createCustomizablePermissionlessConstantProductPool` function `customizableParams` no longer need to pass in `padding`
3839
- `calculateTradingFee` and `calculateProtocolTradingFee` function receive `PoolFees` instead of `PoolState`

dynamic-amm-quote/src/depeg/spl_stake.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,33 @@ use std::convert::TryInto;
66
pub fn get_virtual_price(bytes: &[u8]) -> Option<u64> {
77
let stake: StakePool = borsh0_10::try_from_slice_unchecked(bytes).ok()?;
88

9-
let virtual_price = (stake.total_lamports as u128)
10-
.checked_mul(depeg::PRECISION as u128)?
11-
.checked_div(stake.pool_token_supply as u128)?;
9+
let total_lamports: u128 = stake.total_lamports.into();
10+
let pool_token_supply: u128 = stake.pool_token_supply.into();
11+
12+
// may be higher if we apply deposit fee
13+
let deposit_price = total_lamports
14+
.checked_mul(depeg::PRECISION.into())?
15+
.checked_div(pool_token_supply)?;
16+
17+
let withdraw_fee_denominator: u128 = stake.sol_withdrawal_fee.denominator.into();
18+
let withdraw_fee_numerator: u128 = stake.sol_withdrawal_fee.numerator.into();
19+
20+
// sanity check
21+
if withdraw_fee_denominator <= withdraw_fee_numerator.checked_mul(10)? {
22+
return deposit_price.try_into().ok();
23+
}
24+
25+
let withdraw_price = total_lamports
26+
.checked_mul(withdraw_fee_denominator.checked_sub(withdraw_fee_numerator)?)?
27+
.checked_mul(depeg::PRECISION.into())?
28+
.checked_div(withdraw_fee_denominator)?
29+
.checked_div(pool_token_supply)?;
30+
31+
// deposit price has weight more than 3 times with withdraw price
32+
let virtual_price = deposit_price
33+
.checked_mul(3)?
34+
.checked_add(withdraw_price)?
35+
.checked_div(4)?;
1236

1337
virtual_price.try_into().ok()
1438
}

ts-client/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "@meteora-ag/dynamic-amm-sdk",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Meteora Dynamic Amm SDK is a typescript library that allows you to interact with Meteora's AMM.",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",
77
"scripts": {
88
"build": "rm -rf dist && tsc -p tsconfig.build.json && tsc -p tsconfig.esm.json",
9-
"test": "jest ./src/amm/tests/initializeCustomizablePermissionlessConstantProductPool.test.ts --runInBand"
9+
"test": "jest ./src/amm/tests/*.test.ts --runInBand"
1010
},
1111
"files": [
1212
"dist"

0 commit comments

Comments
 (0)