Self-Serve Integration via Contracts
This method of integration is more involved and is not recommended by the Treasure core team. For a more seamless integration leveraging APIs and SDKs, consider leveraging the Treasure Development Kit (TDK) instead. Little to no developer support will be provided.
Contract Addresses
Click here to view the latest contract addresses.
Contract Calls Samples
Create Vault
Navigate to the NftVaultFactory's createVault function
Enter an array of CollectionData type objects
- For example, if you want to create a vault for Zeeverse Zee Items (ERC-1155), the function param would be:
- The vault can also be restricted to specific token IDs. For example, a vault only for the Gloop Zee Item would be created with the following param:
- The resulting transaction will emit a
VaultCreated
event, and the value inTopics[0]
is the newly created vault’s contract address. For the examples below, we will use the Common Zee Items vault created at 0x8D5d711340047f9e855BBe86Ee0D8d7e7412585a
Add Liquidity
Creating a liquidity pool
If you are the first one adding liquidity for a specific pair, you get to set the price. For this example, we’ll create a Zee Items-MAGIC pool:
Mint 1,000 MAGIC
Approve the MagicswapV2Router address as a spender of 1,000 MAGIC
Approve the MagicswapV2Router address as an operator on the Zeeverse Zee Items
Navigate to the MagicswapV2Router's addLiquidityNFT function and enter the following parameters:
This will deposit 15 Zee Items and 1,000 MAGIC into the pool, meaning each Zee Item will be worth ~66 MAGIC at pool creation
Adding to an existing liquidity pool
If a liquidity pool for your desired pair has already been created, then the price is already set and your deposit must conform to the current pool pricing. The two sides of the pool are always balanced, so you need to deposit equal value amounts on both sides. To do so, you first need to determine the value of each side of the pool.
For the examples below, we will use the VEE-Common Zee Items pool created at 0x77d3d38ef24a2179b5147d8ebdab84b26012b210. If the contract is not verified on Arbiscan, you can use the UniswapV2Pair ABI to make read contract calls.
Price the assets
- If you don’t know which tokens are token0 and token1, call those read functions to get their addresses as results:
token0
:0x23BE0504127475387A459FE4b01E54f1E336fFae
(VEE)token1
:0x8D5d711340047f9e855BBe86Ee0D8d7e7412585a
(Common Zee Items Vault)
- Fetch the pair’s reserves with the getReserves function, and get a response like:
This means that ~91,014 VEE and 89 Common Zee Items are deposited in the pool right now. Since we know both sides are equal, we can calculate the price of 1 Common Zee Item:
1 Common Zee Item = ~1022.63 VEE
Approve the assets for transfer
On the VEE contract, call approve with the MagicswapV2Router (0xd0A4FbCc5cDe863A2BE50C75b564Efd942b03154) as spender and an amount of 1022632345971128889762
On the Vee Items contract, call setApprovalForAll with the MagicswapV2Router (0xd0A4FbCc5cDe863A2BE50C75b564Efd942b03154) as operator and approved with a value of true”>
Add liquidity
On the MagicswapV2Router
contract, call addLiquidityNFT
with the following params:
- MagicswapV2Router contract
_vault
is a tuple of typeNftVaultLiquidityData
token
is the NFT’s vault addresscollection
is a list of the NFT addressestokenId
is a list of the NFT token IDsamount
is a list of the number of NFT tokens. The lists all need to have the same lengths for a transaction to be valid_tokenB
is the ERC-20 token address_amountBDesired
is the amount of_tokenB
that we are expecting to deposit to the pool_amountBMin
is the minimum amount of_tokenB
we are willing to deposit after slippage
Assuming a slippage setting of 0.5%:
_to
is the address that should receive the resulting LP tokens_deadline
is any future timestamp, in seconds
Remove Liquidity
When you add liquidity by depositing ERC-20 and ERC-1155 tokens, you are minted LP tokens (ERC-20) to represent your holdings. When you want to remove liquidity, you burn those LP tokens in exchange for equal amounts of the ERC-20 and ERC-1155 tokens.
On the LP token contract
- Call
approve
with theMagicswapV2Router
(0xd0A4FbCc5cDe863A2BE50C75b564Efd942b03154
) as spender and the amount of tokeLP ns you will remoburnl getReserves to get the latest reserve info - Call totalSupply to get the latest total supply info
The amount of NFTs you can withdraw depends on the amount of LP tokens you are going to burn, rounded down to the nearest whole number:
On the MagicswapV2Router contract, call removeLiquidityNFT with the following params:
_vault
follows the same format as_addLiquidityNFT
_tokenB
is the ERC-20 token address_lpAmount
is the amount of LP token to burn_amountAMin
the amount of NFTs we want to receive_amountBMin
is the minimum amount of_tokenB
we are willing to receive after slippage
_to
is the address that should receive the resulting LP tokens_deadline
is any future timestamp, in seconds_swapLeftover
is true if we want to swap any leftover NFT dust into_tokenB
instead of receiving a fractional NFT Vault token
Swap ERC-20 to NFT
We’ll cover the “exact out” scenario, where a user specifies an exact number of NFTs they want to receive, and calculate the required ERC-20 swap input taking into account current reserves, fees and price impact.
To properly complete a swap, you will need to calculate the value of _amountInMax
, the maximum amount of ERC-20 tokens the user is willing to pay for the trade after fees and slippage.
Calculate _amountInMax
- Fetch the pair’s reserves with the
getReserves
function - On the
UniswapV2Factory
contract, callgetTotalFee
with the pool address as the parameter to receive the total fees set for this pair with base 10,000:
Calculate the amountIn with the following logic:
For example, if looking to swap for 1 Common Zee Item:
Calculate the amountInMax with the following logic:
Assuming a slippage setting of 0.5%:
Price impact is the difference between amountIn
and a straight trade based on current reserves:
Approve the assets for transfer
On the VEE contract, call approve
with the MagicswapV2Router
(0xd0A4FbCc5cDe863A2BE50C75b564Efd942b03154
) as spender and an amount of 1042622482722649889964
Swap
On the MagicswapV2Router contract, call swapTokensforNft with the following params:
_collection
is a list of the NFT addresses_tokenId
is a list of the NFT token IDs_amount
is a list of the number of NFT tokens- The lists all need to have the same lengths for a transaction to be valid
_amountInMax
from the previous step_path
is a list of addresses that this swap route will take. For a single pool, this will just be both sides of the pool (e.g., Common Zee Item to VEE swap will be the Common Zee Items vault and the VEE token address). Specifying more than two addresses in this path allows you to route through multiple pools, for example doing a trade from Common Zee Items to MAGIC, when only the VEE-Common Zee Items and MAGIC-VEE pools exist._to
is the address that should receive the resulting ERC-20 tokens_deadline
is any future timestamp, in seconds
Swap NFT to ERC-20
We’ll cover the “exact in” scenario, where a user specifies an exact number of NFTs they want to trade, and calculate the resulting ERC-20 swap taking into account current reserves, fees and price impact.
Approve the assets for transfer
On the Zee Items contract, call setApprovalForAll
with the MagicswapV2Router
(0xd0A4FbCc5cDe863A2BE50C75b564Efd942b03154
) as operator and approved with a value of true
Calculate _amountOutMin
To properly complete a swap, you will need to calculate the value of _amountOutMin
, the minimum amount of ERC-20 tokens the user is willing to receive for the trade after fees and slippage.
- Fetch the pair’s reserves with the getReserves function
- On the UniswapV2Factory contract, call
getTotalFee
with the pool address as the parameter to receive the total fees set for this pair with base 10,000:
- Calculate the amountOut with the following logic:
For example, if looking to swap 1 Common Zee Item:
Calculate the amountOutMin with the following logic:
Assuming a slippage setting of 0.5%:
- Price impact is the difference between
amountOut
and a straight trade based on current reserves:
Swap
On the MagicswapV2Router contract, call swapNftForTokens
with the following params:
_collection
is a list of the NFT addresses_tokenId
is a list of the NFT token IDs_amount
is a list of the number of NFT tokens- The lists all need to have the same lengths for a transaction to be valid
_amountOutMin
from the previous step
- The lists all need to have the same lengths for a transaction to be valid
_path
is a list of addresses that this swap route will take. For a single pool, this will just be both sides of the pool (e.g., Common Zee Item to VEE swap will be the Common Zee Items vault and the VEE token address). Specifying more than two addresses in this path allows you to route through multiple pools, for example doing a trade from Common Zee Items to MAGIC, when only the VEE-Common Zee Items and MAGIC-VEE pools exist._to
is the address that should receive the resulting ERC-20 tokens_deadline
is any future timestamp, in seconds