-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
257 lines (233 loc) · 8.58 KB
/
server.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var express = require('express');
const axios = require("axios");
var W3CWebSocket = require('websocket').w3cwebsocket;
require('dotenv').config();
const mongoose = require('mongoose');
const FactomBlocks = require('./models/FactomBlocksSchema');
const BlockchainDOTcom = require('./models/BlockchainDOTcomSchema');
const Helpers = require("./lib/helpers")
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
mongoose.Promise = global.Promise;
mongoose.connection
.on('connected', () => { console.log(`Connected to database`), CallHarm() })
.on('error', (err) => { console.log(`Connection error: ${err.message}`) });
let app = express();
let vep = app.listen(6001, () => console.log(`Express Server Now Running On ${vep.address().port}`));
// Socket to listen to Factoms address on Blockchain.com
setupWebSocket = () => {
console.log("setupWebSocket Called")
let client = new W3CWebSocket('wss://ws.blockchain.info/inv');
client.onerror = function (error) {
console.log('Connection Error Bitcoin', error);
};
client.onopen = function () {
console.log('WebSocket Client Bitcoin Connected');
function sendNumber() {
if (client.readyState === client.OPEN) {
client.send(`{"op":"addr_sub", "addr":"1K2SXgApmo9uZoyahvsbSanpVWbzZWVVMF"}`);
}
}
setInterval(() => {
sendNumber();
}, 300000)
sendNumber();
};
client.onclose = function () {
console.log('echo-protocol Client Bitcoin Closed');
setTimeout(setupWebSocket, 1000)
};
client.onmessage = function (e) {
CallHarm();
setTimeout(() => {
if (typeof e.data === 'string') {
let obj = JSON.parse(e.data);
let outScript = obj.x.out[1].script;
let keyMR = outScript.substring(outScript.length - 64, outScript.length);
let height = parseInt(outScript.substring(12, 20), 16);
let transHash = obj.x.hash;
let time = obj.x.time;
let SaveData = new BlockchainDOTcom({
script: outScript,
keymr: keyMR,
height: height,
btc_trans_hash: transHash,
time: time,
})
Helpers.UpdateFactomBlockWithBTCHash(SaveData, "On Socket message")
}
}, 1000)
};
}
setupWebSocket();
// Function to call Harmony to find latest Factom blocks.
CallHarm = () => {
console.log("CallHarm Called")
axios({
method: "GET",
url: "https://api.factom.com/v1/dblocks",
headers: {
"Content-Type": "application/json",
"app_id": process.env.APPID,
"app_key": process.env.APPKEY
}
}).then(res => {
res.data.data.forEach(block => {
let SaveBlock = new FactomBlocks({
height: block.height,
keymr: block.keymr,
started_at: block.started_at,
})
SaveBlock.save().then(() => {
}).catch(err => err.code === 11000 ? null : console.log("FactomBlocks Save Error: ", err));
});
}).catch(err => console.log("CallHarm ERROR", err.response.data))
}
DoINeedToCatchUp = async () => {
let noHashList = await Promise.resolve(Helpers.GetBlocksWithoutBTCHashORWithoutConfirmation(1))
let randomNum = Helpers.RandomNum();
if (noHashList.length > randomNum) {
noHashList.splice(0, randomNum).forEach((block) => {
axios({
method: "GET",
url: `https://api.factom.com/v1/dblocks/${block.keymr}`,
headers: {
"Content-Type": "application/json",
"app_id": process.env.APPID,
"app_key": process.env.APPKEY
}
}).then(res => {
if (res.data.data.btc_transaction !== null) {
axios({
method: "GET",
url: `https://blockchain.info/rawtx/${res.data.data.btc_transaction}`
}).then(blockres => {
let outScript = blockres.data.out[1].script;
let keyMR = outScript.substring(outScript.length - 64, outScript.length);
let height = parseInt(outScript.substring(12, 20), 16);
let transHash = blockres.data.hash;
let time = blockres.data.time;
let SaveData = new BlockchainDOTcom({
script: outScript,
keymr: keyMR,
height: height,
btc_trans_hash: transHash,
time: time,
})
Helpers.UpdateFactomBlockWithBTCHash(SaveData, "do I need to catch up, larger than 1 - 5")
}).catch(err => console.log("Blockchain.com Error: ", err.response))
}
})
})
} else {
noHashList.forEach((block) => {
axios({
method: "GET",
url: `https://api.factom.com/v1/dblocks/${block.keymr}`,
headers: {
"Content-Type": "application/json",
"app_id": process.env.APPID,
"app_key": process.env.APPKEY
}
}).then(res => {
if (res.data.data.btc_transaction !== null) {
axios({
method: "GET",
url: `https://blockchain.info/rawtx/${res.data.data.btc_transaction}`
}).then(blockres => {
let outScript = blockres.data.out[1].script;
let keyMR = outScript.substring(outScript.length - 64, outScript.length);
let height = parseInt(outScript.substring(12, 20), 16);
let transHash = blockres.data.hash;
let time = blockres.data.time;
let SaveData = new BlockchainDOTcom({
script: outScript,
keymr: keyMR,
height: height,
btc_trans_hash: transHash,
time: time,
})
Helpers.UpdateFactomBlockWithBTCHash(SaveData, "do I need to catch up, less than 1 - 5")
}).catch(err => console.log("Blockchain.com Error: ", err.response))
} else {
if (noHashList.length > 0) {
noHashList.splice(0, 10).forEach((block2) => {
axios({
method: "GET",
url: `https://blockchain.info/rawaddr/1K2SXgApmo9uZoyahvsbSanpVWbzZWVVMF`
}).then(res => {
res.data.txs.forEach(tx => {
let keymr = tx.out[1].script.substring(tx.out[1].script.length - 64, tx.out[1].script.length)
if (keymr === block2.keymr) {
FactomBlocks.findOneAndUpdate({ keymr: block2.keymr }, { btc_hash: tx.hash }, (err, data) => {
if (err) console.log("Err in find", err)
})
}
})
}).catch(err => { console.log("FindingBTCHASH ERROR: ", err) })
})
}
}
})
})
}
}
// Function that checks if there are new Bitcoin transactions saved and matches them to Factom Blocks
CheckSavedBitcoinMessages = async() => {
let sorted = await Promise.resolve(Helpers.GetAllBlockchainDOTcoms());
for (let i = 0; i <= sorted.length - 1; i++) {
FactomBlocks.findOneAndUpdate({ keymr: sorted[i].keymr }, { btc_hash: sorted[i].btc_trans_hash }, (err, data) => {
if (err) console.log("Err in find in CheckSavedBitcoinMessages: ", err)
})
}
}
FindingConfirmations = () => {
axios({
method: "GET",
url: `https://api.blockcypher.com/v1/btc/main/addrs/1K2SXgApmo9uZoyahvsbSanpVWbzZWVVMF?includeScript=true`
}).then(res => {
res.data.txrefs.forEach((trans) => {
if (trans.confirmations > 0) {
FactomBlocks.findOneAndUpdate({ btc_hash: trans.tx_hash }, { btc_conf: true }, (err, data) => {
if (err) console.log("Err in find in FindingConfirmations: ", err)
})
}
})
}).catch(err => console.log("err", err))
}
setInterval(() => {
Helpers.SlackBTCBalance(process.env.URL)
Helpers.SlackNotification(process.env.URL)
}, 13000)
setInterval(() => {
CallHarm()
DoINeedToCatchUp()
CheckSavedBitcoinMessages()
}, 300000)
setInterval(() => {
FindingConfirmations()
}, 300010);
GettingETHTxs = () => {
axios({
method: "GET",
url: `http://api.etherscan.io/api?module=account&action=txlist&address=0x334A31B3d9DE02e9B88FB3308a4406dF14D4Ae17&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken`
}).then(res => {
for (let i = 0; i < res.data.result.length; i++) {
let tx = res.data.result[i]
let SaveData = new EthereumTx({
eth_block: parseInt(tx.blockNumber),
time: new Date(parseInt(tx.timeStamp) * 1000),
eth_trx_hash: tx.hash,
confirmations: parseInt(tx.confirmations),
input: tx.input,
windowmr: tx.input.substring(tx.input.length - 64),
dbheight_max: parseInt(tx.input.substring(70, 74), 16),
dbheight_min: parseInt(tx.input.substring(70, 74), 16) - 999,
})
SaveData.save()
}
})
}
setTimeout(() => {
// GettingETHTxs()
}, 200)
// Helpers.GettingBackUp(196397, 196647)