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.
To see how much MEL bob has:
melwallet-cli --wallet-path ./bob.json summary
Network: testnet
Address: t7v9tegt6bm6dv9t6e56ktdap3ych5htw83wa69z0shwa7nt3xbkn0
Balances:
500.00000 MELSwapping 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.
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:
melwallet-cli --wallet-path ./bob.json swap --value 100.0 --from MEL --to SYM --wait
SWAPPING
From: 100.000000 MEL
To: 50.000000 SYM (approximate)
Fee: 0.000000 MEL
Proceed? [y/N] y
..................
Transaction 3717d9d6a93f2e5c4c420745dccbb72d9ed109285362f6a45785e6f73cd8ef58 confirmed!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.
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