Coding Avalanche Smart Contracts
Tags: Solidity, Avalanche, Blockchain, Blockchain Development, Ethereum

Coding a Smart World Series — Avalanche Smart Contracts

What is Avalanche blockchain?

Avalanche is a new blockchain ecosystem. The promoters of Avalanche consider it as a blockchain ecosystem running on 3rd generation consensus protocols. The Avalanche platform will fall into an interoperable blockchain platform like Cosmos, Polkadot, etc.

Avalanche is an open-source platform for launching DApps, Defi DApps, and other interoperable blockchain networks, including enterprise blockchain networks in one highly scalable and interoperable blockchain ecosystem.

A critical difference between Avalanche and other decentralized networks like Ethereum is its consensus protocol. Consensus protocols in Avalanche operate through repeated sub-sampled voting.

A quick peek into Avalanche Consensus protocol.

Avalanche consensus protocol, like the Nakamoto consensus protocol, is probabilistic. Avalanche uses a voting mechanism by its validators to reach consensus. The voting process is what makes Avalanche's consensus different. The core idea of the Avalanche Consensus protocol is metastability. This basically means the consensus protocol will never have an undecided state between two choices and will always encourage to move to one of the choices.

At a high-level, a single validator in the network selects a random set of validators and asks them to vote whether a transaction set is valid or not. The requested validator forms a weighted result of all the votes after every iteration of voting requests. The validator repeats this process with a new set of random validators selected every time. The validator repeats this process until it has enough probabilistic proof of the voting outcome and the chance of the results going wrong is negligible or does not exist.

Avalanche consensus is like an iterative polling process, which will eventually tip to one of the choices.

A quick peek into Avalanche Architecture.

To begin with, everything in the Avalanche platform is a subnet — subnetwork.

A Subnet, or Subnetwork, is a dynamic set of validators working together to achieve consensus on the state of a set of blockchains. Each blockchain is validated by exactly one Subnet. A Subnet can validate arbitrarily many blockchains. A node may be a member of arbitrarily many Subnets. More here…

A Subnet manages its own membership and can have its constituent validators meet certain criteria to fulfill a specific geographic location and industry's regulatory requirements.

There is a default Subnet called Primary Network in the Avalanche platform, which validates Avalanche’s built-in blockchains. All members of all Subnets must also be a member of the Primary Network. To become a member of the Primary Network, one must stake some Avalanche tokens. In essence, all other blockchains' validators must also validate Avalanche’s built-in blockchains and must have staked Avalanche tokens.

Chains refer to blockchains in the Avalanche platform. Each chain is assigned a ChainID and can be part of one and only one subnet. A blockchain will also have a consensus mechanism.

The default Subnet, Primary Network, has three built-in blockchains.

The Platform Chain [P-Chain] is the metadata blockchain on Avalanche and coordinates validators, keeps track of active subnets, and enables new subnets.

The Exchange Chain [X-Chain] is the default asset blockchain on Avalanche and enables new assets, exchanging between assets, and cross-subnet transfers. Applications which does not need smart contract functionality use this chain to create assets and move them.

The Contract Chain [C-Chain] is the default smart contract blockchain on Avalanche and enables the creation of Ethereum-compatible smart contracts.

In a nutshell, the Avalanche platform will have a dynamic set of validators called Subnets which can validate multiple blockchain networks. Each Subnet will have a specific set of properties or requirements catering to the domain or industry or geography of the supported applications. The Subnets can validate any number of blockchains but a specific blockchain will always be validated by a specific Subnet. Also, a specific blockchain can only be part of one Subnet. There can be private Subnets for Enterprise blockchains where data privacy is restricted to authorized validators. Existing blockchain networks like Bitcoin and Ethereum can each have their own Subnet in Avalanche and port their state to the Avalanche platform and use its consensus mechanism for faster performance. The Avalanche platform uses Proof of Stake for consensus implementation. There is a default Subnet called Primary Network with three built-in blockchains and all the member validators in any subnet will be by default part of this default network. A Validator can validate multiple Subnets but it's also mandatory to validate the default Subnet and stake 2000 AVAX — which is the native token of the platform.

Deploy Smart Contract in the Avalanche platform.

The good part of deploying a smart contract to Avalanche blockchain is using your existing smart contracts and tools you have used in the Ethereum platform to use Avalanche blockchain and its consensus mechanism.

So let's go to the Remix tool. For the Solidity Smart Contract, I have used the same one I have been using in this series, Simple Open Auction. I promise I will stop using this from now on :)

The Remix tool is custom-built for Ethereum. But while connecting to MetaMask, there is an option to connect to a custom RPC instead of the standard Ethereum Mainnet & Test Networks available by default. This is the hack we will use to let Remix & MetaMask connect to a Test Network in Avalanche blockchain. The steps are clearly mentioned in the Avalanche docs here. I have connected to the FUJI Test Network available in the Avalanche platform. Once the MetaMask is configured, you will have to use the faucet to fund your C-Chain Address in Avalanche with AVAX tokens.