0.0.1-rc-3 • Published 10 months ago

@kitelabs/swap-widget v0.0.1-rc-3

Weekly downloads
-
License
-
Repository
-
Last release
10 months ago

Mosaic Swap Widget

The Swap Widget bundles the whole swapping experience into a single React component that developers can easily embed in their app with a few lines of code.

Installation

Install the widgets library via npm ,pnpm or yarn.

yarn add @kitelabs/swap-widget
npm i --save @kitelabs/swap-widget

Usage

// import it in the root component in react, (ex: main.tsx/App.tsx)
import '@kitelabs/swap-widget/style.css' 

import SwapWidget from '@kitelabs/swap-widget'
<SwapWidget wallet={wallet} />

💡 Demo & Example code

Widget configs

import { Asset } from '@/constants/asset'
import { AnyRawTransaction, UserTransactionResponse } from '@aptos-labs/ts-sdk'
import {
  AptosSignAndSubmitTransactionInput,
  AptosSignAndSubmitTransactionOutput,
  AptosSignTransactionOutput,
  UserResponse,
  WalletAccount,
} from '@aptos-labs/wallet-standard'
import { CSSProperties } from 'react'

export type WidgetProps = {
  /**
   * wallet provider object, should have some mandatory field
   */
  wallet: AptosWalletStandard
  /**
   * contact us to get a new api key to use our api
   */
  notification?: boolean
  /**
   * default: 1
   */
  defaultInputAmount?: string
  /**
   * default slippage in bips, ex: if set to 50, it will be 0.5%
   */
  defaultSlippage?: number
  /**
   * list slippage options in settings UI, value should be in bips, default: [5, 10, 50, 100]
   */
  slippageOptions?: [number, number, number, number]
  /**
   * custom style
   */
  style?: WidgetStyle
  /**
   * custom theme colors
   */
  theme?: Theme
  /**
   * fa address (recommended) or coinType, ex: `0x275f508689de8756169d1ee02d889c777de1cebda3a7bbcce63ba8a27c563c6f::tokens::USDT` or `0xe161897670a0ee5a0e3c79c3b894a0c46e4ba54c6d2ca32e285ab4b01eb74b66`
   */
  defaultTokenInAddress?: string
  /**
   * same as defaultTokenInAddress
   */
  defaultTokenOutAddress?: string
  /**
   * custom threshold to show high or very high price impact, default: { high: 5, veryHigh: 10 }
   */
  priceImpactWarnings?: PriceImpactWarnings
  /**
   *
   * callback when tx is submitted successfully
   */
  onTxSubmit?: (data: { hash: string; metadata: SwapMetadata }) => void
  /**
   *
   * callback when tx is submitted failed
   */
  onTxFail?: (data: { error: Error; metadata: SwapMetadata }) => void
  /**
   *
   * callback when tx is confirmed successfully
   */
  onTxSuccess?: (data: { hash: string; receipt: UserTransactionResponse; metadata: SwapMetadata }) => void
} & (LocalStateProps | GlobalStateProps)

export interface AptosWalletStandard {
  account: WalletAccount | undefined
  signAndSubmitTransaction(
    input: AptosSignAndSubmitTransactionInput,
  ): Promise<UserResponse<AptosSignAndSubmitTransactionOutput>>
  signTransaction(
    transaction: AnyRawTransaction,
    asFeePayer?: boolean,
  ): Promise<UserResponse<AptosSignTransactionOutput>>
}

export type Theme = {
  background?: string
  primary?: string
  secondary?: string
  error?: string
  baseBlack?: string
  blackGrey?: string
  baseGrey?: string
  borderGrey?: string
}

type SelectFunc = (token: Asset) => void

type SelectState = {
  tokenIn: Asset | undefined
  tokenOut: Asset | undefined
  onChangeTokenIn: SelectFunc
  onChangeTokenOut: SelectFunc
  /**
   *
   * event when click switch token / arrow icon
   */
  onSwitchToken: (newTokenIn: Asset, newTokenOut: Asset) => void
}

type LocalStateProps = {
  /**
   *  use our local state or use your state
   *  if you set to false, you need to specify all: tokenIn, tokenOut, onChangeTokenIn, onChangeTokenOut, onSwitchToken
   *  if you set to true, sdk will use our local widget state
   *  default true
   */
  useLocalState?: true
} & Partial<SelectState>

type GlobalStateProps = {
  useLocalState: false
} & SelectState

export type WidgetStyle = {
  /**
   * outer div
   */
  wrapper?: CSSProperties
  /**
   * inner div
   */
  container?: CSSProperties
  inputGroup?: CSSProperties
  outputGroup?: CSSProperties
  /**
   * panel in the bottom
   */
  swapDetail?: CSSProperties
  settings?: CSSProperties
  arrowIcon?: CSSProperties
  selectTokenModal?: CSSProperties
  /**
   * swap button
   */
  ctaButton?: CSSProperties
}

export type PriceImpactWarnings = {
  high: number
  veryHigh: number
}

export type SwapMetadata = { amountIn: string; amountOut: string; tokenIn: Asset; tokenOut: Asset }