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

  • Get values of instaswap context via useInstaSwap.

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

  • Code snippet to show how to run execute function.

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