My Avalanche learning experience

Динар Мусин
6 min readApr 2, 2021

I want to tell you about this exprerience and give recommendations to delete all the possible questions.

Let’s start learning this interesting way.

At first don’t remember create a folder and its title have to contain only small letters and without numbers. It’s for a reason. Check availability of any code editor like VSCode etc, the latest version of NodeJS (12.x+).

If you are not sure in version NodeJS and you just open recent folder in VSCode, create new terminal and check your current version with:

The next step is to create a directory for your project where you will initialize the new Node.js project. You can do this using the following command:

As a result, you will see a new file package.json created in the current directory.

Run the following command to install all dependencies:

You will also see the packages added to your package.json file:

We can continue by creating an .env file that will hold all our sensitive data and make sure to create a .gitignore file with the following content:

Open .env file and add a few environmental variables that we will need for all our tutorials:

Make sure you have replaced YOUR_API_KEY with the valid DataHub access key.

Can finally start building our application. Let’s create a new file client.js in the root directory of your project, with the following content:

This helper file throughout the Pathway, it provides us with a nice little function to create and configure the Avalanche node client.

Now create an actual file and load our helper code. Create a connect.js file with the following:

Good, go to run our code:

If all the past steps were correct you should see output similar to this:

Now, we will be creating your first Avalanche account on X-Chain using the Avalanche JavaScript API and DataHub.

Start by creating a new file create_account.js:

Make sure to create the directory credentials first with:

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

For configuring keychain onto the code, replace the // 1. Configure keychain inside our main function with:

For generating private key replace // 2. Generate private key inside our main function with:

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

For checking address balance we’re replacing // 3. Check address balance part of the main function:

Let’s run a code:

You should be able to see a similar output:

We will not be able to send funds to another address until we get some test tokens to our X-Chain address(however empty balance). Head over to the Avalanche Faucet page, enter the address from the output above (must be your own address!) and click on “Request 1 AVAX”. You should see the successful confirmation page, meaning that your address is now funded with 2 AVAX.

Re-run the node create_account.js command. We should be getting the balance output this time:

Both P and X chains, they offer different information and require separate clients. Continue and prepare a new file, called query.js:

a placeholder method for querying information using different clients

Add the following code into the queryInfo method:

Onto the code, add the following snippet into the queryPChain method:

We’re fetching subnets and validators.

Add to the existing method:

Thanks the code we can inspect validator’s reward address.

Add too:

Query generic P-Chain info like block height, minimum amount for staking activities, and total supply.

Add a few lines of code into the queryXChain method:

We’re fetching the default TX fee on the X-Chain.

Add to the existing method:

To check on transaction status to see if it’s accepted or rejected.

Add too:

To get all asset balances of an address.

Run the code

Let’s create a very simple one — a token transfer, and specifically on X-Chain.

Start by creating a new file transfer.js:

Add the following code to the // 1. Init keychain part of the main function:

to load the private key into the keychain, so we can create and sign the transaction.

Add into // 2. Prepare transaction part of the main function.

to obtain the UTXOs

Add too:

some details about the transaction
to determine the real asset ID for symbol AVAX:
to see any changes after our transaction is submitted.

Before we can broadcast a transaction, it must be signed with a private key.

Add to // 3. Send transaction to network part of the main function.

sign the transaction
We’re ready to broadcast the transaction
check if our transaction gets accepted

Run the code to see our code in action:

We’ll see the next:

Write in the terminal:

ethereumjs-util to deal with Ethereum-based credentials.

Create a new file interchain_transfer.js with the following content:

Replace the // 1. Init keychain in the main function with the following code:

configure the keychain

Replace the // 2. Init Eth key for the C-Chain part of the main function with:

The code above decodes our Avalanche private key and uses the Etherium helper library to convert it into an address on EVM chain.

Add the following code into the createExport method body:

First step in the interchain transfer is to create an export transaction

Add the following code into the createImport method:

Replace the // 3. Perform transfer in the main function with the following code:

Run the code with:

If everything is correct you will see the output:

This is my experience. Thanks for reading. And want to thank Figment Learn for the opportunity to gain this experience!

--

--