Skip to content

Instantly share code, notes, and snippets.

@davekaj
Last active September 27, 2019 03:38
Show Gist options
  • Save davekaj/13458e20fe0633622d442e1152c1f676 to your computer and use it in GitHub Desktop.
Save davekaj/13458e20fe0633622d442e1152c1f676 to your computer and use it in GitHub Desktop.
/*
AccrueInterest() - updates the market total borrows and lends. If we mimic this, we can
get the pseudo values of accrue interest based on the block delta from the last time
accrue interest was calculated. What follows is a breakdown of what we need
to calculate to get the pseudo values for the market
Note you'll have to handle big numbers being returned. The mantissa makes a lot of calculations hard to follow, along
with the decimals of each token.... care must be taken here! Although the output values are easy to verify, if you are
wrong you are usually wrong by 10^18, or other big exponents.
THOUGHT - We could do block trigger updates for all Markets (only 7 or 8 right now)
- Then the user balance updates are all we would have to do
- This actually sounds like a great idea
- It would be good to run the block trigger after any event handlers, because we
- couldactually skip the update on that block
*/
import { Market } from '../types/schema'
function queryTimeMarketAndAccountComputation(
marketAddress,
marketSymbol,
currentBlockNumber,
account,
) {
let market = Market.load(marketAddress)
const cashPrior = market.totalCash
const totalBorrows = market.totalBorrows
const totalReserves = market.totalReserves
// To get Annual Percent Return (APR) : perBlockBorrowInterest * 2102400. but we shouldn't need this
const borrowRate = market.perBlockBorrowInterest
const latestBlock = currentBlockNumber // This is the single special change that gives us updated values :D
const accrualBlockNumber = market.accrualBlockNumber
const blockDelta = latestBlock - accrualBlockNumber
const simpleInterestFactor = borrowRate * blockDelta
const interestAccumulated = simpleInterestFactor * totalBorrows
const totalBorrowsNew = interestAccumulated + totalBorrows
const reserveFactor = market.reserveFactorMantissa // TODO - dave add this into market schema
const totalReservesNew = interestAccumulated * reserveFactor + totalReserves
const borrowIndex = market.borrowIndex
const borrowIndexNew = simpleInterestFactor * borrowIndex + borrowIndex
// TODO - Maybe
// RECALCUATE THE NEW PER BLOCK BORROW AND PER BLOCK INTEREST
// THE CHANGE WILL BE SO SMALL THOUGH, PROBABLY LESS THAN 0.01%
// We can implement this later if we really want
// also, if we use block triggers for markets, we can probably easily do it every block
/* HANDLE BORROWING UPDATES */
// We need to mimic the function borrowBalanceStoredInternal()
// Here borrowIndexNew is the updated one we just calculated
// Todo - Optimization - could implement checking if this user has ever borrowed
const cTokenInfo = CTokenInfo.load(marketSymbol.concat(account))
const borrowBalance = cTokenInfo.borrowBalance // TODO - dave needs to add this to CTokenInfo
const interestIndex = cTokenInfo.interestIndex // TODO - dave needs to add this to CTokenInfo
const principalTimesIndex = borrowBalance * borrowIndexNew
const updatedBorrowBalance = principalTimesIndex / interestIndex
/* HANDLE LENDING UPDATES */
// We mimic exchangeRate calculation to get an updated exchange rate
// exchangeRate = (totalCash + totalBorrows - totalReserves) / totalSupply
const totalSupply = market.totalSupply
// Here we use the two New values to get the updated value for the user
const newExchangeRate = (cashPrior + totalBorrowsNew - totalReservesNew) / totalSupply
const cTokenBalance = cTokenInfo.cTokenBalance
const updatedSupplyBalance = cTokenBalance * newExchangeRate
return [updatedBorrowBalance, updatedSupplyBalance]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment