Swap Executor
How to implement a SwapExecutor for a Protocol
Overview
The ISwapExecutor
interface is designed to perform swaps on a liquidity pool. It allows for flexible interaction by accepting either the amount of the input token or the amount of the output token as parameters, returning the corresponding swapped amount. This interface is essential for creating a SwapExecutor
specific to a protocol.
The SwapExecutor
works in conjunction with the SwapStructEncoder
, which encodes the necessary data required for the swap. This encoded data is passed to the SwapExecutor
, enabling it to perform the swap according to the protocol's specific logic.
Key Methods
swap(uint256 givenAmount, bytes calldata data)
Purpose: To perform a token swap, either specifying the input amount to get the output amount or vice versa.
Parameters:
givenAmount
: The amount of the token (input or output) for the swap.data
: Encoded information necessary for the swap (e.g., pool address, token addresses - depends on the protocol), provided by theSwapStructEncoder
.
Returns: The amount of the token swapped.
Implementation Steps
Define Protocol-Specific Logic: Implement the
swap
function to interact with the protocol's liquidity pool. Use thedata
parameter to encode necessary information like pool and token addresses.Handling Input and Output: Depending on the provided
givenAmount
, determine whether it's an input or output swap. Calculate the corresponding swapped amount based on the pool's pricing logic.Error Handling: Use
ISwapExecutorErrors
(InvalidParameterLength
andUnknownPoolType
) to manage potential errors, such as invalid parameter lengths or unknown pool types in the swap logic.Token Approvals: If the protocol requires token approvals (allowances) before swaps can occur, manage these approvals within the implementation to ensure smooth execution of the swap.
Token Transfer Support: Ensure that the implementation supports transferring received tokens to a designated receiver address, either within the swap function or through an additional transfer step.
Gas Efficiency: Ensure the implementation is gas-efficient. Strive for optimal performance in the swap logic. The usage of assembly is not necessary.
Security Considerations: Follow common security best practices, such as validating inputs, ensuring proper access control, and safeguarding against reentrancy attacks.
Example Implementation
See the example implementation of a SwapExecutor
for Balancer here.
Last updated