How to Create and Deploy an Avalanche Smart Contract?

How to Create and Deploy an Avalanche Smart Contract?

The advent of blockchain technology and the subsequent emergence of bitcoin was a breakthrough in IT. Blockchain is applied in many important areas, such as healthcare, financial infrastructure, supply chain, and the IT industry. However, the development of blockchain technology did not stop with the appearance of bitcoin, as developers sought to find applications for this innovative technology in many areas.

Over time, developers have found the possibility of launching dApps. This gave them the autonomy and security that blockchain is now highly valued for. Since then, the technology has only accelerated in development.

The current blockchain-based platforms are superior to bitcoin and are more advanced. This is because bitcoin has several issues such as scalability, slow transaction speed, interoperability, environmental issues, etc. However, the technology has evolved, and now these and many other problems have long been resolved in blockchain protocols.

Avalanche is one of the most advanced and latest additions to blockchain networks. Launched in 2020, Avalanche is an open-source protocol that enables the creation of decentralized applications and enterprise blockchains. The high speed of transactions and advanced features are worth noting advantages.

Furthermore, thanks to its unique infrastructure, the platform has solved scalability and security problems (there is also Avalanche Security Audit for this). Such benefits of Avalanche distinguish it from other ecosystems, making it the optimal platform for developing decentralized applications and smart contracts. Next, we will take a closer look at the Avalanche platform and learn how to deploy smart contracts.

Development and Deployment of Avalanche Smart Contracts

We will use the Solidity programming language to develop and deploy the Avalanche smart contract. You can create smart contracts on this platform with various tools such as Hardhat, Truffle, and Remix. In this case, we will use Truffle, which helps you write and compile smart contracts, interact with deployed contracts, create artifacts, and more. You can install Truffle using the “npm install -g truffle” command. You will also need NodeJS v8.9.4+ and Avalanche Network Runner to run the local avalanche network.

#1 Avalanche LAN launch

To get started, you will need AvalancheNetwork Runner, which you can launch and interact with the local network. We start five node avalanche network:

cd /path/to/avalanche-network-runner
./go run examples/local/fivenonetwork/main.go

The network will run with five nodes until you exit (using the keyboard shortcut CTRL + C).

#2 Creating a Truffle Directory and Installing Dependencies

Create a “truffle” directory in a new terminal tab and install dependencies. To change to the directory where another directory will be created, use the following code:

cd /path/to/directory

Create a new directory and name it truffle with the following code:

mkdir truffle; cd truffle

Next, you need to install the web3.js library, which will allow you to interact with the Ethereum virtual machine and AvalancheJS:

npm install web3 avalanche -s

Create a truffle template project:

truffle init

Once the local development network is created, it will support static addresses. To create accounts with pre-funded addresses, use @truffle/hdwallet-provider:

npm install @truffle/hdwallet-provider

#3 Update truffle-config.js

Running “truffle init” will generate new files. If everything is done correctly, then one of them will be called “truffle-config.js”. Here you should add the following lines of code:

const Web3 = require("web3");
const HDWalletProvider = require("@truffle/hdwallet-provider");
const protocol = "http";
const ip = "localhost";
const port = 9650;
const provider = new Web3.providers.HttpProvider(
const privateKeys = [
module.exports = {
  networks: {
    development: {
      provider: () => {
        return new HDWalletProvider({
          privateKeys: privateKeys,
          providerOrUrl: provider,
      network_id: "*",
      gas: 3000000,
      gasPrice: 225000000000,

Here you can change the “protocol”, “ip” and “port” in case you need to send API calls to another AvalancheGo node.

#4 Storage.sol

Next, in a new file called “Storage.sol” in the “contracts” directory, add the following code:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.8.0;
 * @title Storage
 * @dev Store & retrieve value in a variable
contract Storage {
    uint256 number;
     * @dev Store value in variable
     * @param num value to store
    function store(uint256 num) public {
        number = num;
     * @dev Return value
     * @return value of 'number'
    function retrieve() public view returns (uint256){
        return number;

This is a storage in which we can write a number to the blockchain and extract a value from it.

#5 New migration

With the help of the following lines of code, we will be able to manage the deployment of the “Storage” smart contract on the blockchain:

const Storage = artifacts.require("Storage");
module.exports = function (deployer) {

#6 Compiling Contracts

After editing Storage.sol don’t forget to run truffle compile:

truffle compile

#7 Truffle Accounts

Next, inside truffle console:

truffle console --network development

Next, enter this code:

truffle(development)> accounts

#8 Run migrations

We are at the finish line. Now that everything is set up, you can run the migrations and deploy the Storage contract:

truffle(development)> migrate --network development

#9 Interaction with the contract

Now that the smart contract has been written, you can interact with it. Let’s try to write a number to the blockchain and then read it. To do this, go to the console and get an instance of the deployed contract:


Now let’s read what we wrote. Hook into the “retrieve” method of the contract instance:

truffle(development)> let i = await instance.retrieve()

After that, you will see undefined. To see the value, call the “.toNumber” method:

truffle(development)> i.toNumber()

After that, in the console, you will see the number you entered (in this case, 12345).

About Avalanche Blockchain

Avalanche is a secure and scalable ecosystem that was created for the development of dApp. It is one of the fastest, programmable, and interoperable smart contract platforms that allow anyone to start building their blockchain-based applications. Avalanche also has its token called AVAX. Avalanche can be described as a hybrid of three embedded blockchains that serve different purposes:

  • Exchange Chain (X-Chain) is required to create, trade, and administer assets and transactions.
  • Platform Chain (P-Chain) manages and creates subnets and coordinates validators.
  • The contract chain (C-Chain) uses an instance of the Ethereum virtual machine to create smart contracts.

Since our main task is creating an Avalanche smart contract, we need to understand the chain. The main feature of the C-chain is that it allows you to create smart contracts compatible with Ethereum. Other than that, it is also the default smart contract blockchain on the network. Avalanche C chain offers excellent throughput, higher speed, and lower Gas price. In addition, thanks to the Proof-of-Stake consensus mechanism based on the Snowman consensus protocol, there is also a faster transaction confirmation time.


How much does it cost to Deploy a Smart Contract on Avalanche?

When contracting with Avalanche, the contractor’s cost is about 0.5 VAX, equivalent to $2.2. In Ethereum, the contract deployment fee was 12.9, including gas costs.

Can I create my Smart Contract on Avalanche?

Of course. You can write a smart contract using our guide for blockchain platforms.

Do Avalanche Smart Contracts Matter?

Yes, contracts are of great importance. With their help, many blockchains work and are necessary for implementing many operations.

Latest Posts