SwapOperations
This contracts handles swaps and SwapPair management
We have a fixed swap fee of 0.3% and an additional dynamic fee, when a swap causes the price to move away from oracle price. The fee depends on how much you move the price away from oracle. We use PYTH as our Oracle for stock prices.
Structs
struct SwapAmount {
uint amount; // including fee
uint fee;
}
This struct is used in getAmountsOut and getAmountsIn.
Swap Pairs
SwapPairs can only be created by the protocol admins. To access swap pairs, we provide a list of functions, similar to Uniswap V2 Factory. For subgraph, you can track the event:
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
allPairsLength
function allPairsLength() external view returns (uint)
This function gives you the number of existing pairs.
isPair
function isPair(address pair) external view returns (bool)
Check if a given address is a SwapPair from our protocol.
getPair
function getPair(address token0, address token1) external view returns (address)
Returns 0x0 or the address for a SwapPair if there is a match. token0
and token1
can be exchanged.
allPairs
function allPairs() external view returns (address[] memory)
Returns a list of all available pairs.
Quotes
To get quotes for swap, we provide the following interfaces:
getAmountsOut
function getAmountsOut(
uint amountIn,
address[] calldata path
) external view
returns (SwapAmount[] memory amounts, bool isUsablePrice);
Given the input amount amountIn
and path
(an array of token address for the swap route, with a minimum length of 2), you will receive a list of the output amounts
after each route hop. All amounts include fixed and dynamic swap fee. isUsablePrice
can be ignored as it indicates if the oracle prices are up-to-date and should always be true (because we update the oracle in regular intervals).
getAmountsIn
function getAmountsIn(
uint amountOut,
address[] calldata path
) external view
returns (SwapAmount[] memory amounts, bool isUsablePrice);
Given the input amount amountOut
and path
(an array of token address for the swap route, with a minimum length of 2), you will receive a list of the required input amounts
for each route hop. All amounts include fixed and dynamic swap fee. isUsablePrice
can be ignored as it indicates if the oracle prices are up-to-date and should always be true (because we update the oracle in regular intervals).
Swap
All our swap functions have an optional priceUpdateData
parameter, to update the prices for our PYTH Oracle. As we frequently update the oracle ourself, there is no need to supply values.
swapExactTokensForTokens
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline,
bytes[] memory priceUpdateData
) public payable
returns (SwapAmount[] memory amounts)
Swaps an exact amountIn
of input tokens for as many output tokens as possible, along the route determined by the path
. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate pairs to trade through (if, for example, a direct pair does not exist).
msg.sender
should have already given theSwapOperations
an allowance of at leastamountIn
on the input token.
amountIn
uint
The amount of input tokens to send.
amountOutMin
uint
The minimum amount of output tokens that must be received for the transaction not to revert.
path
address[] calldata
An array of token addresses. path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
to
address
Recipient of the output tokens.
deadline
uint
Unix timestamp after which the transaction will revert.
priceUpdateData
bytes[] memory
PYTH price data. It is sufficient to not provide any data at all.
amounts
SwapAmount[] memory
All subsequent output token amounts including fees and the fee of each hop (fixed + dynamic swap fee).
swapTokensForExactTokens
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline,
bytes[] memory priceUpdateData
) public payable
returns (SwapAmount[] memory amounts)
Receive an exact amountOut
of output tokens for as few input tokens as possible, along the route determined by the path
. The first element of path is the input token, the last is the output token, and any intermediate elements represent intermediate tokens to trade through (if, for example, a direct pair does not exist).
msg.sender
should have already given theSwapOperations
an allowance of at leastamountInMax
on the input token.
amountOut
uint
The amount of output tokens to receive.
amountInMax
uint
The maximum amount of input tokens that can be required before the transaction reverts.
path
address[] calldata
An array of token addresses. path.length
must be >= 2. Pools for each consecutive pair of addresses must exist and have liquidity.
to
address
Recipient of the output tokens.
deadline
uint
Unix timestamp after which the transaction will revert.
priceUpdateData
bytes[] memory
PYTH price data. It is sufficient to not provide any data at all.
amounts
SwapAmount[] memory
All subsequent output token amounts including fees and the fee of each hop (fixed + dynamic swap fee).
Last updated