forked from Overtorment/Cashier-BTC
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker1.js
90 lines (76 loc) · 3.2 KB
/
worker1.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
/**
* ==============================================================================
* BTCz-Pay
* ==============================================================================
*
* Version 0.2.1 (production v1.0)
*
* Self-hosted bitcoinZ payment gateway
* https://github.com/MarcelusCH/BTCz-Pay
*
* ------------------------------------------------------------------------------
* worker.js Independent nodejs worker
* ------------------------------------------------------------------------------
*
* Worker iterates through all addresses,
* marks paid and fires pingback
*
* ==============================================================================
*/
let rp = require('request-promise')
let storage = require('./models/storage') // Load db call functions
let blockchain = require('./models/blockchain') // Load blockchain functions
let config = require('./config') // Load configuration file
let logger = require('./utils/logger') // Load the logger module
;(async () => {
while (1) {
let wait = ms => new Promise(resolve => setTimeout(resolve, ms))
let job = await storage.getUnprocessedAdressesNewerThanPromise(Date.now() - config.process_unpaid_for_period)
await processJob(job)
await wait(5050)
}
})()
async function processJob (rows) {
try {
console.log('WORKER 1', ['Check for received and mark as paid...'])
rows = rows || {}
rows.rows = rows.rows || []
for (const row of rows.rows) {
let json = row.doc
// Check recieved amount by the address - 0 unconfirmed / 1 confirmed
let received = await blockchain.getReceivedByAddress(json.address)
// If confirmed is >= as expected or unconfirmed is >= expected
// and SpeedSweep true, mark as paid
if (received[config.confirmation_before_forward].result >= json.btc_to_ask ||
(received[0].result >= (json.btc_to_ask+((json.btc_to_ask/100)*
config.speed_sweep_fee))) && json.speed_sweep==1) {
// Log if paid
logger.log('WORKER 1', [json._id, 'address: '+json.address, ''
+'expect: '+json.btc_to_ask, ''
+'confirmed: '+received[config.confirmation_before_forward].result, ''
+'unconfirmed: '+received[0].result, ''
+'speed_sweep: '+json.speed_sweep])
// Update the couchdb document
json.processed = 'paid'
json.paid_on = Date.now()
logger.log('WORKER 1', [json._id, 'Mark paid ! '])
await storage.saveJobResultsPromise(json)
// Set URL parameter
let URLset = json.callback_url
if (URLset.indexOf('?') !== -1) {
URLset = URLset +'&secret='+json.secret+'&state=5'
} else {
URLset = URLset +'?secret='+json.secret+'&state=5'
}
// Fire server side pingback
rp({uri: URLset}).then((result) => {
logger.log('WORKER 1', [json._id, 'Pingback success done: ' , URLset])
}).catch((error) => {
logger.error('WORKER 1', [json._id, 'Pingback success fail: ' , URLset, error.message, error.stack])
})
} // end if
} // end for
} catch (error) {
logger.error('WORKER 1', [ error.message, error.stack ])
} // end try
}