Currently there is a Typescript package available on NPM to help with your implementation of the Aave Protocol. The SDK provides easy to use methods to interact with the Aave Protocol, as well as methods to calculate user positions such as the health factor.
The SDK requires a basic understanding of Javascript/Typescript, as well as experience using NPM.
An example of using the SDK with data fetched from the subgraph:
import { v1, v2 } from'@aave/protocol-js';// returns user summary data in big units.v1.formatUserSummaryData( poolReservesData, rawUserReserves, userId, usdPriceEth, currentTimestamp);// returns user summary data in small units with 0 decimal places, except health-factor.v1.computeRawUserSummaryData( poolReservesData, rawUserReserves, userId, usdPriceEth, currentTimestamp);// returns reserves data formatted to big units.v2.formatReserves(reserves, currentTimestamp);
For a list of transaction methods, see the Github repo.
Rates calculation
Calculating rates in Aave is somewhat complicated due to the interest bearing nature of aTokens.
The reason for that is twofold:
liquidity of aTokens constantly changes which is making utilization based rates extremely dynamic (it's changing every second)
depositor interest is not only based on interest, but also includes factors like flash premiums (so it's usually "a little more")
Therefore the Aave protocol keeps track of two values:
Rate: The rate is a purely utilization based metric which represents the current rate at a specific point in time.
Index: The index keeps track of reserve growth also incorporating things like the flash premium.
/*You can fetch the data rquired from graphql.This query would provide the mot up to date & the first ever data.This data could be used to calculate the average liquidityRate since inception.{ reserves(first: 1) { lastUpdateTimestamp liquidityIndex variableBorrowIndex paramsHistory(first: 1, orderDirection: asc, orderBy: timestamp){ timestamp liquidityIndex variableBorrowIndex } }}*/// data returned by gqlconstresult= { lastUpdateTimestamp:1611926412, liquidityIndex:"1031830159181667334741382194", variableBorrowIndex:"1048002659480593195934069307", paramsHistory: [ { timestamp:1606903009, liquidityIndex:"1021187617641523092754480830", variableBorrowIndex:"1034162815675781948442542345", }, ],};constarchivedRate=calculateAverageRate(result.paramsHistory[0].liquidityIndex,result.liquidityIndex,result.paramsHistory[0].timestamp,result.lastUpdateTimestamp);