[Hardhat-Solidity] 2. Compile Smart Contract

in hive-101145 •  4 years ago 

오늘은 Compile 에 대해서 알아봅니다.


Compile

  • 간단한 토큰 기능이 있는 Solidity를 아래와 같은 코드로 정의합니다.
// Sample에 사용될 Token.sol
// Solidity files have to start with this pragma.
// It will be used by the Solidity compiler to validate its version.
pragma solidity ^0.8.5;

// This is the main building block for smart contracts.
contract Token {
    // Some string type variables to identify the token.
    // The `public` modifier makes a variable readable from outside the contract.
    string public name = "My Hardhat Token";
    string public symbol = "MHT";

    // The fixed amount of tokens stored in an unsigned integer type variable.
    uint256 public totalSupply = 1000000;

    // An address type variable is used to store ethereum accounts.
    address public owner;

    // A mapping is a key/value map. Here we store each account balance.
    mapping(address => uint256) balances;

    /**
     * Contract initialization.
     *
     * The `constructor` is executed only once when the contract is created.
     */
    constructor() {
        // The totalSupply is assigned to transaction sender, which is the account
        // that is deploying the contract.
        balances[msg.sender] = totalSupply;
        owner = msg.sender;
    }

    /**
     * A function to transfer tokens.
     *
     * The `external` modifier makes a function *only* callable from outside
     * the contract.
     */
    function transfer(address to, uint256 amount) external {
        // Check if the transaction sender has enough tokens.
        // If `require`'s first argument evaluates to `false` then the
        // transaction will revert.
        require(balances[msg.sender] >= amount, "Not enough tokens");

        // Transfer the amount.
        balances[msg.sender] -= amount;
        balances[to] += amount;
    }

    /**
     * Read only function to retrieve the token balance of a given account.
     *
     * The `view` modifier indicates that it doesn't modify the contract's
     * state, which allows us to call it without executing a transaction.
     */
    function balanceOf(address account) external view returns (uint256) {
        return balances[account];
    }
}

  • npx hardhat compile 명령어로 Compile을 합니다.

  • Compile 완료시 아래와 같이 프로젝트 폴더가 생성됩니다.(Greeter.sol 및 관련 파일은 Sample 에서 만들어진 내용으로 무시해도 좋습니다.)

  • artifacts 폴더에 빌드정보가 난수.json 형태로 남아 있습니다.

  • artifacts/contracts 폴더에는 compile 된 solidity 파일에서 생성된 파일들이 존재합니다.(abi 등은 여기서 받아 사용하면 됩니다.)

  • artifacts/hardhat 폴더에는 solidity 에 import 하여 사용되는 solidity 파일이 컴파일 된다.(예시로 console.sol)

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";


contract Greeter {
  string greeting;

  constructor(string memory _greeting) {
    console.log("Deploying a Greeter with greeting:", _greeting);
    greeting = _greeting;
  }

  function greet() public view returns (string memory) {
    return greeting;
  }

  function setGreeting(string memory _greeting) public {
    console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
    greeting = _greeting;
  }
}

  • 특이하게도 solidity 소스에 console.log가 사용됩니다. 디버깅이 쉽지 않은 solidity 상 꽤나 유용한 라이브러리 같습니다.
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@happyberrysboy transfered 50 KRWP to @krwp.burn. voting percent : 100.00%, voting power : 35.82%, steem power : 1811917.69, STU KRW : 1200.
@happyberrysboy staking status : 12340 KRWP
@happyberrysboy limit for KRWP voting service : 12.34 KRWP (rate : 0.001)
What you sent : 50 KRWP (Voting Percent over 100 %)
Refund balance : 41.852 KRWP [55714019 - a4e85e39d04c37f2f2ea23e48fc2173344f0ce02]

자꾸 이상한거 올리지 마십쇼! ㅋㅋㅋ
즐거운 불금 되세요^^

ㅎㅎㅎㅎ 요즘 살짝 또 개발에 불타오를랑 말랑 하고 있숩니다 ㅎㅎㅎㅎ
독거형님도 즐겁고 즐겁고 행복하고 건강하고 산뜻하고 뜻깊은 주말 되시길 간절히 바래봅니다!! ^^

Oh awesome, i recently started with solidity and this is very usefull to me . thanks