Aave v2 Whitepaper
Aave v2 Whitepaper
Aave v2 Whitepaper
V2.0
wow@aave.com
December 2020
Abstract
This document describes the definitions and theory behind the Aave Protocol V2, including how it improves
V1, new features and different aspects of the implementation.
Contents
1 Introduction 1
2 Architectural Specifications 2
3 Features Specifications 3
3.1 aTokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 Debt Tokenization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.3 Variable Debt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.4 Stable Debt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.5 Flash Loans V2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.6 Collateral trading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.7 Repay with collateral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.8 Credit Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Formal Definitions 10
1 Introduction
The Aave Protocol marks a shift from a decentralised P2P lending strategy (direct loan relationship between lenders
and borrowers, like in ETHLend) to a pool-based strategy. Loans do not need to be individually matched, instead
they rely on the pooled funds, as well as the amounts borrowed and their collateral. This enables instant loans with
characteristics based on the state of the pool.
The following paper describes the key functionalities that differentiate the Aave Protocol V2 from V1. The main
driver for the development of Aave V2 was to improve the sub-optimal solutions implemented in V1, precisely:
• Gas inefficiency
• Architecture simplification favoring automated testing through fuzzers and formal verification tools
• Code simplification
Some of the solutions currently implemented in V1 were designed and developed when the Ethereum Network was
notably different. The exponential growth of DeFi has grown the number of transactions twofold, with further
pressure from increased gas costs due to the Istanbul release.
1. Debt Tokenisation. The borrowers debt is now represented by tokens instead of internal accounting within
the contract, enabling:
• Code simplification with regards to the borrowing aspect of the protocol - some of the calculations are
now implicit to the minting/burning logic of the debt tokens.
• Simultaneous borrows with variable and multiple stable rates. In V1, borrowers’ loans are either variable
or stable with previous borrows automatically switched to the latest loan rate. In V2, users can hold
multiple loans combining variable and multiple stable rate positions. Multiple stable rate positions are
handled through the weighted average of the rates of the stable rate loans being taken.
• Native credit delegation - through the concept of delegation, users can delegate to other addresses by
opening a credit line. Some interesting tools can be developed on top, for example:
– Borrow from a cold wallet - users can keep their collateral in cold wallets, simply delegating their
credit to a hot wallet to borrow. The whole position can be managed from the hot wallet, with
borrow, repay and add more collateral; while funds are kept under the safety of a cold wallet.
– Credit delegation facilities - users can take undercollateralised loans as long as they receive delegation
from other users that provide collateral.
– Automated yield farming - credit delegation makes it possible to build tools to to automatically open
credit lines to farm yields on multiple protocols.
2. Flash Loans V2. Flash Loans are a disruptive functionality of Aave V1, which allow the creation of a variety
of tools to refinance, collateral swap, arbitrage and liquidate. Flash Loans have become a powerful mechanism
of DeFi. In the initial V1 Flash Loan, a significant limitation is that it cannot be used within Aave. Aave V2
implements a solution to enable Flash Loans usage in combination with any of the other functionalities of the
protocol, powering many new possibilities:
– Collateral trading. Change the exposure from one/multiple collaterals to another, without the need of
closing the debt positions.
– Repay a loan with the collateral. Use collateral deposit in the protocol to repay a debt position.
– Margin trading. Creation of margin positions that can be used for trading later on.
– Debt swap. Change the debt exposure from one asset to another.
– Margin deposits
1
2 Architectural Specifications
The diagram bellow describes the high level architecture of Aave V2:
Some major architectural differences distinguish V2 in Figure 1 from the compartimented structure of V1 in Figure 2:
1. Funds that were previously stored in the LendingPoolCore contract are now stored within each specific aToken.
This gives the protocol better segregation between assets, which favors the implementation of yield farming
aware aTokens.
2. LendingPoolCore and LendingPoolDataProvider have been removed and replaced with libraries. This
reduced the gas footprint of all the actions by 15 to 20% while optimising the code complexity and verbosity.
3. All actions now happen through LendingPool; replacing the previously required redeem() on every single
aToken.
2
Figure 2: Aave V1 Protocol Architecture
3 Features Specifications
3.1 aTokens
V2 aTokens bring the following updates:
• No interest rate redirection in the base aToken implementation - this may be reintroduced in the future
3
In V1, N It was stored as user x index N I(x) after each action, and for a user x, the cumulated aToken balance was
calculated as follows:
P B(x)
CBt (x) = N I(x)t It with P B(x) the principal balance of user x
In V2, the user index factually disappears as a storage variable, and is stored together with the principal balance as
a ratio that is called Scaled Balance, ScB. The balance of the user is calculated leading to increase or decrease on
every action resulting in the mint or burn of aTokens:
• Deposits. When a user deposits an amount m in the protocol, his scaled balance updates as follows:
m
ScBt (x) = ScBt−1 (x) + N It
• Withdrawals. When a user withdraws an amount m in the protocol, his scaled balance updates as follows:
m
ScBt (x) = ScBt−1 (x) − N It
At any point in time, the aToken balance of a user can be written as:
aBt (x) = ScBt (x)N It
V Rt ∆T
V It = (1 + Tyear ) V It−1
V I(x) = V It(x)
With ScV Bt (x) the scaled balance of a user x at time t, m the transaction amount, V Nt the normalised variable
debt:
• Borrows. When a user x borrows an amount m from the protocol the scaled balance updates
4
m
ScV Bt (x) = ScV Bt−1 (x) + V Nt
• Repays/Liquidations. When a user x repays or gets liquidated an amount m the scaled balance updates
m
ScV Bt (x) = ScV Bt−1 (x) − V Nt
At any point in time, the total variable debt balance of a user can be written as:
V D(x) = ScV B(x)Dt
This variable debt token balance replaces the user variable borrow balance of V1, described in section 1.2 of the the
V1 Whitepaper.
SRt is stored in the stable rate token for each specific currency. The stable debt token SD(x) balance for
a user x is defined as follows:
SRt ∆T
SD(x) = SB(x)(1 + Tyear) )
In V1, SR(x), the stable rate of user x, is always equal to the stable rate of the last loan taken, with previous
loans rebalancing upon new loans. From V2, the stable rate SR(x) is calculated per asset reserve across the i
stable loans:
V2 offers a novel protection against reentrancy, allowing to use Flash Loans for all Aave transactions.
Solution
The initial implementation was chosen for a specific ETH codepath; unfortunately ETH does not support pull
payment strategy. In this new version 2, all the ETH-specific code has been removed, resulting in the following
changes:
5
Figure 3: Aave V1 Flash Loan
1. The Flash Loan initiates with the transfer of funds to the Flash Loan executor u,
2. At the end, funds are pulled back from the executor u for an amount equal to the borrowed funds plus
the fee. If pulling the funds does not succeed, the Flash Loan fails; for example, when there is lack of
approval for the funds or lack of funds to cover the debt.
This technically removes the need to snapshot the balances before and after, therefore removing the need for
reentrancy check.
Additionally, Flash Loans now support multiple modes: the Flash Loan caller may decide to keep the loan
open, subject to appropriate liquidity to cover collateral requirements.
– The flashLoan() function is called by the user, passing as parameters the address of a receiver contract,
implementing the IFlashLoanReceiver interface, which is an untrusted contract that should be veri-
fied by the user in advance; a list of underlying assets to swap from, a list of amounts of those assets and
one extra parameter containing the asset to swap to and the max slippage chosen by the user, both encoded.
6
– The receiver contract will then use the funds received to swap them to the destination asset, depositing
again on behalf of the user and withdrawing the user’s deposits in the Flashed-currencies in order to
repay the Flash Loan.
One example would be:
(i) An user has 100 LINK and 20 UNI deposited in the protocol, with a debt of 100 USDC. He wants to
swap both his deposited LINK and UNI to AAVE, without needing to pay back any debt within one
transaction.
(ii) The user calls the flashLoan() function passing as parameters the address of the receiver contract which
contains the logic to do the operation, the addresses of LINK and UNI; 100 and 200 as amounts and,
encoded, the address of the asset to swap to (AAVE) and 2% as maximum slippage on the trades.
(iii) The receiver contract will swap the indicated amounts of LINK and UNI to AAVE, by using a decentralised
exchange.
(iii) The receiver contract will deposit the resultant AAVE on behalf of the user in the Aave Protocol.
(iv) The receiver contract will transfer the equivalent Flashed amount in aLINK and aUNI from the user, will
redeem them to LINK and UNI and will approve the pool to pull those funds at the end of the Flash
Loan to repay.
This functionality is implemented through the function approveDelegation() on each debt token. Users will
be able to draw up to their allowance for the specific debt mode (stable or variable). The borrow() function
7
has an onBehalfOf parameter for the caller to specify the address used to draw credit.
– A delegator can delegate credit to multiple entities but a delegatee can only draw credit from a single
delegator at once. One cannot aggregate the delegators debt in a single borrow()
– A delegator can simultaneously delegate stable and variable credit to an entity, but a delegatee cannot
draw variable and stable credit from a single borrow()
The V2 release, optimises execution costs by approximating this exponential formula with a binomial expansion,
which works well with a small base. The implementation used the following binomial expansion:
The function calculateCompoundedInterest, (MathUtils.sol line 46) implements the first three expan-
sions which gives a good approximation of the compounded interest for up to a 5 year loan duration. This
results in a slight underpayment offset by the gas optimisation benefits.
It’s important to note that this behaves a little differently for variable and stable borrowing:
– For variable borrows, interests are accrued on any action of any borrower;
– For stable borrows, interests are accrued only when a specific borrower performs an action, increasing the
impact of the approximation. Still, the difference seems reasonable given the savings in the cost of the
transaction.
1. For each user, keep a list of assets being used as collateral/borrowed, updated whenever a user de-
posits/withdraws/borrows/repays. When calculating the total system collateral, rather than looping
through all the supported assets, the function calculateUserAccountData() could target specific user’s
assets.
This solution doesn’t present any limitations, though it is more gas intensive - when considering costs of
removing items from the list, checking each aToken balance, stable or variable debt, whether a user is
depositing, borrowing or both...
8
Figure 4: Users Collaterals/Borrows Bitmask Structure
2. Create a bitmask with the structure figure 4. The bitmask has a 256 bit size, it is divided in pairs of
bits, one for each asset. The first bit of the pair indicates if an asset is used as collateral by the user, the
second whether an asset is borrowed by the user. This implementation imposes the constraints:
– Only 128 assets can be supported, to add more, another uint256 needs to be used.
– For the calculation of the account data, the protocol would still need to query all listed assets.
It presents the following advantages compared to a list based solution:
– Additional assets cost only 5k gas, instead of 20k gas for writing on the list
– Extremely cheap verification of a user’s asset borrowing (with 0xAAAAA...!=0) or depositing
(configuration=0)
– Immediate access to what assets are being deposited/borrowed/both - by fetching the aTokens/debt
tokens balances
– In calculateUserAccountData(), the configuration can be cached at the beginning and used to
perform all the calculations, which greatly saves on SLOADs.
Both solutions were implemented and tested, the second solution was preferred since the 128 asset limitation
provides space for growth, and more so in light of Aave’s multimarkets.
9
5 Formal Definitions
Variable Description
Tl , last updated Timestamp of the last update of the reserve data. Tl is updated every time a
timestamp borrow, deposit, redeem, repay, swap or liquidation event occurs.
∆T , delta time ∆T = T − Tl
∆T
∆Tyear , yearly pe- ∆Tyear = Tyear
riod
Lasset
t , total liquidity Total amount of liquidity available in the assets reserve. The decimals of this value
of an asset depend on the decimals of the currency.
SDtasset , total stable Total amount of liquidity borrowed at a stable rate represented in debt tokens.
debt token
V Dtasset , total vari- Total amount of liquidity borrowed at a variable rate represented in debt tokens.
able debt tokens
Dtasset , total debt Total amount of liquidity borrowed. Dtasset = V Dtasset + SDtasset
asset
Uoptimal , target utili- The utilisation rate targeted by asset reserve, beyond the interest rates rise
sation rate sharply.
asset
#Rbase , base bor- Constant for Btasset = 0. V R for variable rate SR
row rate for stable.Expressed in ray.
asset
#Rslope1 , interest Constant representing the scaling of the interest rate versus the utilisation, when
rate slope below U < Uoptimal . V R for variable rate SR for stable. Expressed in ray.
Uoptimal
asset
#Rslope2 , interest Constant representing the scaling of the interest rate versus the utilisation, when
rate slope above U ≥ Uoptimal . V R for variable rate SR for stable. Expressed in ray.
Uoptimal
10
Variable Description
SRtasset , stable rate The rate of stable borrows based on the formula
above. In ray.
P SRi (x)SDi (x)
SRt (x), overall sta- Overall stable rate of an user across i loans SR(x) = i SDi (x)
ble rate of a user x
SDSRt−1 −SB(x)SR(x)
SDt −SB(x) , if SD − SB(x) > 0
LRt , current liquid- Function of the overall borrow rate R and the LRt = Rt Ut
ity rate utilisation rate U .
Interest cumulated by the reserve during the time LIt = (LRt ∆Tyear + 1)LIt−1
LIt , cumulated liq-
interval ∆T , updated whenever a borrow, deposit,
uidity index
repay, redeem, swap, liquidation event occurs. LI0 = 1 × 1027 = 1 ray
N It , reserve nor- Ongoing interest cumulated by the reserve. N It = (LTR ∆Tyear + 1)LIt−1
malised income
V I(x), user cumu- Variable borrow index of the specific user, stored V I(x) = V It(x)
lated variable bor- when a user opens a variable borrow position.
row index
11
Variable Description
P B(x), user princi- Balance stored when a user opens a borrow position. In case of multiple borrows,
pal borrow balance the compounded interest is cumulated each time and it becomes the new principal
borrow balance.
m
For a withdrawal: ScB(x)t = ScBt−1 (x) − N It
12