UNPKG

zaif

Version:

Zaif API wrapper for Node.js

164 lines (137 loc) 3.93 kB
'use strict'; import crypto from 'crypto'; import rp from 'request-promise'; import qs from 'qs'; import { Observable, AsyncSubject } from 'rx'; import { client as WebSocketClient } from 'websocket'; import config from './config'; export default class Zaif { constructor(key, secret) { this.key = key; this.secret = secret; } _createNonce () { return Math.floor(new Date().getTime() / 1000); } _createSignature(canonicalString) { return crypto.createHmac('sha512', this.secret).update(canonicalString).digest('hex'); } _createHeader(params) { const canonicalString = qs.stringify(params); const signature = this._createSignature(canonicalString); return { 'Content-Type': 'application/x-www-form-urlencoded', Key: this.key, Sign: signature }; } _privateRequest(method, params = {}) { if (this.key !== undefined && this.secret !== undefined) { params.method = method; params.nonce = this._createNonce(); return rp({ headers: this._createHeader(params), url: config.baseUrl.private, method: 'POST', form: params, json: true }); } else { throw new Error('Private API requires secret token and key.'); } } _publicRequest(path) { return rp({ url: `${config.baseUrl.public}${path}`, method: 'GET', json: true }); } lastPrice(currencyPair) { return this._publicRequest(`/last_price/${currencyPair}`); } ticker(currencyPair) { return this._publicRequest(`/ticker/${currencyPair}`); } trades(currencyPair) { return this._publicRequest(`/trades/${currencyPair}`); } depth(currencyPair) { return this._publicRequest(`/depth/${currencyPair}`); } getInfo() { return this._privateRequest('get_info'); } getInfo2() { return this._privateRequest('get_info2'); } getPersonalInfo() { return this._privateRequest('get_personal_info'); } tradeHistory(params) { return this._privateRequest('trade_history', params); } activeOrders(currencyPair=null) { return this._privateRequest('active_orders', { currency_pair: currencyPair }); } trade(currencyPair, action, price, amount, limit=null) { return this._privateRequest('trade', { currency_pair: currencyPair, action: action, price: price, amount: amount, limit: limit }); } cancelOrder(orderId) { return this._privateRequest('cancel_order', { order_id: orderId }); } withdraw(currency, address, amount, params={}) { return this._privateRequest('withdraw', Object.assign({ currency: currency, address: address, amount: amount }, params)); } dopositHistory(currency, params={}) { params.currency = currency; return this._privateRequest('depositHistory', params); } withdrawHistory(currency, params={}) { params.currency = currency; return this._privateRequest('withdrawHistory', params); } stream(currencyPair ) { const subject = new AsyncSubject(); const client = new WebSocketClient(); client.connect(`${config.baseUrl.stream}?currency_pair=${currencyPair}`); client.on('connectFailed', error => { subject.onError(error); }); client.on('connect', connection => { console.log('WebSocket Client Connected'); const receiver = Observable.create(observer => { connection.on('error', error => { observer.onError(error); }); connection.on('close', () => { observer.onCompleted(); }); connection.on('message', msg => { if (msg.type === 'utf8') { observer.onNext(msg.utf8Data); } }); }); subject.onNext(receiver); subject.onCompleted(); }); return subject.asObservable().selectMany(receiver => { return receiver; }); } }