Wallet SDK

Notice: Beta Release🔴

This library is under rapid development and there may be frequent breaking changes. An audit is pending.

Avalanche Wallet SDK (Beta)

The Avalanche Wallet SDK is a TypeScript library for creating and managing non-custodial wallets on the Avalanche network.
It provides high-level methods to transact on Avalanche's X-Chain, P-Chain and C-Chain.
The wallet types are supported:
    Singleton Wallets
    Ledger Wallets
    Mnemonic Wallets
    XPUB Wallets
Using avalanche-wallet-sdk, developers can:
    Receive and send tokens and NFTs.
    Transfer funds between chains
    Add a node to the validator set
    Delegate stake to a validator
    Create keystore files from wallet instances
    Get the transaction history of a wallet
    Mint NFTs on the X-Chain

Installation

Source code can be found in this library's Github repo.

Install with npm

npm install --save @avalabs/avalanche-wallet-sdk

Install with yarn

yarn add @avalabs/avalanche-wallet-sdk

Classes

See here for Classes exposed by this library.

Example Usage

Event Listeners

Every wallet instance will fire events to indicate changes in its state.
1
// Create a wallet instance
2
let myWallet = MnemonicWallet.create()
3
4
// Fired when the wallet starts using a new address
5
// Used only with HD wallets (Mnemonic, Ledger, and XPUB)
6
myWallet.on('addressChanged', (addresses)=>{
7
// Use the most recent addresses from the wallet
8
})
9
10
// Fired when X chain balance is updated
11
myWallet.on('balanceChangedX', (newBalance)=>{
12
// Recent X chain balance
13
})
14
15
// Fired when P chain AVAX balance is updated
16
myWallet.on('balanceChangedP', (newBalance)=>{
17
// Recent P chain AVAX balance
18
})
19
20
// Fired when C chain AVAX balance is updated
21
myWallet.on('balanceChangedC', (newBalance)=>{
22
// Recent C chain AVAX balance
23
})
Copied!

Sending AVAX

1
import {MnemonicWallet, BN} from '@avalabs/avalanche-wallet-sdk'
2
3
let myWallet = MnemonicWallet.create()
4
5
// Mnemonic wallets need to find their HD index on startup
6
await myWallet.resetHdIndices()
7
8
// Update the UTXOs
9
await myWallet.updateUtxosX()
10
11
// Send 1 nAVAX
12
let to = "X-avax1r20dtfehaat9wev69ajzzfcwtll903vlcx50uh"
13
let amount = new BN(1)
14
let txID = await myWallet.sendAvax(to, amount)
Copied!

Changing Networks

By default the SDK is connected to the Avalanche Mainnet.
1
import { NetworkConstants, Network} from '@avalabs/avalanche-wallet-sdk';
2
3
// Set to Mainnet
4
Network.setNetwork(NetworkConstants.MainnetConfig)
5
6
// Set to Fuji Testnet
7
Network.setNetwork(NetworkConstants.TestnetConfig)
Copied!

Printing BN (Big Number)

Token amounts are represented in their smallest divisible unit using BN.js. The Utils namespace has helper functions to display BN numbers in a human readable way.
1
import {Utils} from '@avalabs/avalanche-wallet-sdk'
2
3
// On X-Chain and P-Chain AVAX has 9 decimals
4
let amtX = new BN(1234567000000)
5
Utils.bnToAvaxX(amtX) // 1,234.567
6
7
// On the C-Chain it has 18
8
let amtC = new BN(1234567000000000000000)
9
Utils.bnToAvaxC(amtC) // 1,234.567
Copied!

Websocket Provider

Use the WebsocketProvider class to update wallet balances in real time without polling.
1
import { Network, NetworkConstants } from 'avalanche-wallet-sdk';
2
3
// Create a websocket provider from the network currently used by the SDK
4
const provider = Network.WebsocketProvider.fromActiveNetwork()
5
6
// To track wallets and update their balances
7
provider.trackWallet(myWallet)
8
9
// To stop tracking wallets
10
// Make sure to call this to avoid memory leaks
11
provider.removeWallet(myWallet)
12
13
// To change provider network
14
provider.setNetwork(NetworkConstants.TestnetConfig) // connect to Fuji testnet
Copied!

Adding ERC20 Tokens

The SDK comes loaded with a set of ERC20 contracts. You can add additional contracts like this:
1
import { Assets } from '@avalabs/avalanche-wallet-sdk'
2
3
// Will try to fetch details about the ERC20 contract
4
try{
5
await Assets.addErc20Token('0x34B6C87bb59Eb37EFe35C8d594a234Cd8C654D50'); // Testnet DAI
6
}catch(e){
7
// Contract not found or not valid
8
}
9
10
// or from known data
11
let tokenData = {
12
chainId: 43114,
13
address: '0xbA7dEebBFC5fA1100Fb055a87773e1E99Cd3507a',
14
decimals: 18,
15
name: 'Dai Stablecoin',
16
symbol: 'DAI',
17
}
18
19
Assets.addErc20TokenFromData(tokenData)
Copied!
Last modified 3mo ago