Aptos: Move 部署代币并添加 LP [Production ready]
之前我们直接部署了一个 ERC20-like 的智能合约,然后创建了第二个账户为它 mint 代币,今天我们使用生产可用的合约代码来发币并创建池子、添加流动性。
这是 最终效果:可在 Coins 看到 Token 和 LP Token
-
首先在
Move.toml
中引入Liquidswap
的 SDK[package] name = "Examples" version = "0.0.1" [dependencies] AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework/", rev = "devnet" } MoveStdlib = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/move-stdlib", rev = "devnet" } [dependencies.Liquidswap] git = 'https://github.com/pontem-network/liquidswap.git' rev = 'devnet' # 因为 liquidswap 还未更新 testnet 的部署,所以暂时不能使用 V2 版本 SDK # [dependencies.LiquidswapRouterV2] # git = 'https://github.com/pontem-network/liquidswap.git' # subdir = 'liquidswap_router_v2/' # rev = 'v0.4.3' [dependencies.LiquidswapLP] git = 'https://github.com/pontem-network/liquidswap.git' subdir = 'liquidswap_lp/' rev = 'devnet' [addresses] RealCoin = '_'
-
编辑代币合约文件
sources/real_coin.move
module RealCoin::real_coin { struct RealCoin {} fun init_module(sender: &signer) { // 使用了 aptos_framework 中 coin 的实现,具备 ERC-like 的功能和一些 mint,burn 等基本的管理功能 aptos_framework::managed_coin::initialize<RealCoin>( sender, b"Real Coin", b"REAL", 8, false, ); } }
-
编辑添加流动性所需的
sources/add_lp.move
(因为aptos-cli
的--types-args
不好用,没法发起调用,又不想使用 TypeScript 的 SDK 来调用,所以直接部署成module
来调用)module RealCoin::add_lp { use std::signer; // 因为 testnet 上面的部署没更新所以现在还不能使用 v2 // use liquidswap::router_v2; use liquidswap::router; use liquidswap::curves::Uncorrelated; use liquidswap::coin_helper::is_sorted; use liquidswap_lp::lp_coin::LP; use aptos_framework::aptos_coin::AptosCoin; use aptos_framework::coin; use RealCoin::real_coin::RealCoin; public entry fun create_pool(account: &signer) { // Check generics sorted. assert!(is_sorted<RealCoin, AptosCoin>(), 0); router::register_pool<RealCoin, AptosCoin, Uncorrelated>( account, ); } public entry fun add_liquidity(account: &signer) { assert!(is_sorted<RealCoin, AptosCoin>(), 0); let account_addr = signer::address_of(account); let (min_my_coin_liq, min_aptos_coin_liq) = router::calc_optimal_coin_values<RealCoin, AptosCoin, Uncorrelated>( 1000, 10, 1, 1 ); let my_coin_liq = coin::withdraw<RealCoin>(account, min_my_coin_liq); let aptos_liq = coin::withdraw<AptosCoin>(account, min_aptos_coin_liq); let (my_coin_remainder, aptos_remainder, lp) = router::add_liquidity<RealCoin, AptosCoin, Uncorrelated>( my_coin_liq, min_my_coin_liq, aptos_liq, min_aptos_coin_liq, ); coin::deposit(account_addr, my_coin_remainder); coin::deposit(account_addr, aptos_remainder); if (!coin::is_account_registered<LP<RealCoin, AptosCoin, Uncorrelated>>(account_addr)) { coin::register<LP<RealCoin, AptosCoin, Uncorrelated>>(account); }; coin::deposit(account_addr, lp); } }
-
编译、部署合约
aptos move compile --named-addresses RealCoin=a3 aptos move publish --profile=a3 --named-addresses RealCoin=a3
-
先给自己 mint 点代币,因为
aptos-cli
对--type-args
的解析有些问题(活着奶爸没理解使用方法),无法使用aptos-cli
调用需要范型的module
,所以我们在这里使用 TypeScript 的 SDK。这里是示例文件const rawTxn = await this.generateTransaction(minter.address(), { function: "0x1::managed_coin::mint", type_arguments: [`${minter.address()}::real_coin::RealCoin`], arguments: [receiverAddress.hex(), amount], });
-
调用
add_lp
模块创建池子并添加 LPaptos move run --profile a3 --function-id "a3::add_lp::create_pool" aptos move run --profile a3 --function-id "a3::add_lp::add_liquidity"