본문 바로가기
프로그래밍/node.js

Node.js로 간단하게 업비트 코인시세 받아오기 - 1

by dryadyou 2021. 9. 22.
반응형

Node.js 간단 시리즈 제1편 업비트 코인 시세 받아오기입니다.

Node.js + Typescript 개발환경에서 진행을 하였으며 tsconfig.json에서 target 속성은 es6 이상으로 하면

async, await에 대한 오류를 해결할 수 있습니다.

저는 yarn을 사용할 것이기 때문에 yarn을 사용하여 packagen.json을 생성해 줍니다.

$ yarn init -y

그리고 타입스크립트를 사용하기 위하여 아래 명령어로 tsconfig.json을 생성합니다.

아래 명령어를 사용하기 위해서는 전역에 typescript가 설치되어있어야 합니다.

만약 설치되어있지 않다면 "yarn global add typescript" 설치해주세요

$ tsc --init

tsconfig.json파일의 속성을 아래와 같이 변경합니다.

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": true,
    "outDir": "./dist", 
    "removeComments": true,
    "strict": true,
    "noImplicitAny": true,
    "moduleResolution": "node", 
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

typescript를 바로 실행해볼 수 있는 ts-node와 typescript도 같이 설치해줍니다. 개발할 때만 필요하기 때문에  -D옵션을 사용하여 devDendencies에 설치합니다.

$yarn add -D ts-node typescript

업비트 API를 Node.js에서 사용할 수 있도록 패키지로 만들어놓았기 때문에 해당 패키지를 설치합니다.

$ yarn add node-upbit

여기까지 설정이 되었다면 이미 90% 이상 완성한 것입니다.

저의 프로젝트 폴더구조입니다. 아주 간단하게 src 폴더에 index.ts 파일을 생성하였습니다.

package.json파일의 설정입니다

node-upbit, ts-node 패키지를 설치했으며, Typescript를 바로 실행하기 위하여 scripts의 dev속성에 ts-node src/index.ts 라고 작성하였습니다.

작성한 코드를 실행하려면 yarn dev라고 터미널에 입력하면 됩니다.

위에서 설치한 node-upbit패키지를 살펴보면 ExchangeService, QuoationService, UtilsService 이렇게 3개의 class를 가지고 있습니다.

ExchangeService는 업비트 access_key, secret_key를 발급받아야 사용할 수 있으며
QuoationService는 별도의 key 없이 사용할 수 있습니다.

저희는 코인의 시세를 가져올 것이기 때문에 key가 필요 없는 QuoationService를 사용하겠습니다.

import { QuoationService } from "node-upbit";	// 1. node-upbit 패키지 import
const quoationService = new QuoationService();	// 2. quoationService 객체생성

getMarketAllInfo: upbit 마켓에 거래되고 있는 코인 정보를 조회합니다.

(async () => {
  // 마켓 코드 조회
  const res = await quoationService.getMarketAllInfo();
})();

마켓코인코드

getMinutesCandles: 분 캔들 조회

(async () => {
	// 분 캔들 조회
    const res2 = await quoationService.getMinutesCandles({
      minutes: "60",	// 기준봉
      marketCoin: "KRW-BTC", // 코인 영문명
      count: 10,	// 가져올 캔들갯수
    });
})();

분캔들

getDayCandles: 일 캔들 조회

(async () => {
  // 일 캔들 조회
  const res2 = await quoationService.getDayCandles({
    marketCoin: "KRW-BTC",
    count: 10,
  });
  console.log(res2);
})();

일캔들

getTicker: 현재가 정보 조회

(async () => {
  // 현재가 정보 조회
  const res2 = await quoationService.getTicker(["KRW-BTC"]);
  console.log(res2);
})();

현재가 정보

getOrderbook: 호가 정보 조회

(async () => {
  // 호가 정보 조회
  const res2 = await quoationService.getOrderbook(["KRW-BTC"]);
  console.log(res2);
})();

호가정보

orderbook_units속성 

orderbook_units

 

반응형

댓글