# Ethereum Tooling

#### On this page

* [<mark style="color:blue;">Remix: Deploying a Smart Contract</mark>](#remix-deploying-a-smart-contract)
  * [<mark style="color:blue;">Connect to Remix</mark>](#connect-to-remix)
  * [<mark style="color:blue;">Deploy and Interact</mark>](#deploy-and-interact)
* [<mark style="color:blue;">Hardhat: Deploying a Smart Contract</mark>](#hardhat-deploying-a-smart-contract)
  * [<mark style="color:blue;">Install Dependencies</mark>](#install-dependencies)
  * [<mark style="color:blue;">Create Hardhat Project</mark>](#create-hardhat-project)
  * [<mark style="color:blue;">Deploying a Smart Contract</mark>](#deploying-a-smart-contract)
* [<mark style="color:blue;">Truffle: Deploying a Smart Contract</mark>](#truffle-deploying-a-smart-contract)
  * [<mark style="color:blue;">Install Dependencies</mark>](#install-dependencies-1)
  * [<mark style="color:blue;">Create Truffle Project</mark>](#create-truffle-project)
  * [<mark style="color:blue;">Truffle configuration</mark>](#truffle-configuration)
  * [<mark style="color:blue;">Deploy contract</mark>](#deploy-contract)
  * [<mark style="color:blue;">Run Truffle tests</mark>](#run-truffle-tests)

### Remix: Deploying a Smart Contract <mark style="color:blue;">#</mark> <a href="#remix-deploying-a-smart-contract" id="remix-deploying-a-smart-contract"></a>

Learn how to deploy a simple Solidity-based smart contract to ICPlaza using the Remix in-browser IDE {synopsis}

[<mark style="color:blue;">Remix</mark>](http://remix.ethereum.org/#lang=en\&optimize=false\&runs=200\&evmVersion=null\&version=soljson-v0.8.18+commit.87f61d96.js) is an in-browser IDE for [<mark style="color:blue;">Solidity</mark>](https://github.com/ethereum/solidity) smart contracts. In this guide, we will learn how to deploy a contract to a running ICPlaza network through Remix and interact with it.

#### Connect to Remix <mark style="color:blue;">#</mark> <a href="#connect-to-remix" id="connect-to-remix"></a>

::: tip If you haven’t already, follow the steps in the [<mark style="color:blue;">Metamask guide</mark>](/icplaza-docs/chain-dev/digital-wallets.md#metamask) to import your ICPlaza private key into Metamask. Start the ICPlaza daemon and REST server. :::

Go to [<mark style="color:blue;">Remix</mark>](http://remix.ethereum.org/#lang=en\&optimize=false\&runs=200\&evmVersion=null). There are some contracts in the File Explorer. Replace these with the source code to `Counter.sol` below. On the left-most bar, select the Solidity Compiler and compile the contract.

```javascript
pragma solidity >=0.7.0 <0.9.0;

contract Counter {
  uint256 counter = 0;

  function add() public {
    counter++;
  }

  function subtract() public {
    counter--;
  }

  function getCounter() public view returns (uint256) {
    return counter;
  }
}
```

Next, select the `Deploy and Run` option. Select `Injected Web3` as the `Environment`. This will open a metamask popup for you to connect your Metamask to Remix. Select `Connect` to confirm.

You should see your account show up in the left-hand panel.

#### Deploy and Interact <mark style="color:blue;">#</mark> <a href="#deploy-and-interact" id="deploy-and-interact"></a>

Now that your account is connected, you are able to deploy the contract. Press the `Deploy` button. A metamask pop-up will appear asking you to confirm. Confirm the transaction. You should see a log for the deployment transaction in the ICPlaza daemon logs:

```bash
I[2020-07-15|17:26:43.155] Added good transaction                       module=mempool tx=877A8E6600FA27EC2B2362719274314977B243671DC4E5F8796ED97FFC0CBE42 res="&{CheckTx:log:\"[]\" gas_wanted:121193 }" height=31 total=1
```

Once the contract has been successfully deployed, you will see it show up in the `Deployed Contracts` section in the left-hand side, as well as a green check in the Remix console showing the transaction details.

Now, you are able to interact with the contract through Remix. For `Counter.sol`, click `add`. This will open a Metamask pop-up asking you to confirm. Confirm the transaction. Then, click `getCounter` to get the count, which should be `1`.

### Hardhat: Deploying a Smart Contract <mark style="color:blue;">#</mark> <a href="#hardhat-deploying-a-smart-contract" id="hardhat-deploying-a-smart-contract"></a>

Learn how to deploy a simple Solidity-based smart contract to ICPlaza using the Hardhat environment {synopsis}

[<mark style="color:blue;">Hardhat</mark>](https://hardhat.org/) is a flexible development environment for building Ethereum-based smart contracts. It is designed with integrations and extensibility in mind

#### Install Dependencies <mark style="color:blue;">#</mark> <a href="#install-dependencies" id="install-dependencies"></a>

Before proceeding, you need to install Node.js (we’ll use v16.x) and the npm package manager. You can download directly from [<mark style="color:blue;">Node.js</mark>](https://nodejs.org/en/download) or in your terminal:

:::: tabs ::: tab Ubuntu

```bash
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

sudo apt install -y nodejs
```

::: ::: tab MacOS

```bash
# You can use homebrew (https://docs.brew.sh/Installation)
$ brew install node

# Or you can use nvm (https://github.com/nvm-sh/nvm)
$ nvm install node
```

::: ::::

You can verify that everything is installed correctly by querying the version for each package:

```bash
$ node -v
...

$ npm -v
...
```

#### Create Hardhat Project <mark style="color:blue;">#</mark> <a href="#create-hardhat-project" id="create-hardhat-project"></a>

To create a new project, navigate to your project directory and run:

```bash
$ npx hardhat

888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

👷 Welcome to Hardhat v2.9.3 👷‍

? What do you want to do? …
  Create a basic sample project
❯ Create an advanced sample project
  Create an advanced sample project that uses TypeScript
  Create an empty hardhat.config.js
  Quit
```

Following the prompts should create a new project structure in your directory. Consult the [<mark style="color:blue;">Hardhat config page</mark>](https://hardhat.org/hardhat-runner/docs/config) for a list of configuration options to specify in `hardhat.config.js`. Most importantly, you should set the `defaultNetwork` entry to point to your desired JSON-RPC network:

:::: tabs ::: tab Local Node

```javascript
module.exports = {
  defaultNetwork: "local",
  networks: {
    hardhat: {
    },
    local: {
      url: "http://localhost:8545/",
      accounts: [privateKey1, privateKey2, ...]
    }
  },
  ...
}
```

::: ::: tab Testnet

```javascript
module.exports = {
  defaultNetwork: "testnet",
  networks: {
    hardhat: {
    },
    testnet: {
      url: "https://eth.bd.ICPlaza.dev:8545",
      accounts: [privateKey1, privateKey2, ...]
    }
  },
  ...
}
```

::: ::::

To ensure you are targeting the correct network, you can query for a list of accounts available to you from your default network provider:

```bash
$ npx hardhat accounts
0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
0x70997970C51812dc3A010C7d01b50e0d17dc79C8
0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC
0x90F79bf6EB2c4f870365E785982E1f101E93b906
...
```

#### Deploying a Smart Contract <mark style="color:blue;">#</mark> <a href="#deploying-a-smart-contract" id="deploying-a-smart-contract"></a>

You will see that a default smart contract, written in Solidity, has already been provided under `contracts/Greeter.sol`:

```javascript
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Greeter {
    string private greeting;

    constructor(string memory _greeting) {
        console.log("Deploying a Greeter with greeting:", _greeting);
        greeting = _greeting;
    }

    function greet() public view returns (string memory) {
        return greeting;
    }

    function setGreeting(string memory _greeting) public {
        console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
        greeting = _greeting;
    }
}
```

This contract allows you to set and query a string `greeting`. Hardhat also provides a script to deploy smart contracts to a target network; this can be invoked via the following command, targeting your default network:

```bash
npx hardhat run scripts/deploy.js
```

Hardhat also lets you manually specify a target network via the `--network <your-network>` flag:

:::: tabs ::: tab Local Node

```bash
npx hardhat run --network {{ $themeConfig.project.rpc_url_local }} scripts/deploy.js
```

::: ::: tab Testnet

```bash
npx hardhat run --network {{ $themeConfig.project.rpc_url_testnet }} scripts/deploy.js
```

::: ::::

Finally, try running a Hardhat test:

```bash
$ npx hardhat test
Compiling 1 file with 0.8.4
Compilation finished successfully


  Greeter
Deploying a Greeter with greeting: Hello, world!
Changing greeting from 'Hello, world!' to 'Hola, mundo!'
    ✓ Should return the new greeting once it's changed (803ms)


  1 passing (805ms)
```

### Truffle: Deploying a Smart Contract <mark style="color:blue;">#</mark> <a href="#truffle-deploying-a-smart-contract" id="truffle-deploying-a-smart-contract"></a>

Learn how to deploy a simple Solidity-based smart contract to ICPlaza using the [<mark style="color:blue;">Truffle</mark>](https://trufflesuite.com/truffle/) environment {synopsis}

Truffle is a development framework for deploying and managing [<mark style="color:blue;">Solidity</mark>](https://github.com/ethereum/solidity) smart contracts.

#### Install Dependencies <mark style="color:blue;">#</mark> <a href="#install-dependencies-1" id="install-dependencies-1"></a>

First, install the latest Truffle version on your machine globally.

```bash
yarn install truffle -g
```

#### Create Truffle Project <mark style="color:blue;">#</mark> <a href="#create-truffle-project" id="create-truffle-project"></a>

In this step we will create a simple counter contract. Feel free to skip this step if you already have your own compiled contract.

Create a new directory to host the contracts and initialize it:

```console
mkdir ICPlaza-truffle
cd ICPlaza-truffle
```

Initialize the Truffle suite with:

```bash
truffle init
```

Create `contracts/Counter.sol` containing the following contract:

```javascript
pragma solidity >=0.7.0 <0.9.0;

contract Counter {
  uint256 counter = 0;

  function add() public {
    counter++;
  }

  function subtract() public {
    counter--;
  }

  function getCounter() public view returns (uint256) {
    return counter;
  }
}
```

Compile the contract using the `compile` command:

```bash
truffle compile
```

Create `test/counter_test.js` containing the following tests in Javascript using [<mark style="color:blue;">Mocha</mark>](https://mochajs.org/):

```javascript
const Counter = artifacts.require("Counter")

contract('Counter', accounts => {
  const from = accounts[0]
  let counter

  before(async() => {
    counter = await Counter.new()
  })

  it('should add', async() => {
    await counter.add()
    let count = await counter.getCounter()
    assert(count == 1, `count was ${count}`)
  })
})
```

#### Truffle configuration <mark style="color:blue;">#</mark> <a href="#truffle-configuration" id="truffle-configuration"></a>

Open `truffle-config.js` and uncomment the `development` section in `networks`:

```javascript
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    },
```

This will allow your contract to connect to your ICPlaza local node.

#### Deploy contract <mark style="color:blue;">#</mark> <a href="#deploy-contract" id="deploy-contract"></a>

In the Truffle terminal, migrate the contract using:

```bash
truffle migrate --network development
```

You should see incoming deployment logs in the ICPlaza daemon Terminal tab for each transaction (one to deploy `Migrations.sol` and the other to deploy `Counter.sol`).

```bash
$ I[2020-07-15|17:35:59.934] Added good transaction                       module=mempool tx=22245B935689918D332F58E82690F02073F0453D54D5944B6D64AAF1F21974E2 res="&{CheckTx:log:\"[]\" gas_wanted:6721975 }" height=3 total=1
I[2020-07-15|17:36:02.065] Executed block                               module=state height=4 validTxs=1 invalidTxs=0
I[2020-07-15|17:36:02.068] Committed state                              module=state height=4 txs=1 appHash=76BA85365F10A59FE24ADCA87544191C2D72B9FB5630466C5B71E878F9C0A111
I[2020-07-15|17:36:02.981] Added good transaction                       module=mempool tx=84516B4588CBB21E6D562A6A295F1F8876076A0CFF2EF1B0EC670AD8D8BB5425 res="&{CheckTx:log:\"[]\" gas_wanted:6721975 }" height=4 total=1
```

#### Run Truffle tests <mark style="color:blue;">#</mark> <a href="#run-truffle-tests" id="run-truffle-tests"></a>

Now, you can run the Truffle tests using the ICPlaza node using the `test` command:

```bash
$ truffle test --network development

Using network 'development'.


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



  Contract: Counter
    ✓ should add (5036ms)


  1 passing (10s)
```

[<mark style="color:blue;">← Localnet</mark>](/icplaza-docs/chain-dev/localnet.md)

[<mark style="color:blue;">Ethereum JSON-RPC →</mark>](/icplaza-docs/chain-dev/ethereum-json-rpc.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ictech.gitbook.io/icplaza-docs/chain-dev/ethereum-tooling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
