forked from Overtorment/Cashier-BTC
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker4.js
102 lines (84 loc) · 3.74 KB
/
worker4.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
/**
* ==============================================================================
* BTCz-Pay
* ==============================================================================
*
* Version 0.2.1 (production v1.0)
*
* Self-hosted bitcoinZ payment gateway
* https://github.com/MarcelusCH/BTCz-Pay
*
* ------------------------------------------------------------------------------
* worker4.js Independent nodejs worker
* ------------------------------------------------------------------------------
*
* worker iterates through all addresses,
* get paid_and_sweeped_unconfirmed, wait confirmation
* and pay to tmp address (for speed pay)
*
* ==============================================================================
*/
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
let signer = require('./models/signer')
require('./smoke-test') // Checking DB & BtcZ node RPC
;(async () => {
while (1) {
let wait = ms => new Promise(resolve => setTimeout(resolve, ms))
let job = await storage.getPaidUnconfirmedAdressesNewerThanPromise(Date.now() - (config.process_paid_for_period*100000))
await processJob(job)
await wait(60000)
}
})()
async function processJob (rows) {
try {
console.log('WORKER 4', ['Check for paid (uncomfirmed) and transferring back to tmp...'])
rows = rows || {}
rows.rows = rows.rows || []
for (const row of rows.rows) {
let json = row.doc
// Wait 10 minutes to be sure it's confirmed
if ((json.paid_on+(10*60000))<=Date.now() ){
// Check in blockchain
let received = await blockchain.getReceivedByAddress(json.address)
// If balance is confirmed (paid), need to transfer back to tmp wallet
if (received[config.confirmation_before_forward].result >= received[0].result && received[0].result > 0) {
// log
logger.log('WORKER 4', [json._id, ''
+'transferring (back): '+received[0].result, ''
+'from: '+json.address, 'to: '+config.tmp_address, ''
+'speed_sweep: '+json.speed_sweep])
// List unspent and create tx
let unspentOutputs = await blockchain.listunspent(json.address)
let createTx = signer.createTransaction
let tx = createTx(unspentOutputs.result, config.tmp_address, (json.btc_to_ask+((json.btc_to_ask/100)*
config.speed_sweep_fee)), config.fee_tx, json.WIF)
// broadcasting
logger.log('WORKER 4', [json._id, 'Broadcasting tx... ' ])
let broadcastResult = await blockchain.broadcastTransaction(tx)
// Log an store result
json.state = 5
json.processed = 'paid_and_sweeped'
json.sweep_result = json.sweep_result || {}
json.sweep_result[Date.now()] = {
'tx': tx,
'broadcast': broadcastResult
}
logger.log('WORKER 4', [json._id, 'Mark as sweeped ! '])
await storage.saveJobResultsPromise(json)
} else {
logger.error('WORKER 4', [json._id, 'Marked as paid (uncomfirmed) but balance not ok.', ''
+'address: '+json.address, ''
+'expect: '+json.btc_to_ask, ''
+'confirmed: '+received[config.confirmation_before_forward].result, ''
+'unconfirmed: '+received[0].result, ''
+'speed_sweep: '+json.speed_sweep])
} // end if
}
} // end for
} catch (error) {
logger.error('WORKER 4', [ error.message, error.stack ])
}
}