Performing Swaps
Performing Token Swaps
Section titled “Performing Token Swaps”SwapKit provides a unified interface for swapping tokens across different chains and protocols. This guide covers how to get quotes and execute swaps using both the granular and all-in-one approaches.
Two Approaches for Using SwapKit
Section titled “Two Approaches for Using SwapKit”You can use SwapKit in two ways:
-
Granular approach - Import only what you need from individual packages (recommended for frontend):
import {
SwapKit } from "@swapkit/core"; import {function SwapKit<Plugins extends ReturnType<typeof createPlugin>, Wallets extends ReturnType<typeof createWallet>>({ config, plugins, wallets, }?: { config?: SKConfigState; plugins?: Plugins; wallets?: Wallets; }): { [key in keyof Plugins]: ReturnType<...>; } & ... 1 more ... & { ...; }
ThorchainPlugin } from "@swapkit/plugins/thorchain"; import {const ThorchainPlugin: { thorchain: (pluginParams: SwapKitPluginParams) => { supportedSwapkitProviders: ProviderName[]; } & { addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>; ... 12 more ...; withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>; }; }
evmWallet } from "@swapkit/wallets/evm-extensions";const evmWallet: { connectEVMWallet: { connectWallet: (connectParams: { addChain: AddChainType; }) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>; supportedChains: EVMChain[]; }; }
-
All-in-one approach - Import everything from the SDK (better for backend/Node.js):
import {
createSwapKit } from "@swapkit/sdk";function createSwapKit(config?: Parameters<typeof SwapKit>[0]): { chainflip: { supportedSwapkitProviders: ProviderName[]; } & { swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>; }; ... 5 more ...; solana: { ...; } & { ...; }; } & { ...; } & { ...; }
Basic Swap Flow
Section titled “Basic Swap Flow”The typical flow for performing a swap consists of these steps:
- Initialize SwapKit and connect wallets
- Get a quote for the swap
- Approve tokens (if needed)
- Execute the swap transaction
Setting Up SwapKit for Swaps
Section titled “Setting Up SwapKit for Swaps”Granular Approach (Frontend Recommended)
Section titled “Granular Approach (Frontend Recommended)”import { enum Chain
Chain } from "@swapkit/helpers";
import { function SwapKit<Plugins extends ReturnType<typeof createPlugin>, Wallets extends ReturnType<typeof createWallet>>({ config, plugins, wallets, }?: {
config?: SKConfigState;
plugins?: Plugins;
wallets?: Wallets;
}): { [key in keyof Plugins]: ReturnType<...>; } & ... 1 more ... & {
...;
}
SwapKit } from "@swapkit/core";
import { const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet } from "@swapkit/wallets/evm-extensions";
import { const keystoreWallet: {
connectKeystore: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
... 21 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>;
supportedChains: (Chain.Arbitrum | ... 18 more ... | Chain.Solana)[];
};
}
keystoreWallet } from "@swapkit/wallets/keystore";
import { const ThorchainPlugin: {
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
}
ThorchainPlugin } from "@swapkit/plugins/thorchain";
import { const EVMPlugin: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}
EVMPlugin } from "@swapkit/plugins/evm";
// Initialize SwapKit with necessary plugins and wallets
const const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
evm: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit = SwapKit<{
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
thorchain: (pluginParams: SwapKitPluginParams) => {
...;
} & {
...;
};
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
config?: SKConfigState | undefined
config: {
apiKeys?: Partial<{
blockchair: string;
kado: string;
keepKey: string;
swapKit: string;
walletConnectProjectId: string;
}> | undefined
apiKeys: {
swapKit?: string | undefined
swapKit: "your-swapkit-api-key",
blockchair?: string | undefined
blockchair: "your-blockchair-api-key",
walletConnectProjectId?: string | undefined
walletConnectProjectId: "your-walletconnect-project-id",
},
},
plugins?: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} | undefined
plugins: { ...const ThorchainPlugin: {
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
}
ThorchainPlugin, ...const EVMPlugin: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}
EVMPlugin },
wallets?: {
connectKeystore: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
... 21 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>;
supportedChains: (Chain.Arbitrum | ... 18 more ... | Chain.Solana)[];
};
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
} | undefined
wallets: { ...const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet, ...const keystoreWallet: {
connectKeystore: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
... 21 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>;
supportedChains: (Chain.Arbitrum | ... 18 more ... | Chain.Solana)[];
};
}
keystoreWallet },
});
// Connect necessary wallets
async function function connectWallets(): Promise<void>
connectWallets() {
// Connect EVM wallet for Ethereum
await const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
evm: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>
connectEVMWallet([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum]);
// If doing cross-chain swaps, connect other chains as needed
// For example, to swap from ETH to BTC:
await const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
evm: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
AVAX?: DerivationPathArray | undefined;
... 20 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>
connectKeystore([enum Chain
Chain.function (enum member) Chain.Bitcoin = "BTC"
Bitcoin], "your secret phrase here");
const const connectedWallets: string[]
connectedWallets = var Object: ObjectConstructor
Provides functionality common to all JavaScript objects.Object.ObjectConstructor.keys(o: {}): string[] (+1 overload)
Returns the names of the enumerable string properties and methods of an object.keys(const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
evm: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.getAllWallets: () => {
ARB: ChainWallet<Chain.Arbitrum> & {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
};
... 21 more ...;
SOL: ChainWallet<...> & {
...;
};
}
getAllWallets());
}
All-in-one Approach (Backend)
Section titled “All-in-one Approach (Backend)”import { enum Chain
Chain } from "@swapkit/helpers";
import { function createSwapKit(config?: Parameters<typeof SwapKit>[0]): {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
... 5 more ...;
solana: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
createSwapKit } from "@swapkit/sdk";
// Initialize SwapKit with all plugins and wallets included
const const swapKit: {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
... 5 more ...;
solana: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit = function createSwapKit(config?: Parameters<typeof SwapKit>[0]): {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
... 5 more ...;
solana: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
createSwapKit({
config?: SKConfigState | undefined
config: {
apiKeys?: Partial<{
blockchair: string;
kado: string;
keepKey: string;
swapKit: string;
walletConnectProjectId: string;
}> | undefined
apiKeys: {
swapKit?: string | undefined
swapKit: "your-swapkit-api-key",
blockchair?: string | undefined
blockchair: "your-blockchair-api-key",
walletConnectProjectId?: string | undefined
walletConnectProjectId: "your-walletconnect-project-id",
},
},
});
// Connect necessary wallets
async function function connectWallets(): Promise<void>
connectWallets() {
// Connect wallets for the chains involved in the swap
await const swapKit: {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
... 5 more ...;
solana: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
AVAX?: DerivationPathArray | undefined;
... 20 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>
connectKeystore(
[enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum, enum Chain
Chain.function (enum member) Chain.Bitcoin = "BTC"
Bitcoin],
"your secret phrase here"
);
}
Getting a Swap Quote
Section titled “Getting a Swap Quote”Before executing a swap, you need to get a quote to see available routes, prices, and fees:
import { const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi } from "@swapkit/helpers/api";
// Basic swap quote
async function function getSwapQuote(): Promise<any>
getSwapQuote() {
// Define the assets to swap
const const sellAsset: "ETH.ETH"
sellAsset = "ETH.ETH"; // Ethereum on Ethereum chain
const const buyAsset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
buyAsset = "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; // USDC on Ethereum
const const sellAmount: "0.1"
sellAmount = "0.1"; // 0.1 ETH
try {
// Get quotes from all available providers
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset,
buyAsset: string
buyAsset,
sellAmount: string
sellAmount,
sourceAddress?: string | undefined
sourceAddress: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
slippage?: number | undefined
slippage: 1,
});
if (!const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.success) { throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error(`Failed to get quote: ${const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.error?: string | undefined
error}`);
}
// Routes sorted by best rate
const const routes: any
routes = const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes;
// Found ${routes.length} routes for swapping ${sellAmount} ${sellAsset} to ${buyAsset}`
// Best rate: 1 ETH = ${Number(routes[0].buyAmount) / Number(sellAmount)} USDC`
return const routes: any
routes;
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}
// Get quotes from a specific provider
async function function getProviderQuote(): Promise<any>
getProviderQuote() {
const { const ProviderName: typeof ProviderName
ProviderName } = await import("@swapkit/helpers");
// For example, get quotes only from Uniswap V3
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset: "ETH.ETH",
buyAsset: string
buyAsset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
sellAmount: string
sellAmount: "0.1",
providers?: string[] | undefined
providers: [const ProviderName: typeof ProviderName
ProviderName.function (enum member) ProviderName.UNISWAP_V3 = "UNISWAP_V3"
UNISWAP_V3], // Specify provider(s)
sourceAddress?: string | undefined
sourceAddress: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
});
return const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data?.routes || [];}
Executing a Swap
Section titled “Executing a Swap”Once you have a quote, you can execute the swap:
Granular Approach
Section titled “Granular Approach”
import {
class AssetValue
AssetValue,
enum Chain
Chain,
enum FeeOption
FeeOption,
enum ProviderName
ProviderName,
} from "@swapkit/helpers";
import { const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi } from "@swapkit/helpers/api";
import { function SwapKit<Plugins extends ReturnType<typeof createPlugin>, Wallets extends ReturnType<typeof createWallet>>({ config, plugins, wallets, }?: {
config?: SKConfigState;
plugins?: Plugins;
wallets?: Wallets;
}): { [key in keyof Plugins]: ReturnType<...>; } & ... 1 more ... & {
...;
}
SwapKit } from "@swapkit/core";
import { const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet } from "@swapkit/wallets/evm-extensions";
import { const EVMPlugin: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}
EVMPlugin } from "@swapkit/plugins/evm";
// Initialize SwapKit with necessary plugins and wallets
const const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit = SwapKit<{
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
plugins?: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} | undefined
plugins: { ...const EVMPlugin: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}
EVMPlugin },
wallets?: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
} | undefined
wallets: { ...const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet },
});
// Connect wallet
await const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>
connectEVMWallet([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum]);
// Complete swap flow
async function function executeSwap(): Promise<{
txHash: string;
txUrl: string;
}>
executeSwap() {
try {
// 1. Get quotes
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset: "ETH.ETH",
buyAsset: string
buyAsset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
sellAmount: string
sellAmount: "0.1",
sourceAddress?: string | undefined
sourceAddress: const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.Ethereum>(chain: Chain.Ethereum) => string
getAddress(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum),
slippage?: number | undefined
slippage: 1,
});
if (!const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.success || !const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes.length) { throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error("No swap routes available");
}
// 2. Select the best route (first one is usually best rate)
const const selectedRoute: any
selectedRoute = const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes[0];
// 3. Create asset value for approvals
const const assetValue: AssetValue
assetValue = await class AssetValue
AssetValue.AssetValue.from<{
asset: any;
value: any;
asyncTokenLookup: true;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
asset: any;
value: any;
asyncTokenLookup: true;
} & AssetValueFromParams): Promise<AssetValue>
from({
asset: any
asset: const selectedRoute: any
selectedRoute.sellAsset,
value: any
value: const selectedRoute: any
selectedRoute.sellAmount,
asyncTokenLookup: true
asyncTokenLookup: true,
});
// 4. Check if we need to approve tokens (for ERC20 tokens)
if (!const assetValue: AssetValue
assetValue.AssetValue.isGasAsset: boolean
isGasAsset && const selectedRoute: any
selectedRoute.tx) {
const const isApproved: boolean
isApproved = await const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.isAssetValueApproved: (assetValue: AssetValue, contractAddress: string) => Promise<boolean>
isAssetValueApproved(
const assetValue: AssetValue
assetValue,
const selectedRoute: any
selectedRoute.tx.from
);
if (!const isApproved: boolean
isApproved) {
// "Approval needed, approving tokens..."
await const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.approveAssetValue: (assetValue: AssetValue, contractAddress: string) => Promise<string>
approveAssetValue(const assetValue: AssetValue
assetValue, const selectedRoute: any
selectedRoute.tx.from);
// "Tokens approved!"
}
}
// 5. Execute the swap
const const txHash: string
txHash = await const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.swap: <"evm">({ route, pluginName, ...rest }: SwapParams<"evm", {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}>) => any
swap({
route: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}
route: const selectedRoute: any
selectedRoute,
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
}) as string;
// 6. Get transaction URL
const const txUrl: string
txUrl = const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.getExplorerTxUrl: ({ chain, txHash }: {
txHash: string;
chain: Chain;
}) => string
getExplorerTxUrl({ chain: Chain
chain: const assetValue: AssetValue
assetValue.AssetValue.chain: Chain
chain as enum Chain
Chain, txHash: string
txHash });
return { txHash: string
txHash, txUrl: string
txUrl };
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}
All-in-one Approach
Section titled “All-in-one Approach”import {
class AssetValue
AssetValue,
enum Chain
Chain,
enum FeeOption
FeeOption,
} from "@swapkit/helpers";
import { const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi } from "@swapkit/helpers/api";
import { function createSwapKit(config?: Parameters<typeof SwapKit>[0]): {
solana: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: ({ route }: SwapParams<"solana", QuoteResponseRoute>) => Promise<...>;
};
... 5 more ...;
radix: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
createSwapKit } from "@swapkit/sdk";
// Initialize SwapKit
const const swapKit: {
solana: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: ({ route }: SwapParams<"solana", QuoteResponseRoute>) => Promise<...>;
};
... 5 more ...;
radix: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit = function createSwapKit(config?: Parameters<typeof SwapKit>[0]): {
solana: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: ({ route }: SwapParams<"solana", QuoteResponseRoute>) => Promise<...>;
};
... 5 more ...;
radix: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
createSwapKit();
// Connect wallet
await const swapKit: {
solana: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: ({ route }: SwapParams<"solana", QuoteResponseRoute>) => Promise<...>;
};
... 5 more ...;
radix: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
AVAX?: DerivationPathArray | undefined;
... 20 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>
connectKeystore([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum], "your secret phrase here");
// Execute swap
async function function executeSwap(): Promise<any>
executeSwap() {
try {
// 1. Get quotes
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset: "ETH.ETH",
buyAsset: string
buyAsset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
sellAmount: string
sellAmount: "0.1",
sourceAddress?: string | undefined
sourceAddress: const swapKit: {
solana: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: ({ route }: SwapParams<"solana", QuoteResponseRoute>) => Promise<...>;
};
... 5 more ...;
radix: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.Ethereum>(chain: Chain.Ethereum) => string
getAddress(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum),
});
if (!const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.success || !const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes.length) { throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error("No swap routes available");
}
// 2. Select the best route
const const selectedRoute: any
selectedRoute = const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes[0];
// 3. Execute the swap
const const txHash: any
txHash = await const swapKit: {
solana: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: ({ route }: SwapParams<"solana", QuoteResponseRoute>) => Promise<...>;
};
... 5 more ...;
radix: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.swap: <"solana" | "chainflip" | "evm" | "kado" | "mayachain" | "thorchain" | "radix">({ route, pluginName, ...rest }: SwapParams<"solana" | "chainflip" | "evm" | "kado" | "mayachain" | "thorchain" | "radix", {
sellAsset: string;
... 18 more ...;
estimatedTime?: {
...;
} | undefined;
}>) => any
swap({
route: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}
route: const selectedRoute: any
selectedRoute,
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
});
return const txHash: any
txHash;
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}
Cross-Chain Swaps with ChainFlip
Section titled “Cross-Chain Swaps with ChainFlip”SwapKit supports cross-chain swaps through ChainFlip:
import {
enum Chain
Chain,
enum FeeOption
FeeOption,
enum ProviderName
ProviderName,
} from "@swapkit/helpers";
import { const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi } from "@swapkit/helpers/api";
import { function SwapKit<Plugins extends ReturnType<typeof createPlugin>, Wallets extends ReturnType<typeof createWallet>>({ config, plugins, wallets, }?: {
config?: SKConfigState;
plugins?: Plugins;
wallets?: Wallets;
}): { [key in keyof Plugins]: ReturnType<...>; } & ... 1 more ... & {
...;
}
SwapKit } from "@swapkit/core";
import { const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet } from "@swapkit/wallets/evm-extensions";
import { const ChainflipPlugin: {
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
}
ChainflipPlugin } from "@swapkit/plugins/chainflip";
// Initialize SwapKit with necessary plugins
const const swapKit: {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit = SwapKit<{
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
plugins?: {
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
} | undefined
plugins: { ...const ChainflipPlugin: {
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
}
ChainflipPlugin },
wallets?: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
} | undefined
wallets: { ...const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet },
config?: SKConfigState | undefined
config: {
integrations?: Partial<SKConfigIntegrations> | undefined
integrations: {
chainflip?: {
useSDKBroker?: boolean;
brokerUrl: string;
} | undefined
chainflip: {
brokerUrl: string
brokerUrl: "https://broker.chainflip.io", // ChainFlip broker URL
},
},
},
});
// Connect source chain wallet (Ethereum)
await const swapKit: {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>
connectEVMWallet([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum]);
// Perform a cross-chain swap from ETH to BTC
async function function crossChainSwap(): Promise<{
txHash: any;
}>
crossChainSwap() {
try {
// Get cross-chain quotes
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset: "ETH.ETH", // Ethereum
buyAsset: string
buyAsset: "BTC.BTC", // Bitcoin
sellAmount: string
sellAmount: "0.1", // 0.1 ETH
providers?: string[] | undefined
providers: [enum ProviderName
ProviderName.function (enum member) ProviderName.CHAINFLIP = "CHAINFLIP"
CHAINFLIP], // Use ChainFlip for cross-chain
sourceAddress?: string | undefined
sourceAddress: const swapKit: {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.Ethereum>(chain: Chain.Ethereum) => string
getAddress(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum),
destinationAddress?: string | undefined
destinationAddress: "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq", // BTC destination
});
if (!const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.success || !const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes.length) { throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error("No cross-chain swap routes available");
}
// Select the route
const const route: any
route = const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes[0];
// Execute the cross-chain swap
const const txHash: any
txHash = await const swapKit: {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.swap: <"chainflip">({ route, pluginName, ...rest }: SwapParams<"chainflip", {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
... 13 more ...;
estimatedTime?: {
...;
} | undefined;
}>) => any
swap({
route: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}
route,
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
// Optional: provide maxBoostFeeBps to speed up the swap if needed
});
return { txHash: any
txHash };
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}
THORChain Swaps
Section titled “THORChain Swaps”SwapKit also supports swaps through THORChain:
import {
enum Chain
Chain,
enum FeeOption
FeeOption,
enum ProviderName
ProviderName,
} from "@swapkit/helpers";
import { const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi } from "@swapkit/helpers/api";
import { function SwapKit<Plugins extends ReturnType<typeof createPlugin>, Wallets extends ReturnType<typeof createWallet>>({ config, plugins, wallets, }?: {
config?: SKConfigState;
plugins?: Plugins;
wallets?: Wallets;
}): { [key in keyof Plugins]: ReturnType<...>; } & ... 1 more ... & {
...;
}
SwapKit } from "@swapkit/core";
import { const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet } from "@swapkit/wallets/evm-extensions";
import { const keystoreWallet: {
connectKeystore: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
... 21 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>;
supportedChains: (Chain.Arbitrum | ... 18 more ... | Chain.Solana)[];
};
}
keystoreWallet } from "@swapkit/wallets/keystore";
import { const ThorchainPlugin: {
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
}
ThorchainPlugin } from "@swapkit/plugins/thorchain";
// Initialize SwapKit
const const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit = SwapKit<{
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
plugins?: {
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} | undefined
plugins: { ...const ThorchainPlugin: {
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
}
ThorchainPlugin },
wallets?: {
connectKeystore: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
... 21 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>;
supportedChains: (Chain.Arbitrum | ... 18 more ... | Chain.Solana)[];
};
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
} | undefined
wallets: { ...const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet, ...const keystoreWallet: {
connectKeystore: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
... 21 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>;
supportedChains: (Chain.Arbitrum | ... 18 more ... | Chain.Solana)[];
};
}
keystoreWallet },
});
// Setup for THORChain swap
async function function setupAndSwap(): Promise<{
txHash: any;
}>
setupAndSwap() {
// Connect source chain wallet (e.g., Ethereum)
await const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>
connectEVMWallet([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum]);
// Connect THORChain wallet if doing a swap to RUNE
await const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | {
ARB?: DerivationPathArray | undefined;
AVAX?: DerivationPathArray | undefined;
... 20 more ...;
SOL?: DerivationPathArray | undefined;
} | undefined) => Promise<boolean>
connectKeystore([enum Chain
Chain.function (enum member) Chain.THORChain = "THOR"
THORChain], "your secret phrase here");
// Perform a swap through THORChain
try {
// Get quotes from THORChain
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
... 9 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset: "ETH.ETH", // Ethereum
buyAsset: string
buyAsset: "THOR.RUNE", // RUNE on THORChain
sellAmount: string
sellAmount: "0.1", // 0.1 ETH
providers?: string[] | undefined
providers: [enum ProviderName
ProviderName.function (enum member) ProviderName.THORCHAIN = "THORCHAIN"
THORCHAIN], // Use THORChain
sourceAddress?: string | undefined
sourceAddress: const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.Ethereum>(chain: Chain.Ethereum) => string
getAddress(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum),
destinationAddress?: string | undefined
destinationAddress: const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.THORChain>(chain: Chain.THORChain) => string
getAddress(enum Chain
Chain.function (enum member) Chain.THORChain = "THOR"
THORChain),
});
if (!const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.success || !const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes.length) { throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error("No THORChain swap routes available");
}
// Select the route
const const route: any
route = const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes[0];
// Execute the THORChain swap
const const txHash: any
txHash = await const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.swap: <"thorchain">({ route, pluginName, ...rest }: SwapParams<"thorchain", {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}>) => any
swap({
route: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}
route,
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
});
return { txHash: any
txHash };
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}
Fee Estimation
Section titled “Fee Estimation”Before executing a swap, you can estimate the transaction fees:
import {
class AssetValue
AssetValue,
enum Chain
Chain,
enum FeeOption
FeeOption,
} from "@swapkit/helpers";
import { const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi } from "@swapkit/helpers/api";
import { function SwapKit<Plugins extends ReturnType<typeof createPlugin>, Wallets extends ReturnType<typeof createWallet>>({ config, plugins, wallets, }?: {
config?: SKConfigState;
plugins?: Plugins;
wallets?: Wallets;
}): { [key in keyof Plugins]: ReturnType<...>; } & ... 1 more ... & {
...;
}
SwapKit } from "@swapkit/core";
import { const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet } from "@swapkit/wallets/evm-extensions";
import { const EVMPlugin: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}
EVMPlugin } from "@swapkit/plugins/evm";
// Initialize SwapKit
const const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit = SwapKit<{
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
plugins?: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} | undefined
plugins: { ...const EVMPlugin: {
evm: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
}
EVMPlugin },
wallets?: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
} | undefined
wallets: { ...const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet },
});
// Connect wallet
await const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>
connectEVMWallet([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum]);
// Estimate swap fees
async function function estimateSwapFees(): Promise<AssetValue | undefined>
estimateSwapFees() {
try {
// 1. Get quotes
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset: "ETH.ETH",
buyAsset: string
buyAsset: "ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
sellAmount: string
sellAmount: "0.1",
sourceAddress?: string | undefined
sourceAddress: const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.Ethereum>(chain: Chain.Ethereum) => string
getAddress(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum),
});
if (!const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.success || !const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes.length) { throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error("No swap routes available");
}
// 2. Select a route
const const route: any
route = const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes[0];
// 3. Create asset value
const const assetValue: AssetValue
assetValue = await class AssetValue
AssetValue.AssetValue.from<{
asset: any;
value: any;
asyncTokenLookup: true;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
asset: any;
value: any;
asyncTokenLookup: true;
} & AssetValueFromParams): Promise<AssetValue>
from({
asset: any
asset: const route: any
route.sellAsset,
value: any
value: const route: any
route.sellAmount,
asyncTokenLookup: true
asyncTokenLookup: true,
});
// 4. Estimate the swap fee
const const estimatedFee: AssetValue | undefined
estimatedFee = await const swapKit: {
evm: {
supportedSwapkitProviders: ProviderName[];
} & {
approveAssetValue: ({ assetValue, spenderAddress, }: {
spenderAddress: string;
assetValue: AssetValue;
}) => Promise<string> | Promise<...>;
isAssetValueApproved: ({ assetValue, spenderAddress, }: {
...;
}) => Promise<...> | Promise<...>;
swap: ({ route: { tx, sellAsset }, feeOptionKey, }: SwapParams<"evm", QuoteResponseRoute>) => Promise<...>;
};
} & {
...;
} & {
...;
}
swapKit.estimateTransactionFee: <"evm">({ type, feeOptionKey, params, }: ({
type: "transfer";
params: EVMTransferParams | GenericTransferParams;
} | {
...;
} | {
...;
}) & {
...;
}) => Promise<AssetValue | undefined>
estimateTransactionFee({
type: "swap"
type: "swap",
feeOptionKey: FeeOption
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
params: GenericSwapParams<{
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}> & {
...;
} & {
...;
}
params: {
route: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
providers: ProviderName[];
sellAmount: string;
expectedBuyAmount: string;
expectedBuyAmountMaxSlippage: string;
... 12 more ...;
estimatedTime?: {
...;
} | undefined;
}
route,
assetValue: AssetValue
assetValue,
},
});
return const estimatedFee: AssetValue | undefined
estimatedFee;
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}
Advanced: Swap with Custom Parameters
Section titled “Advanced: Swap with Custom Parameters”For more advanced use cases, you can customize swap parameters:
import {
enum Chain
Chain,
enum FeeOption
FeeOption,
enum ProviderName
ProviderName,
} from "@swapkit/helpers";
import { const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi } from "@swapkit/helpers/api";
import { function SwapKit<Plugins extends ReturnType<typeof createPlugin>, Wallets extends ReturnType<typeof createWallet>>({ config, plugins, wallets, }?: {
config?: SKConfigState;
plugins?: Plugins;
wallets?: Wallets;
}): { [key in keyof Plugins]: ReturnType<...>; } & ... 1 more ... & {
...;
}
SwapKit } from "@swapkit/core";
import { const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet } from "@swapkit/wallets/evm-extensions";
import { const ThorchainPlugin: {
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
}
ThorchainPlugin } from "@swapkit/plugins/thorchain";
import { const ChainflipPlugin: {
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
}
ChainflipPlugin } from "@swapkit/plugins/chainflip";
// Initialize SwapKit
const const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
chainflip: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit = SwapKit<{
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
thorchain: (pluginParams: SwapKitPluginParams) => {
...;
} & {
...;
};
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
plugins?: {
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
} | undefined
plugins: { ...const ThorchainPlugin: {
thorchain: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<...>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
}
ThorchainPlugin, ...const ChainflipPlugin: {
chainflip: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
}
ChainflipPlugin },
wallets?: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
} | undefined
wallets: { ...const evmWallet: {
connectEVMWallet: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
supportedChains: EVMChain[];
};
}
evmWallet },
});
// Connect wallets
await const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
chainflip: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>
connectEVMWallet([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum, enum Chain
Chain.function (enum member) Chain.BinanceSmartChain = "BSC"
BinanceSmartChain]);
// Advanced swap with custom parameters
async function function advancedSwap(): Promise<{
txHash: any;
}>
advancedSwap() {
try {
// Custom quote parameters
const const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse = await const SwapKitApi: {
mayachainMidgard: {
getLiquidityPositionRaw: (address: string) => Promise<MemberDetailsMayachain>;
getBorrowerDetailRaw: (address: string) => Promise<...>;
... 6 more ...;
getLiquidityPosition: (address: string) => Promise<...>;
};
... 14 more ...;
getChainflipDepositChannel(body: BrokerDepositChannelParams): Promise<...>;
}
SwapKitApi.function getSwapQuote(json: QuoteRequest): Promise<{
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
... 9 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}>
getSwapQuote({
sellAsset: string
sellAsset: "ETH.ETH",
buyAsset: string
buyAsset: "BNB.BNB",
sellAmount: string
sellAmount: "0.5",
// Specific providers to use
providers?: string[] | undefined
providers: [
enum ProviderName
ProviderName.function (enum member) ProviderName.THORCHAIN = "THORCHAIN"
THORCHAIN,
enum ProviderName
ProviderName.function (enum member) ProviderName.CHAINFLIP = "CHAINFLIP"
CHAINFLIP,
],
// Custom addresses
sourceAddress?: string | undefined
sourceAddress: const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
chainflip: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.Ethereum>(chain: Chain.Ethereum) => string
getAddress(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum),
destinationAddress?: string | undefined
destinationAddress: const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
chainflip: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.getAddress: <Chain.BinanceSmartChain>(chain: Chain.BinanceSmartChain) => string
getAddress(enum Chain
Chain.function (enum member) Chain.BinanceSmartChain = "BSC"
BinanceSmartChain),
// Custom slippage tolerance (2.5%)
slippage?: number | undefined
slippage: 2.5,
// Affiliate fee (optional)
// affiliateBps: 20, // 0.2% fee
// affiliateAddress: "thor1youraffiliateaddresshere",
});
if (!const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.success || !const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes.length) { throw new var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+2 overloads)
Error("No advanced swap routes available");
}
// Get the best route
const const route: any
route = const quoteResponse: {
quoteId: string;
routes: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}[];
error?: string | undefined;
providerErrors?: {
...;
}[] | undefined;
}
quoteResponse.data.routes[0];
// Execute the swap with custom parameters
const const txHash: any
txHash = await const swapKit: {
thorchain: {
supportedSwapkitProviders: ProviderName[];
} & {
addLiquidity: ({ baseAssetValue, assetValue, baseAssetAddr, assetAddr, isPendingSymmAsset, mode, }: AddLiquidityParams) => Promise<{
baseAssetTx: string | void;
assetTx: string | void;
}>;
... 12 more ...;
withdraw: ({ memo, assetValue, percent, from, to }: WithdrawParams) => Promise<...>;
};
chainflip: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.swap: <"thorchain" | "chainflip">({ route, pluginName, ...rest }: SwapParams<"thorchain" | "chainflip", {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
...;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}>) => any
swap({
route: {
sellAsset: string;
buyAsset: string;
destinationAddress: string;
meta: {
tags: PriorityLabel[];
chainflip?: {
sellAsset: {
asset: string;
chain: string;
};
... 7 more ...;
maxBoostFeeBps?: number | undefined;
} | undefined;
... 8 more ...;
referrer?: string | undefined;
};
... 15 more ...;
estimatedTime?: {
...;
} | undefined;
}
route,
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
// Additional ChainFlip-specific parameters
// maxBoostFeeBps: 5, // 0.05% max fee boost
});
return { txHash: any
txHash };
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}