Swapping tokens
A guide on swapping tokens on Mel.
Prerequisites
In the last section we sent wallet bob
some MEL. In this section we'll use wallet bob
to swap some tokens. Make sure you have access to at least 500 MEL.
Fees, exchange rates, and other numbers in this guide are entirely fictional!
To see how much MEL bob
has:
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 for another for a fixed pool fee of 0.5%. Using melwallet-cli
, we swap 100 MEL for some SYM at the market rate:
Pools
In the above trade, 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 are 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:
A constant-product pool like Melswap works to ensure all trades can be satisfied. This means that 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:
bob
now has 1 liquidity token for the MEL/SYM pool, called CUSTOM-526bd177e93a854e08216aff6b48cf7f3ae7b4cbd202fdfde39271d1ad90a3bd
.
Last updated