Classic pool
SyncSwap classic pool is based on this well-known constant product invariant:
Swap
To perform a swap, users must send the amount to be used as input and call swap
(since this must be performed in a single transaction, it is normally delegated to the router contract).
The following calculations assume a swap from the first to the second asset of the pair that makes up the pool. The calculations in the reverse direction are specular. The amount of tokens resulting from the swap is computed as
Mint
The liquidity used to perform swaps is provided by liquidity providers who deposit liquidity in exchange for LP tokens. Liquidity providers must send the liquidity they want to provide to the pool and call mint
. This is normally performed by the router contract.
The amount of LP tokens (liquidity) minted to the user is equal to
where
The fee parameters are calculated to account for unbalanced minting: if the user providing liquidity does not provide both assets in a balanced way, the appropriate amount of the asset in excess is converted to the other asset to keep the balance, incurring in swap fees.
The parameter represents the total supply of LP tokens, including the newly minted LP tokens minted as protocol fees. If enabled, protocol fees cause the minting of additional LP tokens that are sent to an address responsible of collecting them. The amount to be minted is computed as
Burn
Opposite to minting, burning allows to get back liquidity in exchange for LP tokens. Users must send LP tokens to the pool before calling burn
(within the same transaction), normally by using the router contract.
The amount of liquidity returned to the user is calculated by
where is the amount of LP tokens being burned by the user and is the amount of LP tokens in circulation.
We noticed a difference between SyncSwap and Trident: where SyncSwap uses the current balance to compute the amounts returned to the user, Trident uses the cached reserves values. According to the SyncSwap team, this change was made to improve gas efficiency. While the balance can be manipulated upwards before calling burn
without affecting the cached reserves values (by transferring tokens to the pool), we do not believe this behavior can be used profitably by an attacker.
Unilateral burn
SyncSwap also allows to burn LP tokens in exchange for one of the two assets of a pool. This is equivalent to burning LP tokens for the pair and swapping the unwanted asset for the desired one.
The amounts of assets gained by burning LP tokens is computed identically to the regular burn functionality, and the conversion rate between the pair matches the amount that would be obtained by a regular swap operation.