Instaswap-core
NPM module which allows developers to easily build exchange platform where users can swap assets into LP tokens of several De-Fi AMM and vice versa.
You can find full example codes here => instaswap-core-example
1. Install Instaswap Core
yarn add @ratio-finance/instaswap-core
2. Load the InstaSwap instance
const conn = new Connection("https://api.metaplex.solana.com");
const instaSwap = new InstaSwap(conn);
await instaSwap.load();
3. Types and Interfaces
export type LpTokenInfo = {
address: string;
decimals: number;
name: string;
platform: string;
swapAccount: string;
swapInfo?: any,
icon?: any,
underlyings: Array<{
mint: string;
decimals: number;
}>;
}
We use TokenInfo
type which is from '@solana/spl-token-registry'
import { TokenInfo } from '@solana/spl-token-registry';
export interface TokenInfo {
readonly chainId: number;
readonly address: string;
readonly name: string;
readonly decimals: number;
readonly symbol: string;
readonly logoURI?: string;
readonly tags?: string[];
readonly extensions?: TokenExtensions;
}
4. Get Input token
import { USDC_MINT } from '@ratio-finance/instaswap-core';
const usdcToken = instaSwap.inputTokens.get(USDC_MINT);
5. Get available LP tokens which insta-swap supports
const lpTokenMap: Map<string, LpTokenInfo> = instaSwap.lpTokens;
const saberUshUsdcLp = lpTokenMap.get("USHETMBXzMCpwLabQQe713npSbupyQrsyb2kBw6PRJi");
console.log("saberUshUsdcLp =", saberUshUsdcLp);
Output:
saberUshUsdcLp = {
address: 'USHETMBXzMCpwLabQQe713npSbupyQrsyb2kBw6PRJi',
decimals: 9,
name: 'USH-USDC',
swapAccount: 'HEDSDjj84KS7LKhZY7ejkCACTdPen62rKhCby1wXtgxc',
platform: 'SABER',
underlyings: [
{
mint: '9iLH8T7zoWhY7sBmj1WK9ENbWdS1nL8n9wAxaeRitTa6',
decimals: 0
},
{
mint: 'JEFFSQ3s8T3wKsvp4tnRAsUBW7Cqgnf8ukBZC4C8XBm1',
decimals: 0
}
]
}
6. Get estimation of LP swap output amount
Swap Single Token into LP Token
const estimatedLpAmount = await instaSwap.getLpOutUiAmount(
usdcToken, // inputToken: TokenInfo | string
usdcSwapAmount, // inputAmount
saberUshUsdcLp, // or lpToken Address
1, // 1% slippage
);
Swap LP Token into Single Token
const estimatedWithdrawAmount = await instaSwap.getWithdrawOutUiAmount(
saberUshUsdcLp, // Input Lp
lpSwapAmount, // Input Amount
usdcToken, // Output Token
1 // Slippage - 1%
);
7. Execute insta-swap transactions
Swap Single Token into LP Token
const result = await instaSwap.getBuyTransactions(
USER_KEYPAIR.publicKey, // user public key
usdcToken, // inputToken: TokenInfo | string
usdcSwapAmount, // inputAmount
saberUshUsdcLp, // or lpToken.address
1, // 1% slippage
);
// now, you can execute transactions
if (result) {
let transactions = result.allTransactions;
for (let i = 0; i < transactions.length; i ++) {
transactions[i].recentBlockhash = (await conn.getLatestBlockhash()).blockhash;
transactions[i].feePayer = USER_KEYPAIR.publicKey;
let txHash = await sendAndConfirmTransaction(conn, transactions[i], [USER_KEYPAIR]);
await sleep(1000);
}
}
Swap LP Token into Single Token
const instaSellTransactions = await instaSwap.getSellTransactions(
USER_KEYPAIR.publicKey, // User Public Key
raydiumUsdtUsdcKey, // Input Lp
lpSwapAmount, // Input Amount
usdcToken, // Output Token
1 // Slippage - 1%
);
// now, you can execute transactions
if (result) {
let transactions = result.allTransactions;
for (let i = 0; i < transactions.length; i ++) {
transactions[i].recentBlockhash = (await conn.getLatestBlockhash()).blockhash;
transactions[i].feePayer = USER_KEYPAIR.publicKey;
let txHash = await sendAndConfirmTransaction(conn, transactions[i], [USER_KEYPAIR]);
await sleep(1000);
}
}