Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. /*
  2. AccrueInterest() - updates the market total borrows and lends. If we mimic this, we can
  3. get the pseudo values of accrue interest based on the block delta from the last time
  4. accrue interest was calculated. What follows is a breakdown of what we need
  5. to calculate to get the pseudo values for the market
  6.  
  7. Note you'll have to handle big numbers being returned. The mantissa makes a lot of calculations hard to follow, along
  8. with the decimals of each token.... care must be taken here! Although the output values are easy to verify, if you are
  9. wrong you are usually wrong by 10^18, or other big exponents.
  10.  
  11. THOUGHT - We could do block trigger updates for all Markets (only 7 or 8 right now)
  12. - Then the user balance updates are all we would have to do
  13. - This actually sounds like a great idea
  14. - It would be good to run the block trigger after any event handlers, because we
  15. - couldactually skip the update on that block
  16.  
  17. */
  18.  
  19. import { Market } from '../types/schema'
  20.  
  21. function queryTimeMarketAndAccountComputation(
  22. marketAddress,
  23. marketSymbol,
  24. currentBlockNumber,
  25. account,
  26. ) {
  27. let market = Market.load(marketAddress)
  28.  
  29.  
  30. const cashPrior = market.totalCash
  31. const totalBorrows = market.totalBorrows
  32. const totalReserves = market.totalReserves
  33.  
  34. // To get Annual Percent Return (APR) : perBlockBorrowInterest * 2102400. but we shouldn't need this
  35. const borrowRate = market.perBlockBorrowInterest
  36.  
  37. const latestBlock = currentBlockNumber // This is the single special change that gives us updated values :D
  38. const accrualBlockNumber = market.accrualBlockNumber
  39. const blockDelta = latestBlock - accrualBlockNumber
  40.  
  41. const simpleInterestFactor = borrowRate * blockDelta
  42. const interestAccumulated = simpleInterestFactor * totalBorrows
  43. const totalBorrowsNew = interestAccumulated + totalBorrows
  44.  
  45. const reserveFactor = market.reserveFactorMantissa // TODO - dave add this into market schema
  46. const totalReservesNew = interestAccumulated * reserveFactor + totalReserves
  47.  
  48. const borrowIndex = market.borrowIndex
  49. const borrowIndexNew = simpleInterestFactor * borrowIndex + borrowIndex
  50.  
  51. // TODO - Maybe
  52. // RECALCUATE THE NEW PER BLOCK BORROW AND PER BLOCK INTEREST
  53. // THE CHANGE WILL BE SO SMALL THOUGH, PROBABLY LESS THAN 0.01%
  54. // We can implement this later if we really want
  55. // also, if we use block triggers for markets, we can probably easily do it every block
  56.  
  57. /* HANDLE BORROWING UPDATES */
  58. // We need to mimic the function borrowBalanceStoredInternal()
  59. // Here borrowIndexNew is the updated one we just calculated
  60. // Todo - Optimization - could implement checking if this user has ever borrowed
  61. const cTokenInfo = CTokenInfo.load(marketSymbol.concat(account))
  62. const borrowBalance = cTokenInfo.borrowBalance // TODO - dave needs to add this to CTokenInfo
  63. const interestIndex = cTokenInfo.interestIndex // TODO - dave needs to add this to CTokenInfo
  64.  
  65. const principalTimesIndex = borrowBalance * borrowIndexNew
  66. const updatedBorrowBalance = principalTimesIndex / interestIndex
  67.  
  68. /* HANDLE LENDING UPDATES */
  69. // We mimic exchangeRate calculation to get an updated exchange rate
  70. // exchangeRate = (totalCash + totalBorrows - totalReserves) / totalSupply
  71. const totalSupply = market.totalSupply
  72.  
  73. // Here we use the two New values to get the updated value for the user
  74. const newExchangeRate = (cashPrior + totalBorrowsNew - totalReservesNew) / totalSupply
  75. const cTokenBalance = cTokenInfo.cTokenBalance
  76. const updatedSupplyBalance = cTokenBalance * newExchangeRate
  77.  
  78. return [updatedBorrowBalance, updatedSupplyBalance]
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement