import { Mint , Redeem , Borrow , RepayBorrow , LiquidateBorrow , Transfer , AccrueInterest , NewReserveFactor , NewMarketInterestRateModel , } from '../types/cREP/CToken' ; import { AccountCToken , Market , Account } from '../types/schema' ; import { createMarket , updateMarket } from './markets' ; import { createAccount , updateCommonCTokenStats , exponentToBigDecimal , cTokenDecimalsBD , cTokenDecimals , createAccountCToken , zeroBD , } from './helpers' ; export function handleMint ( event ) { } export function handleRedeem ( event ) { } export function handleBorrow ( event ) { let market = Market . load ( event . address . toHexString ( ) ) ; let accountID = event . params . borrower . toHex ( ) ; let cTokenStats = updateCommonCTokenStats ( market . id , market . symbol , accountID , event . transaction . hash , event . block . timestamp . toI32 ( ) , event . block . number . toI32 ( ) ) ; let borrowAmountBD = event . params . borrowAmount . toBigDecimal ( ) . div ( exponentToBigDecimal ( market . underlyingDecimals ) ) ; let previousBorrow = cTokenStats . storedBorrowBalance ; cTokenStats . storedBorrowBalance = event . params . accountBorrows . toBigDecimal ( ) . div ( exponentToBigDecimal ( market . underlyingDecimals ) ) . truncate ( market . underlyingDecimals ) ; cTokenStats . accountBorrowIndex = market . borrowIndex ; cTokenStats . totalUnderlyingBorrowed = cTokenStats . totalUnderlyingBorrowed . plus ( borrowAmountBD ) ; cTokenStats . save ( ) ; let account = Account . load ( accountID ) ; if ( account == null ) { account = createAccount ( accountID ) ; } account . hasBorrowed = true ; account . save ( ) ; if ( previousBorrow . equals ( zeroBD ) && ! event . params . accountBorrows . toBigDecimal ( ) . equals ( zeroBD ) ) { market . numberOfBorrowers = market . numberOfBorrowers + 1 ; market . save ( ) ; } } export function handleRepayBorrow ( event ) { let market = Market . load ( event . address . toHexString ( ) ) ; let accountID = event . params . borrower . toHex ( ) ; let cTokenStats = updateCommonCTokenStats ( market . id , market . symbol , accountID , event . transaction . hash , event . block . timestamp . toI32 ( ) , event . block . number . toI32 ( ) ) ; let repayAmountBD = event . params . repayAmount . toBigDecimal ( ) . div ( exponentToBigDecimal ( market . underlyingDecimals ) ) ; cTokenStats . storedBorrowBalance = event . params . accountBorrows . toBigDecimal ( ) . div ( exponentToBigDecimal ( market . underlyingDecimals ) ) . truncate ( market . underlyingDecimals ) ; cTokenStats . accountBorrowIndex = market . borrowIndex ; cTokenStats . totalUnderlyingRepaid = cTokenStats . totalUnderlyingRepaid . plus ( repayAmountBD ) ; cTokenStats . save ( ) ; let account = Account . load ( accountID ) ; if ( account == null ) { createAccount ( accountID ) ; } if ( cTokenStats . storedBorrowBalance . equals ( zeroBD ) ) { market . numberOfBorrowers = market . numberOfBorrowers - 1 ; market . save ( ) ; } } export function handleLiquidateBorrow ( event ) { let liquidatorID = event . params . liquidator . toHex ( ) ; let liquidator = Account . load ( liquidatorID ) ; if ( liquidator == null ) { liquidator = createAccount ( liquidatorID ) ; } liquidator . countLiquidator = liquidator . countLiquidator + 1 ; liquidator . save ( ) ; let borrowerID = event . params . borrower . toHex ( ) ; let borrower = Account . load ( borrowerID ) ; if ( borrower == null ) { borrower = createAccount ( borrowerID ) ; } borrower . countLiquidated = borrower . countLiquidated + 1 ; borrower . save ( ) ; } export function handleTransfer ( event ) { let marketID = event . address . toHexString ( ) ; let market = Market . load ( marketID ) ; if ( market . accrualBlockNumber != event . block . number . toI32 ( ) ) { market = updateMarket ( event . address , event . block . number . toI32 ( ) , event . block . timestamp . toI32 ( ) ) ; } let amountUnderlying = market . exchangeRate . times ( event . params . amount . toBigDecimal ( ) . div ( cTokenDecimalsBD ) ) ; let amountUnderylingTruncated = amountUnderlying . truncate ( market . underlyingDecimals ) ; let accountFromID = event . params . from . toHex ( ) ; if ( accountFromID != marketID ) { let accountFrom = Account . load ( accountFromID ) ; if ( accountFrom == null ) { createAccount ( accountFromID ) ; } let cTokenStatsFrom = updateCommonCTokenStats ( market . id , market . symbol , accountFromID , event . transaction . hash , event . block . timestamp . toI32 ( ) , event . block . number . toI32 ( ) ) ; cTokenStatsFrom . cTokenBalance = cTokenStatsFrom . cTokenBalance . minus ( event . params . amount . toBigDecimal ( ) . div ( cTokenDecimalsBD ) . truncate ( cTokenDecimals ) ) ; cTokenStatsFrom . totalUnderlyingRedeemed = cTokenStatsFrom . totalUnderlyingRedeemed . plus ( amountUnderylingTruncated ) ; cTokenStatsFrom . save ( ) ; if ( cTokenStatsFrom . cTokenBalance . equals ( zeroBD ) ) { market . numberOfSuppliers = market . numberOfSuppliers - 1 ; market . save ( ) ; } } let accountToID = event . params . to . toHex ( ) ; if ( accountToID != marketID ) { let accountTo = Account . load ( accountToID ) ; if ( accountTo == null ) { createAccount ( accountToID ) ; } let cTokenStatsTo = updateCommonCTokenStats ( market . id , market . symbol , accountToID , event . transaction . hash , event . block . timestamp . toI32 ( ) , event . block . number . toI32 ( ) ) ; let previousCTokenBalanceTo = cTokenStatsTo . cTokenBalance ; cTokenStatsTo . cTokenBalance = cTokenStatsTo . cTokenBalance . plus ( event . params . amount . toBigDecimal ( ) . div ( cTokenDecimalsBD ) . truncate ( cTokenDecimals ) ) ; cTokenStatsTo . totalUnderlyingSupplied = cTokenStatsTo . totalUnderlyingSupplied . plus ( amountUnderylingTruncated ) ; cTokenStatsTo . save ( ) ; if ( previousCTokenBalanceTo . equals ( zeroBD ) && ! event . params . amount . toBigDecimal ( ) . equals ( zeroBD ) ) { market . numberOfSuppliers = market . numberOfSuppliers + 1 ; market . save ( ) ; } } } export function handleAccrueInterest ( event ) { updateMarket ( event . address , event . block . number . toI32 ( ) , event . block . timestamp . toI32 ( ) ) ; } export function handleNewReserveFactor ( event ) { let marketID = event . address . toHex ( ) ; let market = Market . load ( marketID ) ; market . reserveFactor = event . params . newReserveFactorMantissa ; market . save ( ) ; } export function handleNewMarketInterestRateModel ( event ) { let marketID = event . address . toHex ( ) ; let market = Market . load ( marketID ) ; if ( market == null ) { market = createMarket ( marketID ) ; } market . interestRateModelAddress = event . params . newInterestRateModel ; market . save ( ) ; }