Instaswap-hook

This hook library is for developers who wish to integrate the instaswap feature in their frontend application.

You can find full example codes here => instaswap-hook-example

1. Install Instaswap-hook

yarn add @ratio-finance/instaswap-hook

2. Import useInstaSwap and its props type

import { 
    useInstaSwap, 
    useInstaSwapProps, 
    PLATFORMS 
} from "@ratio-finance/instaswap-hook";

3. Wrap with InstaSwapProvider

import { InstaSwapProvider } from "@ratio-finance/instaswap-hook";

const InstaSwapWrapper: React.FC = ({ children }) => {
  const { connection } = useConnection();
  const wallet = useWallet();
  return (
    <InstaSwapProvider connection={connection} userPublicKey={wallet.publicKey}>
      {children}
    </InstaSwapProvider>
  )
}

4. Get values via useInstaSwap

  • Parameter type of useInstaSwap

export type useInstaSwapProps = {
  amount?: number;
  token?: string;
  lpToken?: string;
  slippage?: number;
  direction?: boolean;  // true: Buy LP Mode,  false: Sell LP Mode
}
  • Get values of instaswap context via useInstaSwap.

const { 
  avaLpTokens,           // Map<string, LpTokenInfo>
  avaTokens,             // Map<string, TokenInfo>
  estimatedOutAmount,    // number
  avaLpMints,            // string[]
  avaTokenMints,         // string[]
  loading,               // boolean
  execute                // (props: ExecuteParams) => Promise<{ err?: any }>,
} = useInstaSwap({
  amount: 1,
  token: USDC_MINT,
  lpToken: RAYDIUM_USDT_USDC_LP,
  slippage: 1, // 0.1%,
  direction: true        // direction is True as default
});

You can check the type of LpTokenInfo and TokenInfo in instaswap-core.

  • avaLpTokens

Available LP tokens which current insta-swap offers.

  • avaTokens

Available tokens which can be swapped into LP tokens in insta-swap.

  • estimatedOutAmount

Estimated output amount of LP token. This is not decimals amount but is ui amount, so that you can directly show this value on ui.

  • avaLpMints, avaTokenMints

avaLpMints: Mint addresses of avaLpTokens

avaTokenMints: Mint addresses of avaTokens

5. Execute insta-swap

  • Parameter type of execute function

export type ExecuteParams = {
  wallet: Pick<SignerWalletAdapter, 'signAllTransactions' | 'publicKey' | 'sendTransaction' | 'signTransaction'>,
  beforeSend?: (txTotalCnt: number, txIdx: number) => void;
  afterSend?: (txTotalCnt: number, txIdx: number, txHash: string) => void;
  onSuccess?: (txResult: string[]) => void;
  onFail?: (txResult: string[]) => void;
  confirmType?: ConfirmTypes; // Default is Processed
}
// ConfirmTypes
export const enum ConfirmTypes {
  Confirmed,  // Confirm every transactions as 'confirmed' after sent
  Processed,  // Confirm every transactions as 'processed' after sent
  ConfirmLastTx,  // Confirm every transactions as 'processed' 
                  // and for only last transaction confirm as 'confirmed'
}
  • Code snippet to show how to run execute function.

  const executeInstaswap = () => {
    if (
      wallet.sendTransaction &&
      wallet.signAllTransactions &&
      wallet.signTransaction
    ) {
      execute({
        connection,
        wallet: {
          sendTransaction: wallet.sendTransaction,
          publicKey: wallet.publicKey,
          signAllTransactions: wallet.signAllTransactions,
          signTransaction: wallet.signTransaction,
        },
        beforeSend: (txTotalCnt, txIdx) => {
          console.log(`sending ${txIdx + 1} of ${txTotalCnt} transactions`);
        },
        afterSend: (txTotalCnt, txIdx, txHash: string) => {
          console.log(
            `sent ${txIdx + 1} of ${txTotalCnt} transactions, txHash: ${txHash}`
          );
        },
        onSuccess: (txResults: string[]) => {
          console.log(`swap success. Hashes: ${txResults}`);
        },
        onFail: (txResults: string[]) => {
          console.log(`swap failed. Hashes: ${txResults}`);
        },
      }).then(res => {
        if (res.err) {
          console.error(res.err);
        } else {
          console.log('Success: no error');
        }
      })
    }
  }

There might be several transactions for insta-swap.

This callback is called before sending a transaction.

txTotalCnt is the count of total transactions and txIdx is the index of transaction being sent.

beforeSend(txTotalCnt, txIdx) => void

This callback is called after sending a transaction whose hash is txHash

afterSend(txTotalCnt, txIdx, txHash: string) => void

This callback is called when insta-swap is finished successfully. txResult is the array of transaction hashes which sent for insta-swap.

onSuccess(txResult: string[]) => void

This callback is called when insta-swap is failed.

onFail(txResult: string[]) => void

Last updated