var Ripple = require('ripple-lib')
var request = require('request')
var RippleAddress = require('../app/models/ripple_address.js');
var RippleTransaction = require('../app/models/ripple_transaction.js');
var RippleSimpleClient = require('../lib/ripple_simple_client.js');
var sequelize = require("../config/initializers/sequelize.js");
var nconf = require('../config/nconf.js');


var client = new RippleSimpleClient({
  apiUrl: nconf.get('RIPPLE_REST_API')
});


// Proposed!!
/*
client.on('payment:inbound', function(payment) {

});

client.on('payment:outbound', function(payment) {

});

client.listener.start();
*/

sequelize.sync().success(function(){

  getHotWallet(function(wallet){
    address = wallet.address;
    client.address = wallet.address;
    getNextNotification();
    console.log("listening for transactions to ", address);
  });

});

function getNextNotification() {
  client.getNextNotification(function(err, notification){
    if (notification) {
      handlePayment(notification, function(){
        setTimeout(getNextNotification, 2000);
      });
    } else {
      setTimeout(getNextNotification, 2000);
    }
  });
}

function getHotWallet(fn) {
  RippleAddress.find({ where: { type: 'hot' }}).complete(function(err, address) {
    fn(address);
  });
}

function lookupTransaction(transactionHash, fn){
  RippleTransaction.find({ where: { 
    transaction_hash: transactionHash 
  }}).complete(fn);
}

function handleUnknownPayment(notification, fn) {
  client.getPayment(notification.tx_hash, function(err, payment) {
    if (payment && payment.dst_tag) {
      RippleAddress.find({ where: { 
        address: address, 
        tag: payment.dst_tag.toString() 
      }}).complete(function(err, address){
        if (address) {
          RippleTransaction.create({
            ripple_address_id: address.id,
            to_address: payment.dst_address,
            from_address: payment.src_address,
            from_amount: payment.src_amount.value,
            from_currency: payment.src_amount.currency,
            from_issuer: payment.src_amount.issuer,
            to_address: payment.dst_address,
            to_amount: payment.dst_amount.value,
            to_currency: payment.dst_amount.currency,
            to_issuer: payment.dst_amount.issuer,
            transaction_state: payment.tx_state,
            transaction_hash: payment.tx_hash,
            issuance: false
          }).complete(function(err, transaction) {
            console.log(err);
            fn();
          });
        } else {
          console.log(err);
          fn();
        }
      });
    } else {
      console.log('no destination tag');
      fn();
    }
  });
}

function handleKnownPayment(notification, record, fn) {
  record.transaction_state = notification.tx_result;
  record.save().complete(fn);
}

function handlePayment(notification,fn) {
  if (notification.type == 'payment') {
    lookupTransaction(notification.tx_hash, function(err, transaction) {
      console.log(notification.tx_hash);
      if (transaction) {
        handleKnownPayment(notification, transaction, fn);
      } else {
        handleUnknownPayment(notification, fn);
      }
    });
  } else {
    fn();
  }
}


@@ -65,276 +65,8 @@
 t')%0A
-var RippleAddress = require('../app/models/ripple_address.js');%0Avar RippleTransaction = require('../app/models/ripple_transaction.js');%0Avar RippleSimpleClient = require('../lib/ripple_simple_client.js');%0Avar sequelize = require(%22../config/initializers/sequelize.js%22);%0A
 var 
@@ -105,17 +105,16 @@
 .js');%0A%0A
-%0A
 var clie
@@ -187,17 +187,16 @@
 ')%0A%7D);%0A%0A
-%0A
 // Propo
@@ -215,62 +215,16 @@
 ent.
-on('payment:inbound', function(payment) %7B%0A%0A%7D);%0A%0Aclient
+listener
 .on(
@@ -232,19 +232,18 @@
 payment:
-out
+in
 bound', 
@@ -266,2634 +266,182 @@
 ) %7B%0A
-%0A%7D);%0A%0Aclient.listener.start();%0A*/%0A%0Asequelize.sync().success(function()%7B%0A%0A  getHotWallet(function(wallet)%7B%0A    address = wallet.address;%0A    client.address = wallet.address;%0A    getNextNotification();%0A    console.log(%22listening for transactions to %22, address);%0A  %7D);%0A%0A%7D);%0A%0Afunction getNextNotification() %7B%0A  client.getNextNotification(function(err, notification)%7B%0A    if (notification) %7B%0A      handlePayment(notification, function()%7B%0A        setTimeout(getNextNotification, 2000);%0A      %7D);%0A    %7D else %7B%0A      setTimeout(getNextNotification, 2000);%0A    %7D%0A  %7D);%0A%7D%0A%0Afunction getHotWallet(fn) %7B%0A  RippleAddress.find(%7B where: %7B type: 'hot' %7D%7D).complete(function(err, address) %7B%0A    fn(address);%0A  %7D);%0A%7D%0A%0Afunction lookupTransaction(transactionHash, fn)%7B%0A  RippleTransaction.find(%7B where: %7B %0A    transaction_hash: transactionHash %0A  %7D%7D).complete(fn);%0A%7D%0A%0Afunction handleUnknownPayment(notification, fn) %7B%0A  client.getPayment(notification.tx_hash, function(err, payment) %7B%0A    if (payment && payment.dst_tag) %7B%0A      RippleAddress.find(%7B where: %7B %0A        address: address, %0A        tag: payment.dst_tag.toString() %0A      %7D%7D).complete(function(err, address)%7B%0A        if (address) %7B%0A          RippleTransaction.create(%7B%0A            ripple_address_id: address.id,%0A            to_address: payment.dst_address,%0A            from_address: payment.src_address,%0A            from_amount: payment.src_amount.value,%0A            from_currency: payment.src_amount.currency,%0A            from_issuer: payment.src_amount.issuer,%0A            to_address: payment.dst_address,%0A            to_amount: payment.dst_amount.value,%0A            to_currency: payment.dst_amount.currency,%0A            to_issuer: payment.dst_amount.issuer,%0A            transaction_state: payment.tx_state,%0A            transaction_hash: payment.tx_hash,%0A            issuance: false%0A          %7D).complete(function(err, transaction) %7B%0A            console.log(err);%0A            fn();%0A          %7D);%0A        %7D else %7B%0A          console.log(err);%0A          fn();%0A        %7D%0A      %7D);%0A    %7D else %7B%0A      console.log('no destination tag');%0A      fn();%0A    %7D%0A  %7D);%0A%7D%0A%0Afunction handleKnownPayment(notification, record, fn) %7B%0A  record.transaction_state = notification.tx_result;%0A  record.save().complete(fn);%0A%7D%0A%0Afunction handlePayment(notification,fn) %7B%0A  if (notification.type == 'payment') %7B%0A    lookupTransaction(notification.tx_hash, function(err, transaction) %7B%0A      console.log(notification.tx_hash);%0A      if (transaction) %7B%0A        handleKnownPayment(notification, transaction, fn);%0A      %7D else %7B%0A        handleUnknownPayment(notification, fn);%0A      %7D%0A    %7D);%0A  %7D else %7B%0A    fn();%0A  %7D%0A%7D%0A
+  // create a new ripple transaction%0A%7D);%0A%0Aclient.listener.on('payment:outbound', function(payment) %7B%0A  // update an existing ripple transaction%0A%7D);%0A%0Aclient.listener.start();%0A%0A*/
 %0A

Remove models from incoming listener worker.