(POA Guide - Part 1) Develop and deploy a smart contract
This article is part of a POA tutorial series:
- (POA Guide - Part 1) - Develop and deploy a smart contract
- (POA Guide - Part 2)- Bridge assets between a sidechain and a mainchain
- POA - Part 3 - Meta-transaction [Coming soon]
POA Network is an Ethereum-based platform that offers an open-source framework for smart contracts. POA Network is a sidechain to Ethereum utilizing Proof of Authority as its consensus mechanism. POA provides developers with the flexibility to code in Ethereum standards with the added benefits of POA Network’s solutions to scalability and interoperability in blockchain networks.
POA has transaction fees just like the Ethereum mainnet, but in comparison to the Ethereum mainnet where fees are paid in Ether, the POA blockchain requires fees in its native currency called POA Token, this token can be obtained on a Crypto Trading platform like Binance.
POA currently has two networks up and running:
- POA Core: The main network requiring POA token
- POA Sokol: A test network requiring POA Sokol token
For the purpose of this tutorial, we will use the POA Sokol network.
Step 1: Connect Metamask to POA Sokol and fund your account
In this first step, we’re going to learn how to connect our Metamask wallet to a POA test network called Sokol and how to fund the account with Test POA tokens.
Unlock your Metamask extension
Go to Settings, enter the following new RPC URL https://sokol.poa.network and click on Save
Metamask should switch to this new private network
Note: A dedicated Browser extension (only Chrome) for POA and very similar to Metamask can also be used: Nifty
In Metamask, copy your account address
Go to the POA Sokol Faucet
Click on Request 0.5 POA
- You can your account balance in Metamask or also see in the POA Sokol Explorer if the transaction went through.
Step 2: Deploy a contract on the POA Sokol network
The second step consists in writing a very simple Smart Contract in Solidity and deploy it on the POA Sokol Network using Truffle framework.
- Install Truffle on your machine
1 | $ npm install -g truffle |
- Initialise your Truffle project
1 | $ truffle init |
Let’s now write a simple Smart Contract that increments and store a counter
Create a file in
contracts/Counter.sol
and paste the following Solidity code
1 | pragma solidity ^0.4.20; |
You can verify that your code compiles correctly with the following command $ truffle compile
Now, we need to create a deployment script
Create a file in
migrations/2_deploy_contracts.js
and paste the following code
1 | var Counter = artifacts.require("./Counter.sol"); |
- Finally we need to configure the connection to the POA Sokol network as well as our wallet info
Install the following JavaScript dependencies:
- truffle-hdwallet-provider enables to sign transactions for addresses derived from a 12-word mnemonic
- dotenv is a module to configure environment variables
1 | $ npm install truffle-hdwallet-provider dotenv --save-dev |
- Copy your mnemonic from your Metamask wallet: Settings / Reveal Seed Words
DO NOT SHARE THOSE 12 WORDS WITH ANYBODY
- Create a
.env
in the Truffle project and copy the mnemonic like this
1 | MNEMONIC="COPY HERE YOUR 12 MNEMONIC WORDS YOU DO NOT WANT TO SHARE" |
- Open the file
truffle.js
and add the following configuration
1 | require('dotenv').config(); |
- Run the deployment to the POA Sokol network
1 | $ truffle migrate --network poa |
Step 3: Interact with the contract from the web app
In the next step, we will develop a dApp using React, Web3 and Truffle to interact with the Smart Contract previously deployed on the POA Sokol network.
- Initialise a React project
1 | $ npx create-react-app frontend |
- Install the necessary dependencies
- truffle-contract: is an Ethereum Smart Contract abstraction library
- web3: This is the Ethereum compatible JavaScript API which implements the Generic JSON RPC spec
1 | $ npm install truffle-contract web3 --save |
- Edit package.json and add the following line under the scripts section to access the Truffle contract artifacts from the webapp
1 | "link-contracts": "run-script-os", |
The full code of this file is available here: package.json
- Open an edit src/App.js
- Connect to the node with web3
1 | import Web3 from 'web3' |
- Use truffle-contract to load the Truffle artefacts and interact with the smart contract
1 | import TruffleContract from 'truffle-contract' |
The full code of this file is available here: App.js
- Create a link to the Truffle JSON artefacts
1 | $ npm run link-contracts:linux:darwin |
- Start the webserver
1 | $ npm start |
- Result
If Metamask is unlocked and connected to the Solok network, The web3 provider connects automatically to the node and retrieves the counter value. When the user clicks on “Increment”, Metamask pops up to sign a transaction and send it to the POA Solok network.
Links and resources
Sokol
Network ID: 77
JSON-RPC Endpoint: https://sokol.poa.network
Block Explorers:
Network Status: https://sokol-netstat.poa.network/
Core
Network ID: 99
JSON-RPC Endpoint:
Block Explorers:
Network Status: https://core-netstat.poa.network/
Inspired by https://www.youtube.com/watch?time_continue=313&v=fezh2buFAt4
Code: https://github.com/gjeanmart/kauri-content/tree/master/poa_tutorial_sokol_part1