@swapkit/core

SwapKit Client that wraps wallets and method in clear and easy to use interface.

Getting started

Installation

<pnpm|bun> add @swapkit/core

Usage

import { Chain, SwapKit, SwapKitApi } from "@swapkit/core"
import { ledgerWallet } from "@swapkit/wallet-ledger"
import { keystoreWallet } from "@swapkit/wallet-keystore"
import { xdefiWallet } from "@swapkit/wallet-xdefi"
import { ThorchainPlugin } from "@swapkit/thorchain"

const client = SwapKit({
  wallets: { ...ledgerWallet, ...keystoreWallet, ...xdefiWallet },
  plugins: { ...ThorchainPlugin }
})

const swap = async () => {
    await client.connectXDEFI([Chain.Bitcoin, Chain.Ethereum])
// await client.connectKeystore([Chain.Bitcoin, Chain.Ethereum], "phrases ...")
// await client.connectLedger([Chain.Bitcoin])
    const { routes } = SwapKitApi.getSwapQuote({
        sellAsset: "ETH.ETH"
        sellAmount: 1,
        buyAsset: "BTC.BTC",
        senderAddress: client.getAddress(Chain.Ethereum),
        recipientAddress: client.getAddress(Chain.Bitcoin),
        slippage: "3",
    })

   const txHash = await client.swap({ pluginName: "thorchain",  ...routes[0] })
    
   const txExplorerUrl = client.getExplorerUrl({ chain: Chain.Ethereum, txHash })
}

Methods

getAddress(chain: Chain): string

Returns address of previously connected chains with `connectX` method from wallet, if not connected returns empty string

const ethAddress = swapKitClient.getAddress(Chain.Ethereum)

if (ethAddress) {
  const addressExplorerUrl = swapKitClient.getExplorerAddressUrl({   
    chain: Chain.Ethereum, address: ethAddress 
  })
}

getBalance(chain: Chain, refresh?: boolean): Promise<AssetValue[]>

Returns current chain wallet balance. Can fetch newest available balance when provided with `refresh` param

const currentBalance = await swapKitClient.getBalance(Chain.Ethereum)

const newestBalance = await swapKitClient.getBalance(Chain.Ethereum, true)

getExplorerAddressUrl(params: { address: string; chain: Chain }): string

Returns url for address explorer. Useful for tracking connected addresses

const ethAddress = swapKitClient.getAddress(Chain.Ethereum)

const etherscanUrl = swapKitClient.getExplorerAddressUrl({
  address: ethAddress,
  chain: Chain.Ethereum,
})

getExplorerTxUrl(params: { txHash: string; chain: Chain }): string

Returns url for transaction explorer. Useful for tracking executed transactions

const ethTxHash = await swapKitClient.swap({ 
  pluginName: "thorchain",
  sellAsset: "ETH.USDC-...",
  ...
})

const txHashExplorerUrl = swapKitClient.getExplorerTxUrl({
  txHash: ethTxHash,
  chain: Chain.Ethereum,
})

getWallet(chain: Chain): ChainWallet

Returns connected wallet chain info

await swapKitClient.connectXDEFI([Chain.Ethereum])

const ethWallet = swapKitClient.getWallet(Chain.Ethereum)

const {
  chain: Chain;
  address: string;
  balance: AssetValue[];
  walletType: WalletOption;
} = ethWallet

getWalletWithBalance(chain: Chain): Promise<ChainWallet>

Fetches newest wallet balances, updated connected wallet with it and returns chain wallet. Useful for loading initial balance for connected chains.

await swapKitClient.connectXDEFI([Chain.Ethereum])

const ethWallet = await swapKitClient.getWalletWithBalance(Chain.Ethereum)

const {
  chain: Chain;
  address: string;
  balance: AssetValue[];
  walletType: WalletOption;
} = ethWallet

swap(params: { pluginName: PluginName, ...paramsForPluginSwap }): Promise<string>

Returns transaction hash after successful execution. Params depeds on connected/used plugin. Check thorchain or chainflip swap params

const ethTxHash = await swapKitClient.swap({ 
  pluginName: "thorchain",
  ...
})

validateAddress(params: { address: string; chain: Chain }): boolean

Validates given address for chain.

This function works only for connected chains

const address = "0x..."
const isValid = swapKitClient.validateAddress({
  chain: Chain.Ethereum
  address,
})

if (isValid) {
  const tXHash = swapKitClient.swap({
    recipient: address
    ...
  })
}

Last updated