lib/processor.js
/*
     Check if the address waiting received their bitcoins
     If the btcs are received and the btc balance its higher that the min amount that every address can have send the btcs to the mainAddress.
     To send all the btcs on the address we get all the unused inputs and create a tx.
     We push the raw tx using the blocker.io api and complete the paymentW.
     Once the paymentWaiting is complete it creates a paymentDone object and trigger the paymentFunction for that type of operation.
    */
Check if the address waiting received their bitcoins
If the btcs are received and the btc balance its higher that the min amount that every address can have send the btcs to the mainAddress.
To send all the btcs on the address we get all the unused inputs and create a tx.
We push the raw tx using the blocker.io api and complete the paymentW.
Once the paymentWaiting is complete it creates a paymentDone object and trigger the paymentFunction for that type of operation.
Check if the address waiting received their bitcoins If the btcs are received and the btc balance its higher that the min amount that every address can have send the btcs to the mainAddress . To send all the btcs on the address we get all the unused inputs and create a tx . We push the raw tx using the blocker . io api and complete the paymentW . Once the paymentWaiting is complete it creates a paymentDone object and trigger the paymentFunction for that type of operation .
function checkAddressWaiting(addressW,callback){
        logger.log('Checking address: '+addressW.string);
        async.waterfall([
            function(callback){
                logger.success('Looking for tx waiting id: '+addressW.paymentWaiting);
                db.paymentsWaiting.findOne({ "_id" : addressW.paymentWaiting },{},function(err,paymentWaiting){
                    if (err){
                        callback(err);
                    } else if (paymentWaiting){
                        callback(null,paymentWaiting);
                    } else {
                        callback('No tx waiting found fo address: '+addressW.string);
                    }
                });  
            },
            function(paymentW,callback){
                logger.success('Getting utxos..');
                request("http://tbtc.blockr.io/api/v1/address/unspent/"+addressW.string, function(err, response, utxos) { 
                    if (err)
                        callback(err);
                    else if (utxos)
                        callback(null,utxos,paymentW);
                    else
                        callback(null,[],paymentW);
                });
            },
            function(utxos,paymentW,callback){
                logger.success('Checking '+JSON.parse(utxos).data.unspent.length+' utxos..');
                checkUtxos(utxos,addressW,function(err,txs,amountW,unusedBalance,unspent,fromAddress){
                    if (err) {
                        callback(err);
                    } else {
                        callback(null,paymentW,txs,amountW,unusedBalance,unspent,fromAddress);
                    }
                });
            },
            function(paymentW,txs,amountW,unusedBalance,unspent,fromAddress,callback){ 
                if ((amountW >= paymentW.quantity)&&(unusedBalance >= config.limitBalance)&&(unspent.length > 0)){
                    var to = {
                        address: config.btcMainAddress, 
                        amount:unusedBalance-config.txFee
                    };
                    sendTX(unspent, to, [addressW.privKey.toString()], function(err,done){
                        if (err) {
                            callback(err,null,null);
                        } else if (done) {
                            paymentW.complete(addressW,fromAddress,txs,function(err,done){
                                if (err){
                                    callback(err,null,null);
                                } else {
                                    logger.success("Tx parsed for address: "+addressW.string+" using txs: "+txs.toString());
                                    callback(null,true,0);
                                }     
                            });
                        } else {
                            callback('Problem sending TX',null,null);
                        }
                    });
                } else if ((amountW >= paymentW.quantity)&&(unspent.length > 0)){                                 
                    logger.success('Parsing tx to done with txs: '+txs.toString());  
                    paymentW.complete(addressW,fromAddress,txs,function(err,done){
                        if (err){
                            callback(err);
                        } else {
                            callback(null,true,unusedBalance);
                        }
                    });
                } else {
                    if ((config.warningTimeout > 0) && paymentW && (!paymentW.warned) && (unspent.length == 0) && ( (((new Date(paymentW.dateBegin.getTime() + config.warningTimeout*60000))-new Date())/60000) < 0)){                      
                        paymentW.warn(function(err,done){
                            if (err){
                                callback(err);
                            } else {
                                logger.success('PaymentW warning..');
                                callback(null,true,0);
                            }
                        });
                    } else if (paymentW && (unspent.length == 0) && ( (((new Date(paymentW.dateBegin.getTime() + config.paymentTimeout*60000))-new Date())/60000) < 0)){
                        paymentW.remove();
                        logger.success('PaymentW timeout, removed.');
                        callback(null,true,0);
                    } else {
                        logger.success('Nothing to parse on address..');
                        callback(null,null,null);
                    }
                }
            },
            function(finishWaiting,unusedBalance,callback){
                if (finishWaiting){
                    logger.success('Address finishing waiting..');
                    addressW.finishWaiting(unusedBalance,function(err){
                        if (err){
                            logger.error(err.toString());
                            callback(err);
                        }else{
                            callback(null);
                        }
                    });
                } else {
                    callback(null);
                }
            }
        ],function(err){
            if (err)
                callback(err);
            else
                callback(null);
        })
    }
function checkAddressWaiting ( addressW , callback ) { logger . log ( 'Checking address: ' + addressW . string ) ; async . waterfall ( [ function ( callback ) { logger . success ( 'Looking for tx waiting id: ' + addressW . paymentWaiting ) ; db . paymentsWaiting . findOne ( { "_id" : addressW . paymentWaiting } , { } , function ( err , paymentWaiting ) { if ( err ) { callback ( err ) ; } else if ( paymentWaiting ) { callback ( null , paymentWaiting ) ; } else { callback ( 'No tx waiting found fo address: ' + addressW . string ) ; } } ) ; } , function ( paymentW , callback ) { logger . success ( 'Getting utxos..' ) ; request ( "http://tbtc.blockr.io/api/v1/address/unspent/" + addressW . string , function ( err , response , utxos ) { if ( err ) callback ( err ) ; else if ( utxos ) callback ( null , utxos , paymentW ) ; else callback ( null , [ ] , paymentW ) ; } ) ; } , function ( utxos , paymentW , callback ) { logger . success ( 'Checking ' + JSON . parse ( utxos ) . data . unspent . length + ' utxos..' ) ; checkUtxos ( utxos , addressW , function ( err , txs , amountW , unusedBalance , unspent , fromAddress ) { if ( err ) { callback ( err ) ; } else { callback ( null , paymentW , txs , amountW , unusedBalance , unspent , fromAddress ) ; } } ) ; } , function ( paymentW , txs , amountW , unusedBalance , unspent , fromAddress , callback ) { if ( ( amountW >= paymentW . quantity ) && ( unusedBalance >= config . limitBalance ) && ( unspent . length > 0 ) ) { var to = { address : config . btcMainAddress , amount : unusedBalance - config . txFee } ; sendTX ( unspent , to , [ addressW . privKey . toString ( ) ] , function ( err , done ) { if ( err ) { callback ( err , null , null ) ; } else if ( done ) { paymentW . complete ( addressW , fromAddress , txs , function ( err , done ) { if ( err ) { callback ( err , null , null ) ; } else { logger . success ( "Tx parsed for address: " + addressW . string + " using txs: " + txs . toString ( ) ) ; callback ( null , true , 0 ) ; } } ) ; } else { callback ( 'Problem sending TX' , null , null ) ; } } ) ; } else if ( ( amountW >= paymentW . quantity ) && ( unspent . length > 0 ) ) { logger . success ( 'Parsing tx to done with txs: ' + txs . toString ( ) ) ; paymentW . complete ( addressW , fromAddress , txs , function ( err , done ) { if ( err ) { callback ( err ) ; } else { callback ( null , true , unusedBalance ) ; } } ) ; } else { if ( ( config . warningTimeout > 0 ) && paymentW && ( ! paymentW . warned ) && ( unspent . length == 0 ) && ( ( ( ( new Date ( paymentW . dateBegin . getTime ( ) + config . warningTimeout * 60000 ) ) - new Date ( ) ) / 60000 ) < 0 ) ) { paymentW . warn ( function ( err , done ) { if ( err ) { callback ( err ) ; } else { logger . success ( 'PaymentW warning..' ) ; callback ( null , true , 0 ) ; } } ) ; } else if ( paymentW && ( unspent . length == 0 ) && ( ( ( ( new Date ( paymentW . dateBegin . getTime ( ) + config . paymentTimeout * 60000 ) ) - new Date ( ) ) / 60000 ) < 0 ) ) { paymentW . remove ( ) ; logger . success ( 'PaymentW timeout, removed.' ) ; callback ( null , true , 0 ) ; } else { logger . success ( 'Nothing to parse on address..' ) ; callback ( null , null , null ) ; } } } , function ( finishWaiting , unusedBalance , callback ) { if ( finishWaiting ) { logger . success ( 'Address finishing waiting..' ) ; addressW . finishWaiting ( unusedBalance , function ( err ) { if ( err ) { logger . error ( err . toString ( ) ) ; callback ( err ) ; } else { callback ( null ) ; } } ) ; } else { callback ( null ) ; } } ] , function ( err ) { if ( err ) callback ( err ) ; else callback ( null ) ; } ) }
Check if the address waiting received their bitcoins
If the btcs are received and the btc balance its higher that the min amount that every address can have send the btcs to the mainAddress.
Check if the address waiting received their bitcoins If the btcs are received and the btc balance its higher that the min amount that every address can have send the btcs to the mainAddress .
//Get PaymentWaiting //Get utxos //Check utxos AddressWaiting //All utxos in AddressWaiting checked, now parse and sendTX if have to //Address have the necessary amount send a tx, after tx is sent its going to wait again //Sending TX //Address dont have the minimun balance to send a tx, it will be set to receive another payment //Check the X minutes timeout to receive payment //If address finish waiting we change their status so we can use it again.