# Development Environment and Tools

{% hint style="info" %}
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**](https://developer.ont.io/).
{% endhint %}

{% hint style="warning" %}
The value of gas price for a transaction must be in multiples of 10^9. The minimum value is 2500\*10^9.
{% endhint %}

## Development Environment and Tools

EVM smart contracts are written using [Solidity](https://docs.soliditylang.org/en/v0.8.6/). You can reuse existing Ethereum contract frameworks to develop and deploy EVM contracts.

### Remix

[Remix IDE](https://remix.ethereum.org/#optimize=false\&runs=200\&evmVersion=null\&version=soljson-v0.8.1+commit.df193b15.js) is an open source development environment for EVM contracts. Remix IDE documentation is [here](https://remix-ide.readthedocs.io/en/latest/).

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.

[![image-20210526142630046](https://github.com/ontio/ontology/raw/master/docs/specifications/evm_refernce/tutorial/image-20210526142630046.png)](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/tutorial/image-20210526142630046.png)

Then, select Solidity environment. Create a new file and name it HelloWorld.sol. Then copy the code of [Hello World contract](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/contract-demo/helloworlddemo/helloworld.sol) and paste it in the file just created.

[![image-20210526143301031](https://github.com/ontio/ontology/raw/master/docs/specifications/evm_refernce/tutorial/image-20210526143301031.png)](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/tutorial/image-20210526143301031.png)

#### **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.

{% hint style="info" %}
**Note**: MetaMask must be configured for Ontology before you deploy the contract.
{% endhint %}

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>"

[![RemixIDE\_Step1](https://github.com/ontio/ontology/raw/master/docs/specifications/evm_refernce/tutorial/metamask_networks.png)](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/tutorial/metamask_networks.png)

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

[![deploy contract](https://github.com/ontio/ontology/raw/master/docs/specifications/evm_refernce/tutorial/remix_deploy.jpg)](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/tutorial/remix_deploy.jpg)

#### **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:

[![invoke contract](https://github.com/ontio/ontology/raw/master/docs/specifications/evm_refernce/tutorial/remix_invoke.jpg)](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/tutorial/remix_invoke.jpg)

### Truffle

Truffle offers tools and frameworks for EVM contract development, testing and management. You can find more details [here](https://www.trufflesuite.com/docs/truffle/quickstart).

Now we will demonstrate how to use Truffle with this [test code](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/contract-demo/truffledemo).

#### **Install Truffle**

First, initialize and install dependencies.

* [Node.js v8+ LTS and npm](https://nodejs.org/en/) (comes with Node)
* [Git](https://git-scm.com/)

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.

```javascript
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.

{% hint style="warning" %}
**Note:** Avoid using ETH units (e.g. wei, gwei, ether, etc.) when writing test scripts.
{% endhint %}

```
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](https://github.com/ontio/ontology/blob/master/docs/specifications/evm_refernce/contract-demo/hardhatdemo) as an example and demonstrate how to use Hardhat.

#### **Install Hardhat**

Please refer to [Hardhat doc](https://hardhat.org/getting-started/) 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:

```javascript
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**

| Item           | Description                                                                                                                                                                                                                                                                                                                                                                                                       |
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| NetworkName    | Ontology MainNet                                                                                                                                                                                                                                                                                                                                                                                                  |
| chainId        | 58                                                                                                                                                                                                                                                                                                                                                                                                                |
| Gas Token      | ONG Token                                                                                                                                                                                                                                                                                                                                                                                                         |
| RPC            | <p><code><https://dappnode1.ont.io:10339></code>,</p><p><code><https://dappnode2.ont.io:10339></code>,</p><p><code><https://dappnode3.ont.io:10339></code>,</p><p><code><https://dappnode4.ont.io:10339></code>,</p><p><code><http://dappnode1.ont.io:20339></code>, <code><http://dappnode2.ont.io:20339></code>, <code><http://dappnode3.ont.io:20339></code>, <code><http://dappnode4.ont.io:20339></code></p> |
| Block Explorer | <https://explorer.ont.io/>                                                                                                                                                                                                                                                                                                                                                                                        |

**TestNet**

| Item           | Description                                                                                                                                                                                                |
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| NetworkName    | Ontology TestNet                                                                                                                                                                                           |
| chainId        | 5851                                                                                                                                                                                                       |
| Gas Token      | ONG Token                                                                                                                                                                                                  |
| RPC            | <p><code><https://polaris1.ont.io:10339></code> , <code><https://polaris2.ont.io:10339></code>,</p><p><code><https://polaris3.ont.io:10339></code>,</p><p><code><https://polaris4.ont.io:10339></code></p> |
| Block Explorer | <https://explorer.ont.io/testnet>                                                                                                                                                                          |

{% hint style="info" %}
Ontology EVM contracts consume ONG as gas fee for execution. You can apply for TestNet ONG [here](https://developer.ont.io/).
{% endhint %}

### EVM Assets on Ontology

| Name | Address                                     |
| ---- | ------------------------------------------- |
| ONG  | 0x00000000000000000000000000000000000000000 |

### OEP-4 Assets

Please refer to this [link](https://explorer.ont.io/tokens/oep4/10/1).
