간단한 Solidity Dapp 개발 배포

in dapp •  6 years ago 

[이전 포스팅 참고]

간단한 solidity 프로그램을 작성하고, 배포 까지 해보자

1 - 간단한 solidity 프로그램 소스 작성

pragma solidity  ^0.4.8;

contract SimpleStorage{
    uint storedData;

    function SimpleStorage(uint x){
        storedData = x;
    }

    function set (uint x){
       storedData = x;
    }

    function get() constant returns (uint){
        return storedData;
    }
}

2 - 계약프로그램 빌드용 Data부분 출력
$ solc -o ./ --bin --optimize SimpleStorage.sol

  • -0 : 출력할곳 지정
  • --bin : 출력 결과를 16진수로 표시
  • --optimize : 최적화 수행 옵션

[ 다음과 같이 Warning 보인다.]

3 - 다음과 같이 소스를 수정해보자

pragma solidity  ^0.4.8;

contract SimpleStorage{
    uint storedData;

    constructor (uint x) public{
        storedData = x;
    }

    function set (uint x) public{
       storedData = x;
    }

    function get() public constant returns (uint){
        return storedData;
    }
}

[소스에 대한 간략한 설명]

  • pragma : 컴파일러 버전을 지정, 반드시 선언해야함
  • contract : 계약을 선언 자바의 클래스와 비슷함
  • constructor : 계약 생성자

4-Contract 프로그램 빌드용 Data 부분 출력
$cat SimpleStorage.bin

5-계약 ABI(Application Binary Interface) 정보를 확인한다.

  • ABI : 말그대로 프로그램(계약)의 인터페이스 정의서

$solc --abi SimpleStorage.sol

======= SimpleStorage.sol:SimpleStorage =======
Contract JSON ABI 
[{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable"
,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutabil
ity":"view","type":"function"},{"inputs":[{"name":"x","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"cons
tructor"}]

6- Geth 기동
$geth --networkid 4649 --nodiscover --maxpeers 0 --datadir /home/devno3kaul/data_testnet console 2>> /home/devno3kaul/data_testnet/geth.log

7- 계약 등록자 계정 잠금 해제
> personal.unlockAccount(eth.accounts[0], "pass0", 0)

8-계약을 블록체인에 등록한다.

8-1 5번의 ABI정보를 사용한다.
>simplestorageContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"x","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}])

8-2 4번에서 출력한 data 정보를 사용한다. 16진수 표기를 위해 0x 를 붙인다.
>simplestorage = simplestorageContract.new({from: eth.accounts[0], data:'0x608060405234801561001057600080fd5b506040516020806100f2833981016040525160005560bf806100336000396000f30060806040526004361060485763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633bc5de308114604d5780635b4b73a9146071575b600080fd5b348015605857600080fd5b50605f6088565b60408051918252519081900360200190f35b348015607c57600080fd5b506086600435608e565b005b60005490565b6000555600a165627a7a723058207e61ade266b66887700143c38e99ebb466623be5599eb527f78129882deac76c0029', gas:3000000})

위에서 address 부분이 아직 undefined 이다.

9- 채굴을 시작한다.
>miner.start(1)

10- simplestorage 를 다시 확인해본다.-계약이 블록체인에 등록된것을 확인한다.
>simplestorage

address에 값이 생긴것을 확인한다.

11- 계약에 접근하기 위한 변수를 생성
> simpleObj = eth.contract(simplestorage.abi).at(simplestorage.address)

12- 블록체인에 등록된 계약에 function을 호출해본다 (값을 11로 호출함)
> simpleObj.set.sendTransaction(0, {from:eth.accounts[0]})
"0xe56059b67a9e75a44b6b8b29ddfe38e05c9c2d0171b9ce04a27b7b7fe88a8fb1"

13- 위 트랜젝션이 처리됐는지 확인한다.
>eth.getTransaction("0xe56059b67a9e75a44b6b8b29ddfe38e05c9c2d0171b9ce04a27b7b7fe88a8fb1")
{
blockHash: "0x7f226c020e0566f87ecbd4eb753425aa5a6eee5a0870aeac279f27b1d17b5601",
blockNumber: 341,
from: "0xead06f7ce504d82837e3241394f5498cfa3df00b",
gas: 90000,
gasPrice: 18000000000,
hash: "0xe56059b67a9e75a44b6b8b29ddfe38e05c9c2d0171b9ce04a27b7b7fe88a8fb1",
input: "0x60fe47b1000000000000000000000000000000000000000000000000000000000000000b",
nonce: 10,
r: "0x436684453ce9115f35735c8717bc5c93567c7d508cc61667087a86295fc9fca2",
s: "0x1d30d2c59502f1598162493bd97adaa65023c6035ca5833010b2b0b646e8bb26",
to: "0xec9071dd0941103058587136ffa5905561f3ff11",
transactionIndex: 0,
v: "0x66",
value: 0
}

14- get을 호출해본다.
>simpleObj.get.call({from:eth.accounts[0]})

간단하게 Solidity코드로 Dapp을 개발 배포 까지 해봄

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!