♻️ Swap
Swap ♻️
To start with the Swap implementation we need to get all the Swap related functions and all the necessary Helper functions from the @saros-finance/sdk:
import {
getSwapAmountSaros,
swapSaros,
genConnectionSolana,
} from "@saros - finance / sdk";
import { PublicKey, clusterApiUrl, Connection } from "@solana / web3.js";Connection
Then we need a Connection to the Solana Cluster(network). we can use the saros-finanace genConnectionSolana helper function or use the solana Connection Class
//using @saros-finance genConnectionSolana function
const connection = genConnectionSolana();
//using @solana/web3.js Connection
const connection = new Connection(clusterApiUrl.mainnet, "confirmed");Swap Configs
So now that we are almost ready lets configure some of the configurations we are going to use for the Swap Operations
//Token Program ID
const TOKEN_PROGRAM_ID = new PublicKey(
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
);
//SAROS SWAP PROGRAM
const SAROS_SWAP_PROGRAM_ADDRESS_V1 = new PublicKey(
"SSwapUtytfBdBn1b9NUGG6foMVPtcWgpRU32HToDUZr"
);
const userAccount = "5UrM9csUEDBeBqMZTuuZyHRNhbRW4vQ1MgKJDrKU1U2v"; // owner address
const SLIPPAGE = 0.5;Swap POOLS
SPL Tokens to perform the Swap against and Pool Params in this example we will use the tokens C98 and USDC
// Pool example on saros C98 to USDC
const USDC_TOKEN = {
id: "usd - coin",
mintAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
symbol: "usdc",
name: "USD Coin",
icon: "https://raw.githubusercontent.com / solana - labs / token - list / main / assets / mainnet / EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v / logo.png",
decimals: "6",
addressSPL: "FXRiEosEvHnpc3XZY1NS7an2PB1SunnYW1f5zppYhXb3",
};
const C98_TOKEN = {
id: "coin98",
mintAddress: "C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9",
symbol: "C98",
name: "Coin98",
icon: "https://coin98.s3.ap - southeast - 1.amazonaws.com / Coin / c98 - 512.svg",
decimals: "6",
addressSPL: "EKCdCBjfQ6t5FBfDC2zvmr27PgfVVZU37C8LUE4UenKb",
};
//PoolParams
const poolParams = {
address: "2wUvdZA8ZsY714Y5wUL9fkFmupJGGwzui2N74zqJWgty",
tokens: {
C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9: {
...C98_TOKEN,
},
EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: {
...USDC_TOKEN,
},
},
tokenIds: [
"C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9",
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
],
};Swap Parameters
We will have a Swap helper function that will Trigger the swapSaros when called. the swapSaros function takes some shitty ton of args:
- connection : the network instance
- fromTokenAccount: a string of the input token account address
- toTokenAccount: a string of the output token account address
- fromAmount: the input token amount(number)
- toAmount: the estimated output amount(number) of the receiving token that is generated by
getSwapAmountSaroshelper function - null: uknown thing to me just add null at the place.
- poolParamsAddress: address of the poolParams (PublicKey)
- sarosSwap_program-address: the program address of Saros Swap Program
- userAccount: the address of the user
- fromTokenMint: mint address of the input token (string)
- toTokenMint: mint address of the receiveing token (string)
Usage
const Swap = async () => {
try {
const fromTokenAccount = C98_TOKEN.addressSPL;
const toTokenAccount = USDC_TOKEN.addressSPL;
const fromTokenMint = C98_TOKEN.mintAddress;
const toTokenMint = USDC_TOKEN.mintAddress;
const amount = parseFloat(10);
//helper function to estimate the receiving amount(output)
const estimateSwap = await getSwapAmountSaros(
connection,
fromTokenMint,
toTokenMint,
amount,
SLIPPAGE,
poolParams
);
//output token amount including fees from slippage min received amount by user(performing the swap)
const { amountOutWithSlippage } = estimateSwap;
const swapResponse = await swapSaros(
connection,
fromTokenAccount.toString(),
toTokenAccount.toString(),
amount,
parseFloat(amountOutWithSlippage),
null,
new PublicKey(poolParams.address),
SAROS_SWAP_PROGRAM_ADDRESS_V1,
userAccount,
fromTokenMint,
toTokenMint
);
const { isError, hash} = swapResponse;
if (isError) {
throw new Error();
} else {
return hash;
}
} catch (error) {
if (error instanceOf Error) console.error(error.message)
}
}This example demonstrates how to:
- Fetch/estimate the minimum receiving amount of the swap
- Carry out a successfull Swap transaction
Congratulations Dev We Successfully Lazy Coded a Swap functionality on your Dapp using Saros AMM SDK: