Skip to main content

Lending Module

The Lending module enables collateralized borrowing and lending with algorithmic interest rates.

Overview

┌─────────────────────────────────────────────────────────────┐
│ Lending Pool (USDC) │
│ │
│ Suppliers Borrowers │
│ ┌──────────┐ ┌──────────┐ │
│ │ Supply │────Interest───────▶│ Borrow │ │
│ │ USDC │◀───Earnings────────│ USDC │ │
│ └──────────┘ └──────────┘ │
│ │ │
│ ▼ │
│ Collateral Required │
│ (wBTC, wETH, etc.) │
│ │
│ Pool Stats: │
│ Total Supplied: $10,000,000 │
│ Total Borrowed: $6,000,000 │
│ Utilization: 60% │
│ Supply APY: 3.2% │
│ Borrow APY: 5.4% │
└─────────────────────────────────────────────────────────────┘

Supplying Assets

Supply to Pool

rp1d tx lending supply 1000000uusdc \
--from my-wallet

Enable as Collateral

rp1d tx lending enable-collateral uusdc \
--from my-wallet

Withdraw Supply

rp1d tx lending withdraw 500000uusdc \
--from my-wallet

Borrowing Assets

Borrow from Pool

rp1d tx lending borrow 500000uusdc \
--from my-wallet

Repay Loan

rp1d tx lending repay 500000uusdc \
--from my-wallet

TypeScript SDK

import { LendingModule } from '@rp1/sdk';

const lending = new LendingModule(client);

// Supply assets
await lending.supply(wallet, '1000000', 'uusdc');

// Enable as collateral
await lending.enableCollateral(wallet, 'uusdc');

// Borrow
await lending.borrow(wallet, '500000', 'uusdc');

// Check position
const position = await lending.getPosition(wallet.address, 'uusdc');
console.log(`Supplied: ${position.suppliedAmount}`);
console.log(`Borrowed: ${position.borrowedAmount}`);

// Check health factor
const health = await lending.getHealthFactor(wallet.address);
console.log(`Health Factor: ${health}`);

Interest Rate Model

Interest rates adjust based on pool utilization:

Utilization Rate = Total Borrowed / Total Supplied

If Utilization < Optimal (80%):
Borrow Rate = Base Rate + (Utilization / Optimal) × Slope1

If Utilization >= Optimal:
Borrow Rate = Base Rate + Slope1 + ((Utilization - Optimal) / (1 - Optimal)) × Slope2

Current Parameters

AssetBase RateSlope1Slope2Optimal
USDC2%4%75%80%
wBTC1%3%100%65%
wETH1%3%100%65%

Rate Examples

UtilizationUSDC Borrow APY
20%3.0%
50%4.5%
80%6.0%
90%43.5%

Collateral Factors

AssetCollateral FactorLiquidation Threshold
USDC85%90%
wBTC75%80%
wETH80%85%
wSOL70%75%

Max Borrow Calculation

Max Borrow = Σ (Collateral Amount × Price × Collateral Factor)

Health Factor

Health Factor = (Collateral Value × Liquidation Threshold) / Total Borrow Value
  • > 1.0: Safe
  • < 1.0: Liquidatable

Liquidation

When Health Factor < 1:

# Liquidate undercollateralized position
rp1d tx lending liquidate \
--borrower rp1underwater... \
--repay-denom uusdc \
--collateral-denom uwbtc \
--amount 100000uusdc \
--from liquidator-wallet

Liquidation Incentive

AssetLiquidation Bonus
USDC5%
wBTC8%
wETH7%

Liquidators repay debt and receive collateral at a discount.

Query Commands

Pool Info

rp1d query lending pool uusdc

User Position

rp1d query lending position \
--owner $(rp1d keys show my-wallet -a) \
--denom uusdc

Health Factor

rp1d query lending health-factor $(rp1d keys show my-wallet -a)

All Pools

rp1d query lending pools

Message Types

MsgSupply

message MsgSupply {
string supplier = 1;
cosmos.base.v1beta1.Coin amount = 2;
}

MsgBorrow

message MsgBorrow {
string borrower = 1;
cosmos.base.v1beta1.Coin amount = 2;
}

MsgLiquidate

message MsgLiquidate {
string liquidator = 1;
string borrower = 2;
string repay_denom = 3;
string repay_amount = 4;
string collateral_denom = 5;
}

Parameters

ParameterDefaultDescription
close_factor50%Max debt repayable in liquidation
liquidation_fee5%Fee to liquidators
reserve_factor10%Protocol fee on interest

Events

EventTypeSupply = "lending_supply"
EventTypeBorrow = "lending_borrow"
EventTypeRepay = "lending_repay"
EventTypeLiquidation = "lending_liquidation"

Risk Management

  1. Monitor health factor: Keep above 1.5 for safety
  2. Diversify collateral: Don't rely on single asset
  3. Watch rates: High utilization means high borrow rates
  4. Set alerts: Monitor liquidation risk

Next Steps