Swapping
This tutorial will walk you through Astroport's swap
functionality using CosmJS.
Setup
Step 1: Import Necessary Libraries
Import the required libraries to connect to the chain and perform transactions.
_3import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";_3import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";_3import { GasPrice } from "@cosmjs/stargate";
Step 2: Define Constants
Specify the required constants, such as the RPC URL, mnemonic, and factory contract address.
This tutorial includes the mnemonic directly in the code for quick setup. While convenient for testing, this exposes a sensitive piece of information and is not secure for production use. Always follow secure key management practices and never expose the mnemonic in real-world scenarios.
_3const rpc = '<rpc-url>'_3const mnemonic = ''_3const pairContractAddress = 'neutron...'
Step 3: Create a Wallet
Create a wallet using the mnemonic for the specific chain prefix.
_4const wallet = await DirectSecp256k1HdWallet.fromMnemonic(_4 mnemonic, { _4 prefix: "neutron" // neutron, or terra_4 });
Step 4: Retrieve Wallet Address
Get the wallet address from the wallet.
_2const firstAccount = await wallet.getAccounts();_2const walletAddress = firstAccount[0].address
Step 5: Set Up the Signing Client
Configure the signing client for connecting to the chain.
_5const signingClient = await SigningCosmWasmClient.connectWithSigner(_5 rpc, _5 wallet, _5 { gasPrice: GasPrice.fromString("0.0053untrn") }_5)
Step 6: Wrap Code Inside an Async Function with Error Handling
Wrap the code inside a start() function, marked as async, to handle the asynchronous operations throughout the code. Also, include a try/catch block for robust error handling. This structure enables the use of await with async functions and ensures proper management of any errors that may occur.
_12const start = async () => {_12 try {_12 // Place all the code inside this try block_12_12 // ..._12_12 } catch (error) {_12 console.error('An error occurred:', error);_12 }_12}_12_12start();
Swap
Astroport implements the same smart contracts API for swapping across its various pool types (xyk, stable, pcl). The swap
function performs a direct swap between two assets in a liquidity pool.
Step 1: Define the Swap Message
Create the swap message with asset details.
_20const swapMsg = {_20 "swap": {_20 "offer_asset": {_20 "info": {_20 "token": {_20 "contract_addr": "..."_20 }_20 }, _20 "amount": "10000000"_20 }, _20 "ask_asset_info": {_20 "native_token": {_20 "denom": "..."_20 }_20 },_20 "belief_price": beliefPrice, _20 "max_spread": "0.1",_20 "to": "...."_20 }_20};
Params | Type | Description |
---|---|---|
[offer_asset ] | Asset | Offer asset |
[ask_asset_info ] | Option<AssetInfo> | Information about an asset stored in a [AssetInfo ] struct |
[belief_price ] | Option<Decimal> | Belief price |
[max_spread ] | Option<Decimal> | The difference between the ask amount before and after the swap operation. If the swap spread exceeds the provided max limit, the swap will fail. If belief_price is provided in combination with max_spread , the pool will check the difference between the return amount (using belief_price ) and the real pool price. |
[to ] | Option<String> | Address receiving tokens (if different from sender) |
Asset
This enum contains asset info and a token amount.
Params | Type | Description |
---|---|---|
info | AssetInfo | Information about an asset stored in a [AssetInfo ] struct |
amount | Uint128 | A token amount |
AssetInfo
AssetInfo is a convenience wrapper to represent whether a token is the native one (from a specific chain, like LUNA for Terra) or not. It also returns the contract address of that token.
Variants | Description |
---|---|
Token | Non-native Token |
NativeToken | Native token |
Simulation
Enpoint
Astroport's native solution for calculating the belief_price
is the simulation
query.
The query takes in an offer_asset
which contains information about the native token and an amount to send.
_17const simulationQuery = await client.queryContractSmart(_17 pairContract,_17 {_17 "simulation": {_17 "offer_asset": {_17 "info": {_17 "native_token": {_17 "denom": "uluna"_17 }_17 },_17 "amount": "100000"_17 }_17 }_17 }_17);_17_17console.log(simulationQuery);
Depending on the token decimal for the assets you are swapping, you may need to normalize the response from the simulation
query before passing it into your swap
message.
_1beliefPrice = String(beliefPrice * 1000000);
Step 2: Execute Swap
Execute the swap transaction.
_9const executeSwap = await signingClient.execute(_9 walletAddress,_9 pairContractAddress,_9 swapMsg,_9 "auto", // fee_9 "" // memo_9);_9_9console.log(JSON.stringify(executeSwap));