Park Chae-yeon

Park Chae-yeon

Student

© 2020

블록체인 Dapp 개발에 트러플 활용하기 2부 - 스마트 컨트랙트(Smart Contract) 작성


블록체인 Dapp 개발에 트러플 활용하기 2부


스마트 컨트랙트(Smart Contract) 작성하기

강의에서는 스마트 컨트랙트를 작성할 프로그램으로 WebStorm을 사용했으나, 이 포스팅에서는 IntelliJ CE 버전으로 진행할 예정이다.

IntelliJ 에서 솔리디티 언어를 작성하려면 플러그인 다운로드 필수 sol_plugin

[File] - [Open] - dapp-example 폴더 선택

intelliJ_file_open

[contracts]에 HelloWorld 솔리디티 파일 만들기

intelliJ_file_open

HelloWorld.sol 솔리디티 컴파일러 버전0.5.8 로 변경

sol_version_check sol_v

HelloWorld.sol 작성

pragma solidity ^0.5.8;

contract HelloWorld {
    string public greeting;

    constructor(string memory _greeting) public {
        greeting = _greeting;
    }
    function setGreeting(string memory _greeting) public{
        greeting = _greeting;
    }
    function say() public view returns(string memory){    //출력
        return greeting;
    }
}

//terminal
$ truffle compile     //contracts에 있는 파일 모두 컴파일

truffle compile 오류 추가 (20.04.13)

truffle로 dapp-example를 truffle init까지 해서 파일 생성했으나 truffle compile 명령어 수행 시 bash: truffle: command not found 오류 발생

  • 해결방법 참고 사이트
    • 최신 버전의 npm 및 노드가 설치되어 있는지 확인 후 npm과 노드를 최신 버전으로 업데이트
      npm install -g truffle
      




HelloWorld.sol 컴파일

compile -> .json 파일 자동 생성

helloworld1 helloworld2

json 파일에서 중요 부분은 abi , bytecode , deployedBytecode

helloworld3




truffle_config.jspath 지정

const path = require("path");
contracts_bulid_directory: path.join(__dirname, "client/src/contracts"),

add_path helloworld4

다시 터미널에서 compile 하면 지정한 위치 에 컴파일 결과가 저장 되는 것을 확인할 수 있다.

$ truffle compile --all

re_compile

path를 따로 지정하지 않고 default값인 경우에 저장 경로

helloworld5

path를 지정해줬을 경우의 파일 저장 경로