학습목표
- 메타마스크를 이용한 이더리움 스마트 컨트랙트 사용
- 직접 메타마스크와 같은 지갑 서비스 구축
목차
- 이더리움 맛보기#1 복습
- 스마트 컨트랙트에서 선언한 각종 기능 이용하기
- Web3.js를 이용한 스마트컨트랙트 이용
- 이더리움 코어 Geth 기동
- 풀노드 구축
- HTTP로 Geth 제어 하기
- PHP로 Geth 제어 하기
- ICO? IEO? STO?
1. 이더리움 맛보기 #1 복습
2. 스마트 컨트랙트에서 선언한 각종 기능 이용하기
- Smart Contract 의 각종 기능을 이용하려면
여러 방법이 있음
대표적으로 Etherscan이라는 이더리움 탐색기 서비스에서 이용하기 - (예제1) 이더리움 맛보기#1 때 발행했던 MPUG Token
https://ropsten.etherscan.io/address/0x8e671bd9e875f26211dc593845a1125ec68e3161#code
코드 Verify 가 안돼 있음
- (예제2)Verify 된 스마트 컨트랙트 Cryptomus#1
https://etherscan.io/address/0x6ad82c945a434edf13602949bb56c82a1a75f3c5#writeContract
위와 같이 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 서비스에 실제 적용한 예제
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
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