Nexera-FiAdapters & DeFi ProtocolsBalancer

BalancerAdapter

Git Source

Extends: BaseAdapter

The BalancerAdapter is designed to interact with the Balancer v2 Protocol.

This adapter enables users to harness the power of Balancer's v2 AMM features and integrate them into their financial strategies.

Enums

SwapOperation

Defines the actions available with respect to Swap Operations:

  • SWAP: Perform a swap against a single pool
  • BATCH_SWAP: Perform a series of swaps against one or multiple pools
enum SwapOperation {SWAP, BATCH_SWAP}

Structs

GeneralExtraData

Defines the necessary data required for performing deposit-wise & withdrawl-wise actions such as:

  • Join pools
  • Exit pools
struct GeneralExtraData {
    bytes32 poolId;
    bytes userData;
    bool fromToInternalBalance;
}

Members

NameTypeDescription
poolIdbytes32The ID of the target pool
userDatabytesABI encoded specific use case data to pass to the target pool. Check the following for more info: Pool Join userData, Pool Exit userData
fromToInternalBalanceboolA flag, which in the context of:-deposit: If true, the sender's Internal Balance (Vault's bookkeeping) will be preferred, performing an ERC20 transfer for the difference between the requested amount and the user's Internal Balance (if any); if false, an ERC20 transfer will be performed for the whole requested amount. -withdraw: If true, tokens will be deposited to recipient's Internal Balance (Vault's bookkeeping) instead of transferred; if false, an ERC20 transfer will be performed.

SingleSwapData

Defines the necessary data for invoking the Vault's swap function. It encapsulates the necessary data for performing a swap against a single pool.

Note: This structure serves as the non-encoded operation data within the broader SwapExtraData struct.

struct SingleSwapData {
    bytes32 poolId;
    IVault.SwapKind kind;
    bool fromInternalBalance;
    bool toInternalBalance;
    bytes userData;
    uint256 limit;
    uint256 deadline;
}

Members

NameTypeDescription
poolIdbytes32The ID of the target pool
kindIVault.SwapKindThe swap kind (GIVEN_IN, or GIVEN_OUT)
fromInternalBalanceboolIf true the sender's Internal Balance (Vault's bookkeeping) will be preferred, performing an ERC20 transfer for the difference between the requested amount and the user's Internal Balance (if any). If false, an ERC20 transfer will be performed for the whole requested amount.
toInternalBalanceboolIf true, tokens will be deposited to recipient's Internal Balance (Vault's bookkeeping) instead of transferred; if false, an ERC20 transfer will be performed.
userDatabytesABI encoded specific use case data to pass to the target pool
limituint256Limit for the swap that depends on the swap kind:GIVEN_IN: The amount of tokens taken from the pool must be greater than or equal to limitGIVEN_OUT: The amount of tokens sent to the pool must be less than or equal to limit
deadlineuint256The deadline epoch timestamp (in seconds) until which the swap can be executed

BatchSwapData

Defines the necessary data for invoking the Vault's batchSwap function. It encapsulates the necessary data for performing a series of swaps against one or multiple pools.

Note: This structure serves as the non-encoded operation data within the broader SwapExtraData struct.

struct BatchSwapData {
    IVault.BatchSwapStep[] swaps;
    IAsset[] assets;
    IVault.SwapKind kind;
    bool fromInternalBalance;
    bool toInternalBalance;
    int256[] limits;
    uint256 deadline;
    uint256 nativeTokenAmount;
}

Members

NameTypeDescription
swapsIVault.BatchSwapStep[]Data for each individual swap executed
assetsIAsset[]Array of assets' addresses to be used in the swaps. ETH is specified with the zero address (sentinel value)
kindIVault.SwapKindThe swap kind (GIVEN_IN, or GIVEN_OUT)
fromInternalBalanceboolIf true the sender's Internal Balance (Vault's bookkeeping) will be preferred, performing ERC20 transfers for the difference between the requested amount and the user's Internal Balance (if any). If false, an ERC20 transfer will be performed for the whole requested amount.
toInternalBalanceboolIf true, tokens will be deposited to recipient's Internal Balance (Vault's bookkeeping) instead of transferred; if false, an ERC20 transfer will be performed.
limitsint256[]Array of limits for each token involved in the batch swap. In the context of swap kind:GIVEN_IN: The amount for each token taken from the pool must be greater than or equal to the respective specified limitGIVEN_OUT: The amount of each token sent to the pool must be less than or equal to the repsective specified limit
deadlineuint256The deadline epoch timestamp (in seconds) until which the batch swap can be executed
nativeTokenAmountuint256The amount of native token to be sent to the poolNote: Pools never interact with ETH directly; it will be wrapped to, or unwrapped from WETH by the Vault

SwapExtraData

Used to indicate the appropriate action of a Swap Operation.

struct SwapExtraData {
    SwapOperation swapOperation;
    bytes extraSwapData;
}

Members

NameTypeDescription
swapOperationSwapOperationThe action to conduct (SWAP, or BATCH_SWAP)
extraSwapDatabytesAdditional ABI encoded data related to the operation ( a , b )

Layout

Used to store crucial data for the Adapter's functionality in a specific storage slot.

struct Layout {
    IVault vault;
    address nativeToken;
}

Members

NameTypeDescription
vaultIVaultThe interface representing the Vault contract instance
nativeTokenaddressThe address of the wrapped native token address

State Variables

STORAGE_SLOT

Unique identifier for the storage slot where the Layout struct is stored.

bytes32 internal immutable STORAGE_SLOT;

Constructor

Sets the value of the STORAGE_SLOT state variable.

The value is computed as the keccak256 hash of the abi encoding of:

  • The Nexera-FI.BalancerAdapter string literal
  • The address of this contract (BalancerAdapter)
constructor();

Functions

initialize

Initializes the Adapter.

function initialize(bytes calldata initData_) external override;

Parameters

NameTypeDescription
initData_bytesThe ABI encoded Layout data

_deposit

Internal function for handling Deposit Operations.

This function is responsible for depositing specified assets into a target Balancer pool.

Called by executeAction function.

function _deposit(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal override;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. The array must contain the tokens (addresss & amount fields) to be deposited in the order of the target pool tokens.
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive (BPT token). The array's length must equal 1 and contain the address of the target pool's BPT token (the amount field is ignored).
extraDatabytesABI encoded data of type GeneralExtraData required for the action.

_withdraw

Internal function for handling Withdraw Operations.

This function is responsible for withdrawing specified assets from a target Balancer pool.

Called by executeAction function.

function _withdraw(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal override;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. The array's length must equal 1 and contain the address of the target pool's BPT token (the amount field is ignored).
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. The array must contain the tokens (addresss & amount fields) to be withdrawn in the order of the target pool tokens.Note: amount fields must be 0 if toInternalBalance == true
extraDatabytesABI encoded data of type GeneralExtraData required for the action.

_swap

Internal function for handling Swap Operations.

This function facilitates the swapping (either SWAP, or BATCH_SWAP) of assets against a target Balancer pool.

Called by executeAction function.

function _swap(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal virtual override;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. In the context of: swap: The array's length must equal 1 and contain the token (address & amount field) to be provided to the pool. batch swap: The out parameter is ignored as all data needed are included in the extraData parameterNote: amount fields must be 0 if toInternalBalance == true
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. In the context of: swap: The array's length must equal 1 and contain the token (address & amount field) to be received from the pool. batch swap: The out parameter is ignored as all data needed are included in the extraData parameterNote: amount fields must be 0 if toInternalBalance == true
extraDatabytesABI encoded data of type SwapExtraData required for the action.

_operate

This function always raises the OPERATION_NOT_SUPPORTED error, as Operate is not an action supported within the Balancer's Vault.

Called by executeAction function.

function _operate(
    IAdapter.TokenAmount[] calldata,
    IAdapter.TokenAmount[] calldata,
    bytes calldata
) internal virtual override;

_collect

This function always raises the OPERATION_NOT_SUPPORTED error, as Collect is not an action supported within the Balancer's Vault.

Called by executeAction function.

function _collect(
    IAdapter.TokenAmount[] calldata,
    IAdapter.TokenAmount[] calldata,
    bytes calldata
) internal virtual override;

_singleSwap

Performs a swap against a single Balancer pool.

Called by _swap function.

function _singleSwap(
    IAdapter.TokenAmount calldata out,
    IAdapter.TokenAmount calldata expectedIn,
    bytes memory extraSwapData
) internal virtual;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send.The array's length must equal 1 and contain the token (address & amount field) to be provided to the pool
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive.The array's length must equal 1 and contain the token (address & amount field) to be received from the pool
extraSwapDatabytesABI encoded data of type SingleSwapData.Note: The repsective amount fields must be 0 within out and/or expectedIn if: fromInternalBalance == true and/or toInternalBalance == true respectively.

_batchSwap

Performs a series of swaps against one or multiple Balancer pools.

Called by _swap function.

function _batchSwap(
    bytes memory extraSwapData
) internal virtual;

Parameters

NameTypeDescription
extraSwapDatabytesABI encoded data of type BatchSwapData that encapsulates all the required data

_validatePool

Internal helper function to validate that the specified token corresponds to the BPT (Balancer Pool Token) of the target pool specified by poolId. It reverts if the validation fails.

It invokes the Vault's getPool method to retrieve the address of the target pool's BPT to validate against.

Called by _deposit and _withdraw functions.

function _validatePool(bytes32 poolId, address token) internal view;

Parameters

NameTypeDescription
poolIdbytes32The ID of the target pool
tokenaddressThe token to be validated as the BPT of the target pool

layout

Retrieves a reference to the Layout struct stored at a specified storage slot.

function layout() internal view returns (Layout storage l);

On this page