Swapping tokens

A guide on swapping tokens on Mel.


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:

melwallet-cli --wallet-path ./bob.json summary

Network:  testnet
Address:  t7v9tegt6bm6dv9t6e56ktdap3ych5htw83wa69z0shwa7nt3xbkn0
500.00000 MEL

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:

melwallet-cli --wallet-path ./bob.json swap --value 100.0 --from MEL --to SYM --wait

From:  100.000000 MEL
To:    50.000000  SYM  (approximate)
Fee:     0.000000 MEL
Proceed? [y/N] y


Transaction 3717d9d6a93f2e5c4c420745dccbb72d9ed109285362f6a45785e6f73cd8ef58 confirmed!
melwallet-cli --wallet-path ./bob.json summary

Network:  testnet
Address:  t7v9tegt6bm6dv9t6e56ktdap3ych5htw83wa69z0shwa7nt3xbkn0
400.00000 MEL
100.00000 SYM


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:

melwallet-cli --wallet-path ./bob.json pool MEL/SYM

1 MEL  = 0.5000000000000000 SYM
1 SYM  = 2.0000000000000000 MEL

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:

melwallet-cli --wallet-path ./bob.json liq-deposit 0.0 SYM 100.0 MEL --wait

Proceed? [y/N] y
Transaction 19b882d37f66cce058dea501c2acffe1cdac8b00e7424e27ad92c864f27ba56d confirmed!

bob now has 1 liquidity token for the MEL/SYM pool, called CUSTOM-526bd177e93a854e08216aff6b48cf7f3ae7b4cbd202fdfde39271d1ad90a3bd.

melwallet-cli --wallet-path ./bob.json summary

Network:      testnet
Address:      t11n9ynz8jhcd1k7h6jx4pvsc4m2qvwjhdp5mx03ega05hkcts8j9g
Balance:      349.50000   MEL
              25.000000   SYM
              1.0000000   CUSTOM-526bd177e93a854e08216aff6b48cf7f3ae7b4cbd202fdfde39271d1ad90a3bd
Staked:       0.000000    SYM

Last updated