# Instaswap-core

You can find full example codes here =>  [instaswap-core-example](https://github.com/RatioFinance/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

<pre><code><strong>import { USDC_MINT } from '@ratio-finance/instaswap-core';
</strong>const usdcToken = instaSwap.inputTokens.get(USDC_MINT); 
</code></pre>

#### 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*:&#x20;

```
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);
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ratio.finance/for-developers/instaswap-sdk/instaswap-core.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
