스마트 컨트랙트(Smart Contract)

2021. 10. 11. 18:20Blockchain/Solidity

스마트 컨트랙트 실습 과정

- 솔리디티 코드 작성

- 솔리디티 코드를 RPC 통신을 통해 실행

- 솔리디티를 실행을 위해 컴파일 // solc

- .sol 파일을 컴파일하면 .abi 파일과 .bin 파일 자동 생성

 

* .abi : Application Binary Interface, 런타임 시 바이너리 코드와 데이터를 실행시키기 위한 json 파일

* .bin : Binary File, 결과물을 출력


실습을 위환 환경 설정

※ NodeJS 환경에서 세팅

 

  • 설치 목록
  1. 트러플(Truffle)
  2. 가나슈(Ganache)
  3. 솔리디티 컴파일러(solc)

 

  • 설치 진행
  1. npm init
  2. npm install -g truffle
  3. npm install -g solc
  4. npm install -g ganache-cli
  5. npm install web3
  6. truffle version // 설치 확인
    solcjs --version // 설치 확인
    ganache-cli --host 0.0.0.0 // 설치 확인 + 실행
    solcjs --abi --bin [파일명] // 컴파일러 실행 방법

GAS

- GAS

* 이더리움 스마트 컨트랙트를 배포하고 실행 할 때 발생하는 수수료를 책정하기 위해 만든 단위

 

- GAS Price

* 기가웨이(Gwei)라는 이더리움의 작은 값의 단위

 

- GAS Limit

* 최대 수수료, 작업량 예상치


RPC 통신 실습 순서1 (curl)

※ WSL 환경에서 실습

 

  • eth_accounts

- 터미널 입력

curl -X POST -d '{"jsonrpc":"2.0","method":"eth_accounts"}' http://127.0.0.1:8545

- 결과물

{"jsonrpc":"2.0","result":[

"0x0432719878827fc46115a304a64766126ce8529b","0x42a7fdf833963b82b78019f33bfb558650786a3d","0xd0bbd2569ef71224561eddcfcce156456c6a8df3","0xad85a3f8796618dbcad4f5f8695d9a7d73910dbf","0xdb41b4747138650a93ea069a2fd8af44f8f11fa4","0x9300eb7255ba219341b973e1ae4cc41facae53df","0xbf95837692d21dbd207248e752f0deec6bd79c82","0x75222a56f6677dd7086a1aaf660ae4820fa123f0","0xa53b44325192c2ec63c8a11d40c09d5dd4751900","0xd6b8c36b5d8625df7e2426be07218d10d6b71591"]}

 

  • eth_getBalance

-터미널 입력

curl -X POST -d '{"jsonrpc":"2.0","method":"eth_getBalance","params":[

"0x0432719878827Fc46115A304A64766126CE8529b"]}' http://127.0.0.1:8545

- 결과물

{"jsonrpc":"2.0","result":"0x56bc75e2d63100000"}


RPC 통신 실습 순서2 (web3)

const Web3 = require('web3');
let connection = new Web3('http://127.0.0.1:8545');

// eth_acctouns
console.log(connection);
connection.eth.getAccounts()
.then(data => {
    console.log(data); // 가나슈 실행 시 Available Accounts와 같은 결과물
})

// eth_getBalance
connection.eth.getBalance('0x0432719878827Fc46115A304A64766126CE8529b')
.then(data => {
    console.log(data); // 16진수 return이 아닌, 10진수 return (web3의 사용자 편의성)
})

const ABI_CODE = JSON.parse('[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"get","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}]');
const BYTECODE = '608060405234801561001057600080fd5b506040518060400160405280600b81526020017f68656c6c6f20776f726c640000000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610166565b82805461006e90610134565b90600052602060002090601f01602090048101928261009057600085556100d7565b82601f106100a957805160ff19168380011785556100d7565b828001600101855582156100d7579182015b828111156100d65782518255916020019190600101906100bb565b5b5090506100e491906100e8565b5090565b5b808211156101015760008160009055506001016100e9565b5090565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061014c57607f821691505b602082108114156101605761015f610105565b5b50919050565b610232806101756000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80636d4ce63c14610030575b600080fd5b61003861004e565b6040516100459190610179565b60405180910390f35b60606000805461005d906101ca565b80601f0160208091040260200160405190810160405280929190818152602001828054610089906101ca565b80156100d65780601f106100ab576101008083540402835291602001916100d6565b820191906000526020600020905b8154815290600101906020018083116100b957829003601f168201915b5050505050905090565b600081519050919050565b600082825260208201905092915050565b60005b8381101561011a5780820151818401526020810190506100ff565b83811115610129576000848401525b50505050565b6000601f19601f8301169050919050565b600061014b826100e0565b61015581856100eb565b93506101658185602086016100fc565b61016e8161012f565b840191505092915050565b600060208201905081810360008301526101938184610140565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101e257607f821691505b602082108114156101f6576101f561019b565b5b5091905056fea2646970667358221220755467b7e26d76459be7b8c248491115b3f0ab13b7266a0c65b17e0b7146473064736f6c63430008090033';

const contract = new connection.eth.Contract(ABI_CODE); // ABI_CODE : json 형태

// 배포(코드 실행), deploy
// 코드 실행하니 GAS 발생
contract.deploy({
    data: BYTECODE,
})
.send({
    from: '0x0432719878827Fc46115A304A64766126CE8529b',
    gas: '6721975',
}, (error, result) => {
    console.log(error);
})
.then(data => {
    /*
        data.options.address
        결과물에 대한 key값
        0xC66E2930E839822b6A010E818A45464d315b0E95
    */
    console.log(data.options.address);
    // return data.methods.get().call(); // get이라는 method를 call(실행), 객체 반환
})
// .then(result => {
//     console.log());
// })

const helloContract = new connection.eth.Contract(ABI_CODE, '0xC66E2930E839822b6A010E818A45464d315b0E95');

helloContract.methods.get().call()
.then(data => {
    console.log(data);
})

 

pragma solidity ^0.8.0;

contract hello {
    string value;
    constructor() {
        value = "hello world";
    }

    // 파일 시스템, 파일에 저장 된 내용을 가져올지 >> storage
    // 메모리에 저장 된 내용을 가져올지 >> memory
    function get() public view returns(string memory) {
        return value;
    }
    // 실행 >> solcjs --bin --abi .\hello.sol
    // 결과 >> [파일명]_[확장자]_[컨트랙트명]
}

'Blockchain > Solidity' 카테고리의 다른 글

투표 DApp 코딩 실습, 메타마스크(MetaMask) 기초 설명  (1) 2021.10.12
솔리디티(Solidity)  (0) 2021.10.08