Skip to main content

AavePooledRouter

AavePooledRouter​

maker contracts deposit/withdraw their user(s) fund(s) on this router, which maintains an accounting of shares attributed to each depositor

_deposit is made via pushAndSupply, and withdraw is made via pull with strict=true. this router ensures an optimal gas cost complexity when the following strategy is used:

  • on the offer logic side:
    • in makerExecute, check whether logic is the first caller to the router. This is done by checking whether the balance of outbound tokens of the router is below the required amount. If so the logic should return a special bytes32 (say "firstCaller") to makerPosthook.
    • in __put__ the logic stores incoming liquidity on the strat balance
    • in __get__ the logic pulls liquidity from the router in a non strict manner
    • in posthookSuccess|Fallback the logic pushes both inbound and outbound tokens to the router. If message from makerExecute is "firstCaller", the logic additionally asks the router to supply all its outbound and inbound tokens to AAVE. This can be done is a single step by calling pushAndSupply
  • on the router side:
    • __pull__ checks whether local balance of token is below required amount. If so it pulls all its funds from AAVE (this includes funds that do not belong to the owner of the calling contract) and sends to caller all the owner's reserve (according to the shares attributed to the owner - except in case of liquidity sharing where only requested amount is transferred). This router then decreases owner's shares accordingly. (note that if AAVE has no liquidity crisis, then the owner's shares will be temporarily 0)
    • __push__ transfers the requested amount of tokens from the calling maker contract and increases owner's shares, but does not supply on AAVE_

aaveManager​

address aaveManager

the manager which controls which pools are allowed.

SetAaveManager​

event SetAaveManager(address manager)

The aaveManager has been set. By emitting this data, an indexer will be able to keep track of what manager is used.

Parameters​

NameTypeDescription
manageraddressthe new manager.

AaveIncident​

event AaveIncident(contract IERC20 token, address maker, address reserveId, bytes32 aaveReason)

An error occurred during deposit to AAVE. By emitting this data, an indexer will be able to keep track of what incidents that has happened.

Parameters​

NameTypeDescription
tokencontract IERC20the deposited token. This is indexed so that RPC calls can filter on it.
makeraddressthe maker contract that was calling pushAndSupply. This is indexed so that RPC calls can filter on it.
reserveIdaddressthe reserve identifier that was calling pushAndSupply. This is indexed so that RPC calls can filter on it.
aaveReasonbytes32the reason from AAVE.

_totalShares​

mapping(contract IERC20 => uint256) _totalShares

the total shares for each token, i.e. the total shares one would need to possess in order to claim the entire pool of tokens.

_sharesOf​

mapping(contract IERC20 => mapping(address => uint256)) _sharesOf

the number of shares for a reserve for a token, i.e. the shares of this router that are attributed to a particular reserve.

OFFSET​

uint256 OFFSET

offset for initial shares to be minted

this amount must be big enough to avoid minting 0 shares via "donation" see https://github.com/code-423n4/2022-09-y2k-finance-findings/issues/449 mitigation proposed here: https://ethereum-magicians.org/t/address-eip-4626-inflation-attacks-with-virtual-shares-and-assets/12677

INIT_MINT​

uint256 INIT_MINT

initial shares to be minted

constructor​

constructor(address addressesProvider) public

contract's constructor

Parameters​

NameTypeDescription
addressesProvideraddressaddress of AAVE's address provider

sharesOf​

function sharesOf(contract IERC20 token, address reserveId) public view returns (uint256 shares)

returns the shares of this router that are attributed to a particular reserve

sharesOf(token,id)/totalShares(token) represent the portion of this contract's balance of tokens that the reserveId can claim

Parameters​

NameTypeDescription
tokencontract IERC20the address of the asset
reserveIdaddressthe reserve identifier

Return Values​

NameTypeDescription
sharesuint256the amount of shares attributed to reserveId.

totalShares​

function totalShares(contract IERC20 token) public view returns (uint256 total)

returns the total shares one would need to possess in order to claim the entire pool of tokens

Parameters​

NameTypeDescription
tokencontract IERC20the address of the asset

Return Values​

NameTypeDescription
totaluint256the total amount of shares

totalBalance​

function totalBalance(contract IERC20 token) external view returns (uint256 balance)

theoretically available funds to this router either in overlying or in tokens (part of it may not be redeemable from AAVE)

this function relies on the AAVE promise that aToken are in one-to-one correspondence with claimable underlying and use the same decimals

Parameters​

NameTypeDescription
tokencontract IERC20the asset whose balance is required

Return Values​

NameTypeDescription
balanceuint256of the asset

_totalBalance​

function _totalBalance(contract IERC20 token, struct HasAaveBalanceMemoizer.BalanceMemoizer memoizer) internal view returns (uint256 balance)

totalBalance with memoization of balance queries

Parameters​

NameTypeDescription
tokencontract IERC20the asset whose balance is required
memoizerstruct HasAaveBalanceMemoizer.BalanceMemoizerthe memoizer

Return Values​

NameTypeDescription
balanceuint256of the asset

balanceOfReserve​

function balanceOfReserve(contract IERC20 token, address reserveId) public view returns (uint256)

computes available funds (modulo available liquidity on AAVE) for a given reserve

Parameters​

NameTypeDescription
tokencontract IERC20the asset one wants to know the balance of
reserveIdaddressthe identifier of the reserve whose balance is queried

Return Values​

NameTypeDescription
[0]uint256available funds for the reserve

_balanceOfReserve​

function _balanceOfReserve(contract IERC20 token, address reserveId, struct HasAaveBalanceMemoizer.BalanceMemoizer memoizer) internal view returns (uint256 balance)

balanceOfReserve with memoization of balance queries

Parameters​

NameTypeDescription
tokencontract IERC20the asset one wants to know the balance of
reserveIdaddressthe identifier of the reserve whose balance is queried
memoizerstruct HasAaveBalanceMemoizer.BalanceMemoizerthe memoizer

Return Values​

NameTypeDescription
balanceuint256available funds for the reserve

_sharesOfAmount​

function _sharesOfAmount(contract IERC20 token, uint256 amount, struct HasAaveBalanceMemoizer.BalanceMemoizer memoizer) internal view returns (uint256 shares)

computes how many shares an amount of tokens represents

Parameters​

NameTypeDescription
tokencontract IERC20the address of the asset
amountuint256of tokens
memoizerstruct HasAaveBalanceMemoizer.BalanceMemoizerthe memoizer

Return Values​

NameTypeDescription
sharesuint256the shares that correspond to amount

_mintShares​

function _mintShares(contract IERC20 token, address reserveId, uint256 amount, struct HasAaveBalanceMemoizer.BalanceMemoizer memoizer) internal

mints a certain quantity of shares for a given asset and assigns them to a reserve

Parameters​

NameTypeDescription
tokencontract IERC20the address of the asset
reserveIdaddressthe address of the reserve who will be assigned new shares
amountuint256the amount of assets added to the reserve
memoizerstruct HasAaveBalanceMemoizer.BalanceMemoizerthe memoizer

_burnShares​

function _burnShares(contract IERC20 token, address reserveId, uint256 amount, struct HasAaveBalanceMemoizer.BalanceMemoizer memoizer) internal

burns a certain quantity of reserve's shares for a given asset

if one is trying to burn shares from a pool that doesn't have any, the call to _sharesOfAmount will return INIT_MINT and thus this contract will throw with "AavePooledRouter/insufficientFunds", even if one is trying to burn 0 shares.

Parameters​

NameTypeDescription
tokencontract IERC20the address of the asset
reserveIdaddressthe address of the reserve who will have shares burnt
amountuint256the amount of assets withdrawn from reserve
memoizerstruct HasAaveBalanceMemoizer.BalanceMemoizerthe memoizer

push​

function __push__(contract IERC20 token, address reserveId, uint256 amount) internal returns (uint256)

Deposit funds on this router from the calling maker contract

no transfer to AAVE is done at that moment.

Parameters​

NameTypeDescription
tokencontract IERC20Token to be transferred
reserveIdaddressdetermines the location of the reserve (router implementation dependent).
amountuint256The amount of tokens to be transferred

Return Values​

NameTypeDescription
[0]uint256

flushBuffer​

function flushBuffer(contract IERC20 token, bool noRevert) public returns (bytes32 reason)

deposit router-local balance of an asset on the AAVE pool

Parameters​

NameTypeDescription
tokencontract IERC20the address of the asset
noRevertbooldoes not revert if supplies throws

Return Values​

NameTypeDescription
reasonbytes32for revert from Aave.

pushAndSupply​

function pushAndSupply(contract IERC20 token0, uint256 amount0, contract IERC20 token1, uint256 amount1, address reserveId) external returns (uint256 pushed0, uint256 pushed1)

pushes each given token from the calling maker contract to this router, then supplies the whole router-local balance to AAVE

an offer logic should call this instead of flush when it is the last posthook to be executed this can be determined by checking during lastLook whether the logic will trigger a withdraw from AAVE (this is the case if router's balance of token is empty) this call be performed even for tokens with 0 amount for the offer logic, since the logic can be the first in a chain and router needs to flush all this function is also to be used when user deposits funds on the maker contract

Parameters​

NameTypeDescription
token0contract IERC20the first token to deposit
amount0uint256the amount of token0 to deposit
token1contract IERC20the second token to deposit
amount1uint256the amount of token1 to deposit
reserveIdaddressthe reserve whose shares should be increased

Return Values​

NameTypeDescription
pushed0uint256the amount of token0 that were successfully pushed
pushed1uint256the amount of token1 that were successfully pushed

pull​

function __pull__(contract IERC20 token, address reserveId, uint256 amount, bool strict) internal returns (uint256)

router-dependent implementation of the pull function

outside a market order (i.e if __pull__ is not called during offer logic's execution) the token balance of this router should be empty. This may not be the case when a "donation" occurred to this contract or if the maker posthook failed to push funds back to AAVE If the donation is large enough to cover the pull request we use the donation funds if strict is not true and when several strats share the same RESERVE_ID, there is a risk that if one of the strat reverts in posthook, it will not deposit funds back onto the router which would make the other strats sharing the RESERVE_ID fail to deliver.

Parameters​

NameTypeDescription
tokencontract IERC20Token to be transferred
reserveIdaddressdetermines the location of the reserve (router implementation dependent).
amountuint256The amount of tokens to be transferred
strictboolwether the caller maker contract wishes to pull at most amount tokens of owner.

Return Values​

NameTypeDescription
[0]uint256pulled The amount pulled if successful; otherwise, 0.

redeemAndTransfer​

function redeemAndTransfer(contract IERC20 token, address reserveId, uint256 amountToTransfer, uint256 amountToRedeem, struct HasAaveBalanceMemoizer.BalanceMemoizer memoizer) internal

redeems some funds from AAVE pool and transfer some amount to msg.sender.

Parameters​

NameTypeDescription
tokencontract IERC20the asset to transfer
reserveIdaddressthe shares on which funds are being drawn
amountToTransferuint256final amount of asset to transfer
amountToRedeemuint256funds that need to be pulled from AAVE for final transfer to succeed
memoizerstruct HasAaveBalanceMemoizer.BalanceMemoizerthe memoizer

withdraw​

function withdraw(contract IERC20 token, address reserveId, uint256 amount) external

withdraw funds from the pool on behalf of some reserve id

Parameters​

NameTypeDescription
tokencontract IERC20the asset to withdraw
reserveIdaddressthe identifier of the share holder
amountuint256the amount to withdraw. Use type(uint).max to require withdrawal of the total balance of the caller

checkList​

function __checkList__(contract IERC20 token, address reserveId) internal view

router-dependent additional checks

Parameters​

NameTypeDescription
tokencontract IERC20is the asset (and possibly its overlyings) whose approval must be checked
reserveIdaddressof the tokens that are being pulled

activate​

function __activate__(contract IERC20 token) internal virtual

router-dependent implementation of the activate function

Parameters​

NameTypeDescription
tokencontract IERC20the asset one wishes to use the router for

revokeLenderApproval​

function revokeLenderApproval(contract IERC20 token) external

revokes pool approval for a certain asset. This router will no longer be able to deposit on AAVE Pool

Parameters​

NameTypeDescription
tokencontract IERC20the address of the asset whose approval must be revoked.

exitMarket​

function exitMarket(contract IERC20 token) external

prevents AAVE from using a certain asset as collateral for lending

Parameters​

NameTypeDescription
tokencontract IERC20the asset address

enterMarket​

function enterMarket(contract IERC20[] tokens) external

re-allows AAVE to use certain assets as collateral for lending

market is automatically entered at first deposit

Parameters​

NameTypeDescription
tokenscontract IERC20[]the asset addresses

claimRewards​

function claimRewards(address[] assets) external returns (address[] rewardList, uint256[] claimedAmounts)

allows AAVE manager to claim the rewards attributed to this router by AAVE

if some rewards are eligible they are sent to aaveManager

Parameters​

NameTypeDescription
assetsaddress[]the list of overlyings (aToken, debtToken) whose rewards should be claimed

Return Values​

NameTypeDescription
rewardListaddress[]the addresses of the claimed rewards
claimedAmountsuint256[]the amount of claimed rewards

setAaveManager​

function setAaveManager(address aaveManager_) public

sets a new AAVE manager

if any reward is active for pure lenders, aaveManager will be able to claim them

Parameters​

NameTypeDescription
aaveManager_addressthe new address of the AAVE manager