뉴스로 돌아가기

Everything You Need To Know About Ontology EVM Contract Development Part 2


Last Week, Ontology announced the official deployment of a TestNet supporting EVM and opened the EVM compatible public beta to developers around the world. At the same time, the “Ontology Security Vulnerability and Threat Intelligence Bounty Programme” was officially launched in cooperation with SlowMist, a well-known code auditing agency. The top reward for reporting a vulnerability is $12,000 in ONG.


In order to encourage community developers to quickly and easily familiarize themselves with the Ontology EVM development environment, we specially prepared a “Handbook for Dummies”, which explains the essentials of Ontology EVM contract development.


In Part 1, we introduced the ChainID, RPC URL, browser address and other configuration information related to the Ontology EVM. Developers can configure MetaMask wallets to the Ontology network based on the above information. In this section, we introduce the tools for developing and deploying EVM contracts on Ontology, and how to use MetaMask plug-in wallets to manage Ethereum wallets. 


2. Introduction to Development Environment Tools


Since the EVM contract is developed in the Solidity language, we have detailed how to build the Remix, Truffle and Hardhat development environment, and enter a series of work such as contract development, compilation, deployment, and debugging. In addition, developers can also directly reuse the existing Ethereum contract framework to develop and deploy EVM contracts on Ontology.


2.1 Remix Development Environment


Remix IDE is an open source Solidity contract integrated development environment that supports users in contract development, compilation, deployment, and debugging. Please see this link for the official English version of Remix IDE.


Below we use a Hello World contract example to show how to use Remix.


2.1.1 Install Development Environment


To use Remix for the first time, you need to find and add the Solidity Compiler and Deploy and Run Transactions modules to the compiler in PLUGIN MANAGER.



Next, select the Solidity environment, create a new file and name it HelloWorld.sol. Then, copy the Hello World contract code that has been written to this file.



2.1.2 Compile Contract


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


2.1.3 Deployment Contract


After compilation, the contract can be deployed to the Ontology network. The following will take the TestNet as an example. Before deploying the contract, you need to connect the MetaMask wallet to the Ontology network (refer to the technical viewpoint of the previous issue), and receive the test ONG as a handling fee on the Ontology Faucet address.


Then, select “Injected Web3” in the Remix environment, and finally click “Deploy” to complete the contract deployment.



2.1.4 Call Contract


After the contract is deployed, the developer can call the methods in the contract. When deploying the Hello World contract in the example, the Hello string will be stored in the contract. We can call the message method of the contract to query this string, as shown below:



2.2 Truffle Development Environment


Truffle is a framework used to assist in the development, testing and management of Ethereum smart contracts. For official documents, please refer to this link.


Below we use this test code as an example to introduce the use of Truffle.


2.2.1 Install Development Environment


To initialize the development environment, first install the configuration files required by the Truffle environment.


Node.js v8+ LTS and npm (comes with Node)

Git


Then install Truffle with the following command.


$ npm install -g truffle


2.2.2 Configure Truffle-Config


First create .secret to store the test mnemonic or private key (can be found in MetaMask)


Then modify the truffle-config file as follows



2.2.3 Deploy the contract to the Ontology network


Execute the following command to deploy the contract.


$ truffle migrate — network ontology


If the following output is displayed, the deployment is successful.


Note: Try not to use Ethereum token units (such as wei, gwei, ether, etc.) when writing test scripts.



2.3 Hardhat Development Environment


Hardhat is a development environment for compiling, deploying, testing and debugging Ethereum applications. Below we use this test code as an example to introduce the use of Hardhat.


2.3.1 Install Development Environment


Please refer to this installation tutorial for installation.


2.3.2 Configure Hardhat-Config


Modify the hardhat.config.js file according to the following code



2.3.3 Deployment Contract


Execute the following command in the project root directory to deploy the contract to the Ontology testnet.


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


Results of the


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


Contract deployed to:


0xB105388ac7F019557132eD6eA90fB4BAaFde6E81 


3. Use MetaMask to Manage Keys


Ontology Network supports developers to use the MetaMask plug-in to manage Ethereum wallet private keys.


MetaMask is a non-custodial wallet. The user’s private key is encrypted with mnemonic words and stored in the local browser. Once the user loses the private key, the use of the wallet cannot be restored. MetaMask connects to Ethereum through Infura. For more detailed information, please click here.


3.1 Install Web3 Environment


The first step is to install the Web3 environment in the dApp:


$ npm install — save Web3


Create a new file named web3.js and copy the following code to the file:



In short, as long as the MetaMask plug-in is installed on the Chrome browser, the Ethereum global variable injected by the plug-in can be used.


In the second step, introduce the following code into your client,


import getWeb3 from’/path/to/web3';


Call the following function: 



3.2 Setting Up An Account


We need to obtain an account from the web3 instance created above to send transactions.



The getAccounts() function returns all accounts of the user in MetaMask. accounts[0] is the account currently selected by the user.


3.3 Contract initialization


After completing the above steps, initialize your contract.


3.4 Call Function


Now you can use the contract instance you just created to call any function you want. What needs special explanation is: the function call() is used to complete the pre-execution operation of the contract, for example:



The function send() is used to call the contract to change the state of the contract, for example:



Stay tuned for Part 3, for a demonstration of the Ontology EVM contract development process!