SwapRouter
Variables
Structs
struct SwapCallbackData {
bytes path;
address payer;
}Description:
Encapsulates the data passed to the storyHuntV3SwapCallback during a swap operation. It includes the swap path and the payer who is responsible for paying the tokens owed.
Functions
constructor(address _factory, address _WIP9, address _tokenDescriptor_)
PeripheryImmutableState(_factory, _WIP9)
ERC721Permit('StoryHunt V3 Positions NFT-V1', 'STH-V3-POS', '1')
{
_tokenDescriptor = _tokenDescriptor_;
}Description:
Initializes the SwapRouter contract with the factory address, Wrapped IP (WIP9) token address, and the token descriptor address. It also initializes inherited contracts.
Parameters
_factory
address
The address of the StoryHunt V3 factory.
_WIP9
address
The address of the Wrapped IP (WIP9) token.
tokenDescriptor
address
The address of the token descriptor contract.
Description:
Callback function invoked by the StoryHuntV3Pool during a swap operation. It ensures that the necessary tokens are paid to the pool after a swap.
Parameters
amount0Delta
int256
The change in token0 balance of the pool. Positive if the pool needs to receive tokens.
amount1Delta
int256
The change in token1 balance of the pool. Positive if the pool needs to receive tokens.
_data
bytes
Encoded data containing the swap path and payer information.
Return Values
None
Requirements
At least one of
amount0Deltaoramount1Deltamust be positive.Validates the callback using the
CallbackValidationlibrary to ensure it originates from a legitimate pool.
Description: Performs a single exact input swap from one token to another within a specified pool.
Parameters
params
ExactInputSingleParams
The parameters defining the exact input swap (details below).
ExactInputSingleParams Struct Definition:
Return Values
amountOut
uint256
The amount of tokenOut received from the swap.
Requirements
The swap must be completed before the specified
deadline.The received amount must be at least
amountOutMinimumto prevent excessive slippage.
Description: Performs a multi-hop exact input swap along the specified path, allowing swaps across multiple pools.
Parameters
params
ExactInputParams
The parameters defining the exact input multi-hop swap (details below).
ExactInputParams Struct Definition:
Return Values
amountOut
uint256
The total amount of tokenOut received from all swaps.
Requirements
The entire swap sequence must be completed before the specified
deadline.The total received amount must be at least
amountOutMinimumto prevent excessive slippage.
Description: Performs a single exact output swap from one token to another within a specified pool.
Parameters
params
ExactOutputSingleParams
The parameters defining the exact output swap (details below).
ExactOutputSingleParams Struct Definition:
Return Values
amountIn
uint256
The amount of tokenIn used to achieve the swap.
Requirements
The swap must be completed before the specified
deadline.The input amount used must not exceed
amountInMaximumto prevent excessive spending.
Description: Performs a multi-hop exact output swap along the specified path, allowing swaps across multiple pools to achieve the desired output amount.
Parameters
params
ExactOutputParams
The parameters defining the exact output multi-hop swap (details below).
ExactOutputParams Struct Definition:
Return Values
amountIn
uint256
The total amount of tokenIn used to achieve the desired tokenOut.
Requirements
The entire swap sequence must be completed before the specified
deadline.The total input amount used must not exceed
amountInMaximumto prevent excessive spending.
Modifiers
Description:
Ensures that the transaction is executed before the specified deadline. If the current block timestamp exceeds the deadline, the transaction is reverted.
Parameters
deadline
uint256
The timestamp by which the transaction must be completed.
Usage
Applied to functions to enforce time constraints, preventing transactions from being executed after a certain time.
Structs (Continued)
While previously covered, additional structs used in function parameters are documented below for clarity.
Description: Parameters for performing a single exact input swap.
Description: Parameters for performing a multi-hop exact input swap.
Description: Parameters for performing a single exact output swap.
Description: Parameters for performing a multi-hop exact output swap.
Events
The SwapRouter contract does not define its own events. It relies on events emitted by the StoryHuntV3Pool during swap operations and other interactions. Users can monitor these events through the pool contracts.
Usage Notes
Performing Exact Input Swaps
Function:
exactInputSingle()andexactInput()Description:
Use
exactInputSingle()for single-hop swaps between two tokens within a specific pool.Use
exactInput()for multi-hop swaps across multiple pools.
Steps:
Define the tokens involved and the fee tier.
Specify the desired input amount and the minimum acceptable output.
Call the appropriate function with the required parameters.
Ensure that the transaction is executed before the
deadline.
Performing Exact Output Swaps
Function:
exactOutputSingle()andexactOutput()Description:
Use
exactOutputSingle()for single-hop swaps where you specify the exact amount of output tokens you want.Use
exactOutput()for multi-hop swaps to achieve a specific output amount across multiple pools.
Steps:
Define the tokens involved and the fee tier.
Specify the desired output amount and the maximum acceptable input.
Call the appropriate function with the required parameters.
Ensure that the transaction is executed before the
deadline.
Handling Callbacks
Function:
storyHuntV3SwapCallback()Description:
After initiating a swap, the pool contract will call this callback to request the payment of tokens owed.
Ensure that your contract correctly implements the callback to handle the token transfers.
Requirements:
Only the legitimate pool contract can invoke this callback.
Proper validation using
CallbackValidationis essential to prevent unauthorized calls.
Path Encoding
Description:
The
pathparameter in multi-hop swaps must be encoded using thePathlibrary.Each segment of the path consists of a token address followed by the fee tier.
Usage:
For example, to swap Token A β Token B β Token C, the path would be encoded as
abi.encodePacked(tokenA, feeAB, tokenB, feeBC, tokenC).