Connecting Wallets
SwapDK provides multiple ways to connect wallets for different blockchain networks. This guide demonstrates how to connect various wallet types using both granular and all-in-one approaches.
Wallet Connection Demo
Try connecting different wallet types to see how SwapDK handles multi-chain connections
Connection Status Disconnected
Select chains and a wallet type, then click "Connect Wallet" to see the connection process.
Note: This is a simulation for demonstration purposes.
In a real application, you'd handle actual wallet connections and user interactions.
Try it live in StackBlitz →
Try it live in StackBlitz →
Two Approaches to Using SwapDK
Section titled “Two Approaches to Using SwapDK”You can use SwapDK in two ways:
-
Granular approach - Import only what you need from individual packages:
import { SwapKit, keystoreWallet } from "@swapdk/sdk"; -
All-in-one approach - Import everything from the SDK (better for backend/Node.js):
import { createSwapKit } from "@swapdk/sdk";
The granular approach is recommended for frontend applications as it results in smaller bundle sizes.
Initialize SwapDK
Section titled “Initialize SwapDK”Granular Approach (Frontend Recommended)
Section titled “Granular Approach (Frontend Recommended)”import { SwapKit, keystoreWallet, ledgerWallet, evmWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ config: { apiKeys: { swapKit: "your-swapdk-api-key", walletConnectProjectId: "your-walletconnect-project-id", }, }, wallets: { ...keystoreWallet, ...ledgerWallet, ...evmWallet },});All-in-one Approach (Backend/Node.js)
Section titled “All-in-one Approach (Backend/Node.js)”import { createSwapKit } from "@swapdk/sdk";
const swapDK = createSwapKit({ config: { apiKeys: { swapKit: "your-swapdk-api-key", walletConnectProjectId: "your-walletconnect-project-id", }, },});Connect with Keystore Wallet
Section titled “Connect with Keystore Wallet”Granular Approach
Section titled “Granular Approach”import { Chain, SwapKit, keystoreWallet, decryptFromKeystore, type Keystore,} from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...keystoreWallet },});
async function connectWithKeystore( keystoreFile: Keystore, password: string, chains: Chain[]) { try { const phrase = await decryptFromKeystore(keystoreFile, password);
await swapDK.connectKeystore(chains, phrase);
const balances = await Promise.all( chains.map(async (chain) => { const wallet = await swapDK.getWalletWithBalance(chain); return wallet.balance; }) );
return { success: true, balances }; } catch (error) { return { success: false, error }; }}
const chains = [Chain.Ethereum, Chain.Bitcoin, Chain.Ripple];await connectWithKeystore(keystoreFile, password, chains);All-in-one Approach
Section titled “All-in-one Approach”import { Chain, createSwapKit, decryptFromKeystore, type Keystore,} from "@swapdk/sdk";
const swapDK = createSwapKit();
async function connectWithKeystore( keystoreFile: Keystore, password: string, chains: Chain[]) { try { const phrase = await decryptFromKeystore(keystoreFile, password);
await swapDK.connectKeystore(chains, phrase);
return { success: true }; } catch (error) { return { success: false, error }; }}Using Hardware Wallets
Section titled “Using Hardware Wallets”Ledger Wallet
Section titled “Ledger Wallet”import { Chain, SwapKit, ledgerWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...ledgerWallet },});
async function connectLedger(chains: Chain[]) { try { await swapDK.connectLedger(chains);
const ethAddress = swapDK.getAddress(Chain.Ethereum);
return { success: true }; } catch (error) { return { success: false, error }; }}Trezor Wallet
Section titled “Trezor Wallet”import { Chain, NetworkDerivationPath, SwapKit, trezorWallet,} from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...trezorWallet },});
async function connectTrezor(chains: Chain[]) { try { await swapDK.connectTrezor([Chain.Bitcoin], NetworkDerivationPath.BTC);
const btcWallet = await swapDK.getWalletWithBalance(Chain.Bitcoin);
return { success: true, balance: btcWallet.balance }; } catch (error) { return { success: false, error }; }}Using Browser Extension Wallets
Section titled “Using Browser Extension Wallets”MetaMask / EVM Wallets
Section titled “MetaMask / EVM Wallets”import { Chain, SwapKit, evmWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...evmWallet },});
async function connectEVMWallet() { try { await swapDK.connectEVMWallet([Chain.Ethereum, Chain.BinanceSmartChain]);
const address = swapDK.getAddress(Chain.Ethereum);
return { success: true, address }; } catch (error) { return { success: false, error }; }}Keplr (Cosmos) Wallet
Section titled “Keplr (Cosmos) Wallet”import { Chain, SwapKit, keplrWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...keplrWallet },});
async function connectKeplr() { try { await swapDK.connectKeplr([Chain.Cosmos]);
const address = swapDK.getAddress(Chain.Cosmos);
return { success: true, address }; } catch (error) { return { success: false, error }; }}Cosmostation (Cosmos) Wallet
Section titled “Cosmostation (Cosmos) Wallet”import { Chain, SwapKit, cosmostationWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...cosmostationWallet },});
async function connectCosmostation() { try { await swapDK.connectCosmostation([Chain.Cosmos, Chain.THORChain]);
const address = swapDK.getAddress(Chain.Cosmos);
return { success: true, address }; } catch (error) { return { success: false, error }; }}Phantom (Solana) Wallet
Section titled “Phantom (Solana) Wallet”import { Chain, SwapKit, phantomWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...phantomWallet },});
async function connectPhantom() { try { await swapDK.connectPhantom([Chain.Solana]);
const address = swapDK.getAddress(Chain.Solana);
return { success: true, address }; } catch (error) { return { success: false, error }; }}Managing Wallet Connections
Section titled “Managing Wallet Connections”import { Chain, SwapKit, evmWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...evmWallet },});
function getAllWallets() { const wallets = swapDK.getAllWallets(); return wallets;}
function disconnectChain(chain: Chain) { swapDK.disconnectChain(chain);}
function disconnectAll() { swapDK.disconnectAll();}Getting Wallet Balances
Section titled “Getting Wallet Balances”import { Chain, SwapKit, evmWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...evmWallet },});
await swapDK.connectEVMWallet([Chain.Ethereum]);
async function getChainBalance(chain: Chain) { const balance = await swapDK.getBalance(chain, true); return balance;}
async function getAllBalances(chains: Chain[]) { const balances = await Promise.all( chains.map(async (chain) => { try { const wallet = await swapDK.getWalletWithBalance(chain); return { chain, balance: wallet.balance }; } catch (error) { return { chain, balance: [], error }; } }) );
return balances;}Using WalletConnect
Section titled “Using WalletConnect”import { Chain, SwapKit, walletconnectWallet } from "@swapdk/sdk";
const swapDK = SwapKit({ config: { apiKeys: { walletConnectProjectId: "YOUR_WALLETCONNECT_PROJECT_ID", }, }, wallets: { ...walletconnectWallet },});
async function connectWalletConnect() { try { await swapDK.connectWalletconnect([ Chain.Ethereum, Chain.BinanceSmartChain, Chain.Cosmos, ]);
const ethAddress = swapDK.getAddress(Chain.Ethereum);
return { success: true }; } catch (error) { return { success: false, error }; }}Advanced: Connecting Multiple Wallets
Section titled “Advanced: Connecting Multiple Wallets”import { Chain, SwapKit, evmWallet, keplrWallet, keystoreWallet,} from "@swapdk/sdk";
const swapDK = SwapKit({ wallets: { ...evmWallet, ...keplrWallet, ...keystoreWallet },});
async function connectMultipleWallets(phrase: string) { try { await swapDK.connectEVMWallet([Chain.Ethereum]);
await swapDK.connectKeplr([Chain.Cosmos]);
await swapDK.connectKeystore([Chain.Bitcoin], phrase);
const wallets = swapDK.getAllWallets();
const addresses = { [Chain.Ethereum]: swapDK.getAddress(Chain.Ethereum), [Chain.Cosmos]: swapDK.getAddress(Chain.Cosmos), [Chain.Bitcoin]: swapDK.getAddress(Chain.Bitcoin), };
return { success: true, addresses }; } catch (error) { return { success: false, error }; }}