-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcheckPaymentsFile.js
95 lines (83 loc) · 2.51 KB
/
checkPaymentsFile.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
var fs = require('fs');
var request = require('request');
/**
* Put your settings here:
* - filename: file to check for payments
* - node: address of your node in the form http://<ip>:<port
*/
var config = {
filename: 'test.json',
node: 'http://5.189.136.6:6869'
};
var payments;
var assets = {};
var assetsFound = 0;
/**
* Method that starts the checking process.
*/
var start = function() {
var paymentsString = fs.readFileSync(config.filename).toString();
payments = JSON.parse(paymentsString);
console.log(payments.length + ' payments found!');
payments.forEach(function(payment) {
if (payment.assetId) {
if (!assets[payment.assetId]) {
assetsFound++;
assets[payment.assetId] = {
amount: payment.amount,
decimals: 0,
name: ''
};
} else {
assets[payment.assetId].amount += payment.amount;
}
} else {
if (!assets['Waves']) {
assetsFound++;
assets['Waves'] = {
amount: payment.amount,
decimals: 8,
name: 'Waves'
};
} else {
assets['Waves'].amount += payment.amount;
}
}
});
addAssetInfo(assets, function() {
for (var assetId in assets) {
var asset = assets[assetId];
console.log((asset.amount / Math.pow(10, asset.decimals)) + ' of ' + asset.name + ' will be paid!');
}
});
};
/**
* Method that adds infor like decimals and name to an asset.
*
* @param assets The asset that have been found
* @param cb The callback that gets executed after all infos are added
*/
var addAssetInfo = function(assets, cb) {
var counter = 0;
for (var assetId in assets) {
if (assetId !== 'Waves') {
request.get(config.node + '/transactions/info/' + assetId, function(err, response, body) {
if (!err) {
var asset = JSON.parse(body);
counter++;
assets[asset.assetId].decimals = asset.decimals;
assets[asset.assetId].name = asset.name;
if (assetsFound === counter) {
cb();
}
}
});
} else {
counter++;
if (assetsFound === counter) {
cb();
}
}
}
};
start();