Skip to content

Commit 6414f35

Browse files
authored
details on how to call liquidity related fns
1 parent f91ab0f commit 6414f35

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pyth-sdk/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,49 @@ println!("0.1 BTC and 0.05 ETH are worth: ({} +- {}) x 10^{} USD",
8181
```
8282

8383
This operation can be useful for pricing, e.g., an LP token that is backed by two underlying currencies.
84+
85+
## Liquidity-related Pricing Measures
86+
87+
A variety of measures should be taken for protocols looking to mitigate liquidity-related risks. To limit deposits and borrows, protocols could simply derive their limit from the off-chain liquidity estimates and then store those values in contract state. No explicit interaction with the Pyth SDK would be needed in this simple case.
88+
89+
To adjust the price at which collateral is valued based on the liquidity information, a protocol can combine the current Pyth price and their estimate of liquidity:
90+
91+
```rust
92+
let btc_usd: Price = ...;
93+
let deposits: u64 = ...;
94+
let deposits_endpoint: u64 = ...;
95+
let rate_discount_initial: u64 = ...;
96+
let rate_discount_final: u64 = ...;
97+
let discount_exponent: i32 = ...;
98+
99+
let price_collateral: Price = btc_usd.get_collateral_valuation_price(
100+
deposits,
101+
deposits_endpoint,
102+
rate_discount_initial,
103+
rate_discount_final,
104+
discount_exponent).ok_or(StdError::not_found("Issue with querying collateral price"))?;
105+
println!("The valuation price for the collateral given {} tokens deposited is ({} +- {}) x 10^{} USD",
106+
deposits, price_collateral.price, price_collateral.conf, price_collateral.expo);
107+
```
108+
109+
Here, `deposits` indicates the total amount of collateral deposited. `get_collateral_valuation_price` takes in the total deposits in the protocol and interpolates between (`0`, `rate_discount_inital`) and (`deposits_endpoint`, `rate_discount_final`) to linearly caclulate the discount at `deposits`. As a note, this function scales down the price depending on the provided discount and deposit inputs, but it does not alter the confidence.
110+
111+
To adjust the price at which a borrow position is valued, a protocol can similarly combine the current Pyth price and their estimate of liquidity:
112+
113+
```rust
114+
let btc_usd: Price = ...;
115+
let borrows: u64 = ...;
116+
let borrows_endpoint: u64 = ...;
117+
let rate_premium_initial: u64 = ...;
118+
let rate_premium_final: u64 = ...;
119+
let premium_exponent: i32 = ...;
120+
121+
let price_borrow: Price = btc_usd.get_borrow_valuation_price(
122+
borrows,
123+
borrows_endpoint,
124+
rate_premium_initial,
125+
rate_premium_final,
126+
discount_exponent).ok_or(StdError::not_found("Issue with querying collateral price"))?;
127+
println!("The valuation price for the borrow given {} tokens borrowed is ({} +- {}) x 10^{} USD",
128+
borrows, price_borrow.price, price_borrow.conf, price_borrow.expo);
129+
```

0 commit comments

Comments
 (0)