Usage
Learn how to use the WDK Core module
This package serves as the main entry point and orchestrator for all WDK wallet modules, allowing you to register and manage different blockchain wallets and protocols through a single interface.
Installation
Install the @tetherto/wdk-core package:
npm install @tetherto/wdkBasic Usage
Importing WDK Core
import WDK from '@tetherto/wdk'Creating a WDK Instance
// Generate a random seed phrase
const seedPhrase = WDK.getRandomSeedPhrase()
// Create WDK instance
const wdk = new WDK(seedPhrase)
// Or use your own seed phrase
const wdk = new WDK('your existing seed phrase here...')Wallet Registration
Registering Wallets
import WalletManagerEvm from '@tetherto/wdk-wallet-evm'
import WalletManagerTon from '@tetherto/wdk-wallet-ton'
import WalletManagerBtc from '@tetherto/wdk-wallet-btc'
// Register wallets for different blockchains
const wdk = new WDK(seedPhrase)
.registerWallet('ethereum', WalletManagerEvm, {
provider: 'https://eth.drpc.org'
})
.registerWallet('ton', WalletManagerTon, {
tonApiKey: 'YOUR_TON_API_KEY',
tonApiEndpoint: 'https://tonapi.io'
})
.registerWallet('bitcoin', WalletManagerBtc, {
provider: 'https://blockstream.info/api'
})Account Management
Getting Accounts
// Get account by blockchain and index
const ethAccount = await wdk.getAccount('ethereum', 0)
const tonAccount = await wdk.getAccount('ton', 0)
// Get account by derivation path
const customEthAccount = await wdk.getAccountByPath('ethereum', "0'/0/1")
const customTonAccount = await wdk.getAccountByPath('ton', "1'/2/3")
// Get addresses
const ethAddress = await ethAccount.getAddress()
const tonAddress = await tonAccount.getAddress()
console.log('Ethereum address:', ethAddress)
console.log('TON address:', tonAddress)Multi-Chain Account Management
// Get accounts for different chains
const accounts = {
ethereum: await wdk.getAccount('ethereum', 0),
arbitrum: await wdk.getAccount('arbitrum', 0),
ton: await wdk.getAccount('ton', 0),
bitcoin: await wdk.getAccount('bitcoin', 0)
}
// Get all addresses
for (const [chain, account] of Object.entries(accounts)) {
const address = await account.getAddress()
console.log(`${chain} address:`, address)
}Balance Operations
Cross-Chain Balance Checking
async function checkAllBalances(wdk) {
const chains = ['ethereum', 'arbitrum', 'ton', 'bitcoin']
const balances = {}
for (const chain of chains) {
try {
const account = await wdk.getAccount(chain, 0)
const balance = await account.getBalance()
balances[chain] = balance
console.log(`${chain} balance:`, balance)
} catch (error) {
console.log(`${chain}: Wallet not registered`)
}
}
return balances
}
// Usage
const balances = await checkAllBalances(wdk)Transaction Operations
Sending Transactions
// Send Ethereum transaction
const ethAccount = await wdk.getAccount('ethereum', 0)
const ethResult = await ethAccount.sendTransaction({
to: '0x...',
value: 1000000000000000000n // 1 ETH
})
console.log('Ethereum transaction:', ethResult.hash)
// Send TON transaction
const tonAccount = await wdk.getAccount('ton', 0)
const tonResult = await tonAccount.sendTransaction({
to: 'EQ...',
value: 1000000000n // 1 TON
})
console.log('TON transaction:', tonResult.hash)Multi-Chain Transaction Function
async function sendMultiChainTransactions(wdk) {
// Ethereum transaction
const ethAccount = await wdk.getAccount('ethereum', 0)
const ethResult = await ethAccount.sendTransaction({
to: '0x...',
value: 1000000000000000000n // 1 ETH
})
console.log('Ethereum transaction:', ethResult.hash)
// TON transaction
const tonAccount = await wdk.getAccount('ton', 0)
const tonResult = await tonAccount.sendTransaction({
to: 'EQ...',
value: 1000000000n // 1 TON
})
console.log('TON transaction:', tonResult.hash)
}Protocol Integration
Registering Protocols
import veloraProtocolEvm from '@tetherto/wdk-protocol-swap-velora-evm'
import Usdt0ProtocolTon from '@tetherto/wdk-protocol-bridge-usdt0-ton'
// Register protocols globally
const wdk = new WDK(seedPhrase)
.registerWallet('ethereum', WalletManagerEvm, ethereumWalletConfig)
.registerWallet('ton', WalletManagerTon, tonWalletConfig)
.registerProtocol('ethereum', 'velora', veloraProtocolEvm, {
apiKey: 'YOUR_velora_API_KEY'
})
.registerProtocol('ton', 'usdt0', Usdt0ProtocolTon, {
tonApiKey: 'YOUR_TON_API_KEY'
})Using Protocols
// Get accounts with protocol support
const ethAccount = await wdk.getAccount('ethereum', 0)
const tonAccount = await wdk.getAccount('ton', 0)
// Use swap protocol
const velora = ethAccount.getSwapProtocol('velora')
const swapResult = await velora.swap({
tokenIn: '0x...',
tokenOut: '0x...',
tokenInAmount: 1000000n
})
// Use bridge protocol
const usdt0 = tonAccount.getBridgeProtocol('usdt0')
const bridgeResult = await usdt0.bridge({
targetChain: 'ethereum',
recipient: '0x...',
token: 'TON_TOKEN_ADDRESS',
amount: 1000000n
})Multiple Protocol Types
// Register different protocol types
const account = await wdk.getAccount('ethereum', 0)
// Swap protocol
account.registerProtocol('velora', veloraProtocolEvm, veloraConfig)
const velora = account.getSwapProtocol('velora')
// Bridge protocol
account.registerProtocol('usdt0', Usdt0ProtocolEvm, usdt0Config)
const usdt0 = account.getBridgeProtocol('usdt0')
// Lending protocol
account.registerProtocol('aave', AaveProtocolEvm, aaveConfig)
const aave = account.getLendingProtocol('aave')Middleware
Logging Middleware
// Register logging middleware
wdk.registerMiddleware('ethereum', async (account) => {
const address = await account.getAddress()
console.log('New Ethereum account created:', address)
})
wdk.registerMiddleware('ton', async (account) => {
const address = await account.getAddress()
console.log('New TON account created:', address)
})
// This will trigger the middleware
const account = await wdk.getAccount('ethereum', 0)Failover Middleware
import { getFailoverCascadeMiddleware } from '@tetherto/wdk-wrapper-failover-cascade'
// Register failover middleware
wdk.registerMiddleware('ethereum', getFailoverCascadeMiddleware({
retries: 3,
delay: 1000,
fallbackProviders: [
'https://backup-rpc-1.com',
'https://backup-rpc-2.com'
]
}))
// Transactions will automatically retry with fallback providers
const account = await wdk.getAccount('ethereum', 0)
const result = await account.sendTransaction(tx) // Will retry on failureComplete Examples
Multi-Chain Wallet Setup
import WDK from '@tetherto/wdk'
import WalletManagerEvm from '@tetherto/wdk-wallet-evm'
import WalletManagerTon from '@tetherto/wdk-wallet-ton'
import veloraProtocolEvm from '@tetherto/wdk-protocol-swap-velora-evm'
async function setupMultiChainWallet() {
// Generate or use existing seed phrase
const seedPhrase = WDK.getRandomSeedPhrase()
// Initialize WDK Manager
const wdk = new WDK(seedPhrase)
.registerWallet('ethereum', WalletManagerEvm, {
provider: 'https://eth.drpc.org'
})
.registerWallet('ton', WalletManagerTon, {
tonApiKey: 'YOUR_TON_API_KEY'
})
.registerProtocol('ethereum', 'velora', veloraProtocolEvm, {
apiKey: 'YOUR_velora_API_KEY'
})
// Get accounts
const ethAccount = await wdk.getAccount('ethereum', 0)
const tonAccount = await wdk.getAccount('ton', 0)
// Get addresses
const ethAddress = await ethAccount.getAddress()
const tonAddress = await tonAccount.getAddress()
console.log('Ethereum address:', ethAddress)
console.log('TON address:', tonAddress)
return { wdk, ethAccount, tonAccount }
}Error Handling
Handling Common Errors
async function handleErrors(wdk) {
try {
// This will throw if no wallet registered for 'tron'
const tronAccount = await wdk.getAccount('tron', 0)
} catch (error) {
console.error('Tron wallet not registered:', error.message)
}
try {
const ethAccount = await wdk.getAccount('ethereum', 0)
// This will throw if no swap protocol registered
const uniswap = ethAccount.getSwapProtocol('uniswap')
} catch (error) {
console.error('Uniswap protocol not registered:', error.message)
}
}Memory Management
async function cleanupExample(wdk) {
// Use the WDK Manager
const ethAccount = await wdk.getAccount('ethereum', 0)
const tonAccount = await wdk.getAccount('ton', 0)
// Perform operations
const ethAddress = await ethAccount.getAddress()
const tonAddress = await tonAccount.getAddress()
// Clean up sensitive data
wdk.dispose()
// After dispose, accounts are no longer usable
// This will throw an error
try {
await ethAccount.getAddress()
} catch (error) {
console.log('Account disposed, cannot access private keys')
}
}
