forked from Overtorment/Cashier-BTC
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker5.js
146 lines (114 loc) · 5.32 KB
/
worker5.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
/**
* ==============================================================================
* BTCz-Pay
* ==============================================================================
*
* Version 0.2.1 (production v1.0)
*
* Self-hosted bitcoinZ payment gateway
* https://github.com/MarcelusCH/BTCz-Pay
*
* ------------------------------------------------------------------------------
* worker5.js Independent nodejs worker
* ------------------------------------------------------------------------------
*
* worker iterates all 15 min. through all addresses,
* get unchecked for return found
* if partially or more paid.
*
* ==============================================================================
*/
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.getUnCheckedAdressesNewerThanPromise(Date.now() - (config.process_paid_for_period*100000))
await processJob(job)
await wait(15*59100)
}
})()
async function processJob (rows) {
try {
console.log('WORKER 5', ['Check for return founds...'])
rows = rows || {}
rows.rows = rows.rows || []
for (const row of rows.rows) {
let json = row.doc
// If paid or expired (only if more that expired time + time in config)
if ((json.state==2 || json.state==5) && (json.timestamp+(config.max_payment_valid*60000)+(config.check_return*60000))<=Date.now()) {
let address = json.address
// Get the unspent total amount
let unspent = await blockchain.listunspent(address)
let TotUnspent = 0
for (value of unspent.result){
TotUnspent += value.amount
}
// Continue if TotUnspent is > 0
if (TotUnspent>0) {
// log
logger.log('WORKER 5', [json._id, ''
+'total unspent: '+TotUnspent, ''
+'address: '+address])
// Get the transactions of the address
let transactions = await blockchain.listTransactions(address)
// Check the first transaction
if (transactions.result[0] != undefined){
// Get the first TXID and decode raw hex (to go back in block)
let transaction = await blockchain.getRawTransaction(transactions.result[0].txid)
let decodeRaw = await blockchain.decodeRawTransaction(transaction.result)
// more test for Z addresses
if (decodeRaw != undefined && decodeRaw.result.vin[0] != undefined) {
// Get the VIN TXID and decode the hex
let RawTransactionVin = await blockchain.getRawTransaction(decodeRaw.result.vin[0].txid)
let decodeRawTransactionVin = await blockchain.decodeRawTransaction(RawTransactionVin.result)
// Check if it's not a local address
let returnAddress = ""
for (let value of decodeRawTransactionVin.result.vout) {
let retAddressTmp = value.scriptPubKey.addresses[0]
let gatewayByAddress = await storage.CheckIfAddressExist(retAddressTmp)
let AddressInDB=gatewayByAddress.rows.length //-gatewayByAddress.offset
// if not in DB, asign it to return address
if (AddressInDB==0){
returnAddress=retAddressTmp
break
}
}
// log
logger.log('WORKER 5', [json._id, ''
+'refound amount: '+TotUnspent, ''
+'from: '+address, 'to: '+returnAddress])
// List unspent and create tx
let unspentOutputs = await blockchain.listunspent(address)
let createTx = signer.createTransaction
let tx = createTx(unspentOutputs.result, returnAddress, TotUnspent, config.fee_tx, json.WIF)
// broadcasting
logger.log('WORKER 5', [json._id, 'Broadcasting return tx... ' ])
let broadcastResult = await blockchain.broadcastTransaction(tx)
// Log an store result
json.return_check = 'checked'
json.sweep_return_result = json.sweep_return_result || {}
json.sweep_return_result[Date.now()] = {
'tx': tx,
'broadcast': broadcastResult
}
logger.log('WORKER 5', [json._id, 'Mark as checked ! ' ])
await storage.saveJobResultsPromise(json)
}
} // end if/else transactions.result[0] == undefined
} else {
// If nothing to return, log an store result
json.return_check = 'checked'
logger.log('WORKER 5', [json._id, 'Mark as checked ! Nothing to return.','address : '+address ])
await storage.saveJobResultsPromise(json)
}// end if/else unspent > 0
} // end if state==2 or 5
} // end for
} catch (error) {
logger.error('WORKER 5', [ error.message, error.stack ])
}
}