# Compiling EVM Smart Contracts

This guide will help you understand how to compil EVM Solidity smart contracts on the Nibiru EVM using hardhat.

# Compiling your Solidity Contracts

Solidity is a statically-typed programming language designed for developing smart contracts that run on the Ethereum Virtual Machine (EVM). To deploy and interact with your contracts on Nibiru EVM, you need to compile them into bytecode and generate the Application Binary Interface (ABI).

# Prerequisites

Before diving into compilation, it's helpful to understand the basics of smart contracts and the EVM. You can refer to the Ethereum Smart Contracts Introduction (opens new window) and the Nibiru's Ethereum Virtual Machine documentation for a foundational understanding.

# The EVM

The EVM executes bytecode, which is a low-level, machine-readable format generated by compiling Solidity code. Each operation in the bytecode is represented by an opcode, which the EVM can execute to perform various tasks, such as arithmetic operations, data manipulation, and control flow.

# Web Applications

The compiler also generates the ABI, a JSON file that describes the deployed contract's functions and their inputs and outputs. The ABI serves as a bridge between your web application and the smart contract, allowing the application to understand and interact with the contract.

# Compile Example with Hardhat

This example is one of many that demonstrates how to use Hardhat for Ethereum development. The following steps guide you through setting up a Hardhat project, configuring it, and compiling a Solidity smart contract. This example assumes you have a basic understanding of Node.js and npm.

# Step 1: Create a package.json file

Save the following content in a package.json file. This file specifies the dependencies needed for your Hardhat project.

Copy { "devDependencies": { "@nomicfoundation/hardhat-toolbox": "^5.0.0", "hardhat": "^2.22.5" }, "dependencies": { "@openzeppelin/contracts": "^4.9.0" } }
  • @nomicfoundation/hardhat-toolbox: A set of tools and plugins for Hardhat.
  • hardhat: The core Hardhat package.
  • @openzeppelin/contracts: A library of secure and community-vetted smart contracts.

# Step 2: Install Dependencies and Initialize Hardhat

Run the following commands to install the specified dependencies and initialize a new Hardhat project.

Copy npm i --check-files npx hardhat init

The npx hardhat init command initializes a new Hardhat project and creates a configuration file.

# Step 3: Configure Hardhat

Edit the generated hardhat.config.js file to match the following configuration:

You should now have a Hardhat config. You can edit it to match this:

Copy require("@nomicfoundation/hardhat-toolbox"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.19", };

In this configuration:

  • require("@nomicfoundation/hardhat-toolbox");: Loads the Hardhat toolbox plugin.
  • solidity: "0.8.19": Specifies the Solidity compiler version.

Note: The reason for using Solidity version 0.8.19 is because of a bug related to ERC3855 (opens new window), which is described in this thread on the Remix forums: "Unable to deploy from Remix - invalid opcode: PUSH0" (opens new window).

# Step 4: Add Your Smart Contract

Replace the example contract in the contracts directory with your actual Solidity code. For instance, if you have a contract file named ERC20Minter.sol, copy it to the contracts directory and rename it to Lock.sol:

Copy cp ERC20Minter.sol contracts/Lock.sol # Example using the embed ERC20Minter.sol

# Step 5: Compile Your Contract

Compile the contract to generate the ABI and bytecode using the following command:

Copy npx hardhat compile

After running this command, Hardhat will compile your Solidity contracts and generate the necessary files in the artifacts directory. These files include the ABI (Application Binary Interface) and the bytecode, which are essential for deploying and interacting with the contract.

This example provides a basic setup for a Hardhat project. You can extend it by adding more configurations, scripts, and contracts as needed.