Skip to content

Commit

Permalink
correct lendRate calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Jul 25, 2023
1 parent 2a90e09 commit b7b90b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/erc-20-pool-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
} from "./utils/constants"
import { addressToBytes, wadToDecimal } from "./utils/convert"
import { getTokenDecimals, getTokenName, getTokenSymbol, getTokenTotalSupply } from "./utils/token-erc20"
import { wmul } from "./utils/math"
import { getRatesAndFees } from "./utils/pool"
import { loadOrCreateFactory } from "./utils/pool-factory"

Expand Down Expand Up @@ -86,7 +85,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void {
pool.t0debt = ZERO_BD
pool.inflator = ONE_BD
pool.borrowRate = wadToDecimal(interestRateResults.value0)
pool.lendRate = wadToDecimal(wmul(interestRateResults.value0, ratesAndFees.lenderInterestMargin))
pool.lendRate = ZERO_BD
pool.borrowFeeRate = wadToDecimal(ratesAndFees.borrowFeeRate)
pool.depositFeeRate = wadToDecimal(ratesAndFees.depositFeeRate)
pool.pledgedCollateral = ZERO_BD
Expand Down
11 changes: 5 additions & 6 deletions src/erc-20-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ import { getBucketId, getBucketInfo, loadOrCreateBucket } from "./utils/bucket"
import { getLendId, loadOrCreateLend } from "./utils/lend"
import { getBorrowerInfo, getLoanId, loadOrCreateLoan } from "./utils/loan"
import { getLiquidationAuctionId, getAuctionInfoERC20Pool, loadOrCreateLiquidationAuction, updateLiquidationAuction, getAuctionStatus, loadOrCreateBucketTake } from "./utils/liquidation"
import { getBurnInfo, updatePool, addLiquidationToPool, addReserveAuctionToPool, getLenderInfo, getRatesAndFees } from "./utils/pool"
import { getBurnInfo, updatePool, addLiquidationToPool, addReserveAuctionToPool, getLenderInfo, getRatesAndFees, calculateLendRate } from "./utils/pool"
import { lpbValueInQuote } from "./utils/common"
import { loadOrCreateReserveAuction, reserveAuctionKickerReward } from "./utils/reserve-auction"
import { incrementTokenTxCount } from "./utils/token-erc20"
import { approveTransferors, loadOrCreateTransferors, revokeTransferors } from "./utils/lp-transferors"
import { loadOrCreateAllowances, increaseAllowances, decreaseAllowances, revokeAllowances } from "./utils/lp-allowances"
import { wmul } from "./utils/math"

export function handleAddCollateral(event: AddCollateralEvent): void {
const addCollateral = new AddCollateral(
Expand Down Expand Up @@ -991,14 +990,15 @@ export function handleResetInterestRate(event: ResetInterestRateEvent): void {
const poolAddress = addressToBytes(event.address)
const pool = Pool.load(poolAddress)!
const ratesAndFees = getRatesAndFees(poolAddress)
updatePool(pool)

resetInterestRate.pool = pool.id
resetInterestRate.oldBorrowRate = pool.borrowRate
resetInterestRate.oldLendRate = pool.lendRate
resetInterestRate.oldBorrowFeeRate = pool.borrowFeeRate
resetInterestRate.oldDepositFeeRate = pool.depositFeeRate
resetInterestRate.newBorrowRate = wadToDecimal(event.params.newRate)
resetInterestRate.newLendRate = wadToDecimal(wmul(event.params.newRate, ratesAndFees.lenderInterestMargin))
resetInterestRate.newLendRate = pool.lendRate
resetInterestRate.newBorrowFeeRate = wadToDecimal(ratesAndFees.borrowFeeRate)
resetInterestRate.newDepositFeeRate = wadToDecimal(ratesAndFees.depositFeeRate)

Expand All @@ -1007,7 +1007,6 @@ export function handleResetInterestRate(event: ResetInterestRateEvent): void {
resetInterestRate.transactionHash = event.transaction.hash

// update pool state
updatePool(pool)
pool.borrowRate = resetInterestRate.newBorrowRate
pool.lendRate = resetInterestRate.newLendRate
pool.borrowFeeRate = wadToDecimal(ratesAndFees.borrowFeeRate)
Expand Down Expand Up @@ -1236,14 +1235,15 @@ export function handleUpdateInterestRate(event: UpdateInterestRateEvent): void {
const poolAddress = addressToBytes(event.address)
const pool = Pool.load(poolAddress)!
const ratesAndFees = getRatesAndFees(poolAddress)
updatePool(pool)

updateInterestRate.pool = pool.id
updateInterestRate.oldBorrowRate = pool.borrowRate
updateInterestRate.oldLendRate = pool.lendRate
updateInterestRate.oldBorrowFeeRate = pool.borrowFeeRate
updateInterestRate.oldDepositFeeRate = pool.depositFeeRate
updateInterestRate.newBorrowRate = wadToDecimal(event.params.newRate)
updateInterestRate.newLendRate = wadToDecimal(wmul(event.params.newRate, ratesAndFees.lenderInterestMargin))
updateInterestRate.newLendRate = pool.lendRate
updateInterestRate.newBorrowFeeRate = wadToDecimal(ratesAndFees.borrowFeeRate)
updateInterestRate.newDepositFeeRate = wadToDecimal(ratesAndFees.depositFeeRate)

Expand All @@ -1252,7 +1252,6 @@ export function handleUpdateInterestRate(event: UpdateInterestRateEvent): void {
updateInterestRate.transactionHash = event.transaction.hash

// update pool state
updatePool(pool)
pool.borrowRate = updateInterestRate.newBorrowRate
pool.lendRate = updateInterestRate.newLendRate
pool.borrowFeeRate = wadToDecimal(ratesAndFees.borrowFeeRate)
Expand Down
14 changes: 10 additions & 4 deletions src/utils/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { LiquidationAuction, Pool, ReserveAuction, Token } from "../../generated
import { ERC20Pool } from '../../generated/templates/ERC20Pool/ERC20Pool'
import { PoolInfoUtils } from '../../generated/templates/ERC20Pool/PoolInfoUtils'

import { poolInfoUtilsAddressTable, ONE_BI, TEN_BI, ONE_WAD_BI } from "./constants"
import { poolInfoUtilsAddressTable, TEN_BI } from "./constants"
import { decimalToWad, wadToDecimal } from './convert'
import { getTokenBalance } from './token-erc20'
import { wdiv, wmul } from './math'
import { wmul, wdiv } from './math'

export function getPoolAddress(poolId: Bytes): Address {
return Address.fromBytes(poolId)
Expand Down Expand Up @@ -61,6 +61,10 @@ export function getRatesAndFees(poolId: Bytes): RatesAndFees {
return new RatesAndFees(lim, bfr, dfr);
}

export function calculateLendRate(borrowRate: BigInt, lenderInterestMargin: BigInt, utilization: BigInt): BigDecimal {
return wadToDecimal(wmul(wmul(borrowRate,lenderInterestMargin), utilization))
}

export class LoansInfo {
poolSize: BigInt
loansCount: BigInt
Expand Down Expand Up @@ -226,8 +230,10 @@ export function updatePool(pool: Pool): void {

// update lend rate and borrow fee, which change irrespective of borrow rate
const ratesAndFees = getRatesAndFees(poolAddress)
const borrowRate = decimalToWad(pool.borrowRate)
pool.lendRate = wadToDecimal(wmul(borrowRate, ratesAndFees.lenderInterestMargin))
pool.lendRate = calculateLendRate(
decimalToWad(pool.borrowRate),
ratesAndFees.lenderInterestMargin,
poolUtilizationInfo.actualUtilization)
pool.borrowFeeRate = wadToDecimal(ratesAndFees.borrowFeeRate)
}

Expand Down

0 comments on commit b7b90b8

Please sign in to comment.