This guide will walk you through building a complete cross-chain bridge application using the Avail Nexus SDK. You’ll learn how to:
Initialize the SDK with a wallet provider
Set up user approval hooks
Fetch cross-chain balances
Execute a bridge transaction
Track transaction progress
1
Import the SDK
First, import the necessary components from the SDK:
import { NexusSDK, NEXUS_EVENTS } from '@avail-project/nexus-core';import type { BridgeParams } from '@avail-project/nexus-core';
These imports give you:
NexusSDK - The main SDK class
NEXUS_EVENTS - Event constants for tracking transaction progress
BridgeParams - TypeScript type for bridge parameters
2
Initialize the SDK
Create and initialize an SDK instance with a wallet provider:
// Create SDK instance (testnet for development)const sdk = new NexusSDK({ network: 'testnet' });// Initialize with MetaMask or any EIP-1193 providerawait sdk.initialize(window.ethereum);
Using a Custom Provider
For Node.js environments or custom wallet implementations:
import { ethers } from 'ethers';// Example: Using ethers.js walletconst wallet = new ethers.Wallet(privateKey, provider);const sdk = new NexusSDK({ network: 'mainnet' });await sdk.initialize(wallet);
The SDK supports both 'mainnet' and 'testnet' networks. Always start with 'testnet' during development.
3
Set Up Approval Hooks
Configure hooks to handle user approvals. These hooks are called when the SDK needs user permission:
// Intent approval hook - called when user needs to approve bridge detailssdk.setOnIntentHook(({ intent, allow, deny }) => { // Display intent details to user console.log('Bridge amount:', intent.destination.amount); console.log('Destination chain:', intent.destination.chainName); console.log('Total fees:', intent.fees.total); console.log('Source chains:', intent.sources); // User approves the transaction allow(); // Or user denies (will throw USER_DENIED_INTENT error) // deny();});// Allowance approval hook - called when token approval is neededsdk.setOnAllowanceHook(({ sources, allow, deny }) => { // Show which tokens need approval sources.forEach(source => { console.log(`Approve ${source.token.symbol} on ${source.chain.name}`); console.log(`Current: ${source.allowance.current}`); console.log(`Required: ${source.allowance.minimum}`); }); // Approve minimum required (recommended) allow(['min']); // Or approve unlimited // allow(['max']);});
Using 'min' for allowances is more secure as it only approves the exact amount needed for the transaction.
4
Fetch User Balances
Retrieve the user’s token balances across all supported chains: