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
| Asset | Base Rate | Slope1 | Slope2 | Optimal |
|---|---|---|---|---|
| USDC | 2% | 4% | 75% | 80% |
| wBTC | 1% | 3% | 100% | 65% |
| wETH | 1% | 3% | 100% | 65% |
Rate Examples
| Utilization | USDC Borrow APY |
|---|---|
| 20% | 3.0% |
| 50% | 4.5% |
| 80% | 6.0% |
| 90% | 43.5% |
Collateral Factors
| Asset | Collateral Factor | Liquidation Threshold |
|---|---|---|
| USDC | 85% | 90% |
| wBTC | 75% | 80% |
| wETH | 80% | 85% |
| wSOL | 70% | 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
| Asset | Liquidation Bonus |
|---|---|
| USDC | 5% |
| wBTC | 8% |
| wETH | 7% |
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
| Parameter | Default | Description |
|---|---|---|
close_factor | 50% | Max debt repayable in liquidation |
liquidation_fee | 5% | Fee to liquidators |
reserve_factor | 10% | Protocol fee on interest |
Events
EventTypeSupply = "lending_supply"
EventTypeBorrow = "lending_borrow"
EventTypeRepay = "lending_repay"
EventTypeLiquidation = "lending_liquidation"
Risk Management
- Monitor health factor: Keep above 1.5 for safety
- Diversify collateral: Don't rely on single asset
- Watch rates: High utilization means high borrow rates
- Set alerts: Monitor liquidation risk