HomeCoinsOntology (ONT)Everything You Need To Know About Ontology EVM Contract Development | by...

Everything You Need To Know About Ontology EVM Contract Development | by The Ontology Team | OntologyNetwork | Sep, 2021

- Advertisement -

Part 3: EVM Contract Development Process Demo

The Ontology Team
1631803616 326 Everything You Need To Know About Ontology EVM Contract Development

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.

4.1 Environmental Preparation

4.2 Contract Design

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

mkdir hardhatdemo
cd hardhatdemo
npm init
npm install — save-dev hardhat
npx 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:

├── artifacts
├── cache
├── contracts
├── hardhat.config.js
├── node_modules
├── package-lock.json
├── package.json
├── scripts
└── test

4.3.6 Test contract

$ npx hardhat test

The execution results are as follows:

$ npx hardhat test
✓ Token
✓ 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.

Stay tuned!

Source link

- Advertisement -
Mr Bitcointehttps://www.bitcointe.com/
“Fact You Need To Know About Cryptocurrency - The first Bitcoin purchase was for pizza.” ― Mohsin Jameel

Most Popular

Bitcoin (BTC) $ 61,533.00
Ethereum (ETH) $ 4,041.00
Tether (USDT) $ 1.00
Bitcoin Cash (BCH) $ 624.73
Litecoin (LTC) $ 195.11
EOS (EOS) $ 4.71
OKB (OKB) $ 27.34
Tezos (XTZ) $ 6.76
LEO Token (LEO) $ 3.42
Cardano (ADA) $ 2.16
Monero (XMR) $ 266.92
Stellar (XLM) $ 0.375637
Chainlink (LINK) $ 30.62
Huobi Token (HT) $ 10.15
TRON (TRX) $ 0.100322
USD Coin (USDC) $ 1.00
Dash (DASH) $ 196.82
NEO (NEO) $ 44.41
IOTA (MIOTA) $ 1.32
NEM (XEM) $ 0.1765
Zcash (ZEC) $ 171.50
Maker (MKR) $ 2,534.16
Pax Dollar (USDP) $ 1.00
Ethereum Classic (ETC) $ 55.09
VeChain (VET) $ 0.137588
TrueUSD (TUSD) $ 1.00
FTX Token (FTT) $ 62.99
KuCoin Token (KCS) $ 14.84
Waves (WAVES) $ 28.45