UNPKG

1.97 kBPlain TextView Raw
1/**
2 * @hidden
3 */
4
5/**
6 */
7//
8// Markets Object
9// BitGo accessor to Bitcoin market data.
10//
11// Copyright 2015, BitGo, Inc. All Rights Reserved.
12//
13
14import * as Bluebird from 'bluebird';
15
16import { common } from '@bitgo/sdk-core';
17
18//
19// Constructor
20//
21const Markets = function (bitgo) {
22 this.bitgo = bitgo;
23};
24
25/**
26 * Get the latest bitcoin price data
27 * @param params {}
28 * @param callback
29 * @returns {*} an object containing price and volume data from the
30 * current day in a number of currencies
31 **/
32Markets.prototype.latest = function (params, callback) {
33 params = params || {};
34 common.validateParams(params, [], [], callback);
35
36 return Bluebird.resolve(
37 this.bitgo.get(this.bitgo.url('/market/latest')).result()
38 ).nodeify(callback);
39};
40
41/**
42 * Get yesterday's bitcoin price data
43 * @param params {}
44 * @param callback
45 * @returns {*} an object containing price and volume data from the
46 * previous day in a number of currencies
47 */
48Markets.prototype.yesterday = function (params, callback) {
49 params = params || {};
50 common.validateParams(params, [], [], callback);
51
52 return Bluebird.resolve(
53 this.bitgo.get(this.bitgo.url('/market/yesterday')).result()
54 ).nodeify(callback);
55};
56
57/**
58 * Get bitcoin price data from up to 90 days prior to today
59 * @param params { currencyName: the code for the desired currency, for example USD }
60 * @param callback
61 * @returns {*} an object containing average prices from a number of previous days
62 */
63Markets.prototype.lastDays = function (params, callback) {
64 params = params || {};
65 common.validateParams(params, ['currencyName'], [], callback);
66
67 const days = !isNaN(parseInt(params.days, 10)) ? parseInt(params.days, 10) : 90;
68
69 if (days && days < 0) {
70 throw new Error('must use a non-negative number of days');
71 }
72
73 return Bluebird.resolve(
74 this.bitgo.get(this.bitgo.url('/market/last/' + days + '/' + params.currencyName)).result()
75 ).nodeify(callback);
76};
77
78module.exports = Markets;