Development Environment and Tools

Necessary tools to start writing EVM contracts

Before getting started, you can apply for testnet ONG tokens that will be used for invoking any contracts you deploy over at the faucet here.

The value of gas price for a transaction must be in multiples of 10^9. The minimum value is 2500*10^9.

Development Environment and Tools

EVM smart contracts are written using Solidity. You can reuse existing Ethereum contract frameworks to develop and deploy EVM contracts.

Remix

Remix IDE is an open source development environment for EVM contracts. Remix IDE documentation is here.

We will now go through an example of a Hello World contract development using Remix.

Initialize Remix

First, locate and activate "Solidity Compiler" and "Deploy and Run Transactions" in PLUGIN MANAGER.

Then, select Solidity environment. Create a new file and name it HelloWorld.sol. Then copy the code of Hello World contract and paste it in the file just created.

Compile Contract

Click on the Solidity Compiler button, select compiler version to 0.5.10 and start compiling HelloWorld.sol

Deploy Contract

The contract is ready to deploy on Ontology after compiling. Here we deploy it on Ontology TestNet.

Note: MetaMask must be configured for Ontology before you deploy the contract.

Select "Custom RPC" in MetaMask networks settings. Fill in and save the info below.

  • Network name: Ontology TestNet

  • Node URL: https://polaris1.ont.io:10339 or https://polaris2.ont.io:10339 or https://polaris3.ont.io:10339 or https://polaris4.ont.io:10339

  • Chain ID: 5851

  • Blockchain Explorer URL: "https://explorer.ont.io/testnet"

Finally, select "Injected Web3" in Remix. Click "Deploy" to finish.

Invoke Contract

Now you can call the method in this contract. The string hello is saved in the contract when you deploy it, you can call the method message to query this string:

Truffle

Truffle offers tools and frameworks for EVM contract development, testing and management. You can find more details here.

Now we will demonstrate how to use Truffle with this test code.

Install Truffle

First, initialize and install dependencies.

Then run this command to install Truffle.

npm install -g truffle

Configure truffle-config

  • Create a new .secret to store the mnemonic phrase or private key (which can be found in MetaMask).

  • Edit the code of truffle-config as below.

const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();
module.exports = {
  networks: {
    ontology: {
     provider: () => new HDWalletProvider(mnemonic, `http://polaris2.ont.io:20339`),
     network_id: 5851,
     port: 20339,            // Standard Ethereum port (default: none)
     timeoutBlocks: 200,
     gas:800000,
     skipDryRun: true
    }
  },
  compilers: {
    solc: {
      version: "0.5.16",    // Fetch exact version from solc-bin (default: truffle's version)
      docker: false,        // Use "0.5.1" you've installed locally with docker (default: false)
      settings: {          // See the solidity docs for advice about optimization and evmVersion
       optimizer: {
         enabled: true,
         runs: 200
       },
       evmVersion: "byzantium"
      }
    }
  }
};

Deploy Contract

Run this command to deploy the contract on the Ontology network.

truffle migrate --network ontology

If successful, you will see the result below.

Note: Avoid using ETH units (e.g. wei, gwei, ether, etc.) when writing test scripts.

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.

Starting migrations...
======================
> Network name:    'ontology'
> Network id:      12345
> Block gas limit: 0 (0x0)
1_initial_migration.js
======================

   Replacing 'Migrations'
   ----------------------
   > transaction hash:    0x9019551f3d60611e1bc6b323f3cf3020d15c8aeb06833d14ff864e24622884aa
   > Blocks: 0            Seconds: 4
   > contract address:    0x53e137A51CfD1E1b088E0d921eB5dBCF9cFa955E
   > block number:        6264
   > block timestamp:     1624876467
   > account:             0x4e7946D1Ee8f8703E24C6F3fBf032AD4459c4648
   > balance:             0.00001
   > gas used:            172969 (0x2a3a9)
   > gas price:           0 gwei
   > value sent:          0 ETH
   > total cost:          0 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:                   0 ETH


2_deploy_migration.js
=====================

   Replacing 'HelloWorld'
   ----------------------
   > transaction hash:    0xf8289b96f2496a8c940ca38d736a554a90f64d927b689921781619499906721b
   > Blocks: 0            Seconds: 4
   > contract address:    0xfbff9bd546B0e0D4b40f6f758847b70050d01b37
   > block number:        6266
   > block timestamp:     1624876479
   > account:             0x4e7946D1Ee8f8703E24C6F3fBf032AD4459c4648
   > balance:             0.00001
   > gas used:            243703 (0x3b7f7)
   > gas price:           0 gwei
   > value sent:          0 ETH
   > total cost:          0 ETH

hello contract address: 0xfbff9bd546B0e0D4b40f6f758847b70050d01b37

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:                   0 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0 ETH

Hardhat

Hardhat is an Ethereum development environment. We will use this test code as an example and demonstrate how to use Hardhat.

Install Hardhat

Please refer to Hardhat doc for details on this step.

Configure hardhat-config

  • Create a new .secret file to save your private key.

  • Update the code of hardhat.config.js as shown below:

require("@nomiclabs/hardhat-waffle");
const fs = require('fs');
const privateKey = fs.readFileSync(".secret").toString().trim();
module.exports = {
    defaultNetwork: "ontology_testnet",
        ontology_testnet: {
            url: "http://polaris2.ont.io:20339",
            chainId: 5851,
            gasPrice:2500000000000,
            gas:2000000,
            timeout:10000000,
            accounts: [privateKey]
        }
    },
    solidity: {
        version: "0.8.0",
        settings: {
            optimizer: {
                enabled: true,
                runs: 200
            }
        }
    },
};

Deploy Contract

Run this command in root of the project directory to deploy the contract on Ontology Chain:

$ npx hardhat run scripts/sample-script.js --network ontology_testnet

The result looks like this:

sss@sss hardhatdemo % npx hardhat run scripts/sample-script.js --network ontology_testnet
RedPacket deployed to: 0xB105388ac7F019557132eD6eA90fB4BAaFde6E81

Network Info

Network Types

MainNet

TestNet

Ontology EVM contracts consume ONG as gas fee for execution. You can apply for TestNet ONG here.

EVM Assets on Ontology

OEP-4 Assets

Please refer to this link.

Last updated