-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorderUtil.js
48 lines (42 loc) · 1.63 KB
/
orderUtil.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
const BigNumber = require('bignumber.js');
const sprintf=require('sprintf-js').sprintf;
const exchangeRateToPrice = (order) => {
if (order.specification.direction === 'buy') {
return BigNumber(1).div(BigNumber(order.properties.makerExchangeRate));
}
return BigNumber(order.properties.makerExchangeRate);
}
const formatAmount = (amount) => {
if (amount.currency === 'XRP') {
return sprintf("%s %s", formatValue(amount.value, amount.currency), amount.currency)
}
return sprintf("%s %s/%s", formatValue(amount.value, amount.currency), amount.currency, amount.counterparty)
}
const formatOrder = (order) => {
return sprintf('sequence: %5s price: %s quantity: %s totalPrice: %s',
order.properties.sequence,
exchangeRateToPrice(order).toFixed(5),
formatAmount(order.specification.quantity),
formatAmount(order.specification.totalPrice))
}
const formatValue = (bn, currency) => {
bn = typeof bn.toFixed === 'function' ? bn : BigNumber(bn);
rv = currency === 'XRP' ? bn.toFixed(6) : bn.toPrecision(16);
// Remove trailing 0.
// See https://github.com/ripple/ripple-lib/pull/1026#issuecomment-534242547
return rv.replace(new RegExp('0+$'),'').replace(new RegExp('\\.$'), '')
}
const formatNewOrder = (order) => {
return sprintf('price: %s quantity: %s totalPrice: %s',
BigNumber(order.totalPrice.value).div(
BigNumber(order.quantity.value)).toFixed(5),
formatAmount(order.quantity),
formatAmount(order.totalPrice))
}
module.exports = {
formatOrder: formatOrder,
formatNewOrder: formatNewOrder,
exchangeRateToPrice: exchangeRateToPrice,
formatValue: formatValue,
formatAmount: formatAmount,
}