# Deployment Tutorial

## Overview

In this getting started tutorial, you will deploy a sample contract within Demex's EVM module.

### What You'll Need

* Solidity Language Knowledge: For this guide, you write the smart contract code in Solidity programming language. Solidity is the primary programming language employed in Ethereum - designed specifically for writing smart contracts. This code will define the rules and logic of the smart contract, specifying how it will function and interact with the Demex network.&#x20;
* A Demex account: You will need a Demex account to deploy the smart contract. The account will hold the necessary funds to pay for the gas fees required for the deployment.
* Sufficient funds: Deploying a smart contract on the Ethereum network requires paying transaction fees, also known as gas fees. The fees are payable in Ether (ETH), the native cryptocurrency of the Ethereum network. You must have sufficient funds in your Ethereum account to cover the transaction fees.
* A deployment script or plugin: You will need deployment tools like HardHat or Truffle, to deploy the smart contract on Demex EVM. These tools will enable you to interact with the Demex network and execute the smart contract deployment transaction.
* Access to a Demex node, either by running your own, connecting to a [public node](https://guide.dem.exchange/evm/broken-reference), or via an API key using a node service.

## Deploying an EVM Contract

To deploy a Solidity Smart Contract via HardHat, you can follow these steps:

**Step 1**: Set up the development environment. Make sure to install the following prerequisites before setting up the project:

* [Node.js & npm(7 and above)](https://nodejs.org/en)
* [Git](https://git-scm.com/)

**Step 2**: Create a new project via `npm init --y`.&#x20;

**Step 3**: Install hardhat dependencies required for the deployment via `npm install --save-dev hardhat` and  `npm install @nomicfoundation/hardhat-toolbox`.

**Step 4**: Create a new hardhat project by running `npx hardhat` in your project directory. Ensure that the `Create an empty hardhat.config.js` option is selected.&#x20;

<figure><img src="https://3548599389-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MO-XZjWplLUJPNs07G8%2Fuploads%2FxQXkfDNybIBYoewm34pL%2Fimage.png?alt=media&#x26;token=8e2fd07f-ae26-4cf5-916e-1de93d05f75b" alt="" width="344"><figcaption></figcaption></figure>

**Step 5**: Create or insert your smart contract in the `contracts` directory. In this example, we will be using a sample ERC20 contract, named `ERC20Token.sol`.

Sample contract:

```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol
interface IERC20 {
    function totalSupply() external view returns (uint);

    function balanceOf(address account) external view returns (uint);

    function transfer(address recipient, uint amount) external returns (bool);

    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
}

contract ERC20 is IERC20 {
    uint public totalSupply;
    mapping(address => uint) public balanceOf;
    mapping(address => mapping(address => uint)) public allowance;
    string public name = "Solidity by Example";
    string public symbol = "SOLBYEX";
    uint8 public decimals = 18;

    function transfer(address recipient, uint amount) external returns (bool) {
        balanceOf[msg.sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(msg.sender, recipient, amount);
        return true;
    }

    function approve(address spender, uint amount) external returns (bool) {
        allowance[msg.sender][spender] = amount;
        emit Approval(msg.sender, spender, amount);
        return true;
    }

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool) {
        allowance[sender][msg.sender] -= amount;
        balanceOf[sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function mint(uint amount) external {
        balanceOf[msg.sender] += amount;
        totalSupply += amount;
        emit Transfer(address(0), msg.sender, amount);
    }

    function burn(uint amount) external {
        balanceOf[msg.sender] -= amount;
        totalSupply -= amount;
        emit Transfer(msg.sender, address(0), amount);
    }
}
```

**Step 6**: Create a `deploy.js` script to deploy your contract under the `scripts` directory.

```
const { ethers } = require("hardhat");

async function main() {

  const [deployer] = await ethers.getSigners();
       
  console.log("Deployer ", deployer.address);

  const balance = await deployer.getBalance();

  console.log("Balance ", balance);

  const Token = await ethers.getContractFactory("ERC20");
  const token = await Token.deploy();

  console.log("Token address: ", token.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
```

**Step 7**: Configure HardHat to use the Demex EVM via `hardhat.config.js`.

```
/** @type import('hardhat/config').HardhatUserConfig */
require('@nomicfoundation/hardhat-toolbox')
module.exports = {
  solidity: '0.8.18',
  networks: {
    testnet: {
      url: 'https://test-evm-api.carbon.network/',
      accounts: [YOUR_PRIVATE_KEY],
    },
  },
}
```

{% hint style="info" %}
Ensure that your private key is inserted into `accounts` before running the next step.&#x20;
{% endhint %}

**Step 8**: Deploy your contract by running `npx hardhat run --network testnet scripts/deploy.js`.

{% hint style="info" %}
In this example, we are deploying to the `testnet` environment. The name of the environment should be the same as the one defined under `networks` in `hardhat.config.js`.
{% endhint %}

**Step 9**: If everything is working correctly, you should observe the corresponding logs being displayed on the console without any further errors.

&#x20;Sample logs:

```
Deployer  0x5161e15Fee8b918d4621703dB75641BbC25301c8
Balance  BigNumber { value: "12333000000000000000000" }
Token address:  0x112601CcF79eF9D634B45ABC972e2863e595569e
```

{% hint style="success" %}
Congratulations, you’ve successfully deployed your first smart contract on Demex EVM!

## Introduction

In this getting started tutorial, you will deploy a sample contract within Demex's EVM module.

### What You'll Need

* Solidity Language Knowledge: For this guide, you write the smart contract code in Solidity programming language. Solidity is the primary programming language employed in Ethereum - designed specifically for writing smart contracts. This code will define the rules and logic of the smart contract, specifying how it will function and interact with the Demex network.&#x20;
* A Demex account: You will need a Demex account to deploy the smart contract. The account will hold the necessary funds to pay for the gas fees required for the deployment.
* Sufficient funds: Deploying a smart contract on the Ethereum network requires paying transaction fees, also known as gas fees. The fees are payable in Ether (ETH), the native cryptocurrency of the Ethereum network. You must have sufficient funds in your Ethereum account to cover the transaction fees.
* A deployment script or plugin: You will need deployment tools like HardHat or Truffle, to deploy the smart contract on Demex EVM. These tools will enable you to interact with the Demex network and execute the smart contract deployment transaction.
* Access to a Demex node, either by running your own, connecting to a [public node](https://guide.dem.exchange/evm/broken-reference), or via an API key using a node service.

## Guide

To deploy a Solidity Smart Contract via HardHat, you can follow these steps:

**Step 1**: Set up the development environment. Make sure to install the following prerequisites before setting up the project:

* [Node.js & npm(7 and above)](https://nodejs.org/en)
* [Git](https://git-scm.com/)

**Step 2**: Create a new project via `npm init --y`.&#x20;

**Step 3**: Install hardhat dependencies required for the deployment via `npm install --save-dev hardhat` and  `npm install @nomicfoundation/hardhat-toolbox`.

**Step 4**: Create a new hardhat project by running `npx hardhat` in your project directory. Ensure that the `Create an empty hardhat.config.js` option is selected.&#x20;

<img src="https://3548599389-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MO-XZjWplLUJPNs07G8%2Fuploads%2FxQXkfDNybIBYoewm34pL%2Fimage.png?alt=media&#x26;token=8e2fd07f-ae26-4cf5-916e-1de93d05f75b" alt="" data-size="original">

**Step 5**: Create or insert your smart contract in the `contracts` directory. In this example, we will be using a sample ERC20 contract, named `ERC20Token.sol`.

Sample contract:

```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.0.0/contracts/token/ERC20/IERC20.sol
interface IERC20 {
    function totalSupply() external view returns (uint);

    function balanceOf(address account) external view returns (uint);

    function transfer(address recipient, uint amount) external returns (bool);

    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint amount) external returns (bool);

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint value);
    event Approval(address indexed owner, address indexed spender, uint value);
}

contract ERC20 is IERC20 {
    uint public totalSupply;
    mapping(address => uint) public balanceOf;
    mapping(address => mapping(address => uint)) public allowance;
    string public name = "Solidity by Example";
    string public symbol = "SOLBYEX";
    uint8 public decimals = 18;

    function transfer(address recipient, uint amount) external returns (bool) {
        balanceOf[msg.sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(msg.sender, recipient, amount);
        return true;
    }

    function approve(address spender, uint amount) external returns (bool) {
        allowance[msg.sender][spender] = amount;
        emit Approval(msg.sender, spender, amount);
        return true;
    }

    function transferFrom(
        address sender,
        address recipient,
        uint amount
    ) external returns (bool) {
        allowance[sender][msg.sender] -= amount;
        balanceOf[sender] -= amount;
        balanceOf[recipient] += amount;
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function mint(uint amount) external {
        balanceOf[msg.sender] += amount;
        totalSupply += amount;
        emit Transfer(address(0), msg.sender, amount);
    }

    function burn(uint amount) external {
        balanceOf[msg.sender] -= amount;
        totalSupply -= amount;
        emit Transfer(msg.sender, address(0), amount);
    }
}
```

**Step 6**: Create a `deploy.js` script to deploy your contract under the `scripts` directory.

```
const { ethers } = require("hardhat");

async function main() {

  const [deployer] = await ethers.getSigners();
       
  console.log("Deployer ", deployer.address);

  const balance = await deployer.getBalance();

  console.log("Balance ", balance);

  const Token = await ethers.getContractFactory("ERC20");
  const token = await Token.deploy();

  console.log("Token address: ", token.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });
```

**Step 7**: Configure HardHat to use the Demex EVM via `hardhat.config.js`.

```
/** @type import('hardhat/config').HardhatUserConfig */
require('@nomicfoundation/hardhat-toolbox')
module.exports = {
  solidity: '0.8.18',
  networks: {
    testnet: {
      url: 'https://test-evm-api.carbon.network/',
      accounts: [YOUR_PRIVATE_KEY],
    },
  },
}
```

{% hint style="info" %}
Ensure that your private key is inserted into `accounts` before running the next step.&#x20;
{% endhint %}

**Step 8**: Deploy your contract by running `npx hardhat run --network testnet scripts/deploy.js`.

{% hint style="info" %}
In this example, we are deploying to the `testnet` environment. The name of the environment should be the same as the one defined under `networks` in `hardhat.config.js`.
{% endhint %}

**Step 9**: If everything is working correctly, you should observe the corresponding logs being displayed on the console without any further errors.

&#x20;Sample logs:

```
Deployer  0x5161e15Fee8b918d4621703dB75641BbC25301c8
Balance  BigNumber { value: "12333000000000000000000" }
Token address:  0x112601CcF79eF9D634B45ABC972e2863e595569e
```

{% hint style="success" %}
Congratulations, you’ve successfully deployed your first smart contract on Demex EVM!
{% endhint %}
{% endhint %}
