The repository contains a single Rust smart contract that mints an
over‑collateralised NEP‑141 stablecoin (nUSD). The contract combines a trove
manager, a stability pool, and the nUSD token itself. This document explains
how the contract behaves from every participant’s perspective, which methods
they call, which assets they provide or receive, and which rewards and risks are
associated with each role.
- Collateral registry – the owner registers NEP‑141 tokens together with a minimum collateral ratio (MCR), recovery ratio, debt ceiling, liquidation penalty, and oracle id. Only registered collateral can be deposited.
- Troves (vaults) – each
(borrower, collateral_id)pair has a trove that tracks deposited collateral, outstanding debt, and the last update timestamp. - Price feeds – a designated oracle account calls
submit_priceto push the latest USD price per collateral. All risk checks depend on this feed. - Borrowing / redemption – borrowers lock collateral through
ft_transfer_call, mintnUSDwithborrow, and can reduce debt withrepayorredeem(burningnUSDagainst another trove’s collateral). - Stability pool –
nUSDholders can deposit their tokens. When an unsafe trove is liquidated, the pool’snUSDis burnt to cancel the debt, and the pool depositors receive the collateral (minus a penalty that goes to the protocol owner). The pool tracks per-share rewards so depositors earn only the liquidation events that happen while they are staked. - Owner utilities – the owner can trigger swaps through a NEAR Intents
router (
trigger_swap_via_intents) to rebalance reserves or route treasury assets.
- How they interact
- Call
storage_depositonce to register their account with thenUSDfungible token. - Transfer NEP‑141 collateral via
ft_transfer_callwith{"action":"deposit_collateral"}to increase their trove balance. - Mint
nUSDwithborrow(collateral_id, amount)as long as the trove’s collateral ratio stays above the configured MCR. - Reduce debt using
repay(burning theirnUSD) orredeemagainst another trove’s collateral when they want to arbitrage the peg. - Withdraw surplus collateral with
withdraw_collateralor close the trove entirely withclose_troveafter repaying all debt.
- Call
- What they provide / receive
- Provide volatile collateral tokens.
- Receive freshly minted
nUSDthat can be sold, swapped, or deposited into the stability pool.
- Rewards
- Cheap leverage when the collateral price appreciates.
- The ability to capture redemption arbitrage by burning
nUSDagainst their own trove.
- Risks
- If the collateral price drops and their ratio falls below MCR, anyone can liquidate their trove. The entire collateral (minus the liquidation penalty) is redistributed to stability pool depositors.
- They must trust the oracle feed; stale or incorrect prices can still trigger a liquidation.
- How they interact
- Call
deposit_to_stability_pool(amount)to movenUSDinto the pool. - Optionally withdraw partially or fully using
withdraw_from_stability_pool; shares are converted back tonUSDusing the pool’s share accounting. - Claim accrued collateral rewards with
claim_collateral_rewardand receive real NEP‑141 tokens.
- Call
- What they provide / receive
- Provide
nUSDliquidity that stands ready to cancel bad debt during liquidations. - Receive collateral from liquidated troves pro‑rata. All rewards are tracked via reward-per-share accumulators so new depositors cannot steal past rewards.
- Provide
- Rewards
- Instant access to discounted collateral whenever a trove is liquidated.
- Compounding yield if the seized collateral appreciates.
- Risks
- Deposited
nUSDcannot be withdrawn during the liquidation transaction; if the pool covers a large liquidation, thenUSDbalance shrinks immediately. - Rewards depend entirely on liquidation volume; in calm markets, deposits may sit idle.
- Deposited
- How they interact
- Monitor troves and call
liquidate(collateral_id, owners[])on any trove whose collateral ratio is below MCR. - Optionally call
redeemto burnnUSDagainst the weakest troves whennUSDtrades below the peg.
- Monitor troves and call
- What they provide / receive
- Provide orchestration: they spend gas to keep the system solvent.
- Receive no direct payout for the liquidation call itself (rewards go to the
stability pool), but can arbitrage by buying discounted collateral or by
acquiring
nUSDcheaply and redeeming it.
- Rewards
- Access to system-wide arbitrage opportunities.
- Risks
- Need to front gas and handle asynchronous NEAR execution; if the oracle price moves during the transaction, the call may fail.
- How they interact
- The address configured as
pyth_oracle_idcallssubmit_priceto push fresh prices for each collateral. Every state-changing method that touches troves consults the cached price.
- The address configured as
- What they provide / receive
- Provide timely, accurate price data (no direct in-contract reward).
- Receive governance trust or off-chain compensation.
- Risks
- An incorrect price can liquidate healthy troves or block borrowing. The operator must maintain infrastructure and SLAs.
- How they interact
- Registers collateral through
register_collateraland manages the list of trusted oracles and the NEAR Intents router. - Can trigger swaps via
trigger_swap_via_intentsto recycle treasury assets or fund future rewards.
- Registers collateral through
- What they provide / receive
- Provide stewardship and upgrades (initially through an owner account, later ideally through a DAO).
- Receive the liquidation penalty portion that is not distributed to the pool (recorded as pending collateral rewards for the owner account).
- Risks
- Misconfiguration (too low MCR or too high debt ceiling) can render the system unsafe.
- Owning the admin key is a security liability; it should migrate to a DAO once the system is hardened.
- Deposit & Borrow
- User calls
ft_transfer_callon a collateral token → contract credits the trove. - User borrows
nUSD→ trove debt increases,nUSDis minted to the borrower. - The
nUSDbalance can be spent, swapped, or deposited into the stability pool.
- User calls
- Repay / Close
- Borrower transfers
nUSDback (either viarepayorft_transfer_callwith a repay action). - Debt decreases; once it reaches zero, borrower can withdraw all collateral and delete the trove.
- Borrower transfers
- Liquidation
- Anyone calls
liquidatewith a list of unsafe troves. - The contract burns
nUSDfrom the stability pool, cancels the debt, and redistributes the collateral minus the penalty. - Pool depositors can claim the collateral immediately; the penalty portion accrues to the owner.
- Anyone calls
- Redemption
- A user burns
nUSDviaredeem, targeting a specific trove. - Debt decreases and collateral is queued as a reward for the redeemer.
- A user burns
- Oracle Update
- The designated oracle account periodically calls
submit_price; borrowing and withdrawals always read the cached price to enforce safety guarantees.
- The designated oracle account periodically calls
- Storage staking – every participant (borrower, pool depositor, owner) must
attach enough NEAR when calling
storage_deposit; collateral transfers also need the underlying FT contract to have storage for both the sender and this contract. - Gas – external calls (
ft_transfer,trigger_swap_via_intents) specify static gas budgets; integration tests rely onmax_gas()to avoid “Exceeded prepaid gas” errors. - Security – the contract has no upgrade hooks inside the business logic, so safe parameter choices and a trustworthy owner/oracle are essential.
- Extensibility – the module split (
types.rs,views.rs,internal.rs) keeps pure view methods isolated from state mutations, making auditing easier and enabling future components (e.g., multiple stability pools) to plug in.
With these mechanics, the contract delivers a fully on-chain borrowing and
liquidation process on NEAR. Borrowers gain capital efficiency, stability pool
contributors earn collateral rewards, and arbitrageurs keep nUSD near its peg
—all orchestrated by the owner-configured parameters and the oracle feed.