My Avalanche learning experience

Leksey Peskov
6 min readApr 6, 2021

Welcome all. Today we will have a very interesting experience. Let’s learn new things. Let’s understand what Avalanche is and how it works inside. And many more interesting things. Sit at your laptops. Start learning!

Download the latest version of NodeJS (12.x+) and any code editor like VSCode etc. Check for the existence of the version of NodeJS by writing this:

Let’s create a directory for your project. It can be done with this command.

You will see a new file package.json created in the current directory.

Write this code to install all necessary dependencies:

You will see a new package-lock.json file and the packages added to your package.json file:

Let’s create an .env for holding all our sensitive data and a .gitignore file with the code:

And in an.env file add a few environmental variables:

replace YOUR_API_KEY with the valid DataHub access key.

We configured everything we need and can finally start building our application.

Let’s create a new file client.js in the root directory, with the code:

the file provides us with a nice little function to create and configure the Avalanche node client

Create a connect.js file with the following:

we query Avalanche node for some basic information, like node software version, network name, number of peers

Let’s run our code:

If all our code is correct, we will see this result:

connected to the node

We will be creating out account programmatically using the Javascript client package. Сreate a new file create_account.jswith the code:

We need to create our private key for making transactions on the network. Create the directory credentials thanks to this:

We will be storing the secret key in ./credentials/keypair.json file

Let’s replace the // 1. Configure keychain with:

For creating a new key, or reading an existing key from the JSON file, we need to replace // 2. Generate private key with:

keyChain.makeKey() generates a new private key and keyChain.importKey(...) loads an existing private key

Finally, to perform balance checking of our X-Chain address, we need to replace // 3. Check address balance:

Let’s fire our code up:

This result will be shown to you if all the past steps are correct:

Let’s go on Avalanche Faucet and request 2 AVAX. Your address is now funded with 2 AVAX.

We understood how fetch data from network(for instance, retrieving address balances).

Let’s prepare a new filequery.js:

a placeholder method for querying information using different clients

Information about the network as software version that the node is running, its bootstrapping status, TX fees, etc can be known from Avalanche Info API. Now,we will learn how to receive query info.

Add the following code into the queryInfo method:

getting the blockchain IDs and their bootstrapped status for primary chains

P-Chain holds information about staking and validators on the network and let’s start interacting with it. Add the following snippets into the queryPChain method:

fetching subnets and validators
can inspect validator’s reward address
block height, minimum amount for staking activities, and total supply

What is Query X-Chain? X-Chain is used for any asset exchange operations and let’s understand how to work with this.

Add these lines of code into the queryXChain method:

fetching the default TX fee on the X-Chain
checking on transaction status
Geting all asset balances of an address

Let’s look at our final result by running the code:

You will see it on success:

How to create a simple transactions, sign and broadcast them on the Avalanche network? It will be further.

Create a new transfer.js file with the following code:

We can create and sign the transaction but first we should load the private key into the keychain. Add the following code to the // 1. Init keychain:

Now we will prepare transaction. Write the following code into // 2. Prepare transaction:

obtaining the UTXOs and determining the real asset ID for symbol AVAX

We have everything we need to be signed and broadcasted on the network.

Start preparing to send a transaction that does not exist yet until it is processed by the network. Write the code into // 3. Send transaction to network:

checking the transaction status, waiting a short amount of time, updating the transaction status and finally displaying the balance

Run the code:

We will see the following on success:

We will learn interchain transfers in Avalanche. They consist of two stages of creation: X-Chain export transaction and C-Chain import transaction.

For dealing with Ethereum-based credentials, we need a javascript package ethereumjs-util so run it:

Create a new file interchain_transfer.js with:

Let’s configure the keychain. Add it into // 1. Init keychain:

initing one for the C-Chain

Write the following code into // 2. Init Eth key for the C-Chain:

deriving our Eth-like address from Avalanche private key

Add the following code into the createExport method body:

Add this code into the createImport function:

to complete the transfer to C-Chain, we need to import the transaction from X-Chain

Finally we are close to finishing running the code by completing import and export. Add the code into // 3. Perform transfer:

Let’s run our code:

If all the past steps were correct we’ll see:

Congratulations, you’ve made it and completed interchain transfers in Avalanche successfully.

Thanks Figment Learn for this interesting Learn Pathway!

--

--