forked from bitcoinz-sites/BtcZ-VaultZ-Info
-
Notifications
You must be signed in to change notification settings - Fork 1
/
btcz-vaultz-info.js
232 lines (186 loc) · 7.8 KB
/
btcz-vaultz-info.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright 2021 The BitcoinZ Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to
* do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHTHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
const express = require('express')
const app = express()
const morgan = require('morgan')
const uuid = require('uuid')
const bodyParser = require('body-parser')
const rp = require('request-promise-native')
const config = require('./config')
const VaultZ = require('./VaultZ-addresses');
const VaultZaddr = VaultZ.addresses
const VaultZpayouts = require('./VaultZ-payouts');
global.VaultZpayoutsINF = VaultZpayouts.payouts
morgan.token('id', function getId (req) {
return req.id
})
// Set application options
app.use(function (req, res, next) {
req.id = uuid.v4()
next()
})
app.use(morgan(':id :remote-addr - :remote-user [:date[clf]] \
":method :url HTTP/:http-version" :status :res[content-length] \
":referrer" ":user-agent"'))
app.set('trust proxy', 'loopback')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json(null))
// WebApp needed path
app.use('/qr', express.static('qr'))
app.use('/css', express.static('docs/css'))
app.use('/images', express.static('docs/images'))
app.use(require('./controllers/website'))
// For EJS rendering
app.set('views', __dirname + '/docs');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.engine('js', require('ejs').renderFile);
app.set('view engine', 'js');
// Define Global VaultZ variables structure
global.vaultZ = {
BlockHeight: 0,
BlockHeightDate: Date(),
AddressesCount: VaultZaddr.length,
AddressesList: VaultZaddr,
AddressesInUseCount: 0,
AddressesInUseDetails: [],
Balance: 0,
Rates: []
};
global.BlockHeightNow = 0;
// Function to retreive VaultZ informations
// Loop trought the API array (redudency)
async function RetreiveVaultZinfo () {
try {
let apiCallStr = "";
let APIreq = {};
let apiType = "";
// ----- Set BLOCKCHAIN informations -----
// Get it from the blockchain API array in config file. Also check if its iquidus or insight
for (i = 0; i < config.blockchain.api.length; i++){
global.vaultZ.BlockHeight = 0;
global.vaultZ.BlockHeightDate = Date();
global.vaultZ.Balance = 0;
global.vaultZ.AddressesInUseCount = 0;
global.vaultZ.AddressesInUseDetails = [];
let apiCallStr_count = "";
let apiCallStr_bal = "";
apiCallStr = config.blockchain.api[i].url;
apiType = config.blockchain.api[i].type;
if (apiType == "iquidus"){ // ------------------------------------- If IQUIDUS API
// Get block height
apiCallStr_count = apiCallStr+"/api/getblockcount";
APIreq = {method: 'GET', uri: apiCallStr_count};
await rp(APIreq).then(response => {
global.vaultZ.BlockHeight = response;
}).catch((err) => {
console.error('RetreiveVaultZinfo get blockchain', [ err.message, err.stack ])
});
// Get adresses balances and used count
for (item of VaultZaddr){
apiCallStr_bal = apiCallStr+"/ext/getbalance/"+item;
APIreq = {method: 'GET', uri: apiCallStr_bal};
await rp(APIreq).then(response => {
if (!response.includes("address not found.") && Number(response)>=10){
global.vaultZ.AddressesInUseCount ++;
global.vaultZ.Balance += Number(response);
global.vaultZ.AddressesInUseDetails.push({"address":item,"balance":Number(response)})
}
}).catch((err) => {
console.error('RetreiveVaultZinfo get blockchain', [ err.message, err.stack ])
});
}
} else if (apiType == "insight") { // --------------------------------- If INSIGHT API
// Get block height
apiCallStr_count = apiCallStr+"/api/status";
APIreq = {method: 'GET', uri: apiCallStr_count};
await rp(APIreq).then(response => {
global.vaultZ.BlockHeight = JSON.parse(response).info.blocks;
}).catch((err) => {
console.error('RetreiveVaultZinfo get blockchain', [ err.message, err.stack ])
});
// Get adresses balances and used count
for (item of VaultZaddr){
apiCallStr_bal = apiCallStr+"/api/addr/"+item+"/balance";
APIreq = {method: 'GET', uri: apiCallStr_bal};
await rp(APIreq).then(response => {
if (!response.includes("Invalid address: Checksum mismatch. Code:1") && Number(response)>=10){
global.vaultZ.AddressesInUseCount ++;
global.vaultZ.Balance += Number(response)/100000000;
global.vaultZ.AddressesInUseDetails.push({"address":item,"balance":Number(response)/100000000})
}
}).catch((err) => {
console.error('RetreiveVaultZinfo get blockchain', [ err.message, err.stack ])
});
}
}
// break if data pulled correctly on the actual BC API
if (global.vaultZ.BlockHeight>0 && global.vaultZ.AddressesInUseCount>0){
console.log('RetreiveVaultZinfo get blockchain', vaultZ);
break;
}
}
// Return error if no dat pulled
if (global.vaultZ.BlockHeight<1){
global.vaultZ = {"error": "No blockchain info set."};
console.log('RetreiveVaultZinfo get blockchain', "No blockchain info set.");
return;
}
// ----- Set currency rates informations -----
// Get it from the rate API array in config file
global.vaultZ.Rates = [];
for (i = 0; i < config.rate.api.length; i++){
apiCallStr = config.rate.api[i];
APIreq = {method: 'GET', uri: apiCallStr};
await rp(APIreq).then(response => {
for (item of JSON.parse(response)){
if (config.rate.currency.includes(item.code)) {
global.vaultZ.Rates.push({"code":item.code,"name":item.name,"rate":item.rate, "value":item.rate*global.vaultZ.Balance})
}
}
}).catch((err) => {
console.error('RetreiveVaultZinfo get currency', [ err.message, err.stack ])
});
// break if data pulled correctly on the actual rate API
if (global.vaultZ.Rates.length>0){
console.log('RetreiveVaultZinfo get currency', global.vaultZ.Rates);
break;
}
}
// Return error if no data pulled
if (global.vaultZ.Rates.length<1){
global.vaultZ.Rates = [{"error": "No rate info set."}];
console.log('RetreiveVaultZinfo get currency', "No currency rates set.");
return;
}
} catch (error) {
console.error('RetreiveVaultZinfo function', [ error.message, error.stack ])
}
}
// Call the VaultZ info each n minutes.
RetreiveVaultZinfo()
setInterval(() => RetreiveVaultZinfo(), 60 * 1000 * config.server.updateEveryMinutes)
// Startup server
let server = app.listen(config.server.port, '127.0.0.1', function () {
console.log('BOOTING UP', `Listening on port ${config.server.port}`)
})
module.exports = server