var Balance = require("../models/balance");
var PendingRippleTransaction = require("../models/pending_ripple_transaction");
var Sequelize = require('sequelize');
var db = require('../../config/initializers/sequelize');

var RippleTx = sequelize.define('ripple_transaction', {
  id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true, notNull: true },
  toCurrency: { type: Sequelize.STRING, notNull: true },
  toAddress: { type: Sequelize.STRING, notNull: true },
  toCurrency: { type: Sequelize.STRING, notNull: true },
  toAmount: { type: Sequelize.DECIMAL, notNull: true },
  fromCurrency: { type: Sequelize.STRING, notNull: true },
  fromAmount: { type: Sequelize.DECIMAL, notNull: true },
  fromAddress: { type: Sequelize.STRING, notNull: true },
  destinationTag: { type: Sequelize.STRING },
  balanceId: { type: Sequelize.INTEGER, notNull: true },
  issuance: { type: Sequelize.BOOLEAN, notNull: true },
  txHash: { type: Sequelize.STRING },
  txState: Sequelize.STRING
}, {
  classMethods: {
		createIncomingWithBalance: function(balance, txParams, fn) {
			var RippleTransaction = this;
			txParams.balanceId = balance.id;	
			txParams.bankAccountId = txParams.destinationTag;
			RippleTransaction.create(txParams).complete(function(err, tx) {
				if (err) { fn(err, null); return false; }
			  fn(null, rippleTransaction);	
			});
		}	
	},
	instanceMethods: {
		updateBalance: function(fn){
			var rippleTransaction = this;
			Balance.find(rippleTransaction.balanceId).complete(function(err, balance) {
				var oldAmount = parseFloat(balance.amount);
				var newAmount = rippleTransaction.toAmount;
				var sign = rippleTransaction.issuance ? -1 : 1;
				var diff = sign * parseFloat(rippleTransaction.toAmount);
				balance.updateAttributes({
					amount: (oldAmount + diff)
				}).complete(fn);
			});
		}
	}
});

module.exports = RippleTx;

@@ -274,16 +274,28 @@
  %7B%0A  id:
+            
  %7B type:
@@ -350,23 +350,8 @@
 ent:
- true, notNull:
  tru
@@ -360,32 +360,36 @@
 %7D,%0A  toCurrency:
+    
  %7B type: Sequeli
@@ -420,32 +420,37 @@
  %7D,%0A  toAddress:
+     
  %7B type: Sequeli
@@ -482,32 +482,36 @@
 %7D,%0A  toCurrency:
+    
  %7B type: Sequeli
@@ -545,24 +545,30 @@
 %0A  toAmount:
+      
  %7B type: Seq
@@ -615,16 +615,18 @@
 urrency:
+  
  %7B type:
@@ -674,16 +674,20 @@
 mAmount:
+    
  %7B type:
@@ -737,16 +737,19 @@
 Address:
+   
  %7B type:
@@ -788,54 +788,8 @@
  %7D,%0A
-  destinationTag: %7B type: Sequelize.STRING %7D,%0A
   ba
@@ -796,16 +796,21 @@
 lanceId:
+     
  %7B type:
@@ -857,16 +857,22 @@
 ssuance:
+      
  %7B type:
@@ -914,14 +914,22 @@
 ,%0A  
-txHash
+destinationTag
 : %7B 
@@ -962,879 +962,97 @@
   tx
-State: Sequelize.STRING%0A%7D, %7B%0A  classMethods: %7B%0A%09%09createIncomingWithBalance: function(balance, txParams, fn) %7B%0A%09%09%09var RippleTransaction = this;%0A%09%09%09txParams.balanceId = balance.id;%09%0A%09%09%09txParams.bankAccountId = txParams.destinationTag;%0A%09%09%09RippleTransaction.create(txParams).complete(function(err, tx) %7B%0A%09%09%09%09if (err) %7B fn(err, null); return false; %7D%0A%09%09%09  fn(null, rippleTransaction);%09%0A%09%09%09%7D);%0A%09%09%7D%09%0A%09%7D,%0A%09instanceMethods: %7B%0A%09%09updateBalance: function(fn)%7B%0A%09%09%09var rippleTransaction = this;%0A%09%09%09Balance.find(rippleTransaction.balanceId).complete(function(err, balance) %7B%0A%09%09%09%09var oldAmount = parseFloat(balance.amount);%0A%09%09%09%09var newAmount = rippleTransaction.toAmount;%0A%09%09%09%09var sign = rippleTransaction.issuance ? -1 : 1;%0A%09%09%09%09var diff = sign * parseFloat(rippleTransaction.toAmount);%0A%09%09%09%09balance.updateAttributes(%7B%0A%09%09%09%09%09amount: (oldAmount + diff)%0A%09%09%09%09%7D).complete(fn);%0A%09%09%09%7D);%0A%09%09%7D%0A%09
+Hash:         %7B type: Sequelize.STRING %7D,%0A  txState:        %7B type: Sequelize.STRING 
 %7D%0A%7D)
-;
 %0A%0Amo

Remove methods for creating ripple txs with balance records.