TokenSwapsModule

Git Source

Inherits: ITokenSwapsModule

Author: EridianAlpha

This contract contains the functions for AavePM to swap tokens using UniswapV3.

State Variables

VERSION

The version of the contract.

Contract is upgradeable so the version is a constant set on each implementation contract.

string public constant VERSION = "0.0.1";

aavePMProxyAddress

The address of the AavePM proxy contract.

The AavePM proxy address is set on deployment and is immutable.

address public immutable aavePMProxyAddress;

Functions

constructor

Contract constructor to set the AavePM proxy address.

The AavePM proxy address is set on deployment and is immutable.

constructor(address _aavePMProxyAddress);

Parameters

NameTypeDescription
_aavePMProxyAddressaddressThe address of the AavePM proxy contract.

onlyAavePM

Modifier to check that only the AavePM contract is the caller.

Uses address(this) since this contract is called by the AavePM contract using delegatecall.

modifier onlyAavePM();

swapTokens

Swaps the entire specified token balance of the contract using a UniswapV3 pool.

Calculates the minimum amount that should be received based on the current pool price ratio and a predefined slippage tolerance. Reverts if there are no tokens in the contract or if the transaction does not meet the amountOutMinimum criteria due to price movements.

function swapTokens(
    string memory _uniswapV3PoolIdentifier,
    string memory _tokenInIdentifier,
    string memory _tokenOutIdentifier
) public onlyAavePM returns (string memory tokenOutIdentifier, uint256 amountOut);

Parameters

NameTypeDescription
_uniswapV3PoolIdentifierstringThe identifier of the UniswapV3 pool to use for the swap.
_tokenInIdentifierstringThe identifier of the token to swap.
_tokenOutIdentifierstringThe identifier of the token to receive from the swap.

Returns

NameTypeDescription
tokenOutIdentifierstringThe identifier of the token received from the swap.
amountOutuint256The amount tokens received from the swap.

approveAndExecuteSwap

Approves and executes the swap using the UniswapV3 router.

Approves the swapRouter to spend the tokenIn and executes the swap.

function approveAndExecuteSwap(
    IAavePM aavePM,
    IV3SwapRouter.ExactInputSingleParams memory params,
    uint256 currentBalance
) private returns (uint256 amountOut);

Parameters

NameTypeDescription
aavePMIAavePMThe Aave Position Manager contract.
paramsIV3SwapRouter.ExactInputSingleParamsThe swap parameters.
currentBalanceuint256The current balance of the token to swap.

Returns

NameTypeDescription
amountOutuint256The amount of tokens received from the swap.

wrapETHToWETH

Wraps all ETH in the contract to WETH.

Wraps all ETH in the contract to WETH even if the amount is 0.

function wrapETHToWETH() public payable onlyAavePM;

uniswapV3CalculateMinOut

Calculates the minimum amount of tokens to receive from a UniswapV3 swap.

Uses the current pool price ratio and a predefined slippage tolerance to calculate the minimum amount.

function uniswapV3CalculateMinOut(
    IAavePM aavePM,
    uint256 _currentBalance,
    address _uniswapV3PoolAddress,
    address tokenOutAddress
) private view returns (uint256 minOut);

Parameters

NameTypeDescription
aavePMIAavePMThe Aave Position Manager contract.
_currentBalanceuint256The current balance of the token to swap.
_uniswapV3PoolAddressaddressThe address of the UniswapV3 pool to use for the swap.
tokenOutAddressaddressThe address of the token to receive from the swap.

Returns

NameTypeDescription
minOutuint256The minimum amount of tokens to receive from the swap.

_isIdentifierETH

Checks if the identifier is for ETH.

Compares the identifier to the ETH identifier and returns true if they match.

function _isIdentifierETH(string memory identifier) private pure returns (bool);

Parameters

NameTypeDescription
identifierstringThe identifier to check.

Returns

NameTypeDescription
<none>boolisETH True if the identifier is for ETH.