블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정...
Transcript of 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정...
![Page 1: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/1.jpg)
묶음 강좌명 : 블록체인
14. 솔리디티로 스마트계약 작성14-3. 스마트계약과 가상화폐
블록체인응용과 실습
![Page 2: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/2.jpg)
1. 가상화폐 거래소 REST API 사용법
2. 스마트계약 가상화폐 예외처리
솔리디티를 사용하여 가상화폐와
스마트계약을 연계한다.
![Page 3: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/3.jpg)
1. 가상화폐거래소REST API 사용법
![Page 4: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/4.jpg)
크롬브라우저 앱스토어
Advanced REST Client
출처 박재현, 암호화폐자동거래시스템, 위키북스
![Page 5: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/5.jpg)
Request URL에 https://api.korbit.co.kr/v1/transactions를GET 방식으로 요청
특별히 파라미터 지정하지 않을 경우에는 비트코인(BTC)에 관한정보를 받아 옴
결과 화면에 200 OK가 출력 시 정상 400, 500 등일 경우비정상
Advanced REST Client
![Page 6: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/6.jpg)
Advanced REST Client
• Timestamp 거래체결 시각
• Price : 화폐 1개에 해당하는원화 가격
• Amount : 체결된 거래량
![Page 7: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/7.jpg)
Advanced REST Client
Request URL에https://api.korbit.co.kr/v1/transactions 뒤에?currency_pair=eth_krw요청
파라미터를 eth_krw로 지정하여 이더리움에 관한정보를 받아 옴
![Page 8: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/8.jpg)
Advanced REST Client
![Page 9: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/9.jpg)
Advanced REST Client
Request URL에https://api.korbit.co.kr/v1/ticker/detailed
최총 체결정보를 가져오는 API
Tick은 조회 시점의 최종 체결 가격을 확인할 수 있는 API
Korbit에서는 최종체결가격과 시장현황 상세정보를 제공함
• 여기서는 Detailed Ticker
![Page 10: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/10.jpg)
Advanced REST Client
![Page 11: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/11.jpg)
REST API(시장현황 상세정보) 결과값
필드 설명
timestamp 최종 체결 시각
last 최종 체결 가격
bid 최우선 매수호가(매수주문 중 가장 높은 가격)
ask 최우선 매도호가(매도주문 중 가장 낮은 가격)
low 최근 24시간 동안의 체결가격 중 가장 낮은 가격
high 최근 24시간 동안의 체결가격 중 가장 높은 가격
volume 거래량
![Page 12: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/12.jpg)
URL : https://api.korbit.co.kr/v1/user/balances
방식 : POST
사용자 지갑 잔액조회 API
요청헤더 : “uthorization: Bearer $ACCESS_TOKEN”
필드 설명
available 현재 거래 가능한 화폐의 수량
Trade_in_use 현재 거래 중인 화폐의 수량
Withdrawal_in_use 현재 출금이 진행 중인 화폐의 수량
![Page 13: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/13.jpg)
URL : https://api.korbit.co.kr/v1/user/orders/buy
방식 : POST
가상화폐 매수 주문 API
요청헤더 : “uthorization: Bearer $ACCESS_TOKEN”
필드 설명
Currency_pair 거래할 화폐의 종류를 지정
Type 주문 형태 (limit :지정가, market : 시장가)
price매수할 가격을 원화 기준으로 지정, 지정가 거래에서만유효
Coin_amount매수하고자 하는 코인의 수량, 시장가 주문인 경우에는해당 수량을 시장가에 구매하는 주문을 생성하며, price파라미터와 coin_amount 파라미터는 사용되지 않음
Flat_amount코인 구매하는데 사용하고자 하는 금액(원화)를 지정, 시장가 주문인 경우에만 유효
![Page 14: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/14.jpg)
가상화폐 매수 주문 API 실행결과
필드 설명
orderId 접수된 주문 id
Status Tjㅇ공이면 “sccess”, 실패일 경우 에러 심볼
Currency_pair 해당 주문에 사용된 거래 통화
StatusName_unchecked : 인증되지 않은 사용자가 주문 넣은 경우
Under_age : 19세 미만 사용자가 매수주문한 경우
Not_enough_krw : KRW 잔액이 부족하여 매수주문 넣을 수 없는 경우
Too_many_orders : 사용자당 최대 주문 건수를 초과한 경우
![Page 15: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/15.jpg)
URL : https://api.korbit.co.kr/v1/user/orders/sell
방식 : POST
가상화폐 매도 주문 API
요청헤더 : “uthorization: Bearer $ACCESS_TOKEN”
필드 설명
Currency_pair거래할 화폐의 종류를 지정 btc_krw, eth_krw,etc_krw(Ethereum classic), xrp_krw(ripple),bch_krw(bitcoin cache), btg_krw(bitcoin gold)
Type 주문 형태 (limit :지정가, market : 시장가)
price 매도할 가격을 원화 기준으로 지정, 지정가거래에서만 유효
Coin_amount
매도하고자 하는 코인의 수량, 시장가 주문인경우에는 해당 수량을 시장가에 매도하는 주문을생성하며, price 파라미터와 coin_amount파라미터는 사용되지 않음.
![Page 16: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/16.jpg)
URL : https://api.korbit.co.kr/v1/user/orders/cancel
방식 : POST
가상화폐 매도 주문 API
필드 설명
Currency_pair 거래할 화폐의 종류를 지정
Id 매수, 매도 주문 id
다른 API와 차이점으로, 취소 주문은 여러 주문에 대해서동시에 가능(id=1000&id=1001&id=1002)
필드 설명
OrderID Id 파라미터로 전달한 주문번호
Status Tjd공시 success 실패시 에러 심볼
![Page 17: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/17.jpg)
URL : https://api.korbit.co.kr/v1/user/orders/orders
방식 : GET
가상화폐 주문 상태 확인 API
필드 설명
Currency_pair 거래할 화폐의 종류를 지정
Status 주문 상태로 조회 unfilled(미체결), partially_filled(부분체결), filled(체결)
Id 주문 id 로 조회
offset 전체 데이터 중 offset (기본값은 0) 번째부터데이터를 가져오도록 지정 가능
Limit 전체 데이터 중 limit 개수 만큼만 가져오도록 지정 가능
![Page 18: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/18.jpg)
이벤트 사용 유스 케이스
트랜잭션의 결과값을 확인해야 하는 경우
• 이더리움 트랜잭션은 비동기로 실행됨
• 컨트랙트의 함수에서 값을 반환하더라도 트랜잭션 실행 시점에는확인할 수 없음(함수 반환값을 이벤트에 기록한 후 조회함으로써 반환값 확인 가능)
컨트랙트에 이더 송금 등 트랜잭션 이력을 기록하고 싶을 경우
• 상태변수로도 관리할 수 있지만 상태변수는 스토리지인 관계로 비용소요 높음
• 반면, 거래이력을 이벤트로 기록하면 이벤트는 로그로 기록되기때문에 스토리지에 비해 아주 저렴한 비용으로 이력 저장 및 관리가능
![Page 19: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/19.jpg)
이벤트 사용 유스 케이스
로그
• 32바이트당 96 가스
스토리지
• 32바이트당 20,000 가스
![Page 20: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/20.jpg)
이벤트 사용 유스 케이스
트랜잭션의 결과값을 확인해야 하는 경우
컨트랙트에 이더 송금 등 트랜잭션 이력을 기록하고 싶을 경우
• 상태변수로도 관리할 수 있음
• 상태변수는 스토리지인 관계로 비용 소요 높음
![Page 21: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/21.jpg)
2. 스마트계약가상화폐예외처리
![Page 22: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/22.jpg)
Wallet 호환토큰을 위한 요소
Wallet과 호환되는 토큰을 만들기 위해서는 name, symbol, decimals 속성이 필요함
![Page 23: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/23.jpg)
이벤트
트랜잭션에서 발생한 로그를 기록하는 기능
EVM은 다섯개의 로깅 함수(log0, log1, log2, log3, log4)를 지원함
• 매개변수가 1개이면 log0, 2개이면 log1, …, 5개이면log4 사용함
• 이벤트 선언하면 이벤트 호출 시 매개변수에 맞는 log함수호출해서 로그를 기록함
![Page 24: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/24.jpg)
이벤트
![Page 25: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/25.jpg)
토큰 이름과 심볼이 상수로 선언되어 있어서 변경 가능하도록공개 상태 변수로 선언하고 컨트랙트 생성자 매개변수로 전달받음
• 미스트 월릿(Mist Wallet)이 이를 각각 토큰의 이름과 토큰의심볼로 사용함
일반화된 지갑 토큰
토큰 컨트랙트에 name, symbol 이 선언되어 있을 경우
![Page 26: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/26.jpg)
일반화된 지갑 토큰
![Page 27: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/27.jpg)
가상화폐 관련 예외상황 처리
송신자가 자신이 보유한 금액보다 더 많은 금액 송신하는 경우
• 수신자가 보유한 잔액에 송신 금액을 추가할 때 오버플로 가능
해결방법(안전한 토큰 만들기)
• 송신자가 보유한 토큰이 송신하려는 금액보다 많은지 검사 필요
• 보유한 것보다 적은 경우 송금 못하도록 함
• 오버플로우 발생시 수신자의 잔액에 송신 금액을 합한 것이 원래수신자의 잔액보다 작아짐
• 보유 금액에 송금 금액을 더한 값이 원래 보유 금액보다 작아지는지검사하는 코드가 추가 필요함
![Page 28: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/28.jpg)
가상화폐 관련 예외상황 처리
컨트랙트에서 상태변수 변경 전에 조건을 먼저 체크하면 됨
안전하지는 않음
![Page 29: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/29.jpg)
revert()를 통한 개선
호출시 예외가 발생하면서 전체 트랜잭션이 실패
트랜잭션에서 발생한 변경 내용이 트랜잭션 실행 이전 상태로 되돌아 감
If 에서 정한조건이 되면revert()
![Page 30: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/30.jpg)
Require()
• 조건문 사용하지 않고도 유사하게 작동이 가능함
• 매개변수로 전달된 조건식을 검사하고 조건이 참이 아닌 경우예외가 발생함
• require 는 입력값, 컨트랙트 상태변수, 다른 함수의 반환값이유효한지 체크하기 위한 목적으로 사용함
출처 조수현, 이더리움베이직, 북스타
revert()를 통한 개선
Assert()
• require와 마찬가지로 조건식이 거짓인 경우 예외를 발생함
• 하지만 assert 함수는 내부적인 에러를 테스트하기 위한목적으로 사용함
![Page 31: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/31.jpg)
require()와 assert()
![Page 32: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/32.jpg)
![Page 33: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/33.jpg)
지금까지살펴본중요내용을토대로우리가실습시주의해야할사항은무엇일까요?
Summary
![Page 34: 블록체인응용과실습 · 2019. 8. 21. · Currency_pair 거래할화폐의종류를지정 Type 주문형태(limit :지정가, market : 시장가) price 매수할가격을원화기준으로지정,](https://reader033.fdocuments.net/reader033/viewer/2022053022/604c11f39398461edb09eba8/html5/thumbnails/34.jpg)
교수님의견
Summary
존재증명(proof of existence)• 개념 및 필요성
• 비트코인과 이더리움 플랫폼에서 존재증명 동작
가상화폐 연계를 위한 REST API, OAuth2 가상화폐 거래소 REST API 사용법
지금까지살펴본중요내용을토대로우리가실습시주의해야할사항은무엇일까요?