My Avalanche learning experience
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:
Run the following command to install all dependencies:
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:
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:
Now create an actual file and load our helper code. Create a connect.js
file with the following:
Good, go to run our code:
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:
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:
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
:
Add the following code into the queryInfo
method:
Onto the code, add the following snippet into the queryPChain
method:
Add to the existing method:
Add too:
Add a few lines of code into the queryXChain
method:
Add to the existing method:
Add too:
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:
Add into // 2. Prepare transaction
part of the main
function.
Add too:
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.
Run the code to see our code in action:
We’ll see the next:
Write in the terminal:
Create a new file interchain_transfer.js
with the following content:
Replace the // 1. Init keychain
in the main
function with the following code:
Replace the // 2. Init Eth key for the C-Chain
part of the main
function with:
Add the following code into the createExport
method body:
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!