Nexera-FiAdapters & DeFi ProtocolsUniswap

UniswapAdapter

Git Source

Extends: BaseAdapter

The UniswapAdapter is designed to interact with the Uniswap V3 Protocol.

This adapter enables users to harness the power of Uniswap v3 trading capabilities and integrate them into their financial strategies.

Structs

DepositExtraData

Used to provide the necessary data when performing a Deposit operation.

struct DepositExtraData {
    uint256 tokenId;
    uint24 fee;
    int24 tickLower;
    int24 tickUpper;
}

Members

NameTypeDescription
tokenIduint256The ID of the target NFT LP token
feeuint24The fee tier for the provided liquidity
tickLowerint24The value of the lower tick threshold
tickUpperint24The value of the upper tick threshold

WithdrawExtraData

Used to provide the necessary data when performing a Withdraw operation.

struct WithdrawExtraData {
    uint256 tokenId;
    uint128 percentageToWithdraw;
}

Members

NameTypeDescription
tokenIduint256The ID of the target NFT LP token
percentageToWithdrawuint128The percentage to withdraw (should be parsed with 4 decimal units)

SwapExtraData

Used to provide the necessary data when performing a Swap operation.

struct SwapExtraData {
    uint24 fee;
    uint160 sqrtPriceLimitX96;
}

Members

NameTypeDescription
feeuint24The fee tier for the swap operation (determines the associated fees)
sqrtPriceLimitX96uint160The square root of the price limit for the swap operation (helps control the price range for the swap)

InitializeData

Defines the necessary data for initializing the Adapter.

struct InitializeData {
    INonfungiblePositionManager nonfungiblePositionManager;
    ISwapRouter swapRouter;
    address nativeToken;
}

Members

NameTypeDescription
nonfungiblePositionManagerINonfungiblePositionManagerThe interface representing the Uniswap NonfungiblePositionManager contract instance
swapRouterISwapRouterThe interface representing the Uniswap SwapRouter contract instance
nativeTokenaddressThe address of the wrapped native token address

Layout

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

struct Layout {
    INonfungiblePositionManager nonfungiblePositionManager;
    ISwapRouter swapRouter;
 
    address nativeToken;
}

Members

NameTypeDescription
nonfungiblePositionManagerINonfungiblePositionManagerThe interface representing the Uniswap NonfungiblePositionManager contract instance
swapRouterISwapRouterThe interface representing the Uniswap SwapRouter 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;

PERCENTAGE

Constant value representing 100% for percentages.

Arguments that refer to percentages are formatted with respect to PERCENTAGE value.

uint128 private constant PERCENTAGE = 10000;

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.UniswapAdapter string literal
  • The address of this contract (UniswapAdapter)
constructor();

Functions

initialize

Initializes the Adapter.

function initialize(bytes calldata initData_) external override;

Parameters

NameTypeDescription
initData_bytesThe ABI encoded InitializeData

_deposit

Internal function for handling Deposit Operations.

This function is responsible for either minting a new NFT LP token or adding liquidity to an existing NFT LP token via interacting with Uniswap's:

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
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesABI encoded data of type DepositExtraData

_withdraw

Internal function for handling Withdraw Operations.

This function is responsible for facilitating withdrawals of liquidity from a NFT LP position. It processes the specified output tokens and verifies the expected input tokens, ensuring accurate and secure withdrawal operations.

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
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesABI encoded data of type WithdrawExtraData

_decreaseLiquidity

Internal helper function for decreasing liquidity for a specific NFT LP position (Withdraw Operation).

This function internally interacts with Uniswap's:

Called by _withdraw function.

function _decreaseLiquidity(
    WithdrawExtraData memory withdrawExtraData,
    IAdapter.TokenAmount[] calldata expectedIn,
    uint128 liquidity
) internal returns (uint256, uint256);

Parameters

NameTypeDescription
withdrawExtraDataWithdrawExtraDataStruct of type WithdrawExtraData with the necessary data (NFT LP tokenId & percentage to withdraw)
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
liquidityuint128The liquidity of the position associated with the specified NFT LP token

Returns

NameTypeDescription
<none>uint256The amount of 1st token (token0) received after the decrease in liquidity
<none>uint256The amount of 2nd token (token1) received after the decrease in liquidity

_swap

Internal function for handling Swap Operations.

This function is responsible for executing token swaps with specified trade settings via interacting with Uniswap's:

Called by executeAction function.

function _swap(
    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
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
extraDatabytesABI encoded data of type SwapExtraData

_collect

Internal function for handling Collect Operations.

This function is responsible for collecting fees owed to a specific position associated with an NFT LP token via interacting with Uniswap's:

Called by executeAction function.

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

Parameters

NameTypeDescription
<none>IAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. Not used in this context.
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. Members refer to the repsecitve maximum amount of the tokens (token0 & token1) that is expected to be collected from the position.
extraDatabytesABI encoded data of type uint256, representing the unique identifier (tokenId) of the NFT LP position from which to collect fees from

_operate

This function always raises the OPERATION_NOT_SUPPORTED error, as Operate is not an action supported within the Uniswap v3 protocol.

Called by executeAction function.

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

_checkAddresses

Internal helper function used for address validation within Withdraw and Collect Operations.

This function interacts with Uniswap's:

Called by _withdraw and _collect functions.

function _checkAddresses(uint256 tokenId, IAdapter.TokenAmount[] memory expectedIn) internal view;

Parameters

NameTypeDescription
tokenIduint256The unique identifier of the NFT LP token position
expectedInAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive

_requireValidInAndOutForSwap

Checks the validity of input (expectedIn) and output (out) parameters for a Swap operation.

It ensures that there is only one token to send and one token to receive, and that the token addresses are not invalid.

Called by _swap function.

function _requireValidInAndOutForSwap(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn
) private pure;

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

_requireValidInAndOutForDepositAndWithdraw

Checks the validity of input (expectedIn) and output (out) parameters for both Deposit and Withdraw operations.

It ensures that there are two token compositions to send and receive, and it verifies that the token addresses are valid and consistent between the input and output.

Called by _deposit and _withdraw functions.

function _requireValidInAndOutForDepositAndWithdraw(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn
) private pure;

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

layout

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

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