User Position
TheUserPosition account tracks a user’s collateral and debt for a specific pair.
Account Structure
Fields
The owner of this position.
The pair this position belongs to.
Applied minimum collateral factor (in BPS) for borrowing token1 using token0 as collateral.
Applied minimum collateral factor (in BPS) for borrowing token0 using token1 as collateral.
Amount of token0 deposited as collateral.
Amount of token1 deposited as collateral.
Debt shares for token0. Used to calculate actual debt including interest. Stored as
u128 for precision.Debt shares for token1. Used to calculate actual debt including interest. Stored as
u128 for precision.PDA bump seed.
PDA Derivation
The user position account is a PDA derived from:POSITION_SEED_PREFIX = b"gamm_position".
Debt Shares Mechanism
Debt is tracked using a share-based system that allows interest to accrue globally without updating each user’s position individually.How It Works
Instead of storing absolute debt amounts, users hold debt shares that represent their proportion of the pool’s total debt. As interest accrues,total_debt increases but shares remain constant, so each share represents progressively more debt.
Initial Scaling
The first borrow creates shares with a scaling factor for precision:Subsequent Borrows
New shares are calculated proportionally:Debt Calculation
User’s actual debt is calculated from their shares:Rounding Rules (Protocol-Favorable)
| Operation | Rounding | Effect |
|---|---|---|
| Borrowing | Round UP shares | User gets slightly more debt |
| Debt calculation | Round UP | User owes slightly more |
| Repayment | Round DOWN shares burned | Protocol retains dust |
Key Methods
calculate_debt0(total_debt0: u64, total_debt0_shares: u128) -> Result<u64>
Calculates the actual debt0 amount (including accrued interest) from debt shares using ceiling division.
calculate_debt1(total_debt1: u64, total_debt1_shares: u128) -> Result<u64>
Calculates the actual debt1 amount (including accrued interest) from debt shares using ceiling division.
get_remaining_borrow_limit(pair: &Pair, debt_token: &Pubkey, applied_min_cf_bps: u16) -> Result<u64>
Calculates the remaining borrow limit for a given debt token.
get_debt_utilization_bps(pair: &Pair, debt_token: &Pubkey) -> Result<u64>
Returns the debt utilization in basis points (debt / borrow power). Values > 10000 indicate undercollateralization.
get_liquidation_price(pair: &Pair, debt_token: &Pubkey) -> Result<u64>
Returns the NAD-scaled liquidation price of the collateral. Returns u64::MAX if position is immediately unsafe, or 0 if there’s no debt.