TokenSwapsModule
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
Name | Type | Description |
---|---|---|
_aavePMProxyAddress | address | The 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
Name | Type | Description |
---|---|---|
_uniswapV3PoolIdentifier | string | The identifier of the UniswapV3 pool to use for the swap. |
_tokenInIdentifier | string | The identifier of the token to swap. |
_tokenOutIdentifier | string | The identifier of the token to receive from the swap. |
Returns
Name | Type | Description |
---|---|---|
tokenOutIdentifier | string | The identifier of the token received from the swap. |
amountOut | uint256 | The 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
Name | Type | Description |
---|---|---|
aavePM | IAavePM | The Aave Position Manager contract. |
params | IV3SwapRouter.ExactInputSingleParams | The swap parameters. |
currentBalance | uint256 | The current balance of the token to swap. |
Returns
Name | Type | Description |
---|---|---|
amountOut | uint256 | The 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
Name | Type | Description |
---|---|---|
aavePM | IAavePM | The Aave Position Manager contract. |
_currentBalance | uint256 | The current balance of the token to swap. |
_uniswapV3PoolAddress | address | The address of the UniswapV3 pool to use for the swap. |
tokenOutAddress | address | The address of the token to receive from the swap. |
Returns
Name | Type | Description |
---|---|---|
minOut | uint256 | The 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
Name | Type | Description |
---|---|---|
identifier | string | The identifier to check. |
Returns
Name | Type | Description |
---|---|---|
<none> | bool | isETH True if the identifier is for ETH. |