Avalanche
Avalanche
#Installation
Magic interacts with the Avalanche blockchain via Magic's extension NPM package @magic-ext/avalanche
. The Avalanche extension also lets you interact with the blockchain using methods from Avalanche's Javascript SDK.
You can skip straight to our github example directly:
👉 Avalanche Example
01npm install --save @magic-ext/avalanche
#Initialization
Note: This is for X-Chain implementation.
01import { Magic } from 'magic-sdk';
02import { AvalancheExtension } from '@magic-ext/avalanche';
03
04const magic = new Magic('YOUR API KEY', {
05 extensions: {
06 xchain: new AvalancheExtension({
07 rpcUrl: 'Avalanche node url',
08 chainId: 'Avalanche chain id',
09 networkId: 4, // Avalanche networkId
10 }),
11 },
12});
#Common Methods
#Get Test AVAX
Before you can send transaction on the Avalanche blockchain, you'll need to acquire some test AVAX (Avalanche's native cryptocurrency for test network).
- Go to our Avalanche Example
- Login with your email address
- Copy your Avalanche public address
- Go to the Avalanche Faucet
- Paste your copied Avalanche public address in the text input
- You can receive 10000000 nAVAX
- Now you can use your test AVAX in our example app
#Send Transaction
Note that the Magic Avalanche extension follows the method names and conventions by Avalanche's Javascript SDK. To send a standard Avalanche blockchain transaction, you can call the magic.xchain.signTransaction()
method.
01import { Magic } from 'magic-sdk';
02import { AvalancheExtension } from '@magic-ext/avalanche';
03import { Avalanche, BinTools, Buffer, BN } from 'avalanche';
04
05const magic = new Magic('YOUR API KEY', {
06 extensions: {
07 xchain: new AvalancheExtension({
08 rpcUrl: 'Avalanche node url',
09 chainId: 'Avalanche chain id',
10 networkId: 4, // Avalanche networkId
11 }),
12 },
13});
14
15const metadata = await magic.user.getMetadata();
16let myNetworkID = 4; //default is 3, we want to override that for our local network
17let myBlockchainID = 'X'; // The XChain blockchainID on this network
18let ava = new Avalanche('testapi.avax.network', 443, 'https', myNetworkID, myBlockchainID);
19let xchain = ava.XChain();
20let assetId = 'nznftJBicce1PfWQeNEVBmDyweZZ6zcM3p78z9Hy9Hhdhfaxm';
21
22let fromAddresses = [metadata.publicAddress];
23let toAddresses = ['X-everest1zr334udmau3xruusmwnyng3hug38errx83h6xq'];
24let sendAmount = 1000000;
25
26const signedTx = await magic.xchain.signTransaction(sendAmount, assetId, toAddresses, fromAddresses, toAddresses);
27
28console.log('signedTX', signedTx);
29
30let txid = await xchain.issueTx(signedTx);
31
32console.log('send transaction', txid);