Skip to content

A quick demo CLI demonstrating the Tetherto WDK (Wallet Development Kit) for multi-chain cryptocurrency wallet functionality.

Notifications You must be signed in to change notification settings

makevoid/try_wdk_node1

Repository files navigation

WDK Demo 1

A quick demo CLI demonstrating the Tetherto WDK (Wallet Development Kit) for multi-chain cryptocurrency wallet functionality.

Features

  • Multi-chain wallet management using a single seed phrase
  • OOP library architecture with reusable wallet and API classes
  • Automatic wallet persistence with local file storage
  • Balance checking for native tokens (MATIC)
  • Transaction sending with fee estimation and confirmation tracking
  • Transaction history via WDK Indexer API for token transfers
  • Support for Polygon (easily extensible to Ethereum, Base, Tron, Bitcoin, etc.)

Installation

npm install

Quick Start

1. Set up your wallet

Create a .wallet file with your 12-word seed phrase:

echo "your twelve word seed phrase goes here like this example phrase" > .wallet

2. Set up WDK API key

Create a .wdk-api-key file with your API key from Tether WDK:

echo "your-api-key-here" > .wdk-api-key

3. Run the scripts

Check your balance:

npm run balance

Send a transaction:

npm run send

Get transaction history:

npm run check

Available Commands

Command Description
npm run balance Check Polygon MATIC balance
npm run send Send a transaction on Polygon
npm run check Get USDT transaction history from WDK Indexer API

Architecture

Library Structure (/lib)

The project uses a modular OOP library architecture for reusability and maintainability:

WalletManager.js

WalletManager (Base class)

  • Core wallet operations for any blockchain
  • Seed phrase loading and management
  • WDK initialization and wallet registration
  • Account access with caching
  • Methods: getAddress(), getBalance(), getNetworkInfo(), quoteTransaction(), sendTransaction(), waitForTransaction()
  • Static utility: formatBalance() for wei/token conversion

PolygonWallet (Specialized class)

  • Pre-configured for Polygon network
  • Extends WalletManager with Polygon-specific defaults
  • Returns formatted balance objects
  • Includes getExplorerUrl() for PolygonScan links

ApiClient.js

ApiClient (Base class)

  • Generic REST API client with GET/POST methods
  • Centralized response handling
  • Structured error handling

WdkApiClient (Specialized class)

  • Pre-configured for WDK Indexer API (https://wdk-api.tether.io)
  • Automatic API key management from .wdk-api-key file
  • Methods: getTokenTransfers(), getTokenBalances(), health()
  • Factory method: fromFile() for easy instantiation

ApiError (Custom error class)

  • Structured API error handling with status codes
  • Helper methods: isRateLimitError(), isAuthError(), isNotFoundError()

Scripts

  • check-polygon.js: Check Polygon MATIC balance using PolygonWallet
  • send-transaction.js: Send transactions on Polygon with fee estimation and confirmation
  • get-transactions.js: Fetch USDT transaction history using WdkApiClient

Usage Examples

Check Balance

import { PolygonWallet } from "./lib/WalletManager.js";

const wallet = new PolygonWallet();
wallet.initialize();

const balance = await wallet.getBalance();
console.log(`${balance.formatted} ${balance.token}`);

Send Transaction

import { PolygonWallet } from "./lib/WalletManager.js";

const wallet = new PolygonWallet();
wallet.initialize();

const address = await wallet.getAddress();

// Send 0.001 MATIC to self
const result = await wallet.sendTransaction({
  to: address,
  value: 1000000000000000n // 0.001 MATIC in wei
});

console.log(`Transaction: ${wallet.getExplorerUrl(result.hash)}`);

// Wait for confirmation
const receipt = await wallet.waitForTransaction(result.hash);
console.log(`Confirmed in block ${receipt.blockNumber}`);

Get Transaction History

import { PolygonWallet } from "./lib/WalletManager.js";
import { WdkApiClient } from "./lib/ApiClient.js";

const wallet = new PolygonWallet();
wallet.initialize();

const apiClient = WdkApiClient.fromFile();
const address = await wallet.getAddress();

const transfers = await apiClient.getTokenTransfers('polygon', 'usdt', address, {
  limit: 20,
  offset: 0
});

console.log(`Found ${transfers.data.length} transactions`);

Extending to Other Chains

To add support for another blockchain (e.g., Ethereum):

import { WalletManager } from "./lib/WalletManager.js";
import WalletManagerEvm from "@tetherto/wdk-wallet-evm";

export class EthereumWallet extends WalletManager {
  constructor(options = {}) {
    super(options);
    this.chainId = "ethereum";
    this.provider = options.provider || "https://eth.llamarpc.com";
    this.nativeToken = "ETH";
  }

  initialize() {
    super.initialize();
    this.registerWallet(this.chainId, WalletManagerEvm, {
      provider: this.provider
    });
    return this;
  }

  async getAddress() {
    return super.getAddress(this.chainId);
  }

  async getBalance() {
    const balance = await super.getBalance(this.chainId);
    return {
      wei: balance,
      formatted: WalletManager.formatBalance(balance, 18, 6),
      token: this.nativeToken
    };
  }

  // ... other methods similar to PolygonWallet
}

Security Notes

  • The .wallet file contains your private seed phrase - keep it secure and never share it
  • The .wdk-api-key file contains your API key - do not commit to version control
  • Both files are in .gitignore by default
  • Use authenticated/private RPC endpoints for production applications
  • The sample uses public RPC endpoints for demonstration purposes

Project Structure

.
├── lib/
│   ├── WalletManager.js    # Wallet management classes
│   ├── ApiClient.js         # API client classes
│   └── index.js             # Central exports
├── check-polygon.js         # Balance checker script
├── send-transaction.js      # Transaction sender script
├── get-transactions.js      # Transaction history script
├── package.json             # Dependencies and npm scripts
├── .wallet                  # Seed phrase (gitignored)
├── .wdk-api-key            # WDK API key (gitignored)
├── CLAUDE.md               # Developer guidance
└── README.md               # This file

WDK Resources

License

ISC

About

A quick demo CLI demonstrating the Tetherto WDK (Wallet Development Kit) for multi-chain cryptocurrency wallet functionality.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published