Avalanche is an open-source platform for launching decentralized applications and enterprise blockchain deployments in one interoperable, highly scalable ecosystem. Avalanche is the first decentralized smart contracts platform built for the scale of global finance, with near-instant transaction finality. Avalanche is a blockchain that promises to combine scaling capabilities and quick confirmation times through its Avalanche Consensus Protocol. It can process 4,500 TPS (transactions per second). For Ethereum, that number is 14 TPS.
Blockchains have traditionally been referred to as being slow and unscalable. Avalanche embraces an innovative approach to concensus that solve these problems without compromising on security.
Avalanche is a high-performance, scalable, customizable, and secure blockchain platform. It targets three 15 broad use cases:
- Building application-specific blockchains, spanning permissioned (private) and permissionless (public) deployments.
- Building and launching highly scalable and decentralized applications (Dapps).
- Building arbitrarily complex digital assets with custom rules, covenants, and riders (smart assets).
- Exchange Chain (X-Chain)
- Platform Chain (P-Chain)
- Contract Chain (C-Chain)
The P-chain is for platform management. It handles requests related to the validator, the subnet, and the blockchain. The C-chain is for contract management. It is based on EVM; hence its API is almost identical to other EVM protocols. It has both RPC and WebSocket endpoints, and it handles requests for smart contracts. The X-chain is for asset exchange. It is Avalanche’s native platform; it is used for creating and trading assets like AVAX and NFTs.
These 3 blockchains are secured by the Avalanche Primary Network with is a special kind of subnet.
The Avalanche Architecture is composed of:
- Subnetworks
- Virtual Machines
The Graph is an open-sourced indexing protocol for organising blockchain data and making it easily accessible using GraphQL. This software collects, processes and stores data from various blockchain applications to facilitate effecient information retrieval. The Graph stored data into various indices called Subgraphs, allowing applications to query it. These queries are initiated using GraphQL, a language originally created by facebook. The Graph has the ability to query networks like Ethereum and IPFS. Anyone can build and publish open subgraphs.
First, install the LTS (long-term support) version of nodejs. This is 16.2.0
at the time of writing. NodeJS bundles npm
.
Next, install the yarn package manager:
npm install -g yarn
To check the current Git version use:
git --version
This tutorial is created to serve as a guide to help developers setup an Avalanche Subnet and Index them using graphQl. We are going to learn how to run a local network using the Avalanche-cli and deploy a basic smart contract using Remix. Then Lastly we will be indexing our subnet using The Graph. This guide is an extension of the Official Avalanche Documentation.
Please note that all command line inputs and sample codes are MacOs and Linux Based. Commands may vary for other operating systems.
In summary, we will be discussing the following:
- Running an EVM Subnet on the Local Network using the Avalanche-cli
- Deploying smart contracts with Remix
- Indexing our subnet using The Graph
We will be creating an EVM on our local machine to give us a basic feel on how a subnet functions. The Avalanche-CLI is a novel tool that allow us to have a local network up in minutes.
Open up you MacOs command line utility and run the following command
On your home directory, create a new directory and cd <newdir>
into the directory. This is where we will be installing all our project dependencies.
curl -sSfL https://raw.githubusercontent.com/ava-labs/avalanche-cli/main/scripts/install.sh | sh -s
This command download the latest binary of the Avalanche-CLI to the current directory where it was executed.
cd
into the bin
folder and export the path variable
cd bin
export PATH=$PWD:PATH
This makes the avalanche
command available globally. For more information about environment-variables and avalanche-cli-commands visit the respective links.
We will be using the avalanche subnet create
command line wizard to get our network running. ASAP.
In the same directory where the binary was installed, run the following command
avalanche subnet create <subnetName>
Substitute <subnetName>
with any perferred name of your choice but without spaces. For this tutorial we are going to call our subnet <fibrinNet>
.
avalanche subnet create fibrinNet
Since this command does not provide any arguements, you would neeed to walk through the configuration wizard to generate a genesis file
for your network.
-
Choose a Virtual Machine (VM): We are going to be making use of the
SubnetEVM
-
Pick a chain ID Every EVM based blockchain has a special parameter called a
chainID
. ChainIDs are expected to be unique values, you can check chainlist.org to see if your proposed chain ID is already in use. We will be making use of the chain ID1970
(A pun on JavaScript dates...lol). -
Airdrop: default to airdrop 1 million tokens to provided address
-
Add a custom precompile to modify the EVM: For this section, we will not be using a pre-compile script
The wizard won't customize every aspect of the Subnet-EVM genesis for you, we will be doing this in the subsequent sections.
To view the list of all created subnets, just execute the following command
avalanche subnet list
To deploy the newly created subnet locally, run the following command
avalanche subnet deploy <subnetName>
When a subnet is run locally, it starts a multi-node (5 node) Avalanche Network in the background.
To test the functionality of the just created subnet, go ahead and add the configuration details to Metamask.
You can create a new metamask account by importing the private key 0x56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027
and start experiencing with this account.
I have a Github Tutorial that explains how to setup your local development environmet including Metamask
.
Lastly don't forget to stop the running local network
avalanche network stop <snapshotName>
We are going to be making use of an online IDE in compiling and deploying our smart contract code, a tool called Remix.
Navigate to the Remix platform and import the test contract we will be making use of.
https://github.com/FibrinLab/example-subgraph/blob/master/contracts/Gravity.sol
This repo contains the official sample subgraph for the gravatar registry.
Compile and deploy the Gravity
smart contract using the Local Subnet you just created. To do this select the deploy
tab and choose injected web3
from the dropdwown. `Please note that Remix automatically detects the appropriate compiler version and makes use of it to compile your contract.
Always make sure to confirm the Environmet Chain ID is the same as that of your selected metamask account
.
With this all set go ahead and deploy your smart contract on your local subnet by clicking the deploy
button. Approve the metamask request and pay the necessary gas fees.
If the the deployment is successful, you should see something like this ===>
Please take note of the deployment address as we will be making use of it subsequently.
The most efficient way to make use of The Graph in indexing our subnet is to host a local Graph Node. This is pretty striaghtforwards to setup once you got requirements up and running. This tutorial is an extension of the Graph-Node Github repository.
The following components are needed:
- Interplanetary File System (IPFS) for hosting our files. Installation instructions.
- PostgreSQL, a database management tool for keeping out data. Installation instructions.
- Rust, we will be building and compiling The Graph Node using the cargo package manager. Installation instructions.
If the above installation instructions are followed correctly, you should have these tools up and running.
After sucessfully installing IPFS, initialise the daemon by running
ipfs init
Next run,
ipfs daemon
Its now time to get our database all set-up.
Run the following commands:
initdb -D .postgres
pg_ctl -D .postgres -l logfile start
createdb graph-node
Clone and build The Graph
node folder
git clone https://github.com/graphprotocol/graph-node
Build the folder by runnning
cargo build
If you have sucessfully installed Rust but the command is not found
, you would need to setup some environmental variables.
Running this command might help.
source $HOME/.cargo/env
Once all dependencies are up and running, run the following command to kick start the node.
cargo run -p graph-node --release -- \
--postgres-url postgresql://postgres:*fill-in-posgresql-username: :*fill-in-posgresql-password @localhost:5432/graph-node \
--ethereum-rpc fuji:http://127.0.0.1:9650/ext/bc/*fill-in-your-blockchain-id/rpc \
--ipfs 127.0.0.1:5001
Before running the above make sure you replace the following: *fill-in-posgresql-username ==> Your Database username. `Defaults to [postgres] *fill-in-posgresql-password ==> Your Database password.
If everything goes smoothly. You should get this.
This is where things get interesting. Change directory into the example-subgraph
folder
Clone the official subgraph repository and install all the dependencies
git clone https://github.com/graphprotocol/example-subgraph
Next, generate the ABI typings
yarn
yarn codegen
Open you the subgraph.yaml
file and make 2 (two) modifications under datasources
.
- Switch the network to
local
- Input the address of the deployed
Gravity
contract in theaddress
field
Finally, run the following
yarn create-local
yarn deploy-local
Congratulations, you have sucessfully deployed a Sub-Graph on a locally deployed subnet.
After successful deployment, you graph node would need a few minutes to scan all the nodes.
Once its done, open up the provided link in the browser.
Open up the link and try running a query by filling this into the query box.
query MyQuery {
gravatars {
id
imageUrl
displayName
}
}
Watch the magic happen.
In summary, we have deployed a Local Subnet using the Avalanche-cli. We further went ahead to deploy smart contracts, run a Graph node and Index our nodes using The Graph
. How cool is that? lol.
Feel free to fork this repository and build great stuff.
Cheers and Happy Coding.
Akanimoh Osutuk