이더리움 맛보기 #2

오상택
8 min readFeb 7, 2019

--

학습목표

  1. 메타마스크를 이용한 이더리움 스마트 컨트랙트 사용
  2. 직접 메타마스크와 같은 지갑 서비스 구축

목차

  1. 이더리움 맛보기#1 복습
  2. 스마트 컨트랙트에서 선언한 각종 기능 이용하기
  3. Web3.js를 이용한 스마트컨트랙트 이용
  4. 이더리움 코어 Geth 기동
  5. 풀노드 구축
  6. HTTP로 Geth 제어 하기
  7. PHP로 Geth 제어 하기
  8. ICO? IEO? STO?

1. 이더리움 맛보기 #1 복습

https://medium.com/@ost5253/%EC%9D%B4%EB%8D%94%EB%A6%AC%EC%9B%80-%EB%A7%9B%EB%B3%B4%EA%B8%B0-1-f43644ba4217

2. 스마트 컨트랙트에서 선언한 각종 기능 이용하기

코드 Verify 가 안돼 있음

위와 같이 transfer이라는 기능을 내 홈페이지에 가져오려면?

  • Web3.js 를 이용한다

3. Web3.js를 이용한 스마트컨트랙트 이용

Javascript 코드로 메타마스크 제어 가능

Javascript로 smart contract 실행 코드를 짜고 metamask로 실행

위 transfer이라는 기능을 실행하기 위한 파라미터

  • 컨트랙트 주소(0x123…)
  • abi
  • To 주소(_to)
  • Amount 값(_value)

ABI란?

  • Application Binary Interface
  • 코드 Verify를 했다면 Etherscan 에서 가져올 수 있음

Web3.js 를 이용한 코드

export function transfer(contract_addr, abi, _to, _value) {        var balance = new BigNumber(_value * Math.pow(10, 18));        var web3 = new Web3(window.web3.currentProvider);        var MyContract = new web3.eth.Contract(abi);        MyContract._address = contract_addr;        MyContract.methods.transfer(_to, balance.toString(10))
.send({
from: accountAddress,
gasPrice: '5000000000',
})
.on('transactionHash', (transactionHash) => {
console.log(transactionHash);
});
}

Cryptomus 서비스에 실제 적용한 예제

https://cryptomus.io/krw

4. 이더리움 코어 Geth 기동

Geth 설치

Geth Run

geth --rpc --rpcapi "admin,db,eth,debug,miner,net,shh,txpool,personal,web3"geth attach http://localhost:8545

Geth 여러가지 명령어들

eth 
personal
web3
...

Geth Run 체크

eth.syncing>> {
currentBlock: 64519,
highestBlock: 7186927,
knownStates: 68300,
pulledStates: 58659,
startingBlock: 0
}
eth.blockNumber>> 1234

계정 생성

personal.newAccount("password")>> "0xad23288757a8bb7489e9c5e0b9a194c6d9a13ab5"

잔액확인

eth.getBalance(eth.accounts[0])>> 0

Transaction 올리기

personal.sendTransaction({
{
"from" : eth.accounts[0], "value" : web3.toWei(10,"ether"),
"to" : eth.accounts[1], ...
},
$password})

5. 풀노드 구축

Requirements

https://medium.com/coinmonks/analyzing-the-hardware-requirements-to-be-an-ethereum-full-validated-node-dc064f167902

geth --datadir=/home/ubuntu  —-cache=1024 —-maxpeers 1000 —-syncmode fast —-rpc —-rpcaddr=0.0.0.0 —-rpcapi “admin,db,eth,debug,miner,net,shh,txpool,personal,web3”--rpccorsdomain “*” console

6. HTTP로 Geth 제어 하기

curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBalance", "params":["0xad23288757a8bb7489e9c5e0b9a194c6d9a13ab5", "latest"], "id":10}' http://127.0.0.1:8545>> {"jsonrpc":"2.0","id":10,"result":"0x0"}

7. PHP로 Geth 제어 하기

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
public function balanceOf($address)
{
$client = new Client(); $data = json_encode(array(
"jsonrpc" => '2.0',
"method" => 'eth_getBalance',
"params" => ["{$address}", "latest"],
"id" => 10
));
$request = new Request('post', http://127.0.0.1:8545, [
'Content-Type' => 'application/json',
], $data);
$response = $client->send($request); $result = $response->getBody()->getContents();
$result = json_decode($result);
return hexdec($result->result) / pow(10, $this->currency->decimals);
}

메타마스크를 이용하지 않는 이더리움 지갑 플랫폼

8. ICO? IEO? STO?

https://m.post.naver.com/viewer/postView.nhn?volumeNo=17103672&memberNo=43874987

--

--