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(
`${protocol}://${ip}:${port}/ext/bc/C/rpc`
);
const privateKeys = [
"0x56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027",
"0x7b4198529994b0dc604278c99d153cfd069d594753d471171a1d102a10438e07",
"0x15614556be13730e9e8d6eacc1603143e7b96987429df8726384c2ec4502ef6e",
"0x31b571bf6894a248831ff937bb49f7754509fe93bbd2517c9c73c4144c0e97dc",
"0x6934bef917e01692b789da754a0eae31a8536eb465e7bff752ea291dad88c675",
"0xe700bdbdbc279b808b1ec45f8c2370e4616d3a02c336e68d85d4668e08f53cff",
"0xbbc2865b76ba28016bc2255c7504d000e046ae01934b04c694592a6276988630",
"0xcdbfd34f687ced8c6968854f8a99ae47712c4f4183b78dcc4a903d1bfe8cbf60",
"0x86f78c5416151fe3546dece84fda4b4b1e36089f2dbc48496faf3a950f16157c",
"0x750839e9dbbd2a0910efe40f50b2f3b2f2f59f5580bb4b83bd8c1201cf9a010a",
];
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) {
deployer.deploy(Storage);
};
#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
[
'0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC',
'0x9632a79656af553F58738B0FB750320158495942',
'0x55ee05dF718f1a5C1441e76190EB1a19eE2C9430',
'0x4Cf2eD3665F6bFA95cE6A11CFDb7A2EF5FC1C7E4',
'0x0B891dB1901D4875056896f28B6665083935C7A8',
'0x01F253bE2EBF0bd64649FA468bF7b95ca933BDe2',
'0x78A23300E04FB5d5D2820E23cc679738982e1fd5',
'0x3C7daE394BBf8e9EE1359ad14C1C47003bD06293',
'0x61e0B3CD93F36847Abbd5d40d6F00a8eC6f3cfFB',
'0x0Fa8EA536Be85F32724D57A37758761B86416123'
]
#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:
truffle(development)> instance.store(12345)
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.
FAQ
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.
Of course. You can write a smart contract using our guide for blockchain platforms.
Yes, contracts are of great importance. With their help, many blockchains work and are necessary for implementing many operations.