• Home
  • About
    • Lajancia Workspace photo

      Lajancia Workspace

      .

    • Learn More
    • Email
    • LinkedIn
    • Instagram
    • Tumblr
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Record
  • Blender
  • Blockchain

3. 이더리움 시작하기

28 Oct 2021

Reading time ~2 minutes

3. 이더리움 시작하기-3

간단한 상호 작용 코드 만들기


pragma solidity >=0.4.22 <0.9.0;

contract Lottery {
    address public owner; //주소

    constructor() public { //배포가 될 때 가장 먼저 시행되는 함수
        owner = msg.sender;
    }

    function getSomeValue() public pure returns (unit256 value) {
        return 5;
    }
}

에러

Please check that your Ethereum client:     - is running     - is accepting RPC connections (i.e., "--rpc" or "--http" option is used in geth)     - is accessible over the network     - is properly configured in your Truffle configuration file (truffle-config.js)
  • 다시 프로젝트를 실행하려고 하니 에러가 발생했다.
  • 이더리움 클라이언트에 문제가 발생했다고 한다.
    • 다시 ganache-cli -d -m tutorial를 실행시키고 truffle migrate --reset을 다시 실행

컨트랙트를 사용하면서 더 많은 gas를 사용했다는 것을 확인할 수 있다.

gas used: 191943 (0x2edc7) 정도 사용됨

현재 ganache가 블록체인 역할을 하고 있기 때문에 가스 사용이 기록된다.

트러플 콘솔 접근


truffle console
  • 위의 명령어를 통해 트러플 콘솔에 접근할 수 있다.
  • 트러플 콘솔에서는 web3를 사용할 수 있다. 사용 가능한 여러가지 function과 object들을 확인할 수 있다.

web3

web3.eth // 자주 사용할 함수
eth.getAccounts() //계정들 확인 가능 ganache에서 받은 주소 10개 확인 가능
Lottery.address // 로터리의 주소 확인 가능. 이 주소는 네트워크에 지정되어 있는 것을 가져옴
Lottery.deployed
Lottery.deployed().then(function(instance){lt=instance})
lt //배포된 로터리 확인 가능
lt.abi //사용 가능한 함수와 파라미터, 리턴 값 등 확인 가능
lt.getSomeValue() //5
  • 우리가 return 5로 설정한 값이 나타난다.

test

const Lottery = artifacts.require("Lottery");

contract('Lottery', function ([deployer, user1, user2]) {
    beforeEach(async () => {
        console.log('before each')

    })

    it('Basic test', async () => {
        console.log('Basic test')
    })
});
  • 파라미터는 현재 우리가 트러플에서 제공받은 계정의 개수인 10개만큼 넣을 수 있다.
  • deployer에 0번째 계정 user1은 1번째 개정, user2는 2번째 개정
  • truffle test test/lottery.test.js 를 실행하면 콘솔이 찍혀나온다.

배포

const Lottery = artifacts.require("Lottery");

contract('Lottery', function ([deployer, user1, user2]) {
    let lottery;
    beforeEach(async () => {
        console.log('before each')
        lottery = await Lottery.new();
    })

    it('Basic test', async () => {
        console.log('Basic test')
        let owner = await lottery.owner();
        let value = await lottery.getSomeValue();

        console.log(`owner ; ${ owner }`);
        console.log(`value: ${value}`);
        assert.equal(value,5)
    })
});
  • 위를 실행하면
  • lottery = await Lottery.new(); 로 항상 새로 배포하는 것이 좋다.


이더리움블록체인트러플 Share Tweet +1