Part 3: EVM Contract Development Process Demo
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 Part 2, we introduced the tools for developing and deploying EVM contracts on Ontology, and how to use MetaMask plug-in wallets to manage Ethereum wallets. In this section, we demonstrate the EVM contract development process.
Below, we use the Hardhat tool to demonstrate the complete process of developing, deploying, and testing, EVM contracts on the Ontology network.
4.1 Environmental Preparation
4.2 Contract Design
4.2.1 Contract Logic
Let’s use a red packet contract as an example, which mainly provides the following functions:
- Send Red Packet
- Receive Red Packet
Each time a red packet is issued, the value of each red packet and the number of people who can claim it must be specified.
For example, the value of a red packet is 100 tokens and the number of people who can claim it is 10. That is, there are 10 different addresses to receive a portion of the red packet. We set the amount of each red packet to be equal, that is, each address can receive 10 tokens.
According to the above logic, we can set the following storage structure:
4.2.2 Defining Contract Events
In the process of contract execution, we can trace the contract execution process by adding events.
In this example, we design the following two events:
- When sending a red packet, the contract generates the ID of the red packet, which must be pushed to the caller through an event.
- When receiving the red packet, you need to push an event to record the red packet ID and the number of tokens received.
event SendRedPacket(uint packetId, uint amount);
event ReceiveRedPacket(uint packetId, uint amount);
4.2.3 Define Function
1) Send red packets. Anyone can call this interface and send a certain amount of tokens to the contract address, so that other addresses can receive red packets from the contract address.
Note: Before calling this method, you need to authorize the contract address to be able to transfer the token from the user’s address, so you need to call the Approve method of the token first.
2) Receive the red packet. Any address can receive red packets by calling this interface. When calling this interface, you need to specify the red packet ID, that is, specify the red packet to be received.
Please refer to this link for the complete code of the contract.
4.3 Use Hardhat to compile and test the contract
4.3.1 Create Hardhat Project
npm install — save-dev hardhat
4.3.2 Modify hardhat.config.js file
Add test network node configuration information
The corresponding addresses for the specified private keys under the accounts filed need to have ONG from the TestNet to pay the transaction fee. You can receive TestNet ONG here.
4.3.3 Document preparation
Put the red packet contract code file in the contracts folder. In order to support the transfer of ERC-20 tokens, we also need EIP20Interface.sol, UniversalERC20.sol, and TokenDemo.sol files, which can be downloaded here.
4.3.4 Add test code under test folder
4.3.5 Compile contract
Execute the following commands in the project root directory to compile the contract
$ npx hardhat compile
Compiling 5 files with 0.8.0
Compilation finished successfully
After the command is executed, the following folder will be generated:
4.3.6 Test contract
$ npx hardhat test
The execution results are as follows:
$ npx hardhat test
✓ sendRedPacket (16159ms)
2 passing (41s)
The above is a complete demonstration process of Ontology EVM contract development. In Part 4, we introduce a Web3 API reference and teach you how to use Ontology Bridge to implement one-click crossover between Ontology digital assets and Ethereum digital assets.