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 client = new RippleSimpleClient({
  apiUrl: process.env.RIPPLE_SIMPLE_API
});

getHotWallet(function(wallet){
  address = wallet.address;
  client.address = wallet.address;
  getNextNotification();
});

function getNextNotification() {
  client.getNextNotification(function(err, notification){
    if (notification) {
      handleNewNotification(notification);
    } 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 handleNewNotification(notification) {
  if (notification.type == 'payment') {
    console.log(notification.tx_hash);
    lookupTransaction(notification.tx_hash, function(err, transaction) {
      console.log(err);
      console.log(transaction);
      if (transaction) {
        transaction.transaction_state = notification.tx_result;
        transaction.save().complete(function(err, transaction){
          console.log(transaction); 
        });
      } else if (notification.tx_direction == 'inbound') {
        console.log('inbound transaction');
      }
    });
  }
}

@@ -349,16 +349,174 @@
 PI%0A%7D);%0A%0A
+%0A// Proposed!!%0A/*%0Aclient.on('payment:inbound', function(payment) %7B%0A%0A%7D);%0A%0Aclient.on('payment:outbound', function(payment) %7B%0A%0A%7D);%0A%0Aclient.listener.start();%0A*/%0A%0A
 getHotWa
@@ -626,16 +626,74 @@
 tion();%0A
+  console.log(%22listening for transactions to %22, address);%0A
 %7D);%0A%0Afun
@@ -812,28 +812,33 @@
       handle
-NewN
+Payment(n
 otification(
@@ -836,29 +836,84 @@
 fication
-(notification
+, function()%7B%0A        setTimeout(getNextNotification, 2000);%0A      %7D
 );%0A    %7D
@@ -1283,23 +1283,1447 @@
 ndle
-NewNotification
+UnknownPayment(notification, fn) %7B%0A  client.getPayment(notification.tx_hash, function(err, payment) %7B%0A    if (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            console.log(transaction.toJSON());%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
 (not
@@ -2727,24 +2727,27 @@
 notification
+,fn
 ) %7B%0A  if (no
@@ -2782,47 +2782,8 @@
 ) %7B%0A
-    console.log(notification.tx_hash);%0A
     
@@ -2873,43 +2873,28 @@
 log(
-err);%0A      console.log(transaction
+notification.tx_hash
 );%0A 
@@ -2929,293 +2929,156 @@
     
-transaction.transaction_state = notification.tx_result;%0A        transaction.save().complete(function(err, transaction)%7B%0A          console.log(transaction); %0A        %7D);%0A      %7D else if (notification.tx_direction == 'inbound') %7B%0A        console.log('inbound transaction');%0A      %7D
+handleKnownPayment(notification, transaction, fn);%0A      %7D else %7B%0A        handleUnknownPayment(notification, fn);%0A      %7D%0A    %7D);%0A  %7D else %7B
 %0A    
-%7D
+fn(
 );%0A 
@@ -3082,8 +3082,9 @@
 ;%0A  %7D%0A%7D%0A
+%0A

Create new ripple transactions when handling notification.