The protocol-js package gives developers access to methods for formatting data and executing transactions on the AAVE protocol.
This package uses ethers v5 as peer dependency, so make sure you have installed it in your project.
npm install --save ethers
npm install --save @aave/protocol-js
AAVE aggregates on-chain protocol data into a variety of different subgraphs on TheGraph which can be queried directly using the playground (links below) and integrated into applications directly via TheGraph API.
The aave-js data formatting methods are a layer beyond graphql which wraps protocol data into more usable formats. Each method will require inputs from AAVE subgraph queries, links to these queries in the source code are provided for each method below.
Check out this getting started guide to get your application integrated with the AAVE subgraphs
The V2 Subgraph contains data for both the V2 and AMM markets. The market which a reserve belongs to can be identified with the pool parameter (market address). The pool id for available markets are below:
import { v1, v2 } from '@aave/protocol-js';
import axios from "axios";
const execute = async (query: string) => {
const res = await axios.post(subgraph_url, {
query,
variable: {},
crossdomain: true,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "json",
}
})
if (res.status === 200) {
if (res.data.errors) return res.data.errors;
return res.data.data
}
}
const getPoolReserveData = async () => {
const query = `query{
reserves {
id
underlyingAsset
name
symbol
decimals
isActive
isFrozen
usageAsCollateralEnabled
borrowingEnabled
stableBorrowRateEnabled
baseLTVasCollateral
optimalUtilisationRate
averageStableRate
stableRateSlope1
stableRateSlope2
baseVariableBorrowRate
variableRateSlope1
variableRateSlope2
liquidityIndex
reserveLiquidationThreshold
variableBorrowIndex
aToken {
id
}
vToken {
id
}
sToken {
id
}
availableLiquidity
stableBorrowRate
liquidityRate
totalPrincipalStableDebt
totalScaledVariableDebt
reserveLiquidationBonus
variableBorrowRate
price {
priceInEth
}
lastUpdateTimestamp
stableDebtLastUpdateTimestamp
reserveFactor
aEmissionPerSecond
vEmissionPerSecond
sEmissionPerSecond
aTokenIncentivesIndex
vTokenIncentivesIndex
sTokenIncentivesIndex
aIncentivesLastUpdateTimestamp
vIncentivesLastUpdateTimestamp
sIncentivesLastUpdateTimestamp
}
}`
return execute(query)
}
const getUserReserveData = async (user: string) => {
const query = `{
userReserves (where: { user: "${user.toLowerCase()}"}){
scaledATokenBalance
reserve {
id
underlyingAsset
name
symbol
decimals
liquidityRate
reserveLiquidationBonus
lastUpdateTimestamp
}
usageAsCollateralEnabledOnUser
stableBorrowRate
stableBorrowLastUpdateTimestamp
principalStableDebt
scaledVariableDebt
variableBorrowIndex
aTokenincentivesUserIndex
vTokenincentivesUserIndex
sTokenincentivesUserIndex
}
}`;
return execute(query)
}
// Fetch ethPriceUSD from GQL Subscription/query
const poolReservesData = (await getPoolReserveData()).reserves;
const rawUserReserves = (await getUserReserveData(wallet.address)).userReserves;
let userAddress = "0x..."
let userSummary = v2.formatUserSummaryData(poolReservesData, rawUserReserves, userAddress.toLowerCase(), usdPriceEth, Math.floor(Date.now()/ 1000))