Skip to content

Commit 789676b

Browse files
authored
Merge pull request #160 from mubking/Share-Price-Calculation-Logic
Share price calculation logic
2 parents e148371 + 32b1bd2 commit 789676b

9 files changed

Lines changed: 6750 additions & 2 deletions

contracts/vault/src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,34 @@ impl YieldVault {
562562
}
563563
}
564564

565+
/// Returns the current share price scaled by 10^18 (1_000_000_000_000_000_000).
566+
///
567+
/// Formula: share_price = (total_assets * 10^18) / total_shares
568+
///
569+
/// To convert to a human-readable price, divide the result by 10^18.
570+
/// Examples:
571+
/// 1_000_000_000_000_000_000 → 1.0 (1 share = 1 token)
572+
/// 1_200_000_000_000_000_000 → 1.2 (1 share = 1.2 tokens, after yield accrual)
573+
/// 500_000_000_000_000_000 → 0.5 (1 share = 0.5 tokens)
574+
///
575+
/// Returns 1:1 (10^18) when no shares have been minted yet (fresh vault).
576+
pub fn get_share_price(env: Env) -> i128 {
577+
let ts = Self::total_shares(env.clone());
578+
let ta = Self::total_assets(env.clone());
579+
580+
if ts == 0 {
581+
// No shares minted yet — initial price is 1:1
582+
return 1_000_000_000_000_000_000i128;
583+
}
584+
585+
// Scale TVL up before dividing to preserve decimal precision.
586+
// Using checked arithmetic to guard against overflow.
587+
ta.checked_mul(1_000_000_000_000_000_000i128)
588+
.expect("overflow in share price calculation")
589+
.checked_div(ts)
590+
.expect("division error in share price calculation")
591+
}
592+
565593
/// Deposits underlying tokens in exchange for vault shares.
566594
///
567595
/// ### Parameters
@@ -711,4 +739,4 @@ impl YieldVault {
711739

712740
Ok(())
713741
}
714-
}
742+
}

0 commit comments

Comments
 (0)