This directory contains practical examples demonstrating how to use the @avalanche-sdk/client package to interact with the Avalanche blockchain.
- Node.js: Version 20 or higher
- npm: Latest version recommended
- TypeScript: Examples are written in TypeScript
-
Navigate to the examples directory:
cd client/examples -
Install the latest SDK version:
npm install @avalanche-sdk/client@latest
-
Run any example:
npx tsx <example-file.ts>
-
Build the SDK from source:
# From the project root npm run build:all -
Create a global symlink:
npm link
-
Navigate to examples and link to local build:
cd client/examples npm link @avalanche-sdk/client -
Run any example:
npx tsx <example-file.ts>
Note: Make sure to create your own
.envfile by copying the.env.examplefile and updating the values. You'll also need to modify theconfig.tsfile to point to your.envfile path. By default, the examples use the values from.env.example, and the test addresses mentioned in the examples as comments (like0x76Dd3d7b2f635c2547B861e55aE8A374E587742DandX-fuji19fc97zn3mzmwr827j4d3n45refkksgms4y2yzz) are derived from the private key values in that file.
sendAvax.ts- Basic AVAX transfer example
Located in react-show-balance-and-cross-chain-transfers/:
The React examples demonstrate how to build a modern web application that integrates with the Avalanche blockchain using the SDK. These examples showcase:
- Wallet Integration: Connecting to Core browser extension via EIP-1193 provider
- Balance Display: Real-time P-Chain and C-Chain AVAX balance monitoring
- Cross-Chain Transfers: Seamless AVAX transfers between Platform and Contract chains
- Network Switching: Dynamic switching between Fuji testnet and Mainnet
- Modern UI: Built with Material-UI and React 18, featuring responsive design
- Real-time Balance Updates: Automatic balance refresh every 20 seconds
- Chain-Specific Logic: Different handling for P-Chain (Platform) vs C-Chain (Contract)
- Error Handling: Comprehensive error states and user feedback
- Responsive Design: Mobile-friendly interface with adaptive layouts
- Type Safety: Full TypeScript implementation with proper type definitions
Due to the Node.js environment assumptions in the Avalanche SDK, several polyfills are required for browser compatibility:
// vite.config.ts
export default defineConfig({
plugins: [react()],
define: {
// Polyfill Node.js globals for browser environment
global: "globalThis",
"process.env": {},
},
resolve: {
alias: {
// Polyfill Node.js modules
process: "process/browser",
util: "util",
},
},
optimizeDeps: {
include: ["process", "util"],
},
});Dependencies to install:
npm install process util
npm install --save-dev @types/nodeWhat are Polyfills?
Polyfills are code that implements a feature on web browsers that do not support that feature. In this case, some dependencies in the Avalanche SDK was designed for Node.js environments and uses Node.js-specific modules like process and util. Since browsers don't have these modules, we need to provide browser-compatible versions. Learn more about polyfills in the MDN Web Docs and web.dev.
-
Navigate to the React examples directory:
cd react-show-balance-and-cross-chain-transfers -
Install dependencies:
npm install
-
Start development server:
npm run dev
Located in prepare-primary-network-txns/:
transfer-avax-from-x-chain-to-p-chain.ts- Transfer AVAX from X-Chain to P-Chaintransfer-avax-from-p-chain-to-x-chain.ts- Transfer AVAX from P-Chain to X-Chaintransfer-avax-from-x-chain-to-c-chain.ts- Transfer AVAX from X-Chain to C-Chaintransfer-avax-from-c-chain-to-x-chain.ts- Transfer AVAX from C-Chain to X-Chaintransfer-avax-from-p-chain-to-c-chain.ts- Transfer AVAX from P-Chain to C-Chaintransfer-avax-from-c-chain-to-p-chain.ts- Transfer AVAX from C-Chain to P-Chain
x-chain/- X-Chain specific operationsp-chain/- P-Chain specific operations, including ACP-236 auto-renewed validator transactionsc-chain/- C-Chain specific operations
Most examples require configuration before running:
-
Network Selection: Examples default to Fuji testnet. Modify the network configuration in each example file to use mainnet or local network.
-
Private Keys: Copy the example environment file in .env and edit with your actual values
-
Addresses: Update recipient addresses to valid Avalanche addresses.
- Never commit your
.envfile - it contains sensitive private keys - Use testnet keys for development and testing
- Keep your mainnet private keys secure and offline
- Testnet Usage: Examples are configured for Fuji testnet by default. Use testnet AVAX for experimentation.
- Security: Never commit private keys or sensitive information to version control.
- Node Version: Ensure you're using Node.js version 20 or higher for compatibility.
- Browser Compatibility: React examples require modern browsers with ES2020+ support.
-
"Cannot find module" errors:
- Ensure you've installed dependencies:
npm install - Check that you're in the correct directory
- Ensure you've installed dependencies:
-
TypeScript compilation errors:
- Verify Node.js version:
node --version - Reinstall dependencies:
rm -rf node_modules && npm install
- Verify Node.js version:
-
Network connection issues:
- Check your internet connection
- Verify the RPC endpoint is accessible
- Consider using a different RPC provider
-
React examples not working in browser:
- Ensure all polyfills are properly configured
- Check browser console for polyfill-related errors
- Verify that
processandutilpackages are installed
-
"process is not defined" errors:
- Ensure polyfills are properly configured in
vite.config.ts - Check that
processpackage is installed
- Ensure polyfills are properly configured in
-
"global is not defined" errors:
- Verify
global: "globalThis"is set in Vite config - This is required for Node.js compatibility in browsers
- Verify
- Avalanche Network Documentation
- GitHub Repository
- Understanding Polyfills
- Vite Configuration Guide
- React with TypeScript
Found an issue or want to add more examples? Please contribute by:
- Forking the repository
- Creating a feature branch
- Adding your example or fix
- Submitting a pull request
Happy building on Avalanche! 🏔️