This article explains how to prepare and execute swap through specific pool.
In this example we will swap 1 XLM to AQUA with Aquarius AMM. The swap will be executed by calling corresponding method of specific liquidity pool.
Direct swap through specific pool is good for contract sub-invocations as it requires less resources than chained path swap. But direct swap may lead to less optimal result.
โ๏ธ Please make sure account has established trustline for asset to receive. Otherwise, swap will fail until trustline is created. For more information please refer to documentation on Stellar.org.
To perform a swap, you need to follow these steps:
1. Identify pool address: This part was covered earlier, please check corresponding article.
2. Specify user secret key, pool address, amount in, input and output token indices: You need to specify pool address, the amount of the input token you want to swap in stroops and swap direction by in and out tokens.
# Your Secret Key (keep it secure!)user_secret_key ="S......."# XLM/AQUA pool addresspool_address ="CCY2PXGMKNQHO7WNYXEWX76L2C5BH3JUW3RCATGUYKY7QQTRILBZIFWV"# Amount in (0.1 XLM), in stroops (1 XLM = 10^7 stroops)amount_in =1000000# tokens are [XLM, AQUA], so XLM index is 0in_idx =0out_idx =1# slippage percentslippage_percent =1
// Your Secret Key (keep it secure!)constuserSecretKey="S.......";// XLM/AQUA pool addressconstpoolAddress="CCY2PXGMKNQHO7WNYXEWX76L2C5BH3JUW3RCATGUYKY7QQTRILBZIFWV";// 0.1 XLM in stroopsconstamountIn=1000000;// tokens are [XLM, AQUA], so XLM index is 0constinIdx=0;constoutIdx=1;// slippage percentconstslippagePercent=1;
3. Calculate minimum amount of token to receive: This can be achieved by simulating estimate_swap pool method.
4. Perform swap operation by submitting corresponding transaction.
Complete Code Examples
This code swaps 1 XLM to AQUA with Aquarius AMM on mainnet.
To successfully execute the code, provide the secret key of a Stellar account with at least 3 XLM and an established trustline for AQUA.
Copy the full code JavaScriptCopy the full code Python