-
Notifications
You must be signed in to change notification settings - Fork 503
/
utils.js
59 lines (53 loc) · 1.17 KB
/
utils.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
'use strict';
const BigNumber = require('bignumber.js');
const core = require('../../core');
function dropsToXrp(drops) {
return (new BigNumber(drops)).dividedBy(1000000.0).toString();
}
function xrpToDrops(xrp) {
return (new BigNumber(xrp)).times(1000000.0).floor().toString();
}
function toRippledAmount(amount) {
if (amount.currency === 'XRP') {
return xrpToDrops(amount.value);
}
return {
currency: amount.currency,
issuer: amount.counterparty ? amount.counterparty : amount.issuer,
value: amount.value
};
}
function wrapCatch(asyncFunction: () => void): () => void {
return function() {
try {
asyncFunction.apply(this, arguments);
} catch (error) {
const callback = arguments[arguments.length - 1];
callback(error);
}
};
}
function composeAsync(wrapper, callback) {
return function(error, data) {
if (error) {
callback(error);
return;
}
let result;
try {
result = wrapper(data);
} catch (exception) {
callback(exception);
return;
}
callback(null, result);
};
}
module.exports = {
core,
dropsToXrp,
xrpToDrops,
toRippledAmount,
wrapCatch,
composeAsync
};