@vinox/soup-sdk-js v4.3.0
AAVE-JS
AAVE is a decentralized non-custodial liquidity market protocol where users can participate as depositors or borrowers. The AAVE Protocol is a set of open source smart contracts which facilitate the lending and borrowing of user funds. These contracts, and all user transactions/balances are stored on a public ledger called a blockchain, making them accessible to anyone.
The aave-js package gives developers access to methods for formatting data and executing transactions on the AAVE protocol.
Quick Start
This package uses ethers v5 as peer dependency, so make sure you have installed it in your project.
npm install --save ethersInstalling
npm install --save @aave/protocol-jsData Formatting Methods
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
V1 GraphQL:
V2 GraphQL (V2 Market and AMM Market) - Playground: https://thegraph.com/explorer/subgraph/aave/protocol-v2 - API: https://api.thegraph.com/subgraphs/name/aave/protocol-v2
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:
- V2 Market: "0xb53c1a33016b2dc2ff3653530bff1848a515c8c5"
- AMM Market: "0xacc030ef66f9dfeae9cbb0cd1b25654b82cfa8d5"
Sample Usage
import { v1, v2 } from '@aave/protocol-js';
// Fetch poolReservesData from GQL Subscription
// Fetch rawUserReserves from GQL Subscription
// Fetch ethPriceUSD from GQL Subscription
let userAddress = "0x..."
let userSummary = v2.formatUserSummaryData(poolReservesData, rawUserReserves, userAddress.toLowerCase(), Math.floor(Date.now() / 1000))User Data
formatUserSummaryData
Returns formatted summary of AAVE user portfolio including: array of holdings, total liquidity, total collateral, total borrows, liquidation threshold, health factor, and available borrowing power
- @param
poolReservesDataGraphQL input: - subscription: src/v1 or v2/graphql/subscriptions/reserves-update-subscription.graphql : Requires input of pool (address of market which can be found above, or remove this filter to fetch all markets) - types: src/v1 or v2/graphql/fragments/pool-reserve-data.graphql - @param
rawUserReservesGraphQL input, query can be found here: - @param
userIdWallet address, MUST BE LOWERCASE! - @param
usdPriceEthCurrent price of USD in ETH in small units (10^18). For example, if ETH price in USD = $1900, usdPriceEth = (1 / 1900) * 10^18 : Can also be fetched using this subscription: /src/v1 or v2/graphql/subscriptions/usd-price-eth-update-subscription.graphql - @param
currentTimestampCurrent Unix timestamp in seconds: Math.floor(Date.now() / 1000) - @param @optional
rewardsInfoInformation used to compute aTokenRewards (deposit rewards), vTokenRewards (variable debt rewards), and sTokenRewards (stable debt rewards). Object with format:
All fields can be fetched from the IncentivesController subgraph entity with the exception of rewardTokenPriceEth. Since reward tokens are not guaranteed to be Aave reserve tokens, the price feed for reward tokens is not directly attached to the controller. For stkAAVE, WMATIC, and WAVAX rewards the reveserve price feed from AAVE, MATIC, and AVAX repectively can be used.{ rewardTokenAddress: string; rewardTokenDecimals: number; incentivePrecision: number; rewardTokenPriceEth: string; emissionEndTimestamp: number; }
v1.formatUserSummaryData(
poolReservesData: ReserveData[],
rawUserReserves: UserReserveData[],
userId: string,
usdPriceEth: BigNumberValue,
currentTimestamp: number
);
v2.formatUserSummaryData(
poolReservesData: ReserveData[],
rawUserReserves: UserReserveData[],
userId: string,
usdPriceEth: BigNumberValue,
currentTimestamp: number,
rewardsInfo?: RewardInformation
);Reserve Data
formatReserves
Returns formatted summary of each AAVE reserve asset
Note: liquidityRate = deposit rate in the return object
- @param
reservesGraphQL input: - subscription: src/v1 or v2/graphql/subscriptions/reserves-update-subscription.graphql : Requires input of pool (address of market which can be found above, or remove this filter to fetch all markets) - types: src/v1 or v2/graphql/fragments/pool-reserve-data.graphql - @param @optional
reservesIndexed30DaysAgoGraphQL input: - @param @optional
currentTimestampCurrent Unix timestamp in seconds: Math.floor(Date.now() / 1000) - @param @optional
rewardTokenPriceEthPrice of reward token in market base currency. Can use the priceInEth from the reserve data if there is a corresponding reserve for the reward token (stkAave -> Aave reserve price, WMATIC -> MATIC reserve price, etc.) - @param @optional
emissionEndTimestampTimestamp of reward emission end. Can be fetched from IncentivesController subgraph entity
v1.formatReserves(
reserves: ReserveData[]
reservesIndexed30DaysAgo?: ReserveRatesData[]
);
v2.formatReserves(
reserves: ReserveData[]
reservesIndexed30DaysAgo?: ReserveRatesData[],
currentTimestamp?: number,
rewardTokenPriceEth?: string,
emissionEndTimestamp?: number
);Transaction Methods
Markets and Networks
The library exports the enabled networks and markets in the Aave protocol as the enums Network and Market
import { Network, Market } from '@aave/protocol-js';Usage
import { TxBuilderV2, Network, Market } from '@aave/protocol-js'
const httpProvider = new Web3.providers.HttpProvider(
process.env.ETHEREUM_URL ||
"https://kovan.infura.io/v3/<project_id>"
);
const txBuilder = new TxBuilderV2(Network.main, httpProvider);
lendingPool = txBuilder.getLendingPool(Market.main); // get all lending pool methodsProviders
The library accepts 3 kinds of providers:
- web3 provider
- JsonRPC url
- no provider: if no provider is passed it will default to ethers Infura / etherscan providers (shared providers, do not use in production)
To learn more about supported providers, see the ethers documentation on providers.
Lending Pool V2
Object that contains all the necessary methods to create Aave lending pool transactions.
The return object will be a Promise array of objects of type:
import { EthereumTransactionTypeExtended } from '@aave/protocol-js'having {tx, txType}
- tx: object with transaction fields.
- txType: string determining the kinds of transaction.
deposit
Deposits the underlying asset into the reserve. A corresponding amount of the overlying asset (aTokens) is minted.
- @param
userThe ethereum address that will make the deposit - @param
reserveThe ethereum address of the reserve - @param
amountThe amount to be deposited - @param @optional
onBehalfOfThe ethereum address for which user is depositing. It will default to the user address - @param @optional
referralCodeIntegrators are assigned a referral code and can potentially receive rewards. It defaults to 0 (no referrer)
lendingPool.deposit({
user, // string,
reserve, // string,
amount, // string,
onBehalfOf, // ? string,
referralCode, // ? string,
});If the user is not approved, an approval transaction will also be returned.
borrow
Borrow an amount of reserve asset.
User must have a collaterised position (i.e. aTokens in their wallet)
- @param
userThe ethereum address that will receive the borrowed amount - @param
reserveThe ethereum address of the reserve asset - @param
amountThe amount to be borrowed, in human readable units (e.g. 2.5 ETH) - @param
interestRateModeWhether the borrow will incur a stable or variable interest rate (1 | 2) - @param @optional
debtTokenAddressThe ethereum address of the debt token of the asset you want to borrow. Only needed if the reserve is ETH mock address - @param @optional
onBehalfOfThe ethereum address for which user is borrowing. It will default to the user address - @param @optional
refferalCodeIntegrators are assigned a referral code and can potentially receive rewards. It defaults to 0 (no referrer)
enum InterestRate {
None = 'None',
Stable = 'Stable',
Variable = 'Variable',
}
lendingPool.borrow({
user, // string,
reserve, // string,
amount, // string,
interestRateMode, // InterestRate;
debtTokenAddress, // ? string;
onBehalfOf, // ? string;
referralCode, // ? string;
});repay
Repays a borrow on the specific reserve, for the specified amount (or for the whole amount, if (-1) is specified).
the target user is defined by onBehalfOf. If there is no repayment on behalf of another account, onBehalfOf must be equal to user.
- @param
userThe ethereum address that repays - @param
reserveThe ethereum address of the reserve on which the user borrowed - @param
amountThe amount to repay, or (-1) if the user wants to repay everything - @param
interestRateModeWhether the borrow will incur a stable or variable interest rate (1 | 2) - @param @optional
onBehalfOfThe ethereum address for which user is repaying. It will default to the user address
enum InterestRate {
None = 'None',
Stable = 'Stable',
Variable = 'Variable',
}
lendingPool.repay({
user, // string,
reserve, // string,
amount, // string,
interestRateMode, // InterestRate;
onBehalfOf, // ? string
});If the user is not approved, an approval transaction will also be returned.
withdraw
Withdraws the underlying asset of an aToken asset.
- @param
userThe ethereum address that will receive the aTokens - @param
reserveThe ethereum address of the reserve asset - @param
amountThe amount of aToken being redeemed - @param @optional
aTokenAddressThe ethereum address of the aToken. Only needed if the reserve is ETH mock address - @param @optional
onBehalfOfThe amount of aToken being redeemed. It will default to the user address
lendingPool.withdraw({
user, // string,
reserve, // string,
amount, // string,
aTokenAddress, // ? string,
onBehalfOf, // ? string
});swapBorrowRateMode
Borrowers can use this function to swap between stable and variable borrow rate modes.
- @param
userThe ethereum address that wants to swap rate modes - @param
reserveThe address of the reserve on which the user borrowed - @param
interestRateModeWhether the borrow will incur a stable or variable interest rate (1 | 2)
enum InterestRate {
None = 'None',
Stable = 'Stable',
Variable = 'Variable',
}
lendingPool.swapBorrowRateMode({
user, // string,
reserve, // string,
interestRateMode, // InterestRate;
});setUsageAsCollateral
Allows depositors to enable or disable a specific deposit as collateral.
- @param
userThe ethereum address that enables or disables the deposit as collateral - @param
reserveThe ethereum address of the reserve - @param
useAsCollateralTrue if the user wants to use the deposit as collateral, false otherwise.
lendingPool.setUsageAsCollateral({
user, // string,
reserve, // string,
usageAsCollateral, // boolean
});liquidationCall
Users can invoke this function to liquidate an undercollateralized position.
- @param
liquidatorThe ethereum address that will liquidate the position - @param
liquidatedUserThe address of the borrower - @param
debtReserveThe ethereum address of the principal reserve - @param
collateralReserveThe address of the collateral to liquidated - @param
purchaseAmountThe amount of principal that the liquidator wants to repay - @param @optional
getATokenBoolean to indicate if the user wants to receive the aToken instead of the asset. Defaults to false
lendingPool.liquidationCall({
liquidator, // string;
liquidatedUser, // string;
debtReserve, // string;
collateralReserve, // string;
purchaseAmount, // string;
getAToken, // ? boolean;
});swapCollateral
Allows users to swap a collateral to another asset
- @param
userThe ethereum address that will liquidate the position - @param @optional
flashIf the transaction will be executed through a flasloan(true) or will be done directly through the adapters(false). Defaults to false - @param
fromAssetThe ethereum address of the asset you want to swap - @param
fromATokenThe ethereum address of the aToken of the asset you want to swap - @param
toAssetThe ethereum address of the asset you want to swap to (get) - @param
fromAmountThe amount you want to swap - @param
toAmountThe amount you want to get after the swap - @param
maxSlippageThe max slippage that the user accepts in the swap - @param @optional
permitSignatureA permit signature of the tx. Only needed when previously signed (Not needed at the moment). - @param
swapAllBool indicating if the user wants to swap all the current collateral - @param @optional
onBehalfOfThe ethereum address for which user is swaping. It will default to the user address - @param @optional
referralCodeIntegrators are assigned a referral code and can potentially receive rewards. It defaults to 0 (no referrer) - @param @optional
useEthPathBoolean to indicate if the swap will use an ETH path. Defaults to false
type PermitSignature = {
amount: tStringCurrencyUnits;
deadline: string;
v: number;
r: BytesLike;
s: BytesLike;
};
await lendingPool.swapCollateral({
user, // string;
flash, // ? boolean;
fromAsset, // string;
fromAToken, // string;
toAsset, // string;
fromAmount, // string;
toAmount, // string;
maxSlippage, // string;
permitSignature, // ? PermitSignature;
swapAll, // boolean;
onBehalfOf, // ? string;
referralCode, // ? string;
useEthPath, // ? boolean;
});repayWithCollateral
Allows a borrower to repay the open debt with the borrower collateral
- @param
userThe ethereum address that will liquidate the position - @param
fromAssetThe ethereum address of the asset you want to repay with (collateral) - @param
fromATokenThe ethereum address of the aToken of the asset you want to repay with (collateral) - @param
assetToRepayThe ethereum address of the asset you want to repay - @param
repayWithAmountThe amount of collateral you want to repay the debt with - @param
repayAmountThe amount of debt you want to repay - @param
permitSignatureA permit signature of the tx. Optional - @param @optional
repayAllDebtBool indicating if the user wants to repay all current debt. Defaults to false - @param
rateModeEnum indicating the type of the interest rate of the collateral - @param @optional
onBehalfOfThe ethereum address for which user is swaping. It will default to the user address - @param @optional
referralCodeIntegrators are assigned a referral code and can potentially receive rewards. It defaults to 0 (no referrer) - @param @optional
flashIf the transaction will be executed through a flasloan(true) or will be done directly through the adapters(false). Defaults to false - @param @optional
useEthPathBoolean to indicate if the swap will use an ETH path. Defaults to false
enum InterestRate {
None = 'None',
Stable = 'Stable',
Variable = 'Variable',
}
await lendingPool.repayWithCollateral({
user, // string;
fromAsset, // string;
fromAToken, // string;
assetToRepay, // string
repayWithAmount, // string;
repayAmount, // string;
permitSignature, // ? PermitSignature;
repayAllDebt, // ? boolean;
rateMode, // InterestRate;
onBehalfOf, // ? string;
referralCode, // ? string;
flash, // ? boolean;
useEthPath, // ? boolean;
});Governance V2
Example of how to use the governance service
import {
TxBuilderV2,
AaveGovernanceV2Interface,
GovernanceDelegationTokenInterface,
} from '@aave/protocol-js';
const httpProvider = new Web3.providers.HttpProvider(
process.env.ETHEREUM_URL ||
"https://kovan.infura.io/v3/<project_id>"
);
const txBuilder = new TxBuilderV2(Network.main, httpProvider);
const gov2 = txBuilder.aaveGovernanceV2Service;
const powerDelegation = txBuilder.governanceDelegationTokenService;create
Creates a Proposal (needs to be validated by the Proposal Validator)
- @param
userThe ethereum address that will create the proposal - @param
targetslist of contracts called by proposal's associated transactions - @param
valueslist of value in wei for each propoposal's associated transaction - @param
signatureslist of function signatures (can be empty) to be used when created the callData - @param
calldataslist of calldatas: if associated signature empty, calldata ready, else calldata is arguments - @param
withDelegatecallsboolean, true = transaction delegatecalls the taget, else calls the target - @param
ipfsHashIPFS hash of the proposal - @param
executorThe ExecutorWithTimelock contract that will execute the proposal
enum ExecutorType {
Short,
Long,
}
--------
gov2.create({
user. // string;
targets, //string[];
values, // string[];
signatures, // string[];
calldatas, // BytesLike[];
withDelegateCalls, // boolean[];
ipfsHash, // BytesLike;
executor, // ExecutorType;
});cancel
Cancels a Proposal. Callable by the _guardian with relaxed conditions, or by anybody if the conditions of cancellation on the executor are fulfilled
- @param
userThe ethereum address that will create the proposal - @param
proposalIdId of the proposal we want to cancel
gov2.cancel({
user, // string
proposalId, // number
})queue
Queue the proposal (If Proposal Succeeded)
- @param
userThe ethereum address that will create the proposal - @param
proposalIdId of the proposal we want to queue
gov2.queue({
user, // string
proposalId, // number
})execute
Execute the proposal (If Proposal Queued)
- @param
userThe ethereum address that will create the proposal - @param
proposalIdId of the proposal we want to execute
gov2.execute({
user, // string
proposalId, // number
})submitVote
Function allowing msg.sender to vote for/against a proposal
- @param
userThe ethereum address that will create the proposal - @param
proposalIdId of the proposal we want to vote - @param
supportBool indicating if you are voting in favor (true) or against (false)
gov2.submitVote({
user, // string
proposalId, // number
support, // boolean
})Governance Delegation
delegate
Method for the user to delegate voting and proposition power to the chosen address
- @param
userThe ethereum address that will create the proposal - @param
delegateeThe ethereum address to which the user wants to delegate proposition power and voting power - @param
governanceTokenThe ethereum address of the governance token
powerDelegation.delegate({
user, // string
delegatee, // string
governanceToken // string
});delegateByType
Method for the user to delegate voting or proposition power to the chosen address
- @param
userThe ethereum address that will create the proposal - @param
delegateeThe ethereum address to which the user wants to delegate proposition power and voting power - @param
delegationTypeThe type of the delegation the user wants to do: voting power ('0') or proposition power ('1') - @param
governanceTokenThe ethereum address of the governance token
powerDelegation.delegateByType({
user, // string
delegatee, // string
delegationType, // string
governanceToken // string
});Faucets
To use the testnet faucets which are compatible with Aave:
import { TxBuilderV2, Network, Market } from '@aave/protocol-js'
const httpProvider = new Web3.providers.HttpProvider(
process.env.ETHEREUM_URL ||
"https://kovan.infura.io/v3/<project_id>"
);
const txBuilder = new TxBuilderV2(Network.main, httpProvider);
const faucet = txBuilder.faucetService;mint
Mint tokens for the usage on the Aave protocol on the Kovan network. The amount of minted tokens is fixed and depends on the token
- @param
userAddressThe ethereum address of the wallet the minted tokens will go - @param
reserveThe ethereum address of the token you want to mint - @param
tokenSymbolThe symbol of the token you want to mint
faucet.mint({
userAddress, // string
reserve, // string
tokenSymbol, // string
});Lint
To lint we use EsLint with typescript plugins and extending Airbnb
npm run lintBuild
To build run:
npm run build // builds with tsdx
npm run build:tsc // builds with tsc3 years ago