Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions finance/betting-market/kani-proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn winnings(stake: u64, distributable: u64, winning_pool: u64) -> Option<u64
/// of the whole pool): the fee never exceeds the losing pool (so `distributable`
/// never underflows), the fee is the ceiling of `losing * fee_bps / 10_000` (it
/// rounds in the program's favor, never the winners'), and
/// `winning + distributable + fee == total` — every base unit is accounted for.
/// `winning + distributable + fee == total` — every minor unit is accounted for.
#[cfg(kani)]
#[kani::proof]
#[kani::solver(cadical)]
Expand All @@ -80,7 +80,7 @@ fn proof_settlement_fee_and_split() {
assert!(fee <= losing); // fee only ever a fraction of the losing pool
assert_eq!(losing, total_pool - winning_pool);
// The fee is the ceiling: at least the exact fraction, and the smallest
// integer that is, so no more than one base unit above it.
// integer that is, so no more than one minor unit above it.
let exact = losing as u128 * fee_bps as u128; // in units of 1/10_000
assert!(fee as u128 * BPS_DENOMINATOR >= exact);
assert!((fee as u128) * BPS_DENOMINATOR < exact + BPS_DENOMINATOR);
Expand Down
2 changes: 1 addition & 1 deletion finance/lending/anchor-v1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ rather than bootstrapping it.

Pricing shares against tracked `total_liquidity` stopped a vault donation, but
`total_liquidity` includes interest owed on borrows. A lone supplier with one
share could borrow a single base unit from their own reserve, let one second of
share could borrow a single minor unit from their own reserve, let one second of
interest round the debt up to two, and then ratchet the share price up about
half again per round by depositing the most that still minted one share and
redeeming it, leaving the rounding with their own share. On the default rate
Expand Down
2 changes: 1 addition & 1 deletion finance/lending/anchor-v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ so a token donated directly to the vault cannot inflate the rate.

That alone does not close the empty-pool inflation attack, because
`total_liquidity` also counts interest owed on borrows, and a supplier can
borrow. A lone supplier holding one share borrows a single base unit from their
borrow. A lone supplier holding one share borrows a single minor unit from their
own reserve; debt rounds up, so a second later it reads as two, and the share is
worth two units without anything having been minted. Deposits and redemptions
round in the pool's favor, so depositing the most that still mints one share
Expand Down
6 changes: 3 additions & 3 deletions finance/lending/anchor-v1/programs/lending/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::errors::LendingError;

/// Which way to break ties when a division truncates. Deposits/redeems and
/// collateral valuations round the user's favourable quantity DOWN; debt and
/// program-owed quantities round UP. The program never loses a base unit to
/// program-owed quantities round UP. The program never loses a minor unit to
/// rounding, so dust cannot be extracted by repeated round-trips.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Rounding {
Expand Down Expand Up @@ -50,7 +50,7 @@ fn mul_div(a: u128, b: u128, denominator: u128, rounding: Rounding) -> Result<u1
}
}

/// Quote-currency value (in FIXED_POINT_SCALE-scaled units) of `amount` base
/// Quote-currency value (in FIXED_POINT_SCALE-scaled units) of `amount` minor
/// units of a token with `decimals`, given `price_scaled` from a price feed.
///
/// `price_scaled` already carries the FIXED_POINT_SCALE factor (it is the real
Expand All @@ -66,7 +66,7 @@ pub fn market_value(
mul_div(amount as u128, price_scaled, divisor, rounding)
}

/// Inverse of [`market_value`]: how many base units of a token with `decimals`
/// Inverse of [`market_value`]: how many minor units of a token with `decimals`
/// are worth `value_scaled` quote-currency value at `price_scaled`.
pub fn value_to_amount(
value_scaled: u128,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Reserve {

pub liquidity_decimals: u8,

/// Base units sitting in `liquidity_vault`, available to borrow or redeem.
/// Minor units sitting in `liquidity_vault`, available to borrow or redeem.
/// The program reads the pool size from this field, not the vault's token
/// balance, so a raw token donation cannot move the exchange rate.
pub available_liquidity: u64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn sole_supplier_leaves_the_minimum_behind() {
/// straight to the vault move nothing. But `total_liquidity` also counts
/// interest owed on borrows, and a supplier can borrow from their own reserve.
///
/// The attacker opens the reserve holding a single share, borrows one base
/// The attacker opens the reserve holding a single share, borrows one minor
/// unit of it against collateral in another reserve, and lets one second pass.
/// Debt is rounded up, so the one unit now reads as two and the lone share is
/// worth two units without having minted anything. From there, deposits and
Expand Down Expand Up @@ -157,7 +157,7 @@ fn inflating_shares_through_own_borrow_does_not_pay() {
}
assert_eq!(env.token_balance(attacker_share), 1);

// Borrow one base unit against collateral in another reserve, and let a
// Borrow one minor unit against collateral in another reserve, and let a
// second of interest round that debt up to two.
env.fund(&attacker, collateral.mint, 1_000_000_000);
let collateral_share = env.supply(&attacker, &collateral, 1_000_000_000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn unhealthy_obligation_liquidated_with_bonus_capped_by_close_factor() {
)
.unwrap();

// Exactly $350 (350M base units) was repaid — close-factor cap, not the full offer.
// Exactly $350 (350M minor units) was repaid — close-factor cap, not the full offer.
assert_eq!(
env.token_balance(liquidator_repay_account),
1_000_000_000 - 350_000_000
Expand Down
2 changes: 1 addition & 1 deletion finance/lending/anchor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ rather than bootstrapping it.

Pricing shares against tracked `total_liquidity` stopped a vault donation, but
`total_liquidity` includes interest owed on borrows. A lone supplier with one
share could borrow a single base unit from their own reserve, let one second of
share could borrow a single minor unit from their own reserve, let one second of
interest round the debt up to two, and then ratchet the share price up about
half again per round by depositing the most that still minted one share and
redeeming it, leaving the rounding with their own share. On the default rate
Expand Down
2 changes: 1 addition & 1 deletion finance/lending/anchor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ so a token donated directly to the vault cannot inflate the rate.

That alone does not close the empty-pool inflation attack, because
`total_liquidity` also counts interest owed on borrows, and a supplier can
borrow. A lone supplier holding one share borrows a single base unit from their
borrow. A lone supplier holding one share borrows a single minor unit from their
own reserve; debt rounds up, so a second later it reads as two, and the share is
worth two units without anything having been minted. Deposits and redemptions
round in the pool's favor, so depositing the most that still mints one share
Expand Down
6 changes: 3 additions & 3 deletions finance/lending/anchor/programs/lending/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::errors::LendingError;

/// Which way to break ties when a division truncates. Deposits/redeems and
/// collateral valuations round the user's favourable quantity DOWN; debt and
/// program-owed quantities round UP. The program never loses a base unit to
/// program-owed quantities round UP. The program never loses a minor unit to
/// rounding, so dust cannot be extracted by repeated round-trips.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Rounding {
Expand Down Expand Up @@ -50,7 +50,7 @@ fn mul_div(a: u128, b: u128, denominator: u128, rounding: Rounding) -> Result<u1
}
}

/// Quote-currency value (in FIXED_POINT_SCALE-scaled units) of `amount` base
/// Quote-currency value (in FIXED_POINT_SCALE-scaled units) of `amount` minor
/// units of a token with `decimals`, given `price_scaled` from a price feed.
///
/// `price_scaled` already carries the FIXED_POINT_SCALE factor (it is the real
Expand All @@ -66,7 +66,7 @@ pub fn market_value(
mul_div(amount as u128, price_scaled, divisor, rounding)
}

/// Inverse of [`market_value`]: how many base units of a token with `decimals`
/// Inverse of [`market_value`]: how many minor units of a token with `decimals`
/// are worth `value_scaled` quote-currency value at `price_scaled`.
pub fn value_to_amount(
value_scaled: u128,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Reserve {

pub liquidity_decimals: u8,

/// Base units sitting in `liquidity_vault`, available to borrow or redeem.
/// Minor units sitting in `liquidity_vault`, available to borrow or redeem.
/// The program reads the pool size from this field, not the vault's token
/// balance, so a raw token donation cannot move the exchange rate.
pub available_liquidity: u64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn sole_supplier_leaves_the_minimum_behind() {
/// straight to the vault move nothing. But `total_liquidity` also counts
/// interest owed on borrows, and a supplier can borrow from their own reserve.
///
/// The attacker opens the reserve holding a single share, borrows one base
/// The attacker opens the reserve holding a single share, borrows one minor
/// unit of it against collateral in another reserve, and lets one second pass.
/// Debt is rounded up, so the one unit now reads as two and the lone share is
/// worth two units without having minted anything. From there, deposits and
Expand Down Expand Up @@ -155,7 +155,7 @@ fn inflating_shares_through_own_borrow_does_not_pay() {
}
assert_eq!(env.token_balance(attacker_share), 1);

// Borrow one base unit against collateral in another reserve, and let a
// Borrow one minor unit against collateral in another reserve, and let a
// second of interest round that debt up to two.
env.fund(&attacker, collateral.mint, 1_000_000_000);
let collateral_share = env.supply(&attacker, &collateral, 1_000_000_000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn unhealthy_obligation_liquidated_with_bonus_capped_by_close_factor() {
)
.unwrap();

// Exactly $350 (350M base units) was repaid — close-factor cap, not the full offer.
// Exactly $350 (350M minor units) was repaid — close-factor cap, not the full offer.
assert_eq!(
env.token_balance(liquidator_repay_account),
1_000_000_000 - 350_000_000
Expand Down
4 changes: 2 additions & 2 deletions finance/lending/quasar/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn price_mantissa_to_scaled(mantissa: u128, exponent: i32) -> Result<u128, P
}
}

/// Quote-currency value (FIXED_POINT_SCALE-scaled) of `amount` base units of a
/// Quote-currency value (FIXED_POINT_SCALE-scaled) of `amount` minor units of a
/// token with `decimals`, given `price_scaled`.
pub fn market_value(
amount: u64,
Expand All @@ -86,7 +86,7 @@ pub fn market_value(
)
}

/// Inverse of [`market_value`]: base units of a token worth `value_scaled`.
/// Inverse of [`market_value`]: minor units of a token worth `value_scaled`.
pub fn value_to_amount(
value_scaled: u128,
decimals: u8,
Expand Down
4 changes: 2 additions & 2 deletions finance/lending/quasar/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2414,7 +2414,7 @@ mod clock_warp {
/// interest owed on borrows, and a supplier can borrow from their own
/// reserve.
///
/// The attacker opens the reserve holding a single share, borrows one base
/// The attacker opens the reserve holding a single share, borrows one minor
/// unit of it against collateral in another reserve, and lets one second
/// pass. Debt is rounded up, so the one unit now reads as two and the lone
/// share is worth two units without having minted anything. From there,
Expand Down Expand Up @@ -2454,7 +2454,7 @@ mod clock_warp {
.assert_success();
assert_eq!(world.tokens(ATTACKER_BORROW_SHARE), 2);

// Borrow one base unit against collateral in another reserve, and let a
// Borrow one minor unit against collateral in another reserve, and let a
// second of interest round that debt up to two.
world
.deposit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anchor_lang::prelude::*;
#[derive(InitSpace)]
pub struct AssetRate {
pub mint: Pubkey,
/// USDC base units per whole token, e.g. 250_000_000 means 1.0 TSLAx = $250
/// USDC minor units per whole token, e.g. 250_000_000 means 1.0 TSLAx = $250
/// with six-decimal USDC. The swaps scale by the asset mint's decimals, so the
/// rate means the same thing whatever precision the asset has.
pub usdc_per_token: u64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anchor_lang::prelude::*;
#[derive(InitSpace)]
pub struct AssetRate {
pub mint: Address,
/// USDC base units per whole token, e.g. 250_000_000 means 1.0 TSLAx = $250
/// USDC minor units per whole token, e.g. 250_000_000 means 1.0 TSLAx = $250
/// with six-decimal USDC. The swaps scale by the asset mint's decimals, so the
/// rate means the same thing whatever precision the asset has.
pub usdc_per_token: u64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct RouterConfig {
#[seeds(b"rate", mint: Address)]
pub struct AssetRate {
pub mint: Address,
/// USDC base units per whole token, e.g. 250_000_000 means 1.0 TSLAx = $250
/// USDC minor units per whole token, e.g. 250_000_000 means 1.0 TSLAx = $250
/// with six-decimal USDC. The swaps scale by the asset mint's decimals, so the
/// rate means the same thing whatever precision the asset has.
pub usdc_per_token: u64,
Expand Down
8 changes: 4 additions & 4 deletions finance/order-book/anchor-v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1263,10 +1263,10 @@ program earned nothing (no fill means no fee).
## 5. Full-lifecycle worked examples

Three scenarios with end-to-end numbers. Both mints are 6-decimal SPL
tokens. 1 BASE = 1 000 000 base units; 1 QUOTE = 1 000 000 quote
units. Where a number in the narrative looks like "price 900", read
that as "900 quote units per 1 base unit" (so for a 1-full-BASE trade
you'd move 900 * 1 000 000 quote units).
tokens. 1 BASE = 1 000 000 BASE minor units; 1 QUOTE = 1 000 000 QUOTE
minor units. Where a number in the narrative looks like "price 900", read
that as "900 QUOTE minor units per 1 BASE minor unit" (so for a
1-full-BASE trade you'd move 900 * 1 000 000 QUOTE minor units).

Market configuration:
- `fee_basis_points = 50` (0.5%)
Expand Down
8 changes: 4 additions & 4 deletions finance/order-book/anchor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1271,10 +1271,10 @@ program earned nothing (no fill means no fee).
## 5. Full-lifecycle worked examples

Three scenarios with end-to-end numbers. Both mints are 6-decimal SPL
tokens. 1 BASE = 1 000 000 base units; 1 QUOTE = 1 000 000 quote
units. Where a number in the narrative looks like "price 900", read
that as "900 quote units per 1 base unit" (so for a 1-full-BASE trade
you'd move 900 * 1 000 000 quote units).
tokens. 1 BASE = 1 000 000 BASE minor units; 1 QUOTE = 1 000 000 QUOTE
minor units. Where a number in the narrative looks like "price 900", read
that as "900 QUOTE minor units per 1 BASE minor unit" (so for a
1-full-BASE trade you'd move 900 * 1 000 000 QUOTE minor units).

Market configuration:
- `fee_basis_points = 50` (0.5%)
Expand Down
20 changes: 10 additions & 10 deletions finance/perpetual-futures/anchor-v1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,45 @@
Liquidity-provider shares are priced against the provider.
`traders_unrealized_pnl` floored both sides' marked value, so the long side
rounded traders' profit down and the short side rounded it up, and
`add_liquidity` or `remove_liquidity` could round a base unit in the
`add_liquidity` or `remove_liquidity` could round a minor unit in the
provider's favour depending on the book. It now takes a `Rounding`, and
`liquidity_provider_aum` takes the direction of the valuation:
`add_liquidity` values the pool rounding up, so a deposit is minted no more
shares than it pays for, and `remove_liquidity` rounds it down, so a
withdrawal is paid no more than its shares are worth. `haircut_ratio` rounds
the traders' liability up, so a fraction of a base unit can only lower `h`.
the traders' liability up, so a fraction of a minor unit can only lower `h`.
Tested by `test_add_liquidity_values_the_pool_rounding_up` (a 100,000 USDC
deposit against an open short marked half a base unit in profit mints
deposit against an open short marked half a minor unit in profit mints
100,000,000,000 shares, not 100,000,000,001) and
`test_remove_liquidity_values_the_pool_rounding_down` (a provider
withdrawing against an open long marked half a base unit in profit is paid
withdrawing against an open long marked half a minor unit in profit is paid
99,999,998,999, not 99,999,999,000). No existing figure changes.

Profit/loss and funding round against the trader. `position_pnl` and
`position_funding` in `instructions/shared.rs` divided with truncation toward
zero, so a fractional loss was booked a base unit small, and funding a trader
owed was charged a base unit short. `position_pnl` now floors toward
zero, so a fractional loss was booked a minor unit small, and funding a trader
owed was charged a minor unit short. `position_pnl` now floors toward
negative infinity, and `position_funding` applies the side's sign first and
then rounds toward positive infinity, so funding the trader pays rounds up
and funding the trader receives rounds down. The walkthrough's figures are exact and unchanged.
Tested by `test_position_pnl_rounds_against_the_trader` and
`test_position_funding_rounds_against_the_trader`.
`test_fees_and_maintenance_requirement_round_up` now liquidates at
$85.10000005 instead of $85.10000004: the old price's loss of 744,999,998.15
base units floors to 744,999,999, and the new one's 744,999,997.65 floors to
minor units floors to 744,999,999, and the new one's 744,999,997.65 floors to
744,999,998, so the position is still liquidated at an equity of exactly
250,000,001.

Every fee rounds up. `basis_points_of` in `instructions/shared.rs` rounds its
result up to the next base unit, so the open, close and liquidation fees and
result up to the next minor unit, so the open, close and liquidation fees and
the maintenance requirement a position is liquidated at each round in the
pool's favour: a fee is never a minor unit short, and a position is never a
minor unit too healthy to liquidate. The insurance fund's cut of a fee the
pool already holds is split by the new `basis_points_of_rounded_down`, and the
program takes the remainder, so the two still add up to the whole fee. The
walkthrough's figures are exact multiples and are unchanged. Tested by
`test_fees_and_maintenance_requirement_round_up`, which opens, closes and
liquidates a position one base unit over $5,000 and checks the 5,000,001-unit
liquidates a position one minor unit over $5,000 and checks the 5,000,001-unit
fees, the 2,500,000 / 2,500,001 insurance and program split, the liquidation
at an equity of exactly 250,000,001 and the 50,000,001 liquidation fee, and by
`test_basis_points_of_rounds_up_and_the_insurance_split_rounds_down` at the
Expand Down Expand Up @@ -78,7 +78,7 @@ owner; `test_initialize_pool` checks the recorded program.
`test_funding_charged_to_long` asserts the exact funding paid, `size * rate *
seconds / FUNDING_PRECISION`, rather than that some was paid, and
`test_first_deposit_below_minimum_fails` asserts `DepositTooSmall` by code one
base unit under the withheld minimum and a single share minted one over it.
minor unit under the withheld minimum and a single share minted one over it.

## 2026-10-01

Expand Down
Loading
Loading