initialize
Initialize the SDK with an EVM-compatible wallet provider.
await sdk . initialize ( provider : EthereumProvider ): Promise < void >
EIP-1193 compatible provider (e.g., MetaMask, WalletConnect, Coinbase Wallet)
Example
MetaMask
WalletConnect
Coinbase Wallet
import { NexusSDK } from '@avail-project/nexus-core' ;
const sdk = new NexusSDK ({ network: 'mainnet' });
// Initialize with MetaMask
await sdk . initialize ( window . ethereum );
Error Handling
import { NexusError , ERROR_CODES } from '@avail-project/nexus-core' ;
try {
await sdk . initialize ( window . ethereum );
console . log ( 'SDK initialized successfully' );
} catch ( error ) {
if ( error instanceof NexusError ) {
if ( error . code === ERROR_CODES . WALLET_NOT_CONNECTED ) {
// Prompt user to connect wallet
console . error ( 'Please connect your wallet' );
} else if ( error . code === ERROR_CODES . CONNECT_ACCOUNT_FAILED ) {
// Retry connection
console . error ( 'Failed to connect account, please retry' );
}
}
}
setEVMProvider
Set or update the EVM provider without full re-initialization. Useful for fetching balances before full initialization.
await sdk . setEVMProvider ( provider : EthereumProvider ): Promise < void >
EIP-1193 compatible provider
Example
// Set provider before initialization to fetch balances
await sdk . setEVMProvider ( window . ethereum );
const balances = await sdk . getBalancesForBridge ();
// Later, complete initialization for full functionality
await sdk . initialize ( window . ethereum );
isInitialized
Check if the SDK is fully initialized and ready for operations.
sdk . isInitialized (): boolean
Returns
true if SDK is fully initialized, false otherwise
Example
if ( sdk . isInitialized ()) {
// Safe to call SDK methods
const result = await sdk . bridge ({
token: 'USDC' ,
amount: 100_000_000 n ,
toChainId: 137 ,
});
} else {
console . log ( 'SDK not initialized yet' );
}
triggerAccountChange
Manually trigger account change detection. Useful for providers that don’t have event hooks like .on(...) and .removeListener(...).
sdk . triggerAccountChange (): void
This method does nothing if the address hasn’t changed since the last check.
Example
// Poll for account changes every 5 seconds
setInterval (() => {
sdk . triggerAccountChange ();
}, 5000 );
Use Case
Some wallet providers don’t emit account change events. Use this method to manually check for changes:
// For providers without event listeners
const checkAccountChange = async () => {
const currentAccount = await provider . request ({
method: 'eth_accounts'
});
if ( currentAccount [ 0 ] !== lastKnownAccount ) {
sdk . triggerAccountChange ();
lastKnownAccount = currentAccount [ 0 ];
}
};
setInterval ( checkAccountChange , 3000 );
deinit
Clean up SDK resources and track session analytics. Call this when unmounting your application or disconnecting the wallet.
await sdk . deinit (): Promise < void >
Example
import { useEffect } from 'react' ;
import { NexusSDK } from '@avail-project/nexus-core' ;
function App () {
const sdk = new NexusSDK ({ network: 'mainnet' });
useEffect (() => {
// Initialize on mount
sdk . initialize ( window . ethereum );
// Cleanup on unmount
return () => {
sdk . deinit ();
};
}, []);
return < div > My App </ div > ;
}
Complete Lifecycle Example
Here’s a complete example showing the full SDK lifecycle:
import { NexusSDK , NEXUS_EVENTS } from '@avail-project/nexus-core' ;
class WalletManager {
private sdk : NexusSDK | null = null ;
async connect () {
// Create SDK instance
this . sdk = new NexusSDK ({
network: 'mainnet' ,
debug: process . env . NODE_ENV === 'development' ,
analytics: {
enabled: true ,
privacy: {
anonymizeWallets: true ,
},
},
});
// Set up hooks before initialization
this . sdk . setOnIntentHook (({ intent , allow , deny }) => {
// Show intent confirmation UI
const confirmed = confirm (
`Bridge ${ intent . sourcesTotal } to ${ intent . destination . chainName } ?`
);
confirmed ? allow () : deny ();
});
this . sdk . setOnAllowanceHook (({ sources , allow , deny }) => {
// Show approval confirmation UI
const confirmed = confirm ( 'Approve token allowance?' );
confirmed ? allow ([ 'min' ]) : deny ();
});
// Initialize with provider
await this . sdk . initialize ( window . ethereum );
console . log ( 'SDK initialized and ready' );
}
async disconnect () {
if ( this . sdk ) {
await this . sdk . deinit ();
this . sdk = null ;
console . log ( 'SDK deinitialized' );
}
}
isReady () : boolean {
return this . sdk ?. isInitialized () ?? false ;
}
getSDK () : NexusSDK {
if ( ! this . sdk || ! this . sdk . isInitialized ()) {
throw new Error ( 'SDK not initialized' );
}
return this . sdk ;
}
}
// Usage
const wallet = new WalletManager ();
// Connect wallet
await wallet . connect ();
// Use SDK
if ( wallet . isReady ()) {
const sdk = wallet . getSDK ();
const balances = await sdk . getBalancesForBridge ();
}
// Disconnect wallet
await wallet . disconnect ();
NexusSDK Class Main SDK class reference
Error Handling Handle initialization errors