Setting Up the Development Environment

Setting Up the Development Environment

Step 1: Install Hardhat

Hardhat is a development environment for Ethereum software designed to make the development process easier and more efficient.

1. Create a new directory as the project folder and enter that directory:

  1. Initialize a new npm project:

  2. Install Hardhat:

  3. Create a new Hardhat project:

Follow the prompts to create a basic sample project.

Step 2: Project Structure

Your project structure should look like the following:

emc-dapp/

├── contracts

│ └── Greeter.sol

├── scripts

│ └── sample-script.js

├── test

│ └── sample-test.js

├── hardhat.config.js

├── package.json

└── README.md

Step 3: Write a smart contract

exist contracts Create a simple smart contract in the directory. For example,Greeter.sol contract:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Greeter {

string private greeting;

constructor(string memory _greeting) {

greeting = _greeting;

}

function greet() public view returns (string memory) {

return greeting;

}

function setGreeting(string memory _greeting) public {

greeting = _greeting;

}

}

Step 4: Compile the contract

Compile your smart contract to make sure everything is set up correctly:

npx hardhat compile

Step 5: Deploy the contract

exist scripts Create a deployment script in the directory, for example deploy.js:

async function main() {

const [deployer] = await ethers.getSigners();

console.log("Deploying contracts with the account:", deployer.address);

const Greeter = await ethers.getContractFactory("Greeter");

const greeter = await Greeter.deploy("Hello, EMC!");

console.log("Greeter deployed to:", greeter.address);

}

main()

.then(() => process.exit(0))

.catch(error => {

console.error(error);

process.exit(1);

});

Deploy the contract to the EMC chain:

npx hardhat run scripts/deploy.js --network emc

Step 6: EMC chain configuration

Update yourhardhat.config.jsTo include the configuration of the EMC chain:

require("@nomiclabs/hard-waffle");

module.exports = {

solidity: "0.8.4",

networks: {

emc: {

url: "https://rpc1.emc.network",

accounts: [`0x${process.env.PRIVATE_KEY}`]

}

}

};

https://rpc1.emc.networkThe actual RPC URL for the EMC Testnet and make sure your private key is set in the environment variables.

Last updated