forked from slickage/baron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
109 lines (101 loc) · 3.58 KB
/
helper.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var BigNumber = require('bignumber.js');
var invoiceWebhooks = require(__dirname + '/invoicewebhooks');
// returns decimal places of provided
var decimalPlaces = function(number) {
if(Math.floor(number) === number) {
return 0;
}
return number.toString().split('.')[1].length || 0;
};
// Truncates a number to four decimal places
var toFourDecimals = function(number) {
number = Number(number).toFixed(8).toString();
var numberArr = number.toString().split('.');
return numberArr[0] + '.' + numberArr[1].substring(0, 4);
};
// Assuming number with 8 decimal places, returns last four digits
var getLastFourDecimals = function(number) {
number = Number(number).toFixed(8).toString();
return number.split('.')[1].substring(4, 8);
};
// Round to decimal place
var roundToDecimal = function(number, decimalPlaces) {
var offset = Math.pow(10, decimalPlaces);
return (Math.round(number * offset) / offset).toFixed(decimalPlaces);
};
// Returns receiveDetail portion of transaction json from wallet notify
var getReceiveDetail = function(details) {
var receiveDetail;
details.forEach(function(detail) {
if(detail.category === 'receive') {
receiveDetail = detail;
}
});
return receiveDetail;
};
// Returns the difference in days, hours, mins, and secs between parameter
var getExpirationCountDown = function (expiration) {
var curTime = new Date().getTime();
var diff = expiration - curTime;
var days = Math.floor(diff / 1000 / 60 / 60 / 24);
diff -= days * 1000 * 60 * 60 * 24;
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var mins = Math.floor(diff / 1000 / 60);
diff -= mins * 1000 * 60;
var secs = Math.floor(diff / 1000);
if (days === 0 && hours !== 0) {
return hours + 'h ' + mins + 'm ' + secs + 's';
}
else if (days === 0 && hours === 0) {
return mins + 'm ' + secs + 's';
}
else {
return days + 'd ' + hours + 'h ' + mins + 'm ' + secs + 's';
}
};
// Returns status of payment
var getPaymentStatus = function(payment, confirmations, invoice) {
confirmations = confirmations ? confirmations : 0; // Pending if there are no confs
var minConfirmations = invoice.min_confirmations;
var status = payment.status;
var confirmationsMet = Number(confirmations) >= Number(minConfirmations);
var expectedAmount = new BigNumber(payment.expected_amount);
var amountPaid = new BigNumber(payment.amount_paid);
if (confirmations === -1) {
status = 'invalid';
}
else if (amountPaid.greaterThan(0) && !confirmationsMet) {
status = 'pending';
}
else if (confirmationsMet) {
var isUSD = invoice.currency.toUpperCase() === 'USD';
var closeEnough = false;
if (isUSD) {
var actualPaid = new BigNumber(payment.amount_paid).times(payment.spot_rate);
var expectedPaid = new BigNumber(payment.expected_amount).times(payment.spot_rate);
actualPaid = roundToDecimal(actualPaid.valueOf(), 2);
expectedPaid = roundToDecimal(expectedPaid.valueOf(), 2);
closeEnough = new BigNumber(actualPaid).equals(expectedPaid);
}
if(amountPaid.equals(expectedAmount) || closeEnough) {
status = 'paid';
}
else if (amountPaid.lessThan(expectedAmount)) {
status = 'partial';
}
else if (amountPaid.greaterThan(expectedAmount)) {
status = 'overpaid';
}
}
return status;
};
module.exports = {
decimalPlaces: decimalPlaces,
toFourDecimals: toFourDecimals,
getLastFourDecimals: getLastFourDecimals,
roundToDecimal: roundToDecimal,
getReceiveDetail: getReceiveDetail,
getExpirationCountDown: getExpirationCountDown,
getPaymentStatus: getPaymentStatus
};