How to create and deploy a smart contract using Aion?

Advanced

How to create and deploy a smart contract using Aion?

Smart Contract Demo-DApp

This will take you through configuring your Aion blockchain, as well as deploying and interacting with a smart contract on Aion. It is meant for those who have some programming knowledge, but not necessarily blockchain.

Prerequisites

Before we begin, make sure you have the following:

You will also need:

Ubuntu Versioning
Currently the compiler binary is built for 64-bit Ubuntu 16.04, other versions of Ubuntu may not be fully compatible

Introduction

We’ll be walking you through a simple counter dApp built on Aion that you can interact with through the terminal. There are four main steps in this tutorial, being:

  • Setting up a Local Development Environment

  • Compiling the Smart Contract

  • Deploying the Smart Contract

  • Interacting with the Smart Contract

Reach out to us!
If you have any questions or encounter difficulties, feel free to connect with Aion on Gitter!

1. Setting up a Local Development Environment

For testing and DApp development purposes, you should configure the Aion kernel to be running the testnet.json file as the genesis file, and modify the config.xml to run internal mining for a local blockchain. For the steps on how to do this, refer to the Local Blockchain Network node set-up.

2. Compiling the Smart Contract

Before deploying the smart contract, we’re going to need to compile our solidity code into an interface (ABI) and bytecode (bin) to allow us to deploy the contract on the blockchain.

  • Application Binary Interface (ABI): JavaScript Object that defines how the machine is to interact with the contract

  • Bytecode: Compiled ‘code’

Configure your smart contract for compiling
  1. Download or clone the aion_qa repository
  2. Navigate into the DemoDApp directory and npm install (This command installs all the packages we’ll need – you can find the dependencies in the package.json file)

  3. Navigate to the DemoDApp/src directory and check that you have a contracts folder containing the Counter.sol file

  4. Open deploy_counter.js file in the src folder and make the following changes:

  • line 10: edit directory path to where your aion/web3 is located (from home, or check the directory using the pwd command)

  • line 28: edit a0 to the index number your address is in (listed under alloc when you open the new config/genesis.json file) Note: Lists are indexed starting at 0

  • line 29: edit pw0 = “password associated with account address”

      5. Save the changes

3. Deploying the Smart Contract

To deploy the smart contract, you will need the contract ABI and bytecode as mentioned above, and an unlocked account with balance.

Running a script to deploy the smart contract
  • Navigate to the DemoDApp/src directory in terminal

  • Run script:

node deploy_counter.js

This script deploys the Counter.sol contract and outputs the contract address.

Note: Save this contract address for testing and interacting with this specific contract instance.

Unlock Accounts

The provided repository for the demo app automatically unlocks your account. However, in practice you will not be able to deploy your smart contract without having unlocked your account either manually or in code. Steps to manually unlock your account can be found here.

4. Interacting with the Smart Contract

Once deployed, you will want to make function calls to the smart contract. We’ll be interacting with the smart contract via the web3 API by calling functions and getters.

Enabling interaction and function calls

  • Navigate to DemoDApp/src/ and open the interact_counter.js file

  • Make the same changes as in the deploy_counter.js script, modifing web3 directory, account, and password:

  • line 10: edit directory path to where your aion/web3 is located (from home, or check the directory using the pwd command)

  • line 28: edit a0 to the index number your address is in (listed under alloc when you open the new config/genesis.json file)
    Note: Lists are indexed starting at 0

  • line 29: edit pw0 = “password associated with account address”

3. Save the changes

4. Reopen the terminal and run the script:

1node interact_counter.js {contractAddress}

5. Now you can interact and test your DApp through the console! Use the following commands: (Remember that that blocks get sealed by default around every 10 seconds, so your transactions will not show up immediately)

 
 getCount

 Receive current count status

 incrementCounter

 Increase counter by 1

 decrementCounter

 Decrease counter by 1

 

Congratulations! You have now created a simple, functional dapp that connects to your private blockchain.

Error Handling

This section will cover common issues one may face during the compilation, deployment, and interaction with their smart contract.
You can configure the config.xml file to show debug/error issues pertaining ot API, VM, transactions, etc. if you wish to understand more about the kernel log.
 

  No changes occur in kernel after         configuration

  Delete the database folder and restart the kernel. This allows the modified configurations to take effect.

  Error output “file can’t be found”

  Ensure your web3 file path is correct (use the pwd command if necessary)

  Error output “cannot find readline-sync”

  Make sure to npm install in the main directory before deploying the smart contract for the first time

 
By this, you complete this workshop successfully!!