This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
quote.js
71 lines (56 loc) · 2 KB
/
quote.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var Exchange = require('bitcoin-exchange-client');
var PaymentMethod = require('./payment-medium');
var Trade = require('./trade');
var { toSatoshi } = Exchange.Helpers;
var isBTC = (c) => c === 'BTC';
var flipCurrency = (c) => isBTC(c) ? 'USD' : 'BTC';
class Quote extends Exchange.Quote {
constructor (obj, baseCurrency, api, delegate, debug) {
super(api, delegate, Trade, PaymentMethod, debug);
var expiresAt = new Date(obj.expires_on);
var timeOfRequest = new Date(obj.current_time);
var btcAmount = toSatoshi(obj.base_amount);
var usdAmount = obj.quote_amount;
this._id = obj.quote_id;
this._expiresAt = expiresAt;
this._timeOfRequest = timeOfRequest;
this._rate = obj.rate;
this._baseCurrency = baseCurrency.toUpperCase();
this._baseAmount = isBTC(this._baseCurrency) ? btcAmount : parseFloat(usdAmount).toFixed(2);
this._quoteCurrency = flipCurrency(this._baseCurrency);
this._quoteAmount = isBTC(this._quoteCurrency) ? btcAmount : parseFloat(usdAmount).toFixed(2);
this._feeAmount = obj.fee_amount;
this._feeCurrency = obj.fee_currency.toUpperCase();
}
get rate () {
return this._rate;
}
get feeAmount () {
return this._feeAmount;
}
get feeCurrency () {
return this._feeCurrency;
}
static getQuote (api, delegate, amount, baseCurrency, quoteCurrency, debug) {
const processQuote = (quote) => {
let q = new Quote(quote, baseCurrency, api, delegate);
q.debug = debug;
return q;
};
const getQuote = (_baseAmount) => {
let action = _baseAmount > 0 ? 'buy' : 'sell';
return api.POST('quote/', {
action: action,
base_currency: 'btc',
quote_currency: 'usd',
amount: Math.abs(_baseAmount),
amount_currency: baseCurrency.toLowerCase()
}, 'v1', 'quotes');
};
return super
.getQuote(-amount, baseCurrency, quoteCurrency, ['BTC', 'EUR', 'GBP', 'USD', 'DKK'], debug)
.then(getQuote)
.then(processQuote);
}
}
module.exports = Quote;