Let's Move is a motivational program to learn Move language and get SUI.
Learning Journal (alva-lin)
Task 5 - Complete the deployment of a Swap on-chain (mainnet)#
Task
Complete the learning of Swap-related knowledge
Complete the on-chain deployment of the first Swap contract
The concept of Swap will not be discussed in detail in this article, it mainly explains the operations after the release package.
The code for this article is located in new_swap, you can download it yourself.
The code includes two initial tokens COIN_A and COIN_B for testing purposes, as well as a Simple_Swap contract.
The code refers to pool.move - sui program examples
Release Package#
Switch the network to the mainnet, then release the package
sui client switch --env mainnet
sui client publish --gas-budget 200000000
In the obtained transaction result, find the Package ID
, TreasuryCap ID
of Coin_A, and TreasuryCap ID
of Coin_B, and export them to environment variables.
Also export your wallet address as a variable.
export PACKAGE_ID=0xfac346bf4cbd8284cc76e76981abc9b30c0f2398cde2913e7b21ead99d1f6536
export COIN_A=$PACKAGE_ID::coin_a::COIN_A
export COIN_B=$PACKAGE_ID::coin_b::COIN_B
export COIN_A_TREASURY_CAP_ID=0xe5636992dde57ee943689be2b4377711e399a5af28c6572c12b114895769e219
export COIN_B_TREASURY_CAP_ID=0x7e29738f66374d2f15a83fe4e93c1f6c114e6fb75367d34438164f16b2c6376c
export ADDRESS=<your wallet address>
Mint Tokens#
Mint COIN_A and COIN_B with different values in advance. Since this is for testing purposes only, the decimals of both tokens are set to 3 for easy identification.
# mint COIN_A
# You can mint coins multiple times in small amounts as needed for subsequent calls
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module coin_a --function mint --args $COIN_A_TREASURY_CAP_ID 1000 $ADDRESS
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module coin_b --function mint --args $COIN_B_TREASURY_CAP_ID 10000 $ADDRESS
Record the output
# 1 coin A
export COIN_A_ID_1=0x588a71ce52b95ec891e8c7593994ca1b34f85c24ca5791a5f88f43a5cda4dd62
export COIN_A_ID_2=0x969d24aa2d4f193448026c7e579e189702320c05a54f201c96dfdaf4f04a0a6f
export COIN_A_ID_3=0x6e768e76b28d7edda8dbfe37d9f7bcfe370645259cdccf0661164b712e0d1ab8
export COIN_A_ID_4=0x5c8e764c236e1ce80119d13d0b1744fa7df652e3664498bb413af2ab3ba2bf5c
# 10 coin B
export COIN_B_ID_1=0x2d6b2436b534e49edfd235fb9a1f9db393d1f7f2d0bb63199cf7c9d0a4b0ac22
export COIN_B_ID_2=0xb8ac379c3fc2238f5cee0e6a8ad2bfab82c01b841869ee8d4817d499aaf5cb7e
export COIN_B_ID_3=0xcfb9b7dc4488bcd40a242189dbed996b9aa401431fa9824a5756ed1ee822239f
export COIN_B_ID_4=0xbf5d27d4024ba6572aea61a60f16324778a8d2f63b99920e64514be6696d2d38
Build Liquidity Pool#
Use COIN_A 1 and COIN_B 1 minted earlier to build a liquidity pool
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_swap \
--function create_pool --type-args $COIN_A $COIN_B \
--args $COIN_A_ID_1 $COIN_B_ID_1
In the output, find the Pool ID
and the LSP ID issued to us
export POOL_ID=0x50cdc0af15ceb8b660d15e843579587699c27c86868dc49e3945e2f73b546118
export LSP_ID_1=0x597a50ce6afacc3d26236ab52aab4342dc258554fe1520412e20675ffd7df9b6
We can use the Pool ID
to view the relevant information of the liquidity pool in SuiScan browser
url: https://suiscan.xyz/mainnet/object/0x50cdc0af15ceb8b660d15e843579587699c27c86868dc49e3945e2f73b546118
Add Liquidity#
Use COIN_A 2 and COIN_B 2 minted earlier to add liquidity
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_swap \
--function add_liquidity --type-args $COIN_A $COIN_B \
--args $POOL_ID $COIN_A_ID_2 $COIN_B_ID_2
Get the second LSP token issued
export LSP_ID_2=0x076230c8207d445f3eb1cf43cd3415304de9721f8ab9940f6127f90a6650ea31
Also check the values of the two LSP tokens, both are the same (3100)
Now let's add an equal amount of liquidity again
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_swap \
--function add_liquidity --type-args $COIN_A $COIN_B \
--args $POOL_ID $COIN_A_ID_3 $COIN_B_ID_3
Get the third LSP token
export LSP_ID_3=0xe44da8865c2b3cd79a5a28fe98706f91c35b7e719f80b71d39624291320d23e2
Remove Liquidity#
Take out the third LSP token and remove liquidity, check the result
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_swap \
--function remove_liquidity --type-args $COIN_A $COIN_B \
--args $POOL_ID $LSP_ID_3
Get the equivalent COIN A token and COIN B token, record them
# 1 coin A
export COIN_A_ID_5=0x0c0fd563d78f58b8ffa6fa21d4a9b75bb11c53cee468201e4c86632271a7008c
# 10 coin B
export COIN_B_ID_5=0xe1565ef7e7bd4569a589242b4393235ba877f488f1ca00231688c8119050d62a
Swap#
At this point, the token quantities in the liquidity pool are as follows
-
Coin A: 2
-
Coin B: 20
Coin A : Coin B = 1:10
Trade Coin A for Coin B#
Take out the fourth Coin A token and initiate a trade with the liquidity pool
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_swap \
--function swap_a_to_b --type-args $COIN_A $COIN_B \
--args $POOL_ID $COIN_A_ID_4
You can see the transaction result, we used 1 Coin A and bought 10.015 Coin B. Record it
# 10.015 coin B
export COIN_B_ID_6=0xdccf55457cf82b6b8a14f6627002fc83a4c4456ccfa1ca16bf8164ea7b3e9db7
In the liquidity pool information, there are still 3 Coin A and 9.985 Coin B.
Trade Coin B for Coin A#
Take out the fourth Coin B token and initiate a trade with the liquidity pool
sui client call --gas-budget 7500000 --package $PACKAGE_ID --module simple_swap \
--function swap_b_to_a --type-args $COIN_A $COIN_B \
--args $POOL_ID $COIN_B_ID_4
You can see the transaction result, we used 10 Coin B and bought 1.503 Coin A. Record it
# 1.503 coin A
export COIN_A_ID_6=0x33ab32733faaa4a8985ccba27692fbf466d54ee854ef919d1d5ae52b444a23dc
In the liquidity pool information, there are still 1.497 Coin A and 19.985 Coin B.
Result Summary#
Initially, the token funds in the wallet and the pool were 2 Coin A and 20 Coin B.
After two trades, the changes in funds in the wallet and liquidity pool are as follows
Token Quantity | Coin A | Coin B |
---|---|---|
Wallet | 2.503 | 20.015 |
Pool | 1.497 | 19.985 |
The ratio of the two tokens changed from 1:10 to 1:13.35, with Coin A appreciating and Coin B depreciating relatively.
In terms of token value, the value of tokens in the wallet is 4 Coin A (or 40 Coin B)
Token Value | Coin A Perspective | Coin B Perspective |
---|---|---|
Wallet | 3.993 | 53.43 |
The change in token value also corresponds to the rise and fall of the tokens.