Calculate the true price of mSOL/SOL
Calculate the true mSOL/SOL price
you need to read Marinade state, an example is available at this link.
marinade_state.msol_price: u64
, and that is mSOL price in SOL multiplied by 0x1_0000_0000 (shifted), so to obtain mSOL/SOL as f64 you should do: let msol_price_f64: f64 = marinade_state.msol_price as f64 / 0x1_0000_0000 as f64
, and then you get the true mSOL/SOL price.How much SOL an amount of mSOL represents
You start with the previous example and some amount of mSOL-lamports, then:
let SOL_lamports = (mSOL_lamports as u128 * marinade_state.msol_price as u128 / 0x1_0000_0000 as u128) as u64
let mSOL_lamports = (mSOL_lamports as u128 * 0x1_0000_0000 as u128 / marinade_state.msol_price as u128) as u64
Derive mSOL/USDC price
If you have access to SOL/USDC price from an oracle, the best way to avoid losses due to price inaccuracies is to derive mSOL/USDC from SOL/USDC and Marinade true price
let mSOL_usdc = (SOL_usdc as u128 * marinade_state.msol_price as u128 / 0x1_0000_0000 as u128) as u64
Notes
msol_price is computed and stored at this link (https://github.com/marinade-finance/liquid-staking-program/blob/main/programs/marinade-finance/src/state/update.rs#L247) after each epoch ends, when SOL staking rewards are added to the pool.
You can also use the fns marinade_state.calc_lamports_from_msol_amount()
and marinade_state.calc_msol_from_lamports()
for better precision when computing mSOL from SOL and vice versa.