StableSwap Overview
StableSwap protocol keywords: Automated market maker (AMM), liquidity pool and swap
Background
As stablecoins evolve, the popular TRC20-USDT is no longer alone on TRON. TRON's expanding stablecoin family has welcomed USDJ, TUSD, USDC, USDD, and other new members. The growing market share and variety of stablecoins have given rise to more complex demand for swapping between different stablecoins. Sun.io's StableSwap protocol is designed to provide swap services for stablecoins or tokens of equivalent value, and it offers lower fees and slippage than competitors in the market in the following ways:
Slippage of StableSwap can be reduced to zero when there are sufficient tokens in the liquidity pool;
StableSwap will increase the transaction price of a token to avoid depletion of its supply in the liquidity pool.
Mechanism
To maintain the transaction price at 1, the most straightforward way is to use the equation of a straight line with a slope of -1 (i.e. x + y = k). The price should also be able to adjust itself to avoid liquidity depletion, which requires the constant product formula x * y = k. The StableSwap protocol incorporates both constant sum and constant product. Its model can be simply regarded as the weighted sum of a constant sum and a constant product, as described by the market making formula in the SUN WhitePaper.
According to the above formula, token swaps have an impact on the value of . Take 2pool (USDD, USDT) as an example: suppose the amount before the transaction is . When a user swaps USDD for USDT, the value of will change to . Substitute into the above equation and you will get the new , and represents the amount of USDT the user will obtain. The values of both and remain unchanged during the calculation. is a constant, and the value of can be found with a given using Newton's Method.
Let , Substitute these expressions into the above equation and you can get:
The corresponding contract code is shown below:
Liquidity Pool
The StableSwap contract of Sun.io allows users to swap between stablecoins and add or remove liquidity. Its USDD-USDT liquidity pool, USDJ-TUSD-USDT liquidity pool, and USDC liquidity pool are now available on the TRON MainNet.
USDD-USDT pool
MainNet contract address: TNTfaTpkdd4AQDeqr8SGG7tgdkdjdhbP5c
USDJ-TUSD-USDT pool
MainNet contract address: TKcEU8ekq2ZoFzLSGFYCUY6aocJBX9X31b
USDC pool
MainNet contract address: TNTfaTpkdd4AQDeqr8SGG7tgdkdjdhbP5c
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's token address
Function: coins(uint256)
Parameter: The i-th token of the stablecoin pool
Get a liquidity pool's size
Function: balances(uint256)
Parameter: The i-th token of the stablecoin pool
Get a liquidity pool's LP token address
Function: lp_token()
Parameter: No input parameters
Get A's value
Function: A()
Parameter: No input parameters
Execute Transaction
Get the amount of Token to swap for
Function: get_dy(uint128,uint128,uint256)
Parameters: In-pool sequence number of the token to be sold, in-pool sequence number of the token to be bought, amount of the token to be sold
Swap
Function: exchange(uint128,uint128,uint256,uint256)
Parameters: In-pool sequence number of the token to be sold, in-pool sequence number of the token to be bought, amount of the token to be sold, minimum amount expected to get
Add & Remove Liquidity
Add liquidity
Function: add_liquidity(uint256[n],uint256)
Parameters: Amount of all tokens to add, minimum LP token amount expected to get
Remove liquidity
Function: remove_liquidity(uint256, uint256[n])
Parameters: LP token amount to remove, minimum token amount expected to get
Last updated