Nexera-FiAdapters & DeFi Protocols

BaseAdapter

Git Source

Extends: IAdapter

The BaseAdapter abstract contract serves as the foundational blueprint for all Adapters within the Nexera-Fi protocol. It defines a set of essential functionalities and interface requirements that all Adapters must adhere to.

By inheriting this contract, developers ensure that their Adapter implementations meet the required standards and seamlessly integrate with the Nexera-Fi protocol.

Custom Errors

CONSTRAINTS_VALIDATION_FAIL

Raised when the execution of an Action results in spending more than expected or receiving less than expected.

error CONSTRAINTS_VALIDATION_FAIL(
    address token,
    uint256 expectedAmount,
    uint256 actualAmount,
    bool expectedPositive,
    bool actualPositive
);

Parameters

NameTypeDescription
tokenaddressThe address of the token for which the validation constraint failed
expectedAmountuint256The expected amount of the token to be received or spent
actualAmountuint256The actual amount of the token that was received or spent
expectedPositiveboolTrue if expected to receive or spent >= expectedAmount, false otherwise
actualPositiveboolTrue if received or spent >= actualAmount, false otherwise

OPERATION_NOT_SUPPORTED

Raised when the Action to be executed is undefined or not supported by the Adapter.

error OPERATION_NOT_SUPPORTED();

Functions

initialize

Virtual intialization function for Adapters.

Developers should override this function to appropriately initialize their Adapter implementation.

function initialize(bytes calldata initData_) external virtual;

Parameters

NameTypeDescription
initData_bytesABI encoded initialization data

executeAction

Executes an operation-related action on a 3rd party service (DEX, Liquidity Pool, etc).

function executeAction(
    IAdapter.Operation op,
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) external returns(IAdapter.TokenAmount[] memory actualIn);

Parameters

NameTypeDescription
opIAdapter.OperationEnum key representing the operation to execute
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesExtra ABI encoded data specific to the Adapter, required for the operation action

Returns

NameTypeDescription
actualInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions actually received

_swap

Virtual internal swap function for Adapters.

Developers should override this function as follows:

  • If the implementation supports the SWAP operation, override it with code to handle the operation
  • If the implementation does not support the SWAP operation, raise the OPERATION_NOT_SUPPORTED error

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesExtra ABI encoded data specific to the Adapter, required for the swap operation

_deposit

Virtual internal deposit function for Adapters.

Developers should override this function as follows:

  • If the implementation supports the DEPOSIT operation, override it with code to handle the operation
  • If the implementation does not support the DEPOSIT operation, raise the OPERATION_NOT_SUPPORTED error

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array ofTokenAmount structs defining the expected token compositions to receive
extraDatabytesExtra ABI encoded data specific to the Adapter, required for the deposit operation

_withdraw

Virtual internal withdraw function for Adapters.

Developers should override this function as follows:

  • If the implementation supports the WITHDRAW operation, override it with code to handle the operation
  • If the implementation does not support the WITHDRAW operation, raise the OPERATION_NOT_SUPPORTED error

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send (should be empty array)
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesExtra ABI encoded data specific to the Adapter, required for the withdraw operation

_collect

Virtual internal collect function for Adapters.

Developers should override this function as follows:

  • If the implementation supports the COLLECT operation, override it with code to handle the operation
  • If the implementation does not support the COLLECT operation, raise the OPERATION_NOT_SUPPORTED error

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send (should be empty array)
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesExtra ABI encoded data specific to the Adapter, required for the collect operation

_operate

Virtual internal operate function for Adapters.

Developers should override this function as follows:

  • If the implementation supports the OPERATE operation, override it with code to handle the operation
  • If the implementation does not support the OPERATE operation, raise the OPERATION_NOT_SUPPORTED error

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesExtra ABI encoded data specific to the Adapter, required for the operate operation

_getBalances

Private view function used to retrieve token balances of Adapter.

This function is designed to calculate the balances of specified tokens based on the provided out and expectedIn arrays.

Called by executeAction function.

function _getBalances(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn
) private view returns(uint256[] memory balances);

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the output token compositions
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected input token compositions

Returns

NameTypeDescription
balancesuint256[]Array with the Adapter's balances for the provided tokens

_calculateAndValidateBalanceDiff

Private pure function used to calculate and validate token balance differences after an operation.

This function is responsible for calculating the differences in token balances before and after an operation, ensuring that the result meets the expected constraints. It performs validation on both spent and received tokens based on the provided inputs and the operation type.

Called by executeAction function.

function _calculateAndValidateBalanceDiff(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    uint256[] memory balancesBefore,
    uint256[] memory balancesAfter,
    IAdapter.Operation op
) private pure returns(IAdapter.TokenAmount[] memory spent, IAdapter.TokenAmount[] memory received);

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the output token compositions
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected input token compositions
balancesBeforeuint256[]Array of Adapter's initial token balances before the operation
balancesAfteruint256[]Array of Adapter's resulting token balances after the operation
opIAdapter.OperationEnum key representing the targeted operation

Returns

NameTypeDescription
spentIAdapter.TokenAmount[]Array of TokenAmount structs representing the tokens spent during the operation
receivedIAdapter.TokenAmount[]Array of TokenAmount structs representing the tokens received after the operation

_balanceOf

Private view function used to retrieve the Adapter's balance of a specified token.

This function retrieves the balance of either a native token (e.g. Ether) or an ERC-20 token held by the implementation contract.

Called by _getBalances function.

function _balanceOf(address tokenOrNative) private view returns(uint256);

Parameters

NameTypeDescription
tokenOrNativeaddressThe address of the token for which the balance is to be retrieved. address(0) indicates native token

Returns

NameTypeDescription
<none>uint256The balance of the specified token held by the implementation contract

On this page