SwapLib
SwapLib
Overview
The SwapLib library provides functionality for swapping tokens to stablecoins using DEX aggregators. It handles the logic for distributing swapped amounts proportionally between releaseable pools and fee pools. The library is used by the V1_2 versions of the fulfillable contracts to enable stablecoin swapping capabilities.
The library consists of two main components:
SwapLib: For handling ERC20 token swaps to stablecoinsSwapNativeLib: For handling native currency swaps to stablecoins
Both components share similar functionality but are optimized for their respective token types. The library is responsible for:
Validating swap parameters and token addresses
Performing the actual swap call to the DEX aggregator
Calculating the received stablecoin amount
Distributing the swapped stablecoins proportionally between releaseable pools and fee pools
The SwapLib responsibilities can be broken down into the following concepts:
Swap Data Structure
SwapData
The SwapData struct captures all the information needed to perform a swap:
toToken: The destination stablecoin addressamount: The amount of tokens to swapcallTo: The DEX aggregator contract addresscallData: The encoded call data for the aggregator
ERC20 Token Swapping
swapERC20ToStable
Swaps ERC20 token pools to stablecoins in a single transaction. The function validates the token addresses and swap amount, then performs the swap and distributes the received stablecoins proportionally between the releaseable and fees pools.
Effects:
Validates the token addresses and swap amount
Calculates the total available balance from releaseable pools and accumulated fees
Subtracts the swap amount from the appropriate pools
Calls
_callSwapto perform the actual swapDistributes the received stablecoins proportionally using
_distributeStableAmountsUpdates the releaseable pools and accumulated fees with the distributed stablecoin amounts
Emits
PoolsSwappedToStableevent
Requirements:
fulfillmentRecord.tokenMUST NOT be the zero addressswapData.toTokenMUST NOT be the zero addressswapData.amountMUST be greater than zeroThe total available balance MUST be greater than or equal to
swapData.amount
Native Currency Swapping
swapNativeToStable
Swaps native currency pools to stablecoins in a single transaction. The function is similar to swapERC20ToStable but is optimized for native currency.
Swap Execution
_callSwap
Performs the actual swap call to the DEX aggregator and calculates the received stablecoin amount. The function approves the aggregator to spend the tokens, calls the aggregator with the provided call data, and calculates the received stablecoin amount by comparing the balance before and after the swap.
Distribution Calculation
_distributeStableAmounts
Distributes the stable amount proportionally between the releaseable and fees pools. The function calculates the releaseable share based on the proportion of the releaseable amount to the total swap amount, and the fees share as the remainder.
Last updated
Was this helpful?