Repay debt first; check collateral withdrawal separately
Wallet amount, gross debt reduction, assigned reserve and collateral release are four different quantities.
The three repayment amounts
repay(amount) takes an 18-decimal local wallet amount excluding reserve. The currency contract calculates freedAmount, which is the gross reduction in position.minted. The difference freedAmount − amount is the currently assigned reserve moved into the position and included in the burn. The return value is the gross debt reduction, not the wallet debit.
When reserve coverage is unimpaired, the normal calculation is freed = floor(amount × 1,000,000 / (1,000,000 − reserveContributionPPM)). If the reserve token balance is below tracked minter reserve, V2 first reduces the effective reserve ppm proportionally:
adjustedReservePPM = floor(reserveContributionPPM × currentReserveBalance
/ trackedMinterReserve) // only when impaired
freedAmount = floor(amount × 1,000,000 / (1,000,000 − adjustedReservePPM))This is why the initial reserve percentage is not a perpetual repayment ratio. Read the current reserve token balance, tracked minter reserve and position reserve contribution at the same block.
Full repayment needs a candidate and simulation
PositionRoller.findRepaymentAmount(position) performs a binary search using current reserve coverage and integer rounding. It can provide a full-repayment candidate; simply multiplying gross debt by one minus the original reserve ratio is not an exact general method. The candidate is state-dependent and must be checked with a simulation of the actual repayment. On the approved USD testnet runtime, the guided flow performs a fresh static call and gas estimate before explicit MetaMask confirmation; imported registries and custom RPC remain read-only.
If the calculated debt reduction exceeds minted, repayment reverts rather than silently clamping. At a 100% reserve contribution with unimpaired adjusted reserve, the freed-amount denominator is zero and the standard quote/candidate path reverts. Treat that edge as an explicit contract state—not zero debt, zero payment or a valid quote.
Debt-free is not closed
Reducing minted to zero does not set the position's closed flag. A debt-free position can potentially mint again if all other checks pass. Closure results from governance denial or from a collateral withdrawal that leaves less than the minimum collateral.
Withdrawal has separate restrictions
Collateral withdrawal requires the owner or recognized roller, no open challenge and now > cooldown. The post-withdrawal balance must still satisfy collateral backing. If the balance falls below minimumCollateral, the position is marked closed; exactly at the minimum does not close it. With positive debt, below-minimum collateral counts as zero for backing and therefore cannot pass. In practice, full withdrawal normally follows full repayment.
A closed position can still have collateral withdrawn subject to challenge, cooldown and backing checks. Closure blocks further minting but is not a blanket freeze on every function.
Repayment and withdrawal clocks differ
repay does not have the minting path's no-challenge, no-cooldown or before-expiration modifiers. Repayment can therefore remain possible during a challenge, during cooldown and after expiration, subject to token and state behavior. Do not block a repayment plan merely because withdrawal is unavailable; do not infer that repayment also unlocks collateral.
Management checklist
- Read gross
minted, reserve contribution, current reserve balances and candidate wallet amount at one block. - Read collateral balance, minimum, price,
challengedAmount, cooldown, expiration, closure and caller/roller identity separately. - Label failed or stale reads unknown and preserve the source block.
- Before any external execution, simulate the exact repay or withdraw call and reconcile the confirmed debt and balances afterward.
Continue with maturity and rolling or challenge restrictions.