-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
129 lines (120 loc) · 3.81 KB
/
api.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
require('dotenv').config();
var bitcoin_rpc = require('node-bitcoin-rpc');
bitcoin_rpc.init(process.env.RPC_HOST, process.env.RPC_PORT, process.env.RPC_USER, process.env.RPC_PASS);
exports.count = new Promise(
function(resolve, reject) {
bitcoin_rpc.call('masternode', ['count'], function (err, res) {
if (err != null)
reject(err);
else if (res.error != null)
reject(res.error.message);
else {
resolve(res.result);
}
})
}
).catch(function(e){
console.log('error: ' + e);
});
exports.validateaddress = function(address) {
return new Promise(
function(resolve, reject) {
bitcoin_rpc.call('validateaddress', [address], function (err, res) {
if (err != null)
reject(err);
else if (res.error != null)
reject(res.error.message);
else {
resolve(res.result);
}
})
}
).catch(function(e){
console.log('error: ' + e);
});
}
exports.confirmations = function(txid) {
return new Promise(
function(resolve, reject) {
bitcoin_rpc.call('getrawtransaction', [txid, 1], function (err, res) {
if (err != null)
reject(err);
else if (res.error != null)
reject(res.error.message);
else {
resolve(res.result);
}
})
}
).catch(function(e){
console.log('error: ' + e);
});
}
exports.submit = function(proposal, txid) {
return new Promise(
function(resolve, reject) {
bitcoin_rpc.call('gobject', ['submit', proposal.split(' ')[0], proposal.split(' ')[1], proposal.split(' ')[2], proposal.split(' ')[3], txid], function(err, res) {
if (err != null)
reject(err);
else if (res.error != null)
reject(res.error.message);
else
resolve(res.result);
})
}
);
}
exports.governanceinfo = function(reason) {
return new Promise(
function(resolve, reject) {
bitcoin_rpc.call('getgovernanceinfo', [], function (err, res) {
if (res.error.code == -32601) {
bitcoin_rpc.call('getfundinginfo', [], function (err, res) {
if (err != null)
reject(err);
else if (res.error != null)
reject(res.error.message);
else {
if (reason == 'fee')
resolve(res.result);
else {
bitcoin_rpc.call('getblockchaininfo', [], function(errB, resB) {
if (errB != null)
reject(errB);
else if (resB.error != null)
reject(resB.error.message);
else {
let chain = resB.result.chain;
let blocks = resB.result.blocks;
let max = (chain == 'test') ? 100 : 26;
var paymentsDate = [];
for (let i = 0; i < max; i++) {
let valor = (((res.result.nextsuperblock) + res.result.superblockcycle * i) - blocks) * 90;
let currentDate = new Date();
currentDate.setSeconds("+" + valor);
if (chain == 'test')
paymentsDate.push(currentDate.toLocaleDateString().replace(/-/g, '/') + ' ' + currentDate.toLocaleTimeString());
else
paymentsDate.push(currentDate.toLocaleDateString().replace(/-/g, '/'));
}
resolve(JSON.stringify(paymentsDate));
}
})
}
}
})
} else {
if (err != null)
reject(err);
else if (res.error != null)
reject(res.error.message);
else {
resolve(res.result);
}
}
})
}
).catch(function(e){
console.log('error: ' + e);
});
}