Executing Transactions
Executing Transactions
Section titled “Executing Transactions”SwapKit provides a unified interface for creating and executing transactions across multiple blockchain networks. This guide explains how to perform transactions with the SwapKit library.
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 ... & { ...; }
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 Asset Transfer
Section titled “Basic Asset Transfer”The most common transaction is transferring assets from one address to another.
Granular Approach (Frontend Recommended)
Section titled “Granular Approach (Frontend Recommended)”
import { class AssetValue
AssetValue, enum Chain
Chain, enum FeeOption
FeeOption } 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 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";
// Initialize SwapKit with specific wallets
const const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit = SwapKit<{
[x: string]: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<...>;
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
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)[];
};
} | undefined
wallets: { ...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 a wallet (required before making transactions)
await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
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"
);
// Create an AssetValue representing the amount to transfer
const const amount: AssetValue
amount = class AssetValue
AssetValue.AssetValue.from<{
chain: Chain.Ethereum;
symbol: string;
value: string;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
chain: Chain.Ethereum;
symbol: string;
value: string;
} & AssetValueFromParams): AssetValue
from({
chain: Chain.Ethereum
chain: enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum,
symbol: string
symbol: "ETH",
value: string
value: "0.01", // 0.01 ETH
});
// Execute the transaction
async function function transferAsset(): Promise<{
txHash: any;
txUrl: string;
}>
transferAsset() {
try {
// Transfer the asset to a recipient
const const txHash: any
txHash = await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit.transfer: ({ assetValue, ...params }: GenericTransferParams | EVMTransferParams) => Promise<any>
transfer({
assetValue: AssetValue
assetValue: const amount: AssetValue
amount,
recipient: string
recipient: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
memo?: string | undefined
memo: "Payment for services", // Optional memo
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast, // Fee speed preference
})
// Get explorer URL for the transaction
const const txUrl: string
txUrl = const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit.getExplorerTxUrl: ({ chain, txHash }: {
txHash: string;
chain: Chain;
}) => string
getExplorerTxUrl({
chain: Chain
chain: enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum,
txHash: string
txHash,
});
return { txHash: any
txHash, txUrl: string
txUrl };
} catch (function (local var) error: unknown
error) {
throw function (local var) error: unknown
error;
}
}
All-in-one Approach (Backend)
Section titled “All-in-one Approach (Backend)”
import { class AssetValue
AssetValue, enum Chain
Chain, enum FeeOption
FeeOption } 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 the SwapKit client with all 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();
// Connect a wallet (required before making transactions)
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],
"your secret phrase here"
);
// Create an AssetValue representing the amount to transfer
const const amount: AssetValue
amount = class AssetValue
AssetValue.AssetValue.from<{
chain: Chain.Ethereum;
symbol: string;
value: string;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
chain: Chain.Ethereum;
symbol: string;
value: string;
} & AssetValueFromParams): AssetValue
from({
chain: Chain.Ethereum
chain: enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum,
symbol: string
symbol: "ETH",
value: string
value: "0.01", // 0.01 ETH
});
// Execute the transaction
async function function transferAsset(): Promise<any>
transferAsset() {
try {
const const txHash: any
txHash = await const swapKit: {
chainflip: {
supportedSwapkitProviders: ProviderName[];
} & {
swap: (swapParams: RequestSwapDepositAddressParams) => Promise<...>;
};
... 5 more ...;
solana: {
...;
} & {
...;
};
} & {
...;
} & {
...;
}
swapKit.transfer: ({ assetValue, ...params }: GenericTransferParams | EVMTransferParams) => Promise<any>
transfer({
assetValue: AssetValue
assetValue: const amount: AssetValue
amount,
recipient: string
recipient: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
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;
}
}
Transferring Different Asset Types
Section titled “Transferring Different Asset Types”Transferring ERC20 Tokens
Section titled “Transferring ERC20 Tokens”
import { class AssetValue
AssetValue, enum Chain
Chain, enum FeeOption
FeeOption } 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";
// Initialize SwapKit and connect wallet
const const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
} & {
...;
}
swapKit = SwapKit<{
[x: string]: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<...>;
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
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 },
});
await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
} & {
...;
}
swapKit.connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>
connectEVMWallet([enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum]);
// Transfer ERC20 tokens (USDC example)
async function function transferERC20(): Promise<any>
transferERC20() {
// Create an AssetValue for USDC
const const usdcAmount: AssetValue
usdcAmount = class AssetValue
AssetValue.AssetValue.from<{
chain: Chain.Ethereum;
symbol: string;
ticker: string;
address: string;
value: string;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
chain: Chain.Ethereum;
symbol: string;
ticker: string;
address: string;
value: string;
} & AssetValueFromParams): AssetValue
from({
chain: Chain.Ethereum
chain: enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum,
symbol: string
symbol: "USDC",
ticker: string
ticker: "USDC",
address: string
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", // USDC contract address
value: string
value: "10", // 10 USDC
});
// Transfer USDC
const const txHash: any
txHash = await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectEVMWallet: (chains: Chain[], walletType?: EVMWalletOptions | undefined, eip1193Provider?: Eip1193Provider | undefined) => Promise<boolean>;
} & {
...;
}
swapKit.transfer: ({ assetValue, ...params }: GenericTransferParams | EVMTransferParams) => Promise<any>
transfer({
assetValue: AssetValue
assetValue: const usdcAmount: AssetValue
usdcAmount,
recipient: string
recipient: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Average = "average"
Average,
});
return const txHash: any
txHash;
}
Transferring Bitcoin
Section titled “Transferring Bitcoin”
import { class AssetValue
AssetValue, enum Chain
Chain, enum FeeOption
FeeOption } 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 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";
// Initialize SwapKit and connect wallet
const const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit = SwapKit<{
[x: string]: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<...>;
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
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)[];
};
} | undefined
wallets: { ...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 },
});
await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
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");
// Transfer Bitcoin
async function function transferBitcoin(): Promise<any>
transferBitcoin() {
// Create an AssetValue for Bitcoin
const const btcAmount: AssetValue
btcAmount = class AssetValue
AssetValue.AssetValue.from<{
chain: Chain.Bitcoin;
symbol: string;
value: string;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
chain: Chain.Bitcoin;
symbol: string;
value: string;
} & AssetValueFromParams): AssetValue
from({
chain: Chain.Bitcoin
chain: enum Chain
Chain.function (enum member) Chain.Bitcoin = "BTC"
Bitcoin,
symbol: string
symbol: "BTC",
value: string
value: "0.001", // 0.001 BTC
});
// Transfer BTC
const const txHash: any
txHash = await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeystore: (chains: Chain[], phrase: string, derivationPathMapOrIndex?: number | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit.transfer: ({ assetValue, ...params }: GenericTransferParams | EVMTransferParams) => Promise<any>
transfer({
assetValue: AssetValue
assetValue: const btcAmount: AssetValue
btcAmount,
recipient: string
recipient: "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
memo?: string | undefined
memo: "Bitcoin transfer example",
});
return const txHash: any
txHash;
}
Transferring Cosmos Assets
Section titled “Transferring Cosmos Assets”
import { class AssetValue
AssetValue, enum Chain
Chain, enum FeeOption
FeeOption } 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 keplrWallet: {
connectKeplr: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: WalletOption.KEPLR | WalletOption.LEAP | undefined) => Promise<boolean>;
supportedChains: (Chain.Cosmos | ... 1 more ... | Chain.THORChain)[];
};
}
keplrWallet } from "@swapkit/wallets/keplr";
// Initialize SwapKit and connect wallet
const const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeplr: (chains: Chain[], walletType?: WalletOption.KEPLR | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit = SwapKit<{
[x: string]: (pluginParams: SwapKitPluginParams) => {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<...>;
}, {
...;
}>({ config, plugins, wallets, }?: {
...;
}): {
...;
} & ... 1 more ... & {
...;
}
SwapKit({
wallets?: {
connectKeplr: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: WalletOption.KEPLR | WalletOption.LEAP | undefined) => Promise<boolean>;
supportedChains: (Chain.Cosmos | ... 1 more ... | Chain.THORChain)[];
};
} | undefined
wallets: { ...const keplrWallet: {
connectKeplr: {
connectWallet: (connectParams: {
addChain: AddChainType;
}) => (chains: Chain[], walletType?: WalletOption.KEPLR | WalletOption.LEAP | undefined) => Promise<boolean>;
supportedChains: (Chain.Cosmos | ... 1 more ... | Chain.THORChain)[];
};
}
keplrWallet },
});
await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeplr: (chains: Chain[], walletType?: WalletOption.KEPLR | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit.connectKeplr: (chains: Chain[], walletType?: WalletOption.KEPLR | WalletOption.LEAP | undefined) => Promise<boolean>
connectKeplr([enum Chain
Chain.function (enum member) Chain.Cosmos = "GAIA"
Cosmos]);
// Transfer Cosmos assets
async function function transferCosmos(): Promise<any>
transferCosmos() {
// Create an AssetValue for ATOM
const const atomAmount: AssetValue
atomAmount = class AssetValue
AssetValue.AssetValue.from<{
chain: Chain.Cosmos;
symbol: string;
value: string;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
chain: Chain.Cosmos;
symbol: string;
value: string;
} & AssetValueFromParams): AssetValue
from({
chain: Chain.Cosmos
chain: enum Chain
Chain.function (enum member) Chain.Cosmos = "GAIA"
Cosmos,
symbol: string
symbol: "ATOM",
value: string
value: "0.1", // 0.1 ATOM
});
// Transfer ATOM
const const txHash: any
txHash = await const swapKit: {
[x: string]: {
supportedSwapkitProviders?: (ProviderName | string)[];
} & Record<string, unknown>;
} & {
connectKeplr: (chains: Chain[], walletType?: WalletOption.KEPLR | ... 1 more ... | undefined) => Promise<boolean>;
} & {
...;
}
swapKit.transfer: ({ assetValue, ...params }: GenericTransferParams | EVMTransferParams) => Promise<any>
transfer({
assetValue: AssetValue
assetValue: const atomAmount: AssetValue
atomAmount,
recipient: string
recipient: "cosmos1abcd1234abcd1234abcd1234abcd1234abcd12",
feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Average = "average"
Average,
memo?: string | undefined
memo: "Cosmos transfer example",
});
return const txHash: any
txHash;
}
Using Toolboxes Directly for Transactions
Section titled “Using Toolboxes Directly for Transactions”For lower-level control, you can use the toolboxes directly instead of SwapKit’s high-level API:
EVM Toolbox Transfers
Section titled “EVM Toolbox Transfers”
import { class AssetValue
AssetValue, enum Chain
Chain, enum FeeOption
FeeOption } from "@swapkit/helpers";
import { function getProvider(chain: EVMChain, customUrl?: string): Promise<ethers.JsonRpcProvider>
getProvider, function getEvmToolbox<T extends EVMChain>(chain: T, params?: EVMToolboxParams): Promise<{
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}>
getEvmToolbox } from "@swapkit/toolboxes/evm";
import { import ethers
ethers } from "ethers";
async function function directEVMTransfer(): Promise<string>
directEVMTransfer() {
// Create a signer
const const provider: ethers.JsonRpcProvider
provider = await function getProvider(chain: EVMChain, customUrl?: string): Promise<ethers.JsonRpcProvider>
getProvider(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum);
const const signer: ethers.Wallet
signer = new import ethers
ethers.new Wallet(key: string | ethers.SigningKey, provider?: null | ethers.Provider): ethers.Wallet
export Wallet
Create a new wallet for the private %%key%%, optionally connected
to %%provider%%.Wallet("your-private-key-here", const provider: ethers.JsonRpcProvider
provider);
// Create a toolbox with the signer
const const ethToolbox: {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<AssetValue>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}
ethToolbox = await getEvmToolbox<Chain.Ethereum>(chain: Chain.Ethereum, params?: EVMToolboxParams): Promise<{
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}>
getEvmToolbox(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum, { signer?: (ChainSigner<EVMTransferParams, string> & ethers.Signer) | ethers.JsonRpcSigner | undefined
signer });
// Create an asset value
const const amount: AssetValue
amount = class AssetValue
AssetValue.AssetValue.from<{
chain: Chain.Ethereum;
symbol: string;
value: string;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
chain: Chain.Ethereum;
symbol: string;
value: string;
} & AssetValueFromParams): AssetValue
from({
chain: Chain.Ethereum
chain: enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum,
symbol: string
symbol: "ETH",
value: string
value: "0.01",
});
// Transfer ETH
const const txHash: string
txHash = await const ethToolbox: {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<AssetValue>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}
ethToolbox.transfer: ({ assetValue, memo, recipient, feeOptionKey, sender, ...tx }: EVMTransferParams) => Promise<string>
transfer({
assetValue: AssetValue
assetValue: const amount: AssetValue
amount,
recipient: string
recipient: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
from: const signer: ethers.Wallet
signer.BaseWallet.address: string
The wallet address.address, feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
});
return const txHash: string
txHash;
}
UTXO Toolbox Transfers
Section titled “UTXO Toolbox Transfers”
import { class AssetValue
AssetValue, enum Chain
Chain, enum FeeOption
FeeOption } from "@swapkit/helpers";
import { function getUtxoToolbox<T extends keyof UTXOToolboxes>(chain: T, params?: UtxoToolboxParams[T] | {
phrase?: string;
derivationPath?: DerivationPathArray;
index?: number;
}): Promise<UTXOToolboxes[T]>
getUtxoToolbox } from "@swapkit/toolboxes/utxo";
async function function directBitcoinTransfer(): Promise<string>
directBitcoinTransfer() {
const const btcToolbox: {
accumulative: ({ inputs, outputs, feeRate: initialFeeRate, chain, }: UTXOCalculateTxSizeParams & {
outputs: TargetOutput[];
chain: UTXOChain;
}) => {
...;
} | {
...;
};
... 13 more ...;
estimateMaxSendableAmount: ({ from, memo, feeRate, feeOptionKey, recipients, }: {
from: string;
memo?: string;
feeRate?: number;
feeOptionKey?: FeeOption;
recipients?: number | TargetOutput[];
}) => Promise<...>;
}
btcToolbox = await getUtxoToolbox<Chain.Bitcoin>(chain: Chain.Bitcoin, params?: {
phrase?: string;
derivationPath?: DerivationPathArray;
index?: number;
} | {
signer: ChainSigner<Psbt, Psbt>;
} | undefined): Promise<...>
getUtxoToolbox(enum Chain
Chain.function (enum member) Chain.Bitcoin = "BTC"
Bitcoin);
// Generate keys from a mnemonic phrase
const const keys: ECPairInterface
keys = await const btcToolbox: {
accumulative: ({ inputs, outputs, feeRate: initialFeeRate, chain, }: UTXOCalculateTxSizeParams & {
outputs: TargetOutput[];
chain: UTXOChain;
}) => {
...;
} | {
...;
};
... 13 more ...;
estimateMaxSendableAmount: ({ from, memo, feeRate, feeOptionKey, recipients, }: {
from: string;
memo?: string;
feeRate?: number;
feeOptionKey?: FeeOption;
recipients?: number | TargetOutput[];
}) => Promise<...>;
}
btcToolbox.createKeysForPath: (params: {
wif?: string;
phrase?: string;
derivationPath?: string;
}) => ECPairInterface
createKeysForPath({
phrase?: string | undefined
phrase: "your twelve word mnemonic phrase here",
derivationPath?: string | undefined
derivationPath: "m/84'/0'/0'/0/0",
});
const const fromAddress: string
fromAddress = const btcToolbox: {
accumulative: ({ inputs, outputs, feeRate: initialFeeRate, chain, }: UTXOCalculateTxSizeParams & {
outputs: TargetOutput[];
chain: UTXOChain;
}) => {
...;
} | {
...;
};
... 13 more ...;
estimateMaxSendableAmount: ({ from, memo, feeRate, feeOptionKey, recipients, }: {
from: string;
memo?: string;
feeRate?: number;
feeOptionKey?: FeeOption;
recipients?: number | TargetOutput[];
}) => Promise<...>;
}
btcToolbox.getAddressFromKeys: (keys: ECPairInterface | BchECPair) => string
getAddressFromKeys(const keys: ECPairInterface
keys);
// Create a function to sign transactions
async function function (local function) signTransaction({ builder, utxos }: {
builder: any;
utxos: any[];
}): Promise<any>
signTransaction({ builder: any
builder, utxos: any[]
utxos }: { builder: any
builder: any, utxos: any[]
utxos: any[] }) {
utxos: any[]
utxos.Array<any>.forEach(callbackfn: (value: any, index: number, array: any[]) => void, thisArg?: any): void
Performs the specified action for each element in an array.forEach((utxo: any
utxo, index: number
index) => {
builder: any
builder.sign(index: number
index, const keys: ECPairInterface
keys);
});
return builder: any
builder.build();
}
// Create an asset value
const const amount: AssetValue
amount = class AssetValue
AssetValue.AssetValue.from<{
chain: Chain.Bitcoin;
symbol: string;
value: string;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
chain: Chain.Bitcoin;
symbol: string;
value: string;
} & AssetValueFromParams): AssetValue
from({
chain: Chain.Bitcoin
chain: enum Chain
Chain.function (enum member) Chain.Bitcoin = "BTC"
Bitcoin,
symbol: string
symbol: "BTC",
value: string
value: "0.001",
});
// Transfer BTC
const const txHash: string
txHash = await const btcToolbox: {
accumulative: ({ inputs, outputs, feeRate: initialFeeRate, chain, }: UTXOCalculateTxSizeParams & {
outputs: TargetOutput[];
chain: UTXOChain;
}) => {
...;
} | {
...;
};
... 13 more ...;
estimateMaxSendableAmount: ({ from, memo, feeRate, feeOptionKey, recipients, }: {
from: string;
memo?: string;
feeRate?: number;
feeOptionKey?: FeeOption;
recipients?: number | TargetOutput[];
}) => Promise<...>;
}
btcToolbox.transfer: ({ memo, recipient, feeOptionKey, feeRate, assetValue, }: UTXOTransferParams) => Promise<string>
transfer({
assetValue: AssetValue
assetValue: const amount: AssetValue
amount,
recipient: string
recipient: "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
from: const fromAddress: string
fromAddress, feeOptionKey?: FeeOption | undefined
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast,
// @ts-ignore - it should return PSBT and it's usually using psbt to sign tx
signTransaction: ({ builder, utxos }: {
builder: any;
utxos: any[];
}) => Promise<any>
signTransaction,
});
return const txHash: string
txHash;
}
Working with Contract Interactions
Section titled “Working with Contract Interactions”For EVM chains, you can interact with smart contracts using the EVM toolbox:
import { enum Chain
Chain } from "@swapkit/helpers";
import { function getEvmToolbox<T extends EVMChain>(chain: T, params?: EVMToolboxParams): Promise<{
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}>
getEvmToolbox, function getProvider(chain: EVMChain, customUrl?: string): Promise<ethers.JsonRpcProvider>
getProvider } from "@swapkit/toolboxes/evm";
import { import ethers
ethers } from "ethers";
import { const erc20ABI: ({
inputs: never[];
stateMutability: string;
type: string;
anonymous?: undefined;
name?: undefined;
outputs?: undefined;
} | {
anonymous: boolean;
inputs: {
indexed: boolean;
internalType: string;
name: string;
type: string;
}[];
name: string;
type: string;
stateMutability?: undefined;
outputs?: undefined;
} | {
...;
})[]
erc20ABI } from "@swapkit/helpers/contracts";
async function function interactWithContract(): Promise<{
name: unknown;
symbol: unknown;
decimals: number;
balance: string;
}>
interactWithContract() {
// Create a provider and signer
const const provider: ethers.JsonRpcProvider
provider = await function getProvider(chain: EVMChain, customUrl?: string): Promise<ethers.JsonRpcProvider>
getProvider(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum);
const const signer: ethers.Wallet
signer = new import ethers
ethers.new Wallet(key: string | ethers.SigningKey, provider?: null | ethers.Provider): ethers.Wallet
export Wallet
Create a new wallet for the private %%key%%, optionally connected
to %%provider%%.Wallet("your-private-key-here", const provider: ethers.JsonRpcProvider
provider);
// Get the EVM toolbox
const const ethToolbox: {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}
ethToolbox = await getEvmToolbox<Chain.Ethereum>(chain: Chain.Ethereum, params?: EVMToolboxParams): Promise<{
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}>
getEvmToolbox(enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum, {signer?: (ChainSigner<EVMTransferParams, string> & ethers.Signer) | ethers.JsonRpcSigner | undefined
signer });
// USDC contract address
const const contractAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
contractAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
// Get token details
const const tokenName: unknown
tokenName = await const ethToolbox: {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}
ethToolbox.call: <unknown>({ callProvider, contractAddress, abi, funcName, funcParams, txOverrides, feeOption, }: CallParams) => Promise<unknown>
call({
contractAddress: string
contractAddress,
abi: readonly ethers.JsonFragment[]
abi: const erc20ABI: ({
inputs: never[];
stateMutability: string;
type: string;
anonymous?: undefined;
name?: undefined;
outputs?: undefined;
} | {
anonymous: boolean;
inputs: {
indexed: boolean;
internalType: string;
name: string;
type: string;
}[];
name: string;
type: string;
stateMutability?: undefined;
outputs?: undefined;
} | {
...;
})[]
erc20ABI,
funcName: string
funcName: "name",
});
const const tokenSymbol: unknown
tokenSymbol = await const ethToolbox: {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}
ethToolbox.call: <unknown>({ callProvider, contractAddress, abi, funcName, funcParams, txOverrides, feeOption, }: CallParams) => Promise<unknown>
call({
contractAddress: string
contractAddress,
abi: readonly ethers.JsonFragment[]
abi: const erc20ABI: ({
inputs: never[];
stateMutability: string;
type: string;
anonymous?: undefined;
name?: undefined;
outputs?: undefined;
} | {
anonymous: boolean;
inputs: {
indexed: boolean;
internalType: string;
name: string;
type: string;
}[];
name: string;
type: string;
stateMutability?: undefined;
outputs?: undefined;
} | {
...;
})[]
erc20ABI,
funcName: string
funcName: "symbol",
});
const const tokenDecimals: number
tokenDecimals = await const ethToolbox: {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}
ethToolbox.call: <number>({ callProvider, contractAddress, abi, funcName, funcParams, txOverrides, feeOption, }: CallParams) => Promise<number>
call<number>({
contractAddress: string
contractAddress,
abi: readonly ethers.JsonFragment[]
abi: const erc20ABI: ({
inputs: never[];
stateMutability: string;
type: string;
anonymous?: undefined;
name?: undefined;
outputs?: undefined;
} | {
anonymous: boolean;
inputs: {
indexed: boolean;
internalType: string;
name: string;
type: string;
}[];
name: string;
type: string;
stateMutability?: undefined;
outputs?: undefined;
} | {
...;
})[]
erc20ABI,
funcName: string
funcName: "decimals",
});
// Get balance
const const balance: string
balance = await const ethToolbox: {
estimateTransactionFee: ({ feeOption, chain, ...txObject }: EIP1559TxParams & {
feeOption: FeeOption;
chain: EVMChain;
}) => Promise<...>;
... 20 more ...;
validateAddress: (address: string) => boolean;
} | {
...;
} | {
...;
}
ethToolbox.call: <string>({ callProvider, contractAddress, abi, funcName, funcParams, txOverrides, feeOption, }: CallParams) => Promise<string>
call<string>({
contractAddress: string
contractAddress,
abi: readonly ethers.JsonFragment[]
abi: const erc20ABI: ({
inputs: never[];
stateMutability: string;
type: string;
anonymous?: undefined;
name?: undefined;
outputs?: undefined;
} | {
anonymous: boolean;
inputs: {
indexed: boolean;
internalType: string;
name: string;
type: string;
}[];
name: string;
type: string;
stateMutability?: undefined;
outputs?: undefined;
} | {
...;
})[]
erc20ABI,
funcName: string
funcName: "balanceOf",
funcParams?: unknown[] | undefined
funcParams: [const signer: ethers.Wallet
signer.BaseWallet.address: string
The wallet address.address],
});
// Format the balance using decimals
const const formattedBalance: string
formattedBalance = import ethers
ethers.function formatUnits(value: ethers.BigNumberish, unit?: string | ethers.Numeric): string
export formatUnits
Converts %%value%% into a //decimal string//, assuming %%unit%% decimal
places. The %%unit%% may be the number of decimal places or the name of
a unit (e.g. ``"gwei"`` for 9 decimal places).formatUnits(const balance: string
balance, const tokenDecimals: number
tokenDecimals);
return {
name: unknown
name: const tokenName: unknown
tokenName,
symbol: unknown
symbol: const tokenSymbol: unknown
tokenSymbol,
decimals: number
decimals: const tokenDecimals: number
tokenDecimals,
balance: string
balance: const formattedBalance: string
formattedBalance,
};
}