diff --git a/src/erc-20-pool-factory.ts b/src/erc-20-pool-factory.ts index 499cbac..c74fe2c 100644 --- a/src/erc-20-pool-factory.ts +++ b/src/erc-20-pool-factory.ts @@ -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" @@ -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 diff --git a/src/erc-20-pool.ts b/src/erc-20-pool.ts index d78d4f4..d2ee345 100644 --- a/src/erc-20-pool.ts +++ b/src/erc-20-pool.ts @@ -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( @@ -991,6 +990,7 @@ 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 @@ -998,7 +998,7 @@ export function handleResetInterestRate(event: ResetInterestRateEvent): void { 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) @@ -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) @@ -1236,6 +1235,7 @@ 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 @@ -1243,7 +1243,7 @@ export function handleUpdateInterestRate(event: UpdateInterestRateEvent): void { 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) @@ -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) diff --git a/src/utils/pool.ts b/src/utils/pool.ts index c6d6cb4..2a99f7d 100644 --- a/src/utils/pool.ts +++ b/src/utils/pool.ts @@ -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) @@ -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 @@ -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) }