Core Concepts
Before diving into SwapKit’s features, it’s essential to understand its core concepts. These building blocks are used throughout the SDK and understanding them will make your development experience much smoother.
AssetValue
Section titled “AssetValue”AssetValue
is SwapKit’s primary way of representing amounts of any asset across all supported chains. It handles decimal precision, formatting, and arithmetic operations automatically.
Creating AssetValue
Section titled “Creating AssetValue”import { class AssetValue
AssetValue } from '@swapkit/sdk';
// From decimal value (what users see)
const const oneEth: AssetValue
oneEth = class AssetValue
AssetValue.AssetValue.from<{
asset: "ETH.ETH";
value: number;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: {
asset: "ETH.ETH";
value: number;
} & AssetValueFromParams): AssetValue
from({
asset: "ETH.ETH"
asset: 'ETH.ETH',
value: number
value: 1
});
// From base units (wei, satoshi, etc.)
const const oneEthInWei: AssetValue
oneEthInWei = class AssetValue
AssetValue.AssetValue.from<{
asset: "ETH.ETH";
value: string;
fromBaseDecimal: boolean;
}>({ value, fromBaseDecimal, asyncTokenLookup, ...fromAssetOrChain }: never): AssetValue
from({
Type '"ETH.ETH"' is not assignable to type 'never'.asset: 'ETH.ETH',
Type 'string' is not assignable to type 'never'.value: '1000000000000000000',
Type 'true' is not assignable to type 'never'.fromBaseDecimal: true
});
// Both create the same AssetValue representing 1 ETH
Asset Notation
Section titled “Asset Notation”SwapKit uses a consistent format for identifying assets:
// Native assets: CHAIN.SYMBOL
'ETH.ETH' // Native Ethereum
'BTC.BTC' // Native Bitcoin
'AVAX.AVAX' // Native Avalanche
// Tokens: CHAIN.SYMBOL-CONTRACT
'ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC on Ethereum
'AVAX.USDC-0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E' // USDC on Avalanche
// Synthetic assets (THORChain)
'ETH/ETH' // Synthetic ETH
'BTC/BTC' // Synthetic BTC
Arithmetic Operations
Section titled “Arithmetic Operations”AssetValue provides safe arithmetic operations:
const const amount1: any
amount1 = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'ETH.ETH', value: number
value: 1.5 });
const const amount2: any
amount2 = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'ETH.ETH', value: number
value: 0.5 });
// Addition
const const sum: any
sum = const amount1: any
amount1.add(const amount2: any
amount2); // 2 ETH
// Subtraction
const const difference: any
difference = const amount1: any
amount1.sub(const amount2: any
amount2); // 1 ETH
// Multiplication
const const doubled: any
doubled = const amount1: any
amount1.mul(2); // 3 ETH
// Division
const const halved: any
halved = const amount1: any
amount1.div(2); // 0.75 ETH
Comparisons
Section titled “Comparisons”Compare AssetValue instances safely:
const const amount1: any
amount1 = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'BTC.BTC', value: number
value: 1 });
const const amount2: any
amount2 = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'BTC.BTC', value: number
value: 0.5 });
const amount1: any
amount1.gt(const amount2: any
amount2); // true - greater than
const amount1: any
amount1.gte(const amount2: any
amount2); // true - greater than or equal
const amount1: any
amount1.lt(const amount2: any
amount2); // false - less than
const amount1: any
amount1.lte(const amount2: any
amount2); // false - less than or equal
const amount1: any
amount1.eq(const amount2: any
amount2); // false - equal
Formatting
Section titled “Formatting”Format values for display:
const const btcAmount: any
btcAmount = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'BTC.BTC', value: number
value: 0.00123456 });
// Format with significant digits
const btcAmount: any
btcAmount.toSignificant(4); // "0.001235"
// Format as currency
const btcAmount: any
btcAmount.toCurrency('USD', 45000); // "$55.56"
// Format with abbreviation
const const largeAmount: any
largeAmount = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'ETH.ETH', value: number
value: 1234567 });
const largeAmount: any
largeAmount.toAbbreviation(); // "1.23M"
// Get raw values
const btcAmount: any
btcAmount.getValue('string'); // "0.00123456"
const btcAmount: any
btcAmount.getBaseValue('string'); // "123456" (in satoshis)
Type Checks
Section titled “Type Checks”Check asset properties:
const const ethAmount: any
ethAmount = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'ETH.ETH', value: number
value: 1 });
const const usdcAmount: any
usdcAmount = Cannot find name 'AssetValue'.AssetValue.from({
asset: string
asset: 'ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
value: number
value: 100
});
const const synthEth: any
synthEth = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'ETH/ETH', value: number
value: 1 });
const ethAmount: any
ethAmount.isGasAsset(); // true - native asset
const usdcAmount: any
usdcAmount.isGasAsset(); // false - token
const synthEth: any
synthEth.isSynthetic(); // true - synthetic asset
SwapKitNumber
Section titled “SwapKitNumber”SwapKitNumber
provides utilities for safe numeric operations with BigInt support:
import { class SwapKitNumber
SwapKitNumber } from '@swapkit/sdk';
// Create from various inputs
const const num1: SwapKitNumber
num1 = new new SwapKitNumber(params: SKBigIntParams): SwapKitNumber
SwapKitNumber(123.456);
const const num2: SwapKitNumber
num2 = new new SwapKitNumber(params: SKBigIntParams): SwapKitNumber
SwapKitNumber('789.012');
const const num3: SwapKitNumber
num3 = new new SwapKitNumber(params: SKBigIntParams): SwapKitNumber
SwapKitNumber({ value: string | number
value: 100, decimal: number
decimal: 18 });
// Arithmetic operations
const const sum: SwapKitNumber
sum = const num1: SwapKitNumber
num1.BigIntArithmetics.add(...args: InitialisationValueType[]): SwapKitNumber
add(const num2: SwapKitNumber
num2);
const const product: SwapKitNumber
product = const num1: SwapKitNumber
num1.BigIntArithmetics.mul(...args: InitialisationValueType[]): SwapKitNumber
mul(const num2: SwapKitNumber
num2);
// Formatting
const num1: SwapKitNumber
num1.BigIntArithmetics.toSignificant(significantDigits?: number): string
toSignificant(4); // "123.5"
const num1: SwapKitNumber
num1.BigIntArithmetics.toFixed(fixedDigits?: number): string
toFixed(2); // "123.46"
Chain Types
Section titled “Chain Types”SwapKit uses consistent chain identifiers across the entire SDK:
import { enum Chain
Chain } from '@swapkit/sdk';
// EVM Chains
enum Chain
Chain.function (enum member) Chain.Ethereum = "ETH"
Ethereum // "ETH"
enum Chain
Chain.function (enum member) Chain.Avalanche = "AVAX"
Avalanche // "AVAX"
enum Chain
Chain.function (enum member) Chain.BinanceSmartChain = "BSC"
BinanceSmartChain // "BSC"
enum Chain
Chain.function (enum member) Chain.Polygon = "MATIC"
Polygon // "MATIC"
enum Chain
Chain.function (enum member) Chain.Arbitrum = "ARB"
Arbitrum // "ARB"
enum Chain
Chain.function (enum member) Chain.Optimism = "OP"
Optimism // "OP"
enum Chain
Chain.function (enum member) Chain.Base = "BASE"
Base // "BASE"
// UTXO Chains
enum Chain
Chain.function (enum member) Chain.Bitcoin = "BTC"
Bitcoin // "BTC"
enum Chain
Chain.function (enum member) Chain.BitcoinCash = "BCH"
BitcoinCash // "BCH"
enum Chain
Chain.function (enum member) Chain.Litecoin = "LTC"
Litecoin // "LTC"
enum Chain
Chain.function (enum member) Chain.Dogecoin = "DOGE"
Dogecoin // "DOGE"
enum Chain
Chain.function (enum member) Chain.Dash = "DASH"
Dash // "DASH"
enum Chain
Chain.Property 'Zcash' does not exist on type 'typeof Chain'.Zcash // "ZEC"
// Cosmos Chains
enum Chain
Chain.function (enum member) Chain.Cosmos = "GAIA"
Cosmos // "GAIA"
enum Chain
Chain.function (enum member) Chain.THORChain = "THOR"
THORChain // "THOR"
enum Chain
Chain.function (enum member) Chain.Maya = "MAYA"
Maya // "MAYA"
enum Chain
Chain.function (enum member) Chain.Kujira = "KUJI"
Kujira // "KUJI"
// Other Chains
enum Chain
Chain.function (enum member) Chain.Solana = "SOL"
Solana // "SOL"
enum Chain
Chain.function (enum member) Chain.Polkadot = "DOT"
Polkadot // "DOT"
enum Chain
Chain.function (enum member) Chain.Chainflip = "FLIP"
Chainflip // "FLIP"
enum Chain
Chain.function (enum member) Chain.Radix = "XRD"
Radix // "XRD"
enum Chain
Chain.function (enum member) Chain.Ripple = "XRP"
Ripple // "XRP"
Error Handling
Section titled “Error Handling”SwapKit uses a typed error system for better debugging:
SwapKitError
Section titled “SwapKitError”The base error class with additional context:
import { class SwapKitError
SwapKitError } from '@swapkit/sdk';
try {
await Cannot find name 'swapKit'.swapKitNo value exists in scope for the shorthand property 'route'. Either declare one or provide an initializer..swap({ route });
} catch (var error: unknown
error) {
if (var error: unknown
error instanceof class SwapKitError
SwapKitError) {
// Access error details
console.Console.error(message?: any, ...optionalParams: any[]): void (+3 overloads)
Prints to `stderr` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args)).
```js
const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr
```
If formatting elements (e.g. `%d`) are not found in the first string then
[`util.inspect()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilinspectobject-options) is called on each argument and the
resulting string values are concatenated. See [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args)
for more information.error('Error:', var error: SwapKitError
error.Error.message: string
message);
console.Console.error(message?: any, ...optionalParams: any[]): void (+3 overloads)
Prints to `stderr` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args)).
```js
const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr
```
If formatting elements (e.g. `%d`) are not found in the first string then
[`util.inspect()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilinspectobject-options) is called on each argument and the
resulting string values are concatenated. See [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args)
for more information.error('Code:', var error: SwapKitError
error.Property 'code' does not exist on type 'SwapKitError'.code);
console.Console.error(message?: any, ...optionalParams: any[]): void (+3 overloads)
Prints to `stderr` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args)).
```js
const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr
```
If formatting elements (e.g. `%d`) are not found in the first string then
[`util.inspect()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilinspectobject-options) is called on each argument and the
resulting string values are concatenated. See [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args)
for more information.error('Details:', var error: SwapKitError
error.Property 'details' does not exist on type 'SwapKitError'.details);
}
}
Common Error Codes
Section titled “Common Error Codes”SwapKit uses standardized error codes:
// Wallet errors
"wallet_not_found"
"wallet_connection_failed"
"wallet_insufficient_funds"
// Transaction errors
"transaction_failed"
"transaction_rejected"
"transaction_timeout"
// API errors
"api_request_failed"
"api_rate_limit"
"api_invalid_response"
// Validation errors
"invalid_address"
"invalid_amount"
"invalid_chain"
Error Context
Section titled “Error Context”Errors include helpful context:
'try' expected.catch (var error: unknown
error) {
if (var error: unknown
error instanceof Cannot find name 'SwapKitError'.SwapKitError) {
// Error might include:
// - Transaction hash
// - Chain information
// - Wallet address
// - API response
console.Console.log(message?: any, ...optionalParams: any[]): void (+3 overloads)
Prints to `stdout` with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html)
(the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args)).
```js
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
```
See [`util.format()`](https://nodejs.org/docs/latest-v24.x/api/util.html#utilformatformat-args) for more information.log('Context:', 'error' is of type 'unknown'.error.info);
}
}
Wallet Types
Section titled “Wallet Types”SwapKit supports various wallet types through the WalletOption
enum:
import { enum WalletOption
WalletOption } from '@swapkit/sdk';
enum WalletOption
WalletOption.function (enum member) WalletOption.METAMASK = "METAMASK"
METAMASK
enum WalletOption
WalletOption.Property 'COINBASE' does not exist on type 'typeof WalletOption'.COINBASE
enum WalletOption
WalletOption.function (enum member) WalletOption.OKX = "OKX"
OKX
enum WalletOption
WalletOption.function (enum member) WalletOption.BITGET = "BITGET"
BITGET
enum WalletOption
WalletOption.function (enum member) WalletOption.EXODUS = "EXODUS"
EXODUS
enum WalletOption
WalletOption.function (enum member) WalletOption.KEPLR = "KEPLR"
KEPLR
enum WalletOption
WalletOption.function (enum member) WalletOption.LEAP = "LEAP"
LEAP
enum WalletOption
WalletOption.function (enum member) WalletOption.COSMOSTATION = "COSMOSTATION"
COSMOSTATION
enum WalletOption
WalletOption.function (enum member) WalletOption.LEDGER = "LEDGER"
LEDGER
enum WalletOption
WalletOption.function (enum member) WalletOption.TREZOR = "TREZOR"
TREZOR
enum WalletOption
WalletOption.function (enum member) WalletOption.KEEPKEY = "KEEPKEY"
KEEPKEY
enum WalletOption
WalletOption.function (enum member) WalletOption.WALLETCONNECT = "WALLETCONNECT"
WALLETCONNECT
enum WalletOption
WalletOption.function (enum member) WalletOption.KEYSTORE = "KEYSTORE"
KEYSTORE
enum WalletOption
WalletOption.Property 'POLKADOTJS' does not exist on type 'typeof WalletOption'. Did you mean 'POLKADOT_JS'?POLKADOTJS
enum WalletOption
WalletOption.function (enum member) WalletOption.TALISMAN = "TALISMAN"
TALISMAN
enum WalletOption
WalletOption.function (enum member) WalletOption.PHANTOM = "PHANTOM"
PHANTOM
enum WalletOption
WalletOption.function (enum member) WalletOption.ONEKEY = "ONEKEY"
ONEKEY
enum WalletOption
WalletOption.Property 'RADIX_MOBILE' does not exist on type 'typeof WalletOption'.RADIX_MOBILE
enum WalletOption
WalletOption.function (enum member) WalletOption.BRAVE = "BRAVE"
BRAVE
enum WalletOption
WalletOption.Property 'TRUSTWALLET' does not exist on type 'typeof WalletOption'.TRUSTWALLET
enum WalletOption
WalletOption.Property 'XDEFI' does not exist on type 'typeof WalletOption'.XDEFI
Fee Options
Section titled “Fee Options”Control transaction fees with the FeeOption
enum:
import { enum FeeOption
FeeOption } from '@swapkit/sdk';
// Available fee options
enum FeeOption
FeeOption.function (enum member) FeeOption.Average = "average"
Average // Standard fee
enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast // 1.5x standard
enum FeeOption
FeeOption.function (enum member) FeeOption.Fastest = "fastest"
Fastest // 2x standard
// Usage
await Cannot find name 'swapKit'.swapKit.transfer({
No value exists in scope for the shorthand property 'assetValue'. Either declare one or provide an initializer.assetValue,
No value exists in scope for the shorthand property 'recipient'. Either declare one or provide an initializer.recipient,
feeOptionKey: FeeOption
feeOptionKey: enum FeeOption
FeeOption.function (enum member) FeeOption.Fast = "fast"
Fast
});
Provider Types
Section titled “Provider Types”SwapKit integrates with multiple swap providers:
import { enum ProviderName
ProviderName } from '@swapkit/sdk';
enum ProviderName
ProviderName.function (enum member) ProviderName.THORCHAIN = "THORCHAIN"
THORCHAIN
enum ProviderName
ProviderName.function (enum member) ProviderName.CHAINFLIP = "CHAINFLIP"
CHAINFLIP
enum ProviderName
ProviderName.function (enum member) ProviderName.ONEINCH = "ONEINCH"
ONEINCH
enum ProviderName
ProviderName.Property 'UNISWAP' does not exist on type 'typeof ProviderName'.UNISWAP
enum ProviderName
ProviderName.Property 'SUSHISWAP' does not exist on type 'typeof ProviderName'. Did you mean 'SUSHISWAP_V2'?SUSHISWAP
enum ProviderName
ProviderName.function (enum member) ProviderName.PANCAKESWAP = "PANCAKESWAP"
PANCAKESWAP
Best Practices
Section titled “Best Practices”1. Always Use AssetValue for Amounts
Section titled “1. Always Use AssetValue for Amounts”// ✅ Good
const const amount: any
amount = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'ETH.ETH', value: number
value: 1 });
await Cannot find name 'swapKit'.swapKit.transferNo value exists in scope for the shorthand property 'recipient'. Either declare one or provide an initializer.({ assetValue: any
assetValue: const amount: any
amount, recipient });
// ❌ Bad - Don't use raw numbers
await Cannot find name 'swapKit'.swapKitNo value exists in scope for the shorthand property 'recipient'. Either declare one or provide an initializer..transfer({ value: number
value: 1, recipient });
2. Handle Decimal Precision
Section titled “2. Handle Decimal Precision”// AssetValue handles decimals automatically
const const usdcAmount: any
usdcAmount = Cannot find name 'AssetValue'.AssetValue.from({
asset: string
asset: 'ETH.USDC-0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
value: number
value: 100 // This is 100 USDC, not 100 * 10^6
});
// For base units, specify fromBaseDecimal
const const weiAmount: any
weiAmount = Cannot find name 'AssetValue'.AssetValue.from({
asset: string
asset: 'ETH.ETH',
value: string
value: '1000000000000000000',
fromBaseDecimal: boolean
fromBaseDecimal: true // This is 1 ETH in wei
});
3. Compare Assets Safely
Section titled “3. Compare Assets Safely”// ✅ Good - Use AssetValue comparison methods
if (Cannot find name 'balance'.balanceCannot find name 'requiredAmount'..gte(requiredAmount)) {
await Cannot find name 'performTransaction'.performTransaction();
}
// ❌ Bad - Don't compare raw values
if (var Number: NumberConstructor
(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.Number(Cannot find name 'balance'.balanceCannot find name 'requiredAmount'..getValue()) >= var Number: NumberConstructor
(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.Number(requiredAmount.getValue())) {
await Cannot find name 'performTransaction'.performTransaction();
}
4. Format for Display
Section titled “4. Format for Display”// Use formatting methods for UI display
const const btcBalance: any
btcBalance = Cannot find name 'AssetValue'.AssetValue.from({ asset: string
asset: 'BTC.BTC', value: number
value: 0.00123456 });
// For display
const const displayValue: any
displayValue = const btcBalance: any
btcBalance.toSignificant(6); // "0.00123456"
// For USD value
const const usdValue: any
usdValue = const btcBalance: any
btcBalance.toCurrency('USD', Cannot find name 'btcPrice'.btcPrice); // "$55.56"
Next Steps
Section titled “Next Steps”Now that you understand the core concepts:
- Set up your Configuration
- Learn about Toolbox Usage for direct chain operations
- Start Building Your First App