Skip to content
This repository has been archived by the owner on Sep 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #255 from clark800/feature/new_balance_changes
Browse files Browse the repository at this point in the history
[TASK] Switch to using ripple-lib-transactionparser to compute transaction balance changes
  • Loading branch information
geertweening committed Jan 7, 2015
2 parents 999c034 + 8b900bf commit ea2a731
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 24 deletions.
34 changes: 19 additions & 15 deletions lib/tx-to-rest-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ var ripple = require('ripple-lib');
var utils = require(__dirname+'/utils');
var _ = require('lodash');
var settings = require('./../api/settings.js');
var parseBalanceChanges = require('ripple-lib-transactionparser').parseBalanceChanges;

function TxToRestConverter() {};

// This is just to support the legacy naming of "counterparty", this
// function should be removed when "issuer" is eliminated
function renameCounterpartyToIssuer(balanceChanges) {
return _.mapValues(balanceChanges, function(changes) {
return _.map(changes, function(change) {
var converted = _.omit(change, 'counterparty');
converted.issuer = change.counterparty;
return converted;
});
});
}

// Paths

/**
Expand Down Expand Up @@ -61,6 +74,9 @@ TxToRestConverter.prototype.parsePaymentFromTx = function(tx, options, callback)
Amount = tx.Amount;
}

var balanceChanges = tx.meta ?
renameCounterpartyToIssuer(parseBalanceChanges(tx.meta)) : [];

var payment = {
// User supplied
source_account: tx.Account,
Expand Down Expand Up @@ -106,21 +122,9 @@ TxToRestConverter.prototype.parsePaymentFromTx = function(tx, options, callback)
result: tx.meta ? tx.meta.TransactionResult : '',
timestamp: (tx.date ? new Date(ripple.utils.toTimestamp(tx.date)).toISOString() : ''),
fee: utils.dropsToXrp(tx.Fee) || '',
source_balance_changes: [],
destination_balance_changes: []
source_balance_changes: balanceChanges[tx.Account] || [],
destination_balance_changes: balanceChanges[tx.Destination] || []
};
// Add source_balance_changes
utils.parseBalanceChanges(tx, tx.Account).forEach(function(amount){
if (amount.value < 0) {
payment.source_balance_changes.push(amount);
}
});
// Add destination_balance_changes
utils.parseBalanceChanges(tx, tx.Destination).forEach(function(amount){
if (amount.value > 0) {
payment.destination_balance_changes.push(amount);
}
});
if (Array.isArray(tx.Memos) && tx.Memos.length > 0) {
payment.memos = [];
for(var m=0; m<tx.Memos.length; m++) {
Expand Down Expand Up @@ -335,4 +339,4 @@ TxToRestConverter.prototype.parseFlagsFromResponse = function(responseFlags, fla
return parsedFlags;
};

module.exports = new TxToRestConverter();
module.exports = new TxToRestConverter();
5 changes: 5 additions & 0 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
"nconf": "~0.6.9",
"node-uuid": "~1.4.1",
"ripple-lib": "^0.10.1-rc2",
"ripple-lib-transactionparser": "^0.1.0",
"sqlite3": "~3.0.2",
"supertest": "^0.13.0",
"winston": "^0.7.3"
},
"devDependencies": {
"assert-diff": "*",
"chai": "*",
"coveralls": "~2.10.0",
"istanbul": "^0.2.10",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ module.exports.RESTTransactionResponseComplexCurrencies = function(options) {
{
"value": "-1e-8",
"currency": "0158415500000000C1F76FF6ECB0BAC600000000",
"issuer": "r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE"
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B"
}
],
"destination_balance_changes": [
Expand Down
2 changes: 1 addition & 1 deletion test/payments-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _ = require('lodash');
var assert = require('assert');
var assert = require('assert-diff');
var ripple = require('ripple-lib');
var testutils = require('./testutils');
var fixtures = require('./fixtures').payments;
Expand Down
4 changes: 2 additions & 2 deletions test/testutils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _ = require('lodash');
var assert = require('assert');
var assert = require('assert-diff');
var async = require('async');
var supertest = require('supertest');
var WSS = require('ws').Server;
Expand Down Expand Up @@ -83,7 +83,7 @@ function checkHeaders(res) {
function checkBody(expected) {
return function(res, err) {
assert.ifError(err);
assert.strictEqual(JSON.stringify(res.body), expected);
assert.deepEqual(res.body, JSON.parse(expected));
};
};

Expand Down
6 changes: 5 additions & 1 deletion test/unit/fixtures/tx-converter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var _ = require('lodash');
var addresses = require('./../../fixtures').addresses;

// VALID's EUR balance with ISSUER goes down from 6.948 to 6.113
// VALID's XRP balance goes down 15000 drops for the fee
// rGAW's USD balance with ISSUER goes down from 615 to 614
// rGAW's EUR balance with ISSUER goes up from 9988 to 9989
module.exports.COMPLICATED_META = {
AffectedNodes : [
{
Expand Down Expand Up @@ -1069,4 +1073,4 @@ module.exports.settingResponseRest = {
hash: '0F480D344CFC610DFA5CAC62CC1621C92953A05FE8C319281CA49C5C162AF40E',
ledger: 8820076,
state: 'validated'
};
};
9 changes: 5 additions & 4 deletions test/unit/tx-converter-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var assert = require('assert');
var assert = require('assert-diff');
var fixtures = require('./fixtures').txConverter;
var addresses = require('./../fixtures').addresses;
var txToRestConverter = require('./../../lib/tx-to-rest-converter.js');
Expand All @@ -22,6 +22,7 @@ suite('unit - converter - Tx to Rest', function() {
var tx = fixtures.paymentTx({
meta: fixtures.COMPLICATED_META
});
tx.Destination = 'rGAWXLxpsy77vWxgYriPZE5ktUfqa6prbG';
var options = {
account: addresses.VALID
};
Expand All @@ -35,8 +36,8 @@ suite('unit - converter - Tx to Rest', function() {
]);

assert.deepEqual(payment.destination_balance_changes, [
{ value: '0.001666666666999', currency: 'EUR', issuer: 'r3PDtZSa5LiYp1Ysn1vMuMzB59RzV3W9QH' },
{ value: '1', currency: 'USD', issuer: 'r3PDtZSa5LiYp1Ysn1vMuMzB59RzV3W9QH' }
{ value: '-1', currency: 'USD', issuer: 'r3PDtZSa5LiYp1Ysn1vMuMzB59RzV3W9QH' },
{ value: '0.833333333333', currency: 'EUR', issuer: 'r3PDtZSa5LiYp1Ysn1vMuMzB59RzV3W9QH' }
]);

done();
Expand Down Expand Up @@ -157,4 +158,4 @@ suite('unit - converter - Tx to Rest', function() {

done();
});
});
});

0 comments on commit ea2a731

Please sign in to comment.