@swapkit/thorchain

Plugin implementing Thorchain support for swaps, liquidity providing, network deposits, bonds, node management, THORNames management and more

Getting started

Installation

<pnpm|bun> add @swapkit/thorchain

Integration

Implementation with @swapkit/core

import { SwapKit } from '@swapkit/core'
import { ThorchainPlugin } from '@swapkit/thorchain'
import { keystoreWallet } from '@swapkit/wallet-keystore'

const swapKitClient = SwapKit({
    wallets: { ...keystoreWallet },
    plugins: { ...ThorchainPlugin },
});

const txHash = await swapKitClient.swap({ pluginName: "thorchain", ...params })
// or
const txHash = await swapKitClient.thorchain.swap(params)

Methods

swap(params: GenericSwapParams | SwapWithRouteParams): string

Swaps assets over cross-chain Thorchain network. Used with routes returned from SwapKitApi.getSwapQuote

const { routes } = await SwapKitApi.getSwapQuote(...)

const txHash = await swapKitClient.thorchain.swap(routes[0])

addLiquidity(params: AddLiquidityParams) => Promise<{ runeTx: string | void; assetTx: string | void; }>

Performs two transactions to deposit RUNE and asset to THORChain Liquidity Pool

import { AssetValue, Chain } from '@swapkit/helpers'

const runeAssetValue = AssetValue.fromChainOrSignature(Chain.THORChain, 100)
const btcAssetValue = AssetValue.fromChainOrSignature(Chain.Bitcoin, 0.01)

const {
 runeTx,
 assetTx,
} = swapKitClient.thorchain.addLiquidity({
  // runeAddr: used when can't connect both chain at once (use addLiquidityPart)
  runeAssetValue,
  assetValue: btcAssetValue,
  mode: "sym"
})

addLiquidityPart(params: AddLiquidityPartParams) => Promise<string>

Performs transaction to deposit RUNE or asset to THORChain Liquidity Pool

import { AssetValue, Chain } from '@swapkit/helpers'

const runeAssetValue = AssetValue.fromChainOrSignature(Chain.THORChain, 100)
const btcAssetValue = AssetValue.fromChainOrSignature(Chain.Bitcoin, 0.01)
const runeAddress = swapKitClient.getAddress(Chain.THORChain)
const btcAddress = swapKitClient.getAddress(Chain.Bitcoin)

const runeTx = swapKitClient.thorchain.addLiquidityPart({ 
  address: btcAddress,
  assetValue: runeAssetValue
  poolAddress: btcAssetValue.toString()
  symmetric: true,
})

const btcTx = swapKitClient.thorchain.addLiquidityPart({ 
  address: runeAddress,
  assetValue: btcAssetValue
  poolAddress: btcAssetValue.toString()
  symmetric: true,
})

deposit(params: CoreTxParams & { router?: string; }) => Promise<string>

Performs deposit to THORChain pool transaction. Can use custom memo.

const depositTxHash = swapKitClient.thorchain.deposit({
  assetValue,
  recipient,
  memo: customDepositMemo, // Create LP Pool, do custom swap, deposit for memoless
})

loan(params: LoanParams) => Promise<string>

Performs transaction to open or close loan.

const repayQuote = await SwapKitApi.getRepayQuote(...)

const txid = await swapkitClient.thorchain.loan({
    type: 'close',
    memo: repayQuote?.memo,
    assetValue: repayAsset.set(amount),
    minAmount: repayAsset.set(expectedAmount),
});

savings(params: SavingsParams) => Promise<string>

Performs transaction to deposit or withdraw provided asset from THORChain Savers

const btcAsset = AssetValue.fromChainOrSignature(Chain.Bitcoin, 1)

const saverVaultDepositTx = await swapkitClient.thorchain.savings({ 
    assetValue,
    type: 'add',
});

withdraw(params: WithdrawParasm) => Promise<string>

Performs transaction to register, extend THORName

const ethAddress = swapKitClient.thorchain.withdraw({
    assetValue: poolAsset,
    percent: "50",
    from: "sym",
    to: "rune",
})

registerThorname(params: RegisterTHORNameParams): string

Performs transaction to register, extend THORName

const txHash = swapKitClient.thorchain.registerThorname({
    assetValue: AssetValue.fromChainOrSignature(Chain.THORChain, amount),
    address, 
    owner, 
    name: "tnsNameToRegister", 
    chain: Chain.Bitcoin, // Chain to assign - only when already registered
})

createLiquidity({ runeAssetValue: AssetValue; assetValue: AssetValue }) => Promise<{ runeTx: string; assetTx: string }>

Performs 2 transactions to create new THORChain Liquidity Pool

const { runeTx, assetTx } = await swapKitClient.thorchain.createLiquidity({
  runeAssetValue: runeAssetAmount,
  assetValue: poolAssetAmount,
});

nodeAction(params: NodeActionParams): Promise<string>

Performs deposit to protocol with convenient method wrapping and structuring proper memo

const bondTxHash = await swapKitClient.nodeAction({
  type: "bond",
  assetValue: AssetValue.fromChainOrSignature(Chain.THORChain, 500_000)
})

getInboundDataByChain(chain: Chain): Promise<InboundAddressesItem>

Returns inbound info of given chain

const ethInboundInfo = await swapKitClient.getInboundDataByChain(Chain.Ethereum)

approveAssetValue(params: ApproveParams): Promise<string | true>

Performs approval transaction or returns true if given asset doesn't need approval

const txHash = await swapKitClient.approveAssetValue({
  assetValue: assetToApprove, 
  contractAddress
})

isAssetValueApproved(params: ApproveParams): Promise<boolean>

Checks if given asset needs approval. Additionally validates provided amount against approved contract spending.

const isAssetApproved = await swapKitClient.isAssetValueApproved({
  assetValue: assetToApprove, 
  contractAddress
})

Types

AddLiquidityPartParams

type AddLiquidityPartParams = {
  assetValue: AssetValue;
  address?: string;
  poolAddress: string;
  symmetric: boolean;
};

AddLiquidityParams

type AddLiquidityParams = {
  runeAssetValue: AssetValue;
  assetValue: AssetValue;
  isPendingSymmAsset?: boolean;
  runeAddr?: string;
  assetAddr?: string;
  mode?: "sym" | "rune" | "asset";
}

ApproveParams

type ApproveParams = {
  assetValue: AssetValue;
  contractAddress?: string;
};

CoreTxParams

type CoreTxParams = {
  assetValue: AssetValue;
  recipient: string;
  memo?: string;
  feeOptionKey?: FeeOption;
  feeRate?: number;
  data?: string;
  from?: string;
  expiration?: number;
};

LoanParams

 type LoanParams = {
  assetValue: AssetValue;
  memo?: string;
  minAmount: AssetValue;
  type: "open" | "close";
};

NodeActionParams

type NodeActionParams = { address: string } & (
  | { type: "bond" | "unbond"; assetValue: AssetValue }
  | { type: "leave"; assetValue?: undefined }
);

SwapWithRouteParams

type SwapWithRouteParams = {
  recipient: string;
  route: QuoteRoute | QuoteRouteV2;
  feeOptionKey?: FeeOption;
  quoteId?: string;
  streamSwap?: boolean;
}

SavingParams

type SavingsParams = { assetValue: AssetValue; memo?: string } & (
  | { type: "add"; percent?: undefined }
  | { type: "withdraw"; percent: number }
);

SwapWithRouteParams

type SwapWithRouteParams = {
  recipient: string;
  route: QuoteRoute | QuoteRouteV2;
  feeOptionKey?: FeeOption;
  quoteId?: string;
  streamSwap?: boolean;
}

RegisterTHORNameParams

type RegisterTHORNameParams = {
  assetValue: AssetValue
  name: string;
  chain: string;
  address: string;
  owner?: string;
  preferredAsset?: string;
  expiryBlock?: string;
};

WithdrawParams

type WithdrawParams = {
  assetValue: AssetValue;
  from: "sym" | "rune" | "asset";
  memo?: string;
  percent: number;
  to: "sym" | "rune" | "asset";
};

Last updated