Adapters
Supported adapters
Aave V2

Aave adapters

There are two adapters related to Aave: the AaveV2 lending pool adapter and wrapped Aave token adapter.

Lending pool adapter

The lending pool adapter allows Credit Accounts to deposit underlyings into Aave V2 to receive aTokens, and vice versa. The lending pool is a single contract that processes deposits and withdrawals in multiple assets, and, consequently, there is only one adapter.

State-changing functions

deposit

Deposits the specified amount of asset to receive the corresponding aToken.

function deposit(address asset, uint256 amount, address, uint16)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
assetAddress of underlying token to deposit.
amountAmount of asset to deposit.

Parameter onBehalfOf is ignored, as it is always set to the Credit Account. referralCode is always set to 0.

Usage:

import {IAaveV2_LendingPoolAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_LendingPoolAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveLendingPoolAdapter,
    callData: abi.encodeCall(IAaveV2_LendingPoolAdapter.deposit, (asset, amount, address(0), 0))
});
 
creditFacade.multicall(calls);

depositDiff

Deposits the difference between the current balance and leftoverAmount to receive the corresponding aToken. If the balance is less than leftoverAmount, does nothing.

function depositDiff(address asset, uint256 leftoverAmount)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
assetAddress of underlying token to deposit.
leftoverAmountAmount of asset to keep on the account.

Usage:

import {IAaveV2_LendingPoolAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_LendingPoolAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveLendingPoolAdapter,
    callData: abi.encodeCall(IAaveV2_LendingPoolAdapter.depositDiff, (asset, leftoverAmount))
});
 
creditFacade.multicall(calls);

withdraw

Burns the specified amount of the corresponding aToken to withdraw asset.

function withdraw(address asset, uint256 amount, address)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
assetAddress of underlying to withdraw.
amountAmount of aTokens to burn.

recipient is ignored, since it's always set to the Credit Account.

Usage:

import {IAaveV2_LendingPoolAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_LendingPoolAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveLendingPoolAdapter,
    callData: abi.encodeCall(IAaveV2_LendingPoolAdapter.withdraw, (asset, amount, address(0)))
});
 
creditFacade.multicall(calls);

withdrawDiff

Burns the difference between the current balance of the corresponding aToken and leftoverAmount, to withdraw asset. If the balance is less than leftoverAmount, does nothing.

function withdrawDiff(address asset, uint256 leftoverAmount)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
assetAddress of underlying to withdraw.
leftoverAmountAmount of aToken to keep on the account.

Usage:

import {IAaveV2_LendingPoolAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_LendingPoolAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveLendingPoolAdapter,
    callData: abi.encodeCall(IAaveV2_LendingPoolAdapter.withdrawDiff, (asset, leftoverAmount))
});
 
creditFacade.multicall(calls);

Wrapped Aave token adapter

Since Gearbox V3 pools do not support rebasing tokens, aTokens need to be wrapped first, in order to serve as the pool's underlying. For that purpose, Gearbox V3 deploys its own custom wrapper tokens and provides an adapter for them, in order to unwrap waTokens into aToken/underlying and use them with other DeFi protocols. There is one waToken per aToken, and one adapter per waToken.

State-changing functions

deposit

Deposits a specified amount of aToken to receive wrapped aToken.

function deposit(uint256 assets)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
assetsAmount of aToken to deposit.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.deposit, (assets))
});
 
creditFacade.multicall(calls);

depositDiff

Deposits the difference between the current balance of aToken and leftoverAssets, to receive wrapped aTokens. If the balance is less than leftoverAmount, does nothing.

function depositDiff(uint256 leftoverAssets)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
leftoverAssetsAmount of aToken to keep on the account.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.depositDiff, (leftoverAssets))
});
 
creditFacade.multicall(calls);

depositUnderlying

Deposits a specified amount of underlying to receive wrapped aToken.

function depositUnderlying(uint256 assets)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
assetsAmount of underlying to deposit.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.depositUnderlying, (assets))
});
 
creditFacade.multicall(calls);

depositDiffUnderlying

Deposits the difference between the current balance of underlying and leftoverAssets, to receive wrapped aTokens. If the balance is less than leftoverAmount, does nothing.

function depositDiffUnderlying(uint256 leftoverAssets)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
leftoverAssetsAmount of underlying to keep on the account.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.depositDiffUnderlying, (leftoverAssets))
});
 
creditFacade.multicall(calls);

withdraw

Burns a specified amount of waToken to receive aToken.

function withdraw(uint256 shares)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
sharesAmount of waToken to burn.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.withdraw, (shares))
});
 
creditFacade.multicall(calls);

withdrawDiff

Burns the difference between the current balance of waToken and leftoverShares, to receive aToken. If the balance is less than leftoverAmount, does nothing.

function withdrawDiff(uint256 leftoverShares)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
leftoverSharesAmount of waToken to burn.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.withdrawDiff, (leftoverShares))
});
 
creditFacade.multicall(calls);

withdrawUnderlying

Burns a specified amount of waToken to receive the underlying.

function withdrawUnderlying(uint256 shares)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
sharesAmount of waToken to burn.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.withdrawUnderlying, (shares))
});
 
creditFacade.multicall(calls);

withdrawDiffUnderlying

Burns the difference between the current balance of waToken and leftoverShares, to receive underlying. If the balance is less than leftoverAmount, does nothing.

function withdrawDiffUnderlying(uint256 leftoverShares)
    external
    returns (uint256 tokensToEnable, uint256 tokensToDisable);
ParameterDescription
leftoverSharesAmount of waToken to burn.

Usage:

import {IAaveV2_WrappedATokenAdapter} from "@gearbox-protocol/integrations-v3/contracts/interfaces/aave/IAaveV2_WrappedATokenAdapter.sol";
 
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = MultiCall({
    target: aaveWrappedTokenAdapter,
    callData: abi.encodeCall(IAaveV2_WrappedATokenAdapter.withdrawDiffUnderlying, (leftoverShares))
});
 
creditFacade.multicall(calls);

AaveV2 Multicaller libraries

AaveV2 adapters have a corresponding library that simplifies the MultiCall building API.

Usage example for the lending pool adapter:

import {AaveV2_LendingPoolMulticaller, AaveV2_LendingPoolCalls} from "@gearbox-protocol/integrations-v3/test/multicall/aave/AaveV2_LendingPoolCalls.sol";
...
 
using AaveV2_LendingPoolCalls for AaveV2_LendingPoolMulticaller;
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = AaveV2_LendingPoolMulticaller(aaveLendingPoolAdapter).withdrawDiff(asset, leftoverAmount);
 
creditFacade.multicall(calls);

Usage example for the waToken adapter:

import {AaveV2_WrappedATokenMulticaller, AaveV2_WrappedATokenCalls} from "@gearbox-protocol/integrations-v3/test/multicall/aave/AaveV2_WrappedATokenCalls.sol";
...
 
using AaveV2_WrappedATokenCalls for AaveV2_WrappedATokenMulticaller;
...
 
MultiCall[] memory calls = new MultiCall[](1);
calls[0] = AaveV2_WrappedATokenMulticaller(aaveWrappedTokenAdapter).deposit(assets);
 
creditFacade.multicall(calls);