Skip to main content

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.

ChainFactory
Terraterra14x9fr055x5hvr48hzy2t4q7kvjvfttsvxusa4xsdcy702mnzsvuqprer8r
Neutronneutron1hptk0k5kng7hjy35vmh009qd5m6l33609nypgf2yc6nqnewduqasxplt4e
NOTE

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​


_3
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
_3
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
_3
import { GasPrice } from "@cosmjs/stargate";

Step 2: Define Constants​


_3
const rpc = "https://neutron-rpc.polkachu.com";
_3
const factory = "neutron1hptk0k5kng7hjy35vmh009qd5m6l33609nypgf2yc6nqnewduqasxplt4e";
_3
const myToken = "factory/neutron1...creator/mytoken"; // the token you created

Step 3: Create the Wallet and Signing Client​


_8
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(process.env.MNEMONIC, {
_8
prefix: "neutron",
_8
});
_8
const [{ address: sender }] = await wallet.getAccounts();
_8
_8
const 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​


_8
const main = async () => {
_8
// Place the code from the next sections here
_8
};
_8
_8
main().catch((error) => {
_8
console.error("An error occurred:", error);
_8
process.exit(1);
_8
});

Create the Pair​


_12
const assetInfos = [
_12
{ native_token: { denom: myToken } },
_12
{ native_token: { denom: "untrn" } },
_12
];
_12
_12
const createTx = await client.execute(
_12
sender,
_12
factory,
_12
{ create_pair: { pair_type: { xyk: {} }, asset_infos: assetInfos } },
_12
"auto"
_12
);
_12
console.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​


_7
const pairInfo = await client.queryContractSmart(factory, {
_7
pair: { asset_infos: assetInfos },
_7
});
_7
_7
const pool = pairInfo.contract_addr;
_7
const lpToken = pairInfo.liquidity_token; // factory/<pool>/astroport/share on Neutron
_7
console.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.


_6
await 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.


_24
const myTokenAmount = "1000000000"; // 1,000 MYTOKEN (6 decimals)
_24
const ntrnAmount = "500000000"; // 500 NTRN
_24
_24
const 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
);
_24
console.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.