Swapping tokens
A guide on swapping tokens on Mel.
Prerequisites
In the last section we sent Bob some MEL. In this section we are going to use that wallet to swap some tokens. To follow along make sure you have access to at least 500 MEL.
Fees, exchange rates, and other numbers in this guide are entirely fictional!
Swapping tokens
Unlike other blockchains where this functionality typically exists in a programmable smart contract, Mel features a built-in, Uniswap-like decentralized exchange (DEX) called Melswap.
We embedded a rudimentary DEX into the L1 not primarily for convenience, but as a trustless price oracle for designing on-chain logic; it is also an important component of the Melmint algorithm that stabilizes MEL.
With Melswap, any user can instantly swap one token and another for a fixed pool fee of 0.5%. Using melwallet-cli
we will swap 100 MEL for some SYM at the market rate.
As you can see in the From
and To
fields, 100 MEL is being swapped for 50 SYM. After user confirmation, melwallet-cli
waits for the transaction to be posted to the blockchain and then outputs the confirmation height and Melscan URL.
Pools
In order to execute the trade above, we interacted with a liquidity pool: a collections of two kinds of assets, in this case MEL and SYM, deposited on-chain. Liquidity pools provide constant-product DEXes, like Melswap, with always-available buyers and sellers, via an exchange rate that automatically adjusts to satisfy any trade without running out of assets in the pool.
Constant-product swapping pools were most notably implemented by Uniswap, and the Uniswap v2 documentation remains the best guide to understanding them further.
To see the current exchange rate and liquidity of any given pool, use the melswap-info
subcommand:
A constant-product pool like Melswap works to ensure all trades can be satisfied and, as such, trades without sufficient liquidity available may receive extremely bad prices.
Providing liquidity
Where does all the liquidity sitting in the pool come from? Melswap incentivizes users to add liquidity to pools by depositing tokens in the pool in exchange for liquidity tokens. Liquidity tokens are unique in that they reflect an ownership of a proportion of the entire pool, instead of an individual token. Let's deposit a total value of 100 MEL in liquidity into the MEL/SYM pool:
As you can see Bob now has 1 liquidity token called MEL~SYM
Last updated