Creating and Seeding a Pool
Overview
This tutorial creates a new pool with CosmJS and makes the first deposit into it. It covers:
- Creating a pair through the factory.
- Finding the pool address and its LP token.
- Approving CW20 tokens (only if one of the assets is a CW20).
- Providing the first liquidity, which sets the pool's starting price.
The example creates an xyk pool on Neutron for a tokenfactory token against NTRN. The same steps work on Terra with the Terra factory, Terra denoms and the Terra wallet settings shown below. For the other pool types and their init_params, see Creating a Pair.
| Chain | Factory |
|---|---|
| Terra | terra14x9fr055x5hvr48hzy2t4q7kvjvfttsvxusa4xsdcy702mnzsvuqprer8r |
| Neutron | neutron1hptk0k5kng7hjy35vmh009qd5m6l33609nypgf2yc6nqnewduqasxplt4e |
This tutorial reads the mnemonic from an environment variable to keep the example short. Use proper key management for anything beyond testing, and never commit a mnemonic.
Setup
Step 1: Import the Libraries
_3import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";_3import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";_3import { GasPrice } from "@cosmjs/stargate";
Step 2: Define Constants
_3const rpc = "https://neutron-rpc.polkachu.com";_3const factory = "neutron1hptk0k5kng7hjy35vmh009qd5m6l33609nypgf2yc6nqnewduqasxplt4e";_3const myToken = "factory/neutron1...creator/mytoken"; // the token you created
Step 3: Create the Wallet and Signing Client
_8const wallet = await DirectSecp256k1HdWallet.fromMnemonic(process.env.MNEMONIC, {_8 prefix: "neutron",_8});_8const [{ address: sender }] = await wallet.getAccounts();_8_8const client = await SigningCosmWasmClient.connectWithSigner(rpc, wallet, {_8 gasPrice: GasPrice.fromString("0.0053untrn"),_8});
On Terra, use prefix: "terra", add hdPaths: [stringToPath("m/44'/330'/0'/0/0")] (stringToPath comes from @cosmjs/crypto) and a uluna gas price such as 0.015uluna.
Step 4: Wrap the Code in an Async Function
_8const main = async () => {_8 // Place the code from the next sections here_8};_8_8main().catch((error) => {_8 console.error("An error occurred:", error);_8 process.exit(1);_8});
Create the Pair
_12const assetInfos = [_12 { native_token: { denom: myToken } },_12 { native_token: { denom: "untrn" } },_12];_12_12const createTx = await client.execute(_12 sender,_12 factory,_12 { create_pair: { pair_type: { xyk: {} }, asset_infos: assetInfos } },_12 "auto"_12);_12console.log("Pair created:", createTx.transactionHash);
create_pair fails if the factory already has a pool for this asset pair. On Terra, CW20 tokens are written as { token: { contract_addr: "terra1..." } }.
Find the Pool Address
_7const pairInfo = await client.queryContractSmart(factory, {_7 pair: { asset_infos: assetInfos },_7});_7_7const pool = pairInfo.contract_addr;_7const lpToken = pairInfo.liquidity_token; // factory/<pool>/astroport/share on Neutron_7console.log({ pool, lpToken });
Approve CW20 Tokens (CW20 Assets Only)
If one of the assets is a CW20 token, allow the pool to transfer the amount you will deposit. Skip this for native assets.
_6await client.execute(_6 sender,_6 "<cw20-token-address>",_6 { increase_allowance: { spender: pool, amount: "1000000" } },_6 "auto"_6);
See the CW20 spec for allowances.
Provide the First Liquidity
The first deposit sets the pool's price: depositing 1,000 MYTOKEN and 500 NTRN starts the pool at 0.5 NTRN per MYTOKEN. Match the market price as closely as you can; any difference is an arbitrage opportunity paid for by your deposit. Part of the first deposit's LP tokens stays locked in the pool.
_24const myTokenAmount = "1000000000"; // 1,000 MYTOKEN (6 decimals)_24const ntrnAmount = "500000000"; // 500 NTRN_24_24const provideTx = await client.execute(_24 sender,_24 pool,_24 {_24 provide_liquidity: {_24 assets: [_24 { info: { native_token: { denom: myToken } }, amount: myTokenAmount },_24 { info: { native_token: { denom: "untrn" } }, amount: ntrnAmount },_24 ],_24 auto_stake: false,_24 },_24 },_24 "auto",_24 "",_24 // native funds, sorted by denom ("factory/..." sorts before "untrn")_24 [_24 { denom: myToken, amount: myTokenAmount },_24 { denom: "untrn", amount: ntrnAmount },_24 ]_24);_24console.log("Liquidity provided:", provideTx.transactionHash);
Set auto_stake: true to stake the LP tokens in the Incentives contract in the same transaction. Staked LP tokens earn rewards only if the pool has incentive schedules: see Claiming Rewards for how to add one.