Skip to content

Instantly share code, notes, and snippets.

@pmitchev
Created November 19, 2015 11:31
Show Gist options
  • Save pmitchev/de174d5cd4b5d0dfa532 to your computer and use it in GitHub Desktop.
Save pmitchev/de174d5cd4b5d0dfa532 to your computer and use it in GitHub Desktop.
var http = require("http");
var https = require("https");
var redis = require('redis');
require('./lib/configReader.js');
global.redisClient = redis.createClient(config.redis.port, config.redis.host);
global.symbol_lc = config.symbol.toLowerCase();
global.address_fc = config.poolServer.poolAddress.charAt(0);
function delete_payment(tx_hash) {
redisClient.keys(config.coin + ':payments:' + address_fc + '*', function(error, replies) {
if (error) {
console.log('error', logSystem, 'Error with getting payments %j', [error]);
return;
}
replies.forEach(function(key) {
var worker = key.split(":")[2];
redisClient.zrange(key, 0, -1, function(error, payments) {
if (error) {
console.log('Error with getting payments %j', [error]);
return;
}
payments.forEach(function(r) {
var tx_hash_single_payee = r.split(":")[0];
if (tx_hash == tx_hash_single_payee) {
console.log(worker + " : " + r);
var payee_amount = parseInt(r.split(":")[1]);
redisClient.hgetall(config.coin + ":workers:" + worker, function(error, results) {
if (error) {
console.log('error', logSystem, 'Error with getting workers %j', [error]);
return;
}
results.balance = parseInt(results.balance);
results.paid = parseInt(results.paid);
console.log(results.balance + " : " + results.paid);
console.log((results.balance + payee_amount) + " : " + (results.paid - payee_amount) + " : " + payee_amount);
redisClient.hset(config.coin + ":workers:" + worker, "balance", results.balance + payee_amount);
redisClient.hset(config.coin + ":workers:" + worker, "paid", results.paid - payee_amount);
redisClient.zrem(key, r);
});
}
});
});
});
})
}
function tx_minergate(key, score) {
var tx_hash = key.split(":")[0];
var options = {
host: 'minergate.com',
port: 443,
path: '/ccapi/1.0/' + symbol_lc + '/transactions/' + tx_hash + '/full',
method: 'GET'
};
var req = https.request(options, function(res) {
console.log("https://minergate.com/full/ccapi/1.0/" + symbol_lc + '/transactions/' + tx_hash + '/full : ' + res.statusCode);
if (res.statusCode == '404') {
tx_chainradar(key, score);
} else if (res.statusCode == '200') {
redisClient.zadd(config.coin + ':payments:confirmed', score, key);
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
function tx_chainradar(key, score) {
var tx_hash = key.split(":")[0];
var options = {
host: 'chainradar.com',
port: 80,
path: '/' + symbol_lc + '/transaction/' + tx_hash,
method: 'GET'
};
var req = http.request(options, function(res) {
console.log("http://chainradar.com/" + symbol_lc + "/transaction/" + tx_hash + " : " + res.statusCode);
if (res.statusCode == '200') {
res.on('data', function(chunk) {
if (chunk.toString().indexOf("Transaction not found") > -1) {
console.log(key + " : rejected");
delete_payment(tx_hash);
redisClient.zadd(config.coin + ':payments:rejected', score, key);
}
});
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
(function init() {
var endDate = Math.round(((new Date()).getTime() / 1000) - 24 * 60 * 60); // 24 hours old or more
redisClient.zrange(config.coin + ':payments:all', 0, endDate, function(error, replies) {
if (error) {
console.log('error', logSystem, 'Error with getting payments %j', [error]);
return;
}
replies.forEach(function(r) {
redisClient.zscore(config.coin + ':payments:all', r, function(err, score) {
// console.log(r + " : " + score);
if (!!r && isNaN(r)) {
redisClient.zscore(config.coin + ':payments:confirmed', r, function(err, score_conf) {
if (!score_conf)
redisClient.zscore(config.coin + ':payments:rejected', r, function(err, score_rej) {
if (!score_rej)
tx_minergate(r, score);
});
});
}
});
});
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment