Skip to content

Commit

Permalink
implemented Matt's formula for lendRate
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Aug 5, 2023
1 parent bf64ab8 commit 6067ba4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
build/
generated/
data*/

# Logs
logs
Expand Down
2 changes: 1 addition & 1 deletion src/erc-20-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ import { getBucketId, getBucketInfo, loadOrCreateBucket } from "./utils/pool/buc
import { getLendId, loadOrCreateLend } from "./utils/pool/lend"
import { getBorrowerInfo, getLoanId, loadOrCreateLoan } from "./utils/pool/loan"
import { getLiquidationAuctionId, getAuctionInfoERC20Pool, loadOrCreateLiquidationAuction, updateLiquidationAuction, getAuctionStatus, loadOrCreateBucketTake } from "./utils/pool/liquidation"
import { getBurnInfo, updatePool, addLiquidationToPool, addReserveAuctionToPool, getLenderInfo, getRatesAndFees, calculateLendRate } from "./utils/pool/pool"
import { getBurnInfo, updatePool, addLiquidationToPool, addReserveAuctionToPool, getLenderInfo, getRatesAndFees } from "./utils/pool/pool"
import { lpbValueInQuote } from "./utils/common"
import { loadOrCreateReserveAuction, reserveAuctionKickerReward } from "./utils/pool/reserve-auction"
import { incrementTokenTxCount } from "./utils/token-erc20"
Expand Down
27 changes: 22 additions & 5 deletions src/utils/pool/pool.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BigDecimal, BigInt, Bytes, Address, dataSource } from '@graphprotocol/graph-ts'
import { BigDecimal, BigInt, Bytes, Address, dataSource, bigInt } from '@graphprotocol/graph-ts'

import { LiquidationAuction, Pool, ReserveAuction, Token } from "../../../generated/schema"
import { ERC20Pool } from '../../../generated/templates/ERC20Pool/ERC20Pool'
import { PoolInfoUtils } from '../../../generated/templates/ERC20Pool/PoolInfoUtils'

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

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

export function calculateLendRate(borrowRate: BigInt, lenderInterestMargin: BigInt, utilization: BigInt): BigDecimal {
export function calculateLendRate(
poolAddress: Address,
borrowRate: BigInt,
lenderInterestMargin: BigInt,
poolPricesInfo: PoolPricesInfo,
debt: BigInt
): BigDecimal {
const meaningfulPriceIndex = max(poolPricesInfo.lupIndex.toU32(), poolPricesInfo.htpIndex.toU32())
const meaningfulDeposit = depositUpToIndex(poolAddress, meaningfulPriceIndex)
if (meaningfulDeposit.equals(ZERO_BI)) return ZERO_BD
const utilization = wdiv(debt, meaningfulDeposit)
return wadToDecimal(wmul(wmul(borrowRate,lenderInterestMargin), utilization))
}

Expand Down Expand Up @@ -231,9 +241,11 @@ export function updatePool(pool: Pool): void {
// update lend rate and borrow fee, which change irrespective of borrow rate
const ratesAndFees = getRatesAndFees(poolAddress)
pool.lendRate = calculateLendRate(
poolAddress,
decimalToWad(pool.borrowRate),
ratesAndFees.lenderInterestMargin,
poolUtilizationInfo.actualUtilization)
poolPricesInfo,
debtInfo.pendingDebt)
pool.borrowFeeRate = wadToDecimal(ratesAndFees.borrowFeeRate)
}

Expand Down Expand Up @@ -316,3 +328,8 @@ export function getDebtInfo(pool: Pool): DebtInfo {
debtInfoResult.value3
)
}

export function depositUpToIndex(poolAddress: Address, index: u32): BigInt {
const poolContract = ERC20Pool.bind(poolAddress)
return poolContract.depositUpToIndex(BigInt.fromU32(index));
}
12 changes: 11 additions & 1 deletion tests/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { positionManagerAddressTable, poolInfoUtilsAddressTable, ZERO_BI, ONE_BI
import { BurnInfo, DebtInfo, LoansInfo, PoolPricesInfo, PoolUtilizationInfo, ReservesInfo } from "../../src/utils/pool/pool"
import { AuctionInfo, AuctionStatus } from "../../src/utils/pool/liquidation"
import { BorrowerInfo } from "../../src/utils/pool/loan"
import { wdiv, wmin } from "../../src/utils/math"
import { wdiv, wmin, wmul } from "../../src/utils/math"

/*************************/
/*** Bucket Assertions ***/
Expand Down Expand Up @@ -555,6 +555,14 @@ export function mockGetTokenInfo(token: Address, expectedName: string, expectedS
.returns([ethereum.Value.fromUnsignedBigInt(expectedTotalSupply)])
}

export function mockDepositUpToIndex(pool: Address, index: BigInt, expectedInfo: BigInt): void {
createMockedFunction(pool, 'depositUpToIndex', 'depositUpToIndex(uint256):(uint256)')
.withArgs([ethereum.Value.fromUnsignedBigInt(index)])
.returns([
ethereum.Value.fromUnsignedBigInt(expectedInfo)
])
}

export class PoolMockParams {
// loans info mock params
poolSize: BigInt
Expand Down Expand Up @@ -631,6 +639,8 @@ export function mockPoolInfoUtilsPoolUpdateCalls(pool: Address, params: PoolMock
BigInt.fromString("850000000000000000"), // 0.85 * 1e18
BigInt.fromString("50000000000000000"), // 0.05 * 1e18
)

mockDepositUpToIndex(pool, params.lupIndex, wmul(params.poolSize, params.actualUtilization))
}

/****************************/
Expand Down

0 comments on commit 6067ba4

Please sign in to comment.