-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewjs.js
95 lines (77 loc) · 2.55 KB
/
newjs.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
const exec = require('child_process').exec;
const Output = require('./models/output')
const Coin = require('./models/coin')
const mongoose = require('mongoose')
const config = require('./config/config').get(process.env.NODE_ENV);
mongoose.Promise = global.Promise;
mongoose.connect(config.DATABASE, { useNewUrlParser: true }, (err) => {
if (err) {
console.log('could not connect to database' + err);
} else {
console.log('connected to db :' + config.DATABASE);
}
});
function get_mn_outputs(mn_cli_file) {
return new Promise(function (resolve, reject) {
exec(`sh get_mn_outputs.sh ${mn_cli_file}`, (err, stdout, stderr) => {
if (!err) {
resolve(stdout)
} else {
reject(err)
}
});
})
}
function add_output(newoutput){
return new Promise(function (resolve, reject) {
newoutput.save((err, output) => {
if (err) {
reject('failed to add output ', err)
reject({ success: false, msg: 'failed to add output ', err })
} else {
console.log('output added')
resolve({ success: true, msg: 'output added' })
}
})
})
}
function get_outputs(txid){
return new Promise(function(resolve,reject){
Output.findOne({ txid: txid }, function(err, output) {
if (!err) {
if (output) {
resolve({ output: output })
} else {
resolve({ msg: "no data found" })
}
} else {
reject({ msg: err })
}
})
})
}
(async function(){
var outputs = await get_mn_outputs('hostmasternode-cli')
var outputsjson = JSON.parse(outputs)
for(let key of Object.keys(outputsjson)){
console.log(key)
console.log(outputsjson[key])
try {
var output_db = await get_outputs(key)
if(!output_db.output){
var newoutput = new Output({
txid : key,
index : outputsjson[key]
})
try {
var output_added = await add_output(newoutput)
} catch (error) {
console.log(error)
}
}
} catch (error) {
console.log(error)
}
}
console.log("outputs ", outputs)
})()