Last active
December 8, 2016 19:09
-
-
Save pedropapa/87a4e219ed04d2707cd9caebc3c2a217 to your computer and use it in GitHub Desktop.
Verifica se uma determinada transação entre contas de um determinado valor foi realizado
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var verifyPayment = require('./verifyPayment'); | |
verifyPayment('1HJBhjzp8QJrVrrFH3eZWVFtiHdTCWU82f', '1Hw1v4rqWqib351iBDx6JoFrFVX4oVu5S5', 0.004036, function(tx) { | |
console.log('Pagamento identificado! depositar créditos'); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var proccessedTxs = []; | |
function verifyPayment(fromAddress, toAddress, amount, callback) { | |
var interval = setInterval(function() { | |
request.get('https://blockchain.info/latestblock', null, function(err, res, body) { | |
if(err) { | |
return console.log(err); | |
} | |
body = JSON.parse(body); | |
var currentHighestBlock = body.height; | |
request.get('https://blockchain.info/address/'+fromAddress+'?format=json', null, function(err, res, body) { | |
if(err) { | |
return console.log(err); | |
} | |
body = JSON.parse(body); | |
var confirmedTxds = 0; | |
for(var x in body.txs) { | |
var confirmations = currentHighestBlock - body.txs[x].block_height; | |
if(confirmations >= 5 && proccessedTxs.indexOf(body.txs[x].hash) == -1) { | |
if(body.txs[x].out[0].addr == toAddress && body.txs[x].inputs[0].prev_out.addr == fromAddress && body.txs[x].out[0].value/100000000 == amount) { | |
++confirmedTxds; | |
proccessedTxs.push(body.txs[x].hash); | |
callback(body.txs[x]); | |
} | |
} | |
} | |
if(confirmedTxds > 0) { | |
clearInterval(interval); | |
} | |
}) | |
}) | |
}, 10000); | |
} | |
module.exports = verifyPayment; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment