세션5. web3.js와 node.js 를 사용한 dapp 개발

24
web3.jsnode.js를 사용한 dApp 개발 김재욱 [email protected]

Upload: jay-jh-park

Post on 22-Jan-2018

3.704 views

Category:

Software


6 download

TRANSCRIPT

Page 1: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

web3.js와 node.js를사용한dApp개발

김재욱[email protected]

Page 2: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

목차

• web3.js, truffle

• dApp 구조및구현과정

• 주요함수소개

• 시연

Page 3: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

web3.js & truffle

• Ethereum Compatible JavaScript API• Ethereum의 JavaScript API 이다.내부적으로는 JSON RPC 를통해 eth / geth 와통신한다.• Geth/Eth 의 Javascript Console이나브라우져, Node.js runtime 에서사용할 수있는 API이다.• Web3.js는 abi(Application Binary Interface)로 EVM과통신한다.• truffle은 solidity로 작성된 Smart Contract 소스 코드를 컴파일하고 테스트하고 이더리움 네트워크에

배포할수 있도록하는 Contract 개발 framework이다.

Page 4: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dAPP 구조

Page 5: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dAPP 구현 과정

Smart Contract 설계 Smart Contract 구현 Smart Contract Compile

dApp구조설계 dApp구현

Contract call 설계

기타 packages send transaction

get transaction

event watch

contract addressabi

Contract, web3 연동

Page 6: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

Set Get

블록체인에데이터를기록하는 것 블록체인의데이터를가져오는 것

Transaction을보낸다. Transaction발생 X

Value(eth), Gas 필요 Value, Gas 필요하지않다.

Hex string 값으로저장한다. Hex string 값을읽어온다.

Transaction이Mining되었다는callback함수를지원하지않는다.

get함수를 호출하면 callback함수로데이터를받아올수 있다.

전송한 Transaction이 Mining되었나를확인하는 코드작성필요한다.

callback함수로데이터를바로 받아온다.

복잡하거나큰데이터 등을저장하기위해서는 swarm과 같은 p2p 스토리지를사용하는 것이좋다.

Contract call 함수의두종류

Page 7: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

abi로 Contract와 web3 연동

1. deploy할Contract import2. contract compile

truffle compile3. contract deploy

truffle deploy

4. abi를포함한 json파일 자동생성5. json 파일에서 Contract의 address 확인

6. json 파일 import후 abi필드 추출7. web3 함수 호출

2,3. truffle migrate

Solidity web3

Page 8: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

abi로 Contract와 web3 연동

deploy할 contract sol 파일을 import 하고 deploy한다.deploy함수의두번째 인자부터는 Contract의생성자이다.

Page 9: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

abi로 Contract와 web3 연동

truffle compile 수행 후 sol과json 파일 1:1 생성 후truffle deploy

deploy한 Contract의 json 파일마다 networks의 address필드생성. 이 address가 contract의 주소이다.

Page 10: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

abi로 Contract와 web3 연동

truffle compile에서생성된 json 파일을불러오고 abi필드추출

abi 필드와 contract address 주소로 contract를 호출한다.

Page 11: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

send transaction

Contract set call send transaction to EVM

web3 Contract

function call

interface

transaction Id

is mining done?

return transaction data

Page 12: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

send transaction

함수명 web3.eth.sendTransaction(transactionObject [, callback])

주요파라미터

from 보내는 wallet의주소

to 받는 wallet의주소

value 보낼이더의 양(hex)

gas 지불할가스의양(hex)

리턴 32Byte의 hex값, Transaction의 Id

Page 13: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

send transaction

트랜잭션이정상적으로기록됐는지확인하기 위해서 res로받은 Transaction Id로getTransaction 함수를 호출한다.

Page 14: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

get transaction

함수명 web3.eth.getTransaction(transactionId [, callback])

주요파라미터transactionId sendTransaction으로받은 Transaction Id

리턴 트랜잭션의정보를담은 객체

Page 15: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

get transaction

•getTransaction의결과가 null 이면해당트랜잭션이 mining이되지않다는뜻이다.

•로직상 mining이되고 진행되어야할부분이있으면 timer를 실행하여주기적으로트랜잭션이mining이되었는지확인해야한다.

•따라서 getTransaction 함수를 x초단위로 호출하여 null이아니면다음로직이 실행되도록코딩한다.

Page 16: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

get transaction

트랜잭션이정상적으로기록됐는지확인하기 위해서 res로받은 Transaction Id로getTransaction 함수를 호출한다. 마이닝이 됐으면 getTransaction결과는 null이아니다.

Page 17: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

get transaction receipt

함수명 web3.eth.getTransactionReceipt(hashString [, callback])

주요파라미터transactionId sendTransaction으로받은 Transaction Id

리턴 트랜잭션의 Receipt 정보를담은객체

Page 18: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

get transaction receipt

Page 19: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

get transaction receipt

•Contract에서 sendTransaction의결과를콜백함수로 받을수있는방법은없다.

•getTransactionReceipt에서 logs의 data 필드는 setTransaction의결과값이 저장이된다.

•data 필드를 파싱하면 sendTransaction의결과값을얻어올 수있다.

Page 20: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

get transaction receipt

•Transfer를호출한 결과가 logs에기록된다.•msg.sender => 00000000000000000000000064e45fa2c1d5c55f48790cad0ae036f073a97353•_to => 000000000000000000000000517fb91985925bf8e3c3da3826fe733b467bc953•value => 00000000000000000000000000000000000000000000000000000000000001f4

data: '0x00000000000000000000000064e45fa2c1d5c55f48790cad0ae036f073a97353000000000000000000000000517fb91985925bf8e3c3da3826fe733b467bc95300000000000000000000000000000000000000000000000000000000000001f4',

Page 21: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

Event Filter

•Contract에서 event 형으로선언한함수가호출되면 EVM에서Event가 발생한다.•event형으로 선언한함수는전부호출을 감지할수있다. 이를콜백처럼활용 가능하다.

Page 22: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

•세션4의 CrowdSale 예제를 node.js + truffle + solidity로구현한 dApp을시연

• node.js : v8.9.0• npm web3.js : 0.18.0• truffle : 4.0.1• solidity 0.4.16• os : mac os• blockchain : ethereumjs/testrpc

Page 23: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

dApp 구현

Conclusion

• HTTP Rest API를설계할때처럼 Solidity – web3를잘설계하는게중요하다.

• 저장할 수있는 hex string 문자열이제한되어있으므로저장할데이터를적절하게설계한다.

• 여러 정보가들어있는긴 hex string이저장될수있으므로 길이를잘계산해서파싱해야한다.

• 문서화는 필수! Solidity의파라미터타입, 리턴타입, 리턴문자열 종류등을잘명시해야한다. JSON과같은형태로리턴할수 없으므로 HTTP Rest API설계문서 작성할때보다더 꼼꼼하고정확한문서작성이 필요하다.

• 테스트코드를작성하자.

Page 24: 세션5. web3.js와 Node.js 를 사용한 dApp 개발

Q&A