-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdeposit.js
More file actions
60 lines (51 loc) · 1.82 KB
/
deposit.js
File metadata and controls
60 lines (51 loc) · 1.82 KB
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
var cron = require('cron').CronJob,
bitcoin = require("bitcoin");
client = new bitcoin.Client({
host: 'rpc.blockchain.info',
port: 80,
user: '4f89815c-1a3d-4b90-afab-fc0d4e5031ab',
pass:'Ozergul.9!',
timeout: 30000
});
function DepositControl() {
client.cmd('listtransactions', '*', 10, function(err, trans) {
if(err) return;
var depositsClient = trans.transactions;
for (var i = 0; i < depositsClient.length; i++) {
InsertOrUpdateDeposit(i)();
};
function InsertOrUpdateDeposit(i) {
return function(){
if(depositsClient[i].category == "receive"){
if(depositsClient[i].amount > 0.00000001){
connection.query('SELECT * FROM deposits WHERE dp_tx_id = ?', depositsClient[i].txid, function(err, depositsQuery) {
if(!depositsQuery[0]) {
//insert
var new_deposit = {
dp_date: new Date().getTime(),
dp_amount: depositsClient[i].amount,
dp_btc_address: depositsClient[i].address,
dp_confirmations: depositsClient[i].confirmations,
dp_tx_id: depositsClient[i].txid
}
connection.query('INSERT INTO deposits SET ?', new_deposit, function(err, result) {
connection.query('UPDATE users SET user_balance = (@cur_value := user_balance) + ? WHERE user_btc_address = ?', [depositsClient[i].amount, depositsClient[i].address], function(err, res) {
console.log("txid inserted: " + depositsClient[i].txid);
})
});
} else {
// update
connection.query('UPDATE deposits SET dp_confirmations = ? WHERE dp_tx_id = ?', [depositsClient[i].confirmations, depositsClient[i].txid], function(err, updatequery) {
});
}
});
}
}
}
}
});
}
var job = new cron('* * * * * *', function() {
DepositControl();
});
job.start();