pub const SHARE_OFFSET: u64 = 1_000;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BtcSharesValue {
pub gross: u64,
pub fee: u64,
}
#[derive(Debug, thiserror::Error)]
pub enum BtcSharesValueError {
#[error("shares exceed the vault's issued shares")]
InsufficientShares,
#[error("claim fee bps exceed the 10000 denominator")]
InvalidClaimFeeBps,
}
pub fn sats_to_btc(
shares: u64,
vault_amount: u64,
vault_shares: u64,
claim_fee_bps: u32,
) -> Result<BtcSharesValue, BtcSharesValueError> {
const BPS_DENOMINATOR: u128 = 10_000;
if claim_fee_bps as u128 > BPS_DENOMINATOR {
return Err(BtcSharesValueError::InvalidClaimFeeBps);
}
if shares == 0 {
return Ok(BtcSharesValue { gross: 0, fee: 0 });
}
if shares > vault_shares {
return Err(BtcSharesValueError::InsufficientShares);
}
let gross = (shares as u128 * (vault_amount as u128 + 1) / (vault_shares as u128 + SHARE_OFFSET as u128)) as u64;
let fee = (gross as u128 * claim_fee_bps as u128 / BPS_DENOMINATOR) as u64;
Ok(BtcSharesValue { gross, fee })
}
#[derive(Debug, thiserror::Error)]
pub enum BtcToSatsError {
#[error("share amount is not computable for the vault state")]
MathOverflow,
}
pub fn btc_to_sats(btc_amount: u64, vault_amount: u64, vault_shares: u64) -> Result<u64, BtcToSatsError> {
u64::try_from(btc_amount as u128 * (vault_shares as u128 + SHARE_OFFSET as u128) / (vault_amount as u128 + 1))
.map_err(|_| BtcToSatsError::MathOverflow)
}
#[cfg(test)]
mod tests {
use super::*;
const CLAIM_FEE_BPS: u32 = 1_000;
#[test]
fn values_shares_at_the_current_exchange_rate() {
assert_eq!(sats_to_btc(50_000, 100, 100_000, CLAIM_FEE_BPS).unwrap(), BtcSharesValue { gross: 50, fee: 5 });
}
#[test]
fn full_drain_of_appreciated_vault_matches_on_chain_payout() {
assert_eq!(sats_to_btc(50_000, 55, 50_000, CLAIM_FEE_BPS).unwrap(), BtcSharesValue { gross: 54, fee: 5 });
}
#[test]
fn floors_gross_and_fee() {
assert_eq!(sats_to_btc(1_000, 10, 3_000, CLAIM_FEE_BPS).unwrap(), BtcSharesValue { gross: 2, fee: 0 });
}
#[test]
fn zero_fee_bps_yields_gross_only() {
assert_eq!(sats_to_btc(50_000, 100, 100_000, 0).unwrap(), BtcSharesValue { gross: 50, fee: 0 });
}
#[test]
fn zero_shares_are_worth_zero() {
assert_eq!(sats_to_btc(0, 100, 100_000, CLAIM_FEE_BPS).unwrap(), BtcSharesValue { gross: 0, fee: 0 });
assert_eq!(sats_to_btc(0, 0, 0, CLAIM_FEE_BPS).unwrap(), BtcSharesValue { gross: 0, fee: 0 });
}
#[test]
fn rejects_claim_fee_above_the_bps_denominator() {
assert!(matches!(sats_to_btc(50_000, 100, 100_000, 10_001), Err(BtcSharesValueError::InvalidClaimFeeBps)));
}
#[test]
fn full_fee_withholds_the_entire_gross() {
assert_eq!(sats_to_btc(50_000, 100, 100_000, 10_000).unwrap(), BtcSharesValue { gross: 50, fee: 50 });
}
#[test]
fn rejects_overdraw() {
assert!(matches!(
sats_to_btc(100_001, 100, 100_000, CLAIM_FEE_BPS),
Err(BtcSharesValueError::InsufficientShares)
));
assert!(matches!(sats_to_btc(1, 5, 0, CLAIM_FEE_BPS), Err(BtcSharesValueError::InsufficientShares)));
}
#[test]
fn handles_max_values_without_overflow() {
let max = u64::MAX;
let value = sats_to_btc(max, max, max, CLAIM_FEE_BPS).unwrap();
assert!(value.gross <= max && value.gross > max - SHARE_OFFSET);
}
#[test]
fn first_deposit_mints_at_the_offset_rate() {
assert_eq!(btc_to_sats(100, 0, 0).unwrap(), 100 * SHARE_OFFSET);
}
#[test]
fn later_deposit_mints_at_exchange_rate_and_floors() {
assert_eq!(btc_to_sats(11, 55, 50_000).unwrap(), 10_017);
assert_eq!(btc_to_sats(10, 55, 50_000).unwrap(), 9_107);
}
#[test]
fn small_deposit_into_appreciated_vault_mints_shares() {
assert_eq!(btc_to_sats(1, 2_000, 1_000_000).unwrap(), 500);
}
#[test]
fn zero_deposit_mints_zero_shares() {
assert_eq!(btc_to_sats(0, 100, 100_000).unwrap(), 0);
assert_eq!(btc_to_sats(0, 0, 0).unwrap(), 0);
}
#[test]
fn shares_without_btc_no_longer_error() {
assert_eq!(btc_to_sats(1, 0, 100).unwrap(), 1_100);
}
#[test]
fn errors_where_on_chain_math_fails() {
assert!(matches!(btc_to_sats(u64::MAX, 1, 2), Err(BtcToSatsError::MathOverflow)));
}
#[test]
fn round_trips_with_sats_to_btc_at_zero_fee() {
let (deposit, vault_amount, vault_shares) = (11, 100, 100_000);
let shares = btc_to_sats(deposit, vault_amount, vault_shares).unwrap();
assert_eq!(shares, 11_000);
let value = sats_to_btc(shares, vault_amount + deposit, vault_shares + shares, 0).unwrap();
assert_eq!(value.gross, deposit);
}
}