Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm trying to detect deposits that user will made. I explained it with comment lines in below.

Can you say are there any flaw in it?

var _ = require('underscore')

var check = function(client, site) {
    // check last 10 transactions
    bitcoin_client.cmd('listtransactions', '*', 10, function(e, transactions) {

            if (!transactions) return;
            var TRANSACTION = transactions.transactions;

            for (var i = 0; i < TRANSACTION.length; i++) {
                depositDetector(i)()
            };

            function depositDetector(i) {
                return function() {
                    //check if it's received
                    if (TRANSACTION[i].category == "receive" && TRANSACTION[i].amount >= 0.00000100) {

                        var txid = TRANSACTION[i].txid
                        var address = TRANSACTION[i].address
                        var amount = TRANSACTION[i].amount
                        var confs = TRANSACTION[i].confirmations

                        // find it in deposits database

                        deposit.find({
                            'txid': txid
                        }).exec(function(e, _deposit) {
                                // if there is deposit with that txid's
                                //save their confirmations
                                if (_deposit.length > 0) {
                                    _.each(_deposit, function(a) {
                                        a.confs = confs;
                                        a.save() //confirmations updated
                                    })

                                } else { // the txid is new, find user with it's address
                                    //and update user with that transaction amount.

                                    user.findOneAndUpdate({
                                        'btc_address': address
                                    }, {
                                        $inc: {
                                            'btc_balance': amount
                                        }
                                    }).exec(function(e, _user) {

                                            // !e && save deposit to database / finished            

                                        }
                                    })



                            }
                        })

                }
            }
        }
    })

}
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.