BalancerAdapter
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 poolBATCH_SWAP
: Perform a series of swaps against one or multiple pools
Structs
GeneralExtraData
Defines the necessary data required for performing deposit-wise & withdrawl-wise actions such as:
- Join pools
- Exit pools
Members
Name | Type | Description |
---|---|---|
poolId | bytes32 | The ID of the target pool |
userData | bytes | ABI encoded specific use case data to pass to the target pool. Check the following for more info: Pool Join userData, Pool Exit userData |
fromToInternalBalance | bool | A 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.
Members
Name | Type | Description |
---|---|---|
poolId | bytes32 | The ID of the target pool |
kind | IVault.SwapKind | The swap kind (GIVEN_IN, or GIVEN_OUT) |
fromInternalBalance | bool | 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. |
toInternalBalance | bool | 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. |
userData | bytes | ABI encoded specific use case data to pass to the target pool |
limit | uint256 | Limit 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 limit GIVEN_OUT : The amount of tokens sent to the pool must be less than or equal to limit |
deadline | uint256 | The 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.
Members
Name | Type | Description |
---|---|---|
swaps | IVault.BatchSwapStep[] | Data for each individual swap executed |
assets | IAsset[] | Array of assets' addresses to be used in the swaps. ETH is specified with the zero address (sentinel value) |
kind | IVault.SwapKind | The swap kind (GIVEN_IN, or GIVEN_OUT) |
fromInternalBalance | bool | If 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. |
toInternalBalance | bool | 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. |
limits | int256[] | 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 limit GIVEN_OUT : The amount of each token sent to the pool must be less than or equal to the repsective specified limit |
deadline | uint256 | The deadline epoch timestamp (in seconds) until which the batch swap can be executed |
nativeTokenAmount | uint256 | The 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.
Members
Name | Type | Description |
---|---|---|
swapOperation | SwapOperation | The action to conduct (SWAP, or BATCH_SWAP) |
extraSwapData | bytes | Additional 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.
Members
Name | Type | Description |
---|---|---|
vault | IVault | The interface representing the Vault contract instance |
nativeToken | address | The address of the wrapped native token address |
State Variables
STORAGE_SLOT
Unique identifier for the storage slot where the Layout struct is stored.
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)
Functions
initialize
Initializes the Adapter.
Parameters
Name | Type | Description |
---|---|---|
initData_ | bytes | The 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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. |
expectedIn | IAdapter.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). |
extraData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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). |
expectedIn | IAdapter.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 |
extraData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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 |
expectedIn | IAdapter.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 |
extraData | bytes | ABI 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.
_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.
_singleSwap
Performs a swap against a single Balancer pool.
Called by _swap function.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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 |
expectedIn | IAdapter.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 |
extraSwapData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
extraSwapData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
poolId | bytes32 | The ID of the target pool |
token | address | The 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.