$ 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 정보를 수정합니다.
// 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;
});