Hardhat
하드햇은 기본적으로 트러플과 같이 이더리움의 스마트 계약을 컴파일, 배포, 테스트 및 디버그하기 위한 개발 환경입니다.
하드햇 스토리지 - https://github.com/NomicFoundation/hardhat
하드햇 문서 - https://hardhat.org/docs
1. 프로젝트 디렉토리 생성 & 하드햇(Hardhat) 설치
$ mkdir meverse-test
$ cd meverse-test
Hardhat을 설치하려면 빈 폴더로 이동하여 npm
프로젝트(예: npm init
)를 초기화하고 다음을 실행합니다.
$ npm install --save-dev hardhat
Hardhat의 경우는 Javascript, Typescript 프로젝트를 생성하고 스마트 컨트렉트 컴파일, 테스트, 배포할 수 있습니다.(가이드에서는 자바스크립트 기준
으로 작성합니다.)
$ 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.12.2 👷
? What do you want to do? …
❯ Create a JavaScript project
Create a TypeScript project
Create an empty hardhat.config.js
Quit
2. 간단한 솔리디티 스마트 컨트랙트 작성
meverse-test/contracts 디렉토리에 MEVerseGreeter.sol을 생성합니다.
$ cd contracts
$ touch MEVerseGreeter.sol
$ vi MEVerseGreeter.sol
MEVerseGreeter.sol에 다음 코드를 작성하세요.
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.6;
contract Mortal {
/* 주소 타입의 소유자(owner) 변수 정의 */
address payable owner;
/* 이 함수는 초기화 시점에 실행되어 컨트랙트 소유자를 설정합니다 */
constructor () public { owner = msg.sender; }
/* 컨트랙트에서 자금을 회수하는 함수 */
function kill() public payable { if (msg.sender == owner) selfdestruct(owner); }
}
contract MEVerseGreeter is Mortal {
/* 문자열 타입의 변수 greeting 정의 */
string greeting;
/* 이 함수는 컨트랙트가 실행될 때 작동합니다 */
constructor (string memory _greeting) public {
greeting = _greeting;
}
/* 주(Main) 함수 */
function greet() public view returns (string memory) {
return greeting;
}
}
다음 명령어를 활용하여 컴파일, 테스트를 수행합니다. (테스트를 하기 위해서는 별도로 test 디렉토리에 테스트를 위한 스크립트를 별도로 작성하여 테스트를 진행합니다.)
$ npx hardhat compile
$ npx hardhat test
3. MEVerse testnet 연결을 위해 hardhat.config.js 수정
hardhat.config.js에 MEVerse Testnet network 정보를 수정합니다.
// hardhat.config.js
...
networks : {
MEVerseTestnet: {
url: "https://rpc.meversetestnet.io",
chainId: 4759, // MEVerse testnet chain id
accounts: [
"adf...583a9", // 컨트랙트를 배포할 계정 private key
],
timeout: 1000000, // 단위 milisec
}
}
....
4. 배포를 위한 deploy.js 작성
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");
async function main() {
const MEVerseGreeter = await hre.ethers.getContractFactory("MEVerseGreeter");
const mev = await MEVerseGreeter.deploy('Hello, MEVerse');
console.log(
`MEVerseGreeter deployed to ${mev.address}, \n - Transaction Hash : ${mev.deployTransaction.hash}`
);
console.log(
`Smart construct deploy successful... MEVerseGreeter.greet() : ${await mev.greet()}`
);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
5. 스마트 컨트랙트 배포 및 확인
다음 명령어로 스마트 컨트랙트를 배포합니다.
$ npx hardhat run scripts/deploy.js --network MEVerseTestnet
MEVerseGreeter deployed to 0xe917d0B105a6305E0b011Ce2388E5D7797b90aff
- Transaction Hash : 0x219fedab595ca2e0b4056cf7b15c6066a5e838a0b9a52caca982d2d22ece53f1
Smart contract deploy successful... MEVerseGreeter.greet() : Hello, MEVerse
배포는 Console화면에서 Contract Address를 확인하고 MEVerse Testnet Scan에서 트랜잭션 해시값으로 검색하여 확인합니다.

Last updated