💱Swapper - Helper for Liquidations and Auto-Leverage
Swapper - Helper for liquidations and auto-leverage
Contract Name:
Swapper
1. Introduction
The Swapper contract is an example contract developed by Angle Labs to show how to swap collateral for stablecoins during the liquidation process or to swap stablecoins for collateral during the auto-leverage process in the Borrowing Module. This is a side contract with no specific role in the Borrowing Module.
Documentation here is given as a helper for those who might be interested in building their own contracts or in using Angle Labs' contract.
2. Contract Details
Interface
Implements the ISwapper interface. This contract is not upgradeable (even though upgradeable versions of it could be deployed).
Constants and Immutable Variables
angleRouter: Reference to a router contract of the protocolcore: Reference to the Core contract of the module which handles all Access Control logicwStETH: Wrapped StETH contractuniV3Router: Uniswap Router contractoneInch: 1Inch Router
Mappings
The contract stores three different mappings to check whether Uniswap, 1Inch and Angle routers have been approved for a given token
3. Key Mechanisms & Concepts
Besides a function to change allowance for a given token accessible by governance, there is only one key function available in this contract: it is the swap function.
function swap(
IERC20 inToken,
IERC20 outToken,
address outTokenRecipient,
uint256 outTokenOwed,
uint256 inTokenObtained,
bytes memory data
) externalGenerally, the swap function in the ISwapper interface notifies a contract that an address should be given outToken from inToken. In this case, this function swaps the inToken to the outToken by either doing mint, or burn from the protocol or/and combining it with a UniV3 or 1Inch swap, or with a StETH wrap.
No slippage checks are performed at the end of each operation, only one slippage check is performed at the end of the call.
Parameters:
inToken: Address of the token receivedoutToken: Address of the token to obtainoutTokenRecipient: Address to which the outToken should be sentoutTokenOwed: Amount of outToken owed by theoutTokenRecipient. If theSwapperis called during an interaction with aVaultManagercontract, for the operation to succeed, theoutTokenRecipient'soutTokenbalance should be greater thanoutTokenOwedat the end of the call. It does not mean that the swap ofinTokenObtainedshould produce at leastoutTokenOwed.inTokenObtainedAmount of collateral obtained by a related address prior to the call to this function. Thisswapperimplementation assumes that theinTokenhave been obtained on this contract directly (and therefore that there is no need for atransferFrom)data: Extra data needed (to encode Uniswap swaps for instance). Specifically, in this implementation,datashould be the abi-encoded version of parameters placed in the following order and of the following type:address intermediateToken: Optional address that can be given to specify in case of a burn the address of the collateral to get in exchange for the stablecoin or in case of a mint the collateral used to mint. If theswapcall does not involve any mint/burn, then this parameter can be the zero addressaddress to: Address to receive the surplus amount of token at the end of the call. This is the address to receive the leftoverinTokenandoutTokenof the swapuint256 minAmountOut: For slippage protection, it is the min amount ofoutTokenschecked at the end of the calluint128 swapType: Type of the swap to execute: ifswapType == 3, then it is optional to swap.0corresponds to aUniswapV3swap,1corresponds to aoneInchswap,2is to wrapStETHtowStETH.uint128 mintOrBurn: Whether amintorburnoperation should be performed beyond the swap:1corresponds to a burn and2to a mint. It is optional. If the value is set to 1 or 2 then the value of theintermediateTokenshould be made non nullbytes data: It's an optional variable to be used either for Uniswap swaps (in which case it is the path), or a 1Inch payload.
When using this contract as a who contract in a VaultManager call, it is important to make sure that this address is the to address as it needs to receive the inToken.
Last updated
Was this helpful?
