ERC-8465 Credit-matched liquidity lending position (Credit-Matched Liquidity)
Draft · Vaults & DeFi · created 2026-10-20
A standard where a holder adds borrowed quote tokens to the base tokens they hold to open a concentrated liquidity position, and repays the loan first from that pool's fees. The position token (NFT) is held in escrow by the contract until repayment.
Why it is needed
Even someone holding an asset with a stable price, like a tokenized stock, cannot earn pool fees in a liquidity pool if no one supplies the opposite (quote) token. Several services already offer this kind of lending, each with its own different contracts and events, so a wallet that wants to display a holder's position, a lender that wants to serve multiple holders, or anyone trying to reconstruct a credit record has to integrate with each one separately. On top of that, the same three failures keep recurring. If a position is valued at the pool's own price, it can be drained by a single-transaction price manipulation (flash loan); if a tokenized stock's transfer registry blocks a particular address, a position stuck at that address becomes a loss for both sides; and because the price stops while the stock market is closed and jumps sharply on reopening, even a price from a few hours ago may in fact be a value that cannot actually be traded at.
What to implement
function decimals() external view returns (uint8);: Returns the number of decimal places the price feed uses.function latestRoundData() external view returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);: Returns the price feed's latest round data (including the price answer and the update time updatedAt).function tradingStatus() external view returns (TradingStatus status, uint64 since);: Returns the trading status of the market where the base token trades (normal trading, closed, halted, permanently suspended, etc.) and the time that status began.event PositionOpened(uint256 indexed positionId, address indexed holder, uint256 indexed agentId, address pool, uint256 baseIn, uint256 quoteLent);: Emitted when a position is opened, recording the holder, identity, pool, and the amounts of base token deposited and quote token borrowed.event FeesApplied(uint256 indexed positionId, uint256 toLender, uint256 toHolder);: Records the amount of quote token that went to the lender and to the holder respectively when fees are settled.event PositionClosed(uint256 indexed positionId, uint256 repaid);: Records the amount repaid when the holder pays off the debt and closes the position.event PositionUnwound(uint256 indexed positionId, uint256 recovered, uint256 shortfall);: Records the amount recovered and the shortfall when a position is forcibly liquidated based on the price feed.event PositionHandedOver(uint256 indexed positionId, uint256 recovered, uint256 outstanding);: Records the quote amount recovered and the remaining receivable when a position is handed over as-is because the price feed has been stale for too long.function quoteToken() external view returns (address);: Returns the address of the quote token (ERC-20) used for lending and repayment.function identityRegistry() external view returns (address);: Returns the address of the ERC-721 registry representing the identity that agentId points to.function lenderFallback() external view returns (address);: Returns the fallback address at which the lender receives the base token or position token.function oracleOf(address pool) external view returns (address priceFeed);: Returns the price feed address to be used for new positions opened on that pool.function quoteLimit(address pool, uint256 baseIn) external view returns (uint256);: Returns the maximum amount of quote token that can be borrowed right now against the base token amount baseIn.function open(address pool, uint256 baseIn, uint256 quoteToBorrow, int24 tickLower, int24 tickUpper, uint256 agentId) external returns (uint256 positionId);: Deposits the base token and borrows the quote token to open a liquidity position in the specified tick range.function position(uint256 positionId) external view returns (Position memory);: Returns the position's full state (holder, pool, debt, status values, etc.).function health(uint256 positionId) external view returns (uint256 valueInQuote, uint256 owed, bool unwindable);: Returns the position's current value, remaining debt, and whether it is eligible for liquidation.function applyFees(uint256 positionId) external returns (uint256 toLender, uint256 toHolder);: Collects the quote fees accumulated in the position, paying the lender first and the remainder to the holder.function close(uint256 positionId) external;: The holder repays the full debt and gets the position token back.function unwind(uint256 positionId) external;: Can be called by anyone; if the liquidation condition is met, it settles the position based on the price feed, or hands it over as-is if the price feed is stale.
In depth
Price comes only from the external feed; the pool's own price is never used
When calculating the amounts that quoteLimit, health, and unwind pay out, the contract never uses the pool's own price, such as the pool's current tick, square-root price, or reserves. Instead it uses only the answer from the price feed (IPriceFeed) designated for each pool. For a price to be usable, answer must be greater than 0 and updatedAt must be no later than the block timestamp and no older than maxPriceAge; if the base token has a status source, it must be in Trading status and updatedAt must be after that status's since. Additionally, the feed's decimals() must be 18 or less, and the price must be greater than 0 and not exceed the maximum allowed price set by the contract. The pool price is used only to determine the ratio in which the two tokens are deposited at mint time and the ratio in which they are withdrawn at unwind time.
The formula for calculating position value
At usable price P, position value is computed as quote + base * P / 10**18 (rounded down). Here base is the amount of base that liquidity holds at P plus the base share already accrued to the position token, and quote is summed the same way. Base is summed first and priced only once, and fees that the position manager has not yet credited to the position token are excluded from the value. P itself is answer * 10**(18+quoteDecimals) / 10**(feedDecimals+baseDecimals), representing the value per 10**18 base units of the quote token, rounded down.
Opening: Collateral Limits and Price Gap Buffer
quoteLimit(pool, baseIn) returns the smaller of baseIn valued at the usable price and the quote the lender can currently lend, and returns 0 if the pool is unsupported or the price is unavailable (it does not revert). open() reverts if both baseIn and quoteToBorrow are 0, if quoteToBorrow exceeds quoteLimit, or if agentId is set but the caller is not the owner of that identity. It takes in the base, adds liquidity to the specified tickLower to tickUpper, and returns any remaining base. Notably, a new position will not open if it is already liquidatable at the usable price lowered by gapBps. This buffer accounts for cases where a session-based asset's next opening price gaps significantly below the prior session's closing price. Whatever value the lender sets for handoverDelay is enforced to be at least 14 days (1,209,600 seconds).
Escrow and Fixed Terms
While a position is in the Open state, the contract holds the position token and does not withdraw liquidity, collect fees, or transfer tokens except as specified by applyFees, close, and unwind. priceFeed, statusSource, maxPriceAge, unwindBps, gapBps, dueAt, and handoverDelay never change after opening, and owed never increases after opening. In other words, the lender can only liquidate or take over the position under the terms that held when it was healthy, and cannot later change those terms to its own advantage.
Health and Liquidatability Determination
For an Open position, health() reverts if the price is unavailable, and otherwise returns valueInQuote, owed, and unwindable. unwindable is true if owed is nonzero and either dueAt is set and the block timestamp is at or past it, or valueInQuote * 10000 < owed * unwindBps. This comparison multiplies out both sides without division, producing an exact boundary with no rounding error. A position with owed equal to 0 is never subject to liquidation even past maturity, and can only be ended by the holder's close().
Fee Handling: Quote Goes to the Lender First, Base Stays in the Position
applyFees() can be called by anyone and only operates on an Open position. It collects the position's quote fees, repays the lender with the smaller of that fee amount and owed, reduces owed by that amount, and pays any remaining quote to the holder. Base token fees are not paid out to anyone and remain inside the position, to be received by the holder along with the position token at close() or included in the final settlement at unwind(). applyFees() never reads the price.
Closing and Unwinding
close() can only be called by the holder and only succeeds if owed is received in full in quote; it never reads the price and returns the position token as is without moving any base token. unwind() can be called by anyone and only operates on an Open position with nonzero owed. If the price is available and health reports unwindable, it withdraws all liquidity and pays the lender quote first; if that falls short, it pays the shortfall directly to the lenderFallback() address in base, converted at price P and rounded up (with the position manager directing payment straight to that address), and leaves the remaining share in the position token, which passes to the holder. If the base token refuses the transfer, this does not fail; in that case the position token itself (along with the remaining base) is sent to lenderFallback().
When the Price Feed Breaks, Hand-over Transfers Without a Price
If the price remains unavailable past maturity, a hand-over is triggered when TERMINAL_GRACE (1 day) has passed since the status source reported Terminal, or when dueAt + handoverDelay has passed and the feed has given no price for that entire handoverDelay, or when Halted has lasted longer than handoverDelay. In this case, unwind() collects only quote, pays the lender up to owed, and if that quote covers owed in full, passes the position token to the holder; otherwise the whole position token goes to lenderFallback(). No base token is moved. An intra-session market closure (SessionBreak), no matter how long, never qualifies for hand-over, and no lender can set handoverDelay shorter than 14 days. This proposal is currently in Draft status, not yet finalized, and may change or be abandoned.
Things to watch out for
- Oracle Manipulation and Stale Prices: Every safeguard protecting the lender ultimately depends on the feed. If the market is thin, there is only a single reporter, or a party to the loan can control the feed, a healthy position can be forced into liquidation, or a loan can be drawn against collateral value that does not actually exist. Use a feed that aggregates multiple sources and has a heartbeat shorter than maxPriceAge, and holders should check the feed, status source, and handoverDelay before opening.
- Market Closures and Price Gaps: While the stock market is closed, there is no price, and the first price after reopening can gap away from the prior price by more than gapBps. In this case, the position's value may already be below its debt the moment it becomes liquidatable, and the lender bears that shortfall. Since there is no price to liquidate against during a closure, the gapBps and unwindBps settings are the only line of defense.
- The Risk of a Short Hand-over Delay: If handoverDelay is as short as a few hours, even a temporary outage the lender can control or manipulate can seize the entire position, including the holder's share, without any price. A delay shorter than the base token market's longest regular closure risks being mistaken for a broken price feed right before the market reopens, so the 14-day floor is a hard requirement, not a recommendation.
- Incorrect or Malicious Status Sources: The status source must be trusted as much as the feed. If a status source controlled by the lender falsely reports Terminal, every position can be handed over, including the holder's share, one day later, and if since is manipulated to a past timestamp, it can be handed over immediately. Holders should only open positions against an independent status source that the lender cannot control, such as the issuer's own status or an adapter to it.
- Tokens That Can Block Transfers: A tokenized stock's transfer registry can block specific addresses. The base token is designed to never pass through the contract itself during either liquidation or hand-over, but if the registry blocks the pool or the position manager itself, that base can never be withdrawn at all; liquidation then pays out quote only, transfers the entire position token to the lender's fallback address, and records the unpaid base as a shortfall.