-
Notifications
You must be signed in to change notification settings - Fork 2
/
blockchainWeb.js
138 lines (112 loc) · 3.67 KB
/
blockchainWeb.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
'use strict';
const blockcypherToken = "";
const got = require('got');
async function findTrx(trxHash, test = 0) {
let network = getNetwork(test);
var url = "https://api.blockcypher.com/v1/" + "btc" + "/" + network + "/txs/" + trxHash + "?limit=1&token=" + blockcypherToken;
console.log("url: " + url);
let data = await got(url).json();
console.log("findTrx: " + (data ? JSON.stringify(data) : ""));
if (data && data.error) {
//wait and retry
sleep(300);
console.log("retry findTrx");
data = await got(url).json();
console.log("findTrx: " + (data ? JSON.stringify(data) : ""));
}
return data && data.error ? null : data;
}
async function getBalance(publicKey, test = 0) {
var minConfirmations = 0;
let network = getNetwork(test);
var url = "https://api.blockcypher.com/v1/" + "btc" + "/" + network + "/addrs/" + publicKey + "?confirmations=" + minConfirmations + "&limit=1&token=" + blockcypherToken;
console.log("url: " + url);
let data = await got(url).json();
console.log("getBalance: " + (data ? JSON.stringify(data) : ""));
if (data && data.error) {
//wait and retry
sleep(300);
console.log("retry getBalance");
data = await got(url).json();
console.log("getBalance: " + (data ? JSON.stringify(data) : ""));
}
return data && data.error ? null : data;
}
async function getUnspentOutputs(publicKey, test = 0, minConfirmations = 0) {
let network = getNetwork(test);
var url = "https://api.blockcypher.com/v1/" + "btc" + "/" + network + "/addrs/" + publicKey + "?confirmations=" + minConfirmations + "&unspentOnly=true&token=" + blockcypherToken;
console.log("url: " + url);
let data = await got(url).json();
console.log("getUnspentOutputs: " + (data ? JSON.stringify(data) : ""));
if (data && data.error) {
//wait and retry
sleep(300);
console.log("retry getUnspentOutputs");
data = await got(url).json();
console.log("getUnspentOutputs: " + (data ? JSON.stringify(data) : ""));
}
return data && data.error ? null : data;
}
async function pushTrx(rawHash, test = 0) {
let network = getNetwork(test);
var url = "https://api.blockcypher.com/v1/" + "btc" + "/" + network + "/txs/push?token=" + blockcypherToken;
console.log("url: " + url);
let data = await got.post(url, {json: {tx: rawHash}}).json();
console.log("pushResponse: " + (data ? JSON.stringify(data) : ""));
if (data && data.error) {
//wait and retry
sleep(300);
console.log("retry pushResponse");
data = await got.post(url, {json: {tx: rawHash}}).json();
console.log("pushResponse: " + (data ? JSON.stringify(data) : ""));
}
return data && data.error ? null : data;
}
function getLastTxHash(data) {
var lastTxRef = getTxRef(data);
return lastTxRef && lastTxRef.length > 0 ? lastTxRef[0].tx_hash : null;
}
function getTxRef(data) {
var txRef = null;
var unconfirmedTxRef = data.unconfirmed_txrefs;
if (unconfirmedTxRef && unconfirmedTxRef.length > 0)
{
txRef = unconfirmedTxRef;
}
if (txRef === null)
{
txRef = data.txrefs;
}
return txRef;
}
async function getFeePerByte() {
//fastestFee, halfHourFee, hourFee
var urlFee = "https://bitcoinfees.earn.com/api/v1/fees/recommended";
const feeObj = await got(urlFee).json();
const feePerByte = feeObj.hourFee + 1;
console.log(JSON.stringify(feeObj));
return feePerByte;
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
function getNetwork(isTest) {
let network = "main";
if(isTest == 1) {
network = "test3";
} else {
//TODO
}
return network;
}
module.exports = {
findTrx,
getBalance,
getUnspentOutputs,
pushTrx,
getFeePerByte,
getTxRef,
getLastTxHash
};