SunSwap Overview
SunSwap protocol keywords: Automated market maker (AMM), liquidity pool and swap
Background
SunSwap AMM (automated market maker) adopts the most used trading model in the world of DeFi today. Unlike the order book, AMMs utilize the constant product model to calculate tokens in the pool, where trades are automatically executed, and the liquidity supply of the trading pair is ensured.
Anyone can deposit a token into a pool in exchange for the pool's dedicated token (LP token) of an equivalent value, and thus become a liquidity provider (LP) for the pool. The amount of the LP token received divided by the pool's total LP reserves represents the liquidity provider's share of assets in the pool, and the liquidity provider can redeem the above assets at any time.
Mechanism
Swapping is based on the constant product formula, with x and y representing the amount of two different tokens (assumed to be token X and token Y), then:
If we want to swap X for Y from the liquidity pool, and assume that the amount of X entered is Δx and the amount of Y obtained is Δy, provided that the pool has sufficient funds, then:
This means that the product of the two tokens in the liquidity pool remains unchanged after the transaction. In this case, if the volume of a transaction only accounts for a minimal fraction of the liquidity pool's total volume, then the transaction price will approximate the ratio of the two tokens' amount:
However, the price of y in the liquidity pool is before the swap. This results in a slippage of y's price:
The larger the transaction volume Δx, the greater the slippage and the more the transaction price deviates from the actual price; likewise, more funds and greater depth in the pool can reduce the slippage and thus lower the losses for users. In actual SunSwap transactions, a 0.3% fee will be deducted before the calculation.
Example: Suppose the liquidity pool has 100 X and 1 Y. If a user wants to trade 20 X, then the actual amount of X traded will be 19.94 X (net of a 0.3% fee), according to the formula x ∗ y = k:
Liquidity Pool
The SunSwap contract of Sun.io allows users to swap between regular tokens and add and remove liquidity. Its V1 and V2 contracts are now running on the TRON MainNet.
V1
Factory contract address: TXk8rQSAvPvBBNtqSoY6nCfsXWCSSpTVQF
V2
Factory contract address: TKWJdrQkqHisa1X8HUdHEfREvTzw4pMAaY
Router contract address: TXF1xDbVGdxFGbovmmmXvBGu8ZiE3Lq4mR
Note: The old Router contract address TKzxdSv2FZKQrEqkKVgp5DcwEXBEKMg2Ax has been deprecated.
Interact with Contract
We utilize TronWeb to facilitate interaction with on-chain contracts. First, you need to initialize the TronWeb instance.
Get Liquidity Pool's Information
Get a liquidity pool's address in V1
Function: getExchange(address)
Parameter: Token address
Get a liquidity pool's address in V2
Function: getPair(address, address)
Parameter: token0's address, token1's address
Execute Transactions
Sell TRX to buy Token in V1
Function: trxToTokenTransferInput(uint256, uint256, address)
Parameters: minimum Token amount expected to buy, time window, recipient's address
Sell Token to buy TRX in V1
Function: tokenToTrxTransferInput(uint256, uint256, uint256, address)
Parameters: Amount of Token to sell, minimum TRX amount expected to buy, time window, recipient's address
Sell Token to buy Token in V1
Function: tokenToTokenSwapInput( uint256, uint256, uint256, uint256, address)
Parameters: Amount of Token to sell, minimum Token amount expected to buy, minimum Token amount expected to buy, time window, recipient's address
Sell Token to buy Token in V2
Function: swapExactTokensForTokens( uint, uint, address[], address, uint)
Parameters: Amount of Token to sell, minimum Token amount expected to buy, swap route, recipient's address, time window
Sell TRX to buy Token in V2
Function: swapExactETHForTokens(uint, address[], address, uint)
Parameters: Token amount expected to buy, swap route, recipient's address, time window
Sell Token to buy TRX in V2
Function: swapExactTokensForETH(uint, uint, address[] , address, uint)
Parameters: Amount of Token to sell, minimum TRX amount expected to buy, swap route, buyer's address, time window
Add & Remove Liquidity
Add liquidity in V1
Function: addLiquidity(uint256, uint256, uint256)
Parameters: Minimum liquidity amount expected to get, maximum liquidity amount to add, time window
Remove liquidity in V1
Function: removeLiquidity(uint256, uint256,uint256, uint256)
Parameters: Liquidity amount to remove, minimum TRX amount expected to get, minimum token amount expected to get, time window
Add liquidity in V2
Function: addLiquidity(address,address,uint,uint,uint,uint,address,uint)
Parameters: tokenA's address, tokenB's address, tokenA's amount expected to add, tokenB's amount expected to add, minimum receiving amount of the tokenA added, minimum receiving amount of the tokenB added, target liquidity pool's address, time window
Remove liquidity in V2
Name: removeLiquidity(address,address,uint,uint,uint,address,uint)
Parameters: tokenA's address, tokenB's address, amount of liquidity to remove, minimum tokenA amount expected to get, minimum tokenB amount expected to get, token's receiving address, time window
Last updated