Magicswap
This module can be used to get information about Magicswap pools, allowing you to perform token swaps and manage pool liquidity.
Public members
Usage
See Assets/Treasure/Example/Scripts/MagicswapUI.cs for usage examples
Swap and liquidity operations require the user’s smart wallet to be connected and a started session (See Connect & Identity)
Token Approval
In order to approve tokens and NFTs for transfer when swapping or adding liquidity,
the token addresses must be added to the callTargets
array in the config JSON or TDKConfig
asset.
LP token addresses (pool ids) must also be added as callTargets
in order to approve for transfer when removing liquidity.
Tokens can be approved using the TDK.Common.ApproveERC*
helpers. For example:
Performing a swap
Step 1: Fetch pool and route data
If isExactOut = true
then amount
is the amount out.
If isExactOut = false
then amount
is the amount in.
See this section below for details.
Step 1.1: For swaps that involve NFTs
The user must select the NFTs to swap (and their quantities)
- if the input token is NFT, select from
routeData.tokenIn.collectionTokenIds
up torouteData.amountIn
NFTs - if the output token is NFT, select from
routeData.tokenOut.collectionTokenIds
up torouteData.amountOut
NFTs
Step 2: Perform the swap
Other types of swap
Different properties of SwapBody
can be used for different kinds of swap:
Swap type | Properties |
---|---|
ERC20 to NFT | amountIn & nftsOut |
ERC20 to ERC20 | amountIn & amountOut |
NFT to NFT | nftsIn & nftsOut |
NFT to ERC20 | nftsIn & amountOut |
The isExactOut
parameter
When calling GetRoute
and Swap
you must specify the isExactOut
parameter.
The same value should be used for both.
isExactOut = true
means that the user has specified the exact amount of tokens they want to receive.
There are a few different scenarios:
- ERC20⇔NFT swaps
- If the user is trading their NFTs,
isExactOut = false
because they must specify exactly how many NFTs they are putting in. - If the user is trading to NFTs,
isExactOut = true
because they must specify exactly how many NFTs they are receiving.
- If the user is trading their NFTs,
- ERC20⇔ERC20 or NFT⇔NFT swaps;
isExactOut
depends on whatever the user specified.- For example, if you’re swapping MAGIC to VEE, then
isExactOut = true
if you specify how much VEE you want, andisExactOut = false
if you specify how much MAGIC you will give.
- For example, if you’re swapping MAGIC to VEE, then
Adding Liquidity
Other types of pools
Different properties of AddLiquidityBody
can be used for different kinds of pools:
Pool type | Properties |
---|---|
ERC20⇔NFT | amount0 & amount0Min & nfts1 |
ERC20⇔ERC20 | amount0 & amount0Min & amount1 & amount1Min |
NFT⇔NFT | nfts0 & nfts1 |
NFT⇔ERC20 | nfts0 & amount1 & amount1Min |
Removing Liquidity
Useful allowance helpers
Some helpers are provided for reading relevant information, such as token balance, allowance, or approval status.