Comment on page
📰

Price feed

Price feeds are a critical component of decentralized lending protocols, enabling the system to function without reliance on a centralized entity. These feeds serve multiple essential functions, including:
  1. 1.
    Borrowing, by ensuring that loans are adequately collateralized.
  2. 2.
    Liquidating, by confirming that a loan is under-collateralized and that the borrower is compensated fairly.
  3. 3.
    Withdrawing delegation rewards, particularly when the assets in the lending pool are not in ADA.

Price Visibility

Prices on Lenfi protocol are consumed differently. Lenfi UI is able to consume prices directly from on-chain contracts. While protocol relies on UTXO locked in a defined contract. While using UI you get the current price, there could be a price mismatch between UI and what the protocol sees.

Price feed mechanics

// Price Feed Data
pub type oracleDatum {
oracle_parameters: OracleParameters,
token_a_amount: Int, // Calculated token amount in a DEX
token_b_amount: Int, // Calculated ADA amount in a DEX
expiration_time: Int, // Time when data can't be used by protocol
maturity_time: Int, // Time from when data can be updated
}
// Non-changing price feed parameters
pub type OracleParameters {
pool_nft_cs: AssetClass,
oracle_nft_cs: AssetClass,
token_a_cs: AssetClass,
token_b_cs: AssetClass,
}
Every data update performs the following actions:
  • Updates token_a_amount and token_b_amount with new averages.
  • Sets a new expiration date (e.g., +45 minutes).
  • Sets a new maturity date (e.g., +15 minutes).
Important validator points are:
  • The oracle can only be updated if a transaction is created after the maturity date.
  • Both the expiration and maturity extension times are hardcoded in the protocol (+30 min; +10 min).
  • New token amounts are averages of old oracle data and current DEX values.
New Oracle data is the average of old Oracle price and current DEX value

Price feed usage

The example below illustrates how the price feed is used in the Lenfi protocol. Assume that the price feed has data of...
//LENFI/ADA pair priceFeed data
{
...
tokenAmount: 1,000,000 //InputReserve (LENFI)
adaAmount: 7,000,000 //OutputReserve (ADA)
}
Where the spot token price could be seen as 7 ADA/LENFI. However, if someone tries to sell 100,000 LENFI tokens they would receive:
The below formula is used to find out collateral value in case it must be sold
// Function to find tokenSale value
saleValue = (inputAmount * outputReserve) / (inputReserve + inputAmount * 0.997)
saleValue = (100000 * 7000000) / (1000000 + 100000 * 0.997)
saleValue = 636,537 ADA
In case someone tries to buy 100,000 LENFI tokens they would have to pay:
The below formula is used to find out debt value in case it must be purchased
// Function to find tokenPurchase price
purchaseValue = (-outputAmount * inputReserve) / (0.997 * outputAmount - outputReserve)
purchaseValue = (-100000 * 7000000) / (0.997 * 100000 - 1000000)
purchaseValue = 777,518 ADA
The above example tells us that:
  • If you sell 100,000 LENFI you would receive 636,537 ADA
  • If you buy 100,000 LENFI you need to pay 777,518 ADA