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 #251 from lumberj/bug/orders_counterparty
Browse files Browse the repository at this point in the history
[BUG] Orders: Correct mishandling XRP amounts and use 'counterparty' (RLJS-172)(RLJS-174)
  • Loading branch information
wltsmrz committed Jan 6, 2015
2 parents a810a09 + a79f7c3 commit 4ae2b7e
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 73 deletions.
16 changes: 15 additions & 1 deletion api/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ function getOrders(request, response, next) {
return promise.spread(getAccountOrders);
};

function getParsedOrders(offers) {
return _.reduce(offers, function(orders, off) {
var taker_gets = utils.parseCurrencyAmount(off.taker_gets);
var taker_pays = utils.parseCurrencyAmount(off.taker_pays);

orders.push({
taker_gets: taker_gets,
taker_pays: taker_pays
});

return orders;
},[]);
}

function respondWithOrders(result) {
var promise = new Promise(function (resolve, reject) {
var orders = {};
Expand All @@ -106,7 +120,7 @@ function getOrders(request, response, next) {
orders.limit = result.limit;
orders.ledger = result.ledger_index;
orders.validated = result.validated;
orders.orders = result.offers;
orders.orders = getParsedOrders(result.offers);

resolve(respond.success(response, orders));
});
Expand Down
19 changes: 18 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module.exports = {
getPackageVersion: getPackageVersion,
getApiVersion: getApiVersion,
getUrlBase: getUrlBase,
parseLedger: parseLedger
parseLedger: parseLedger,
parseCurrencyAmount: parseCurrencyAmount
};

function dropsToXrp(drops) {
Expand Down Expand Up @@ -190,3 +191,19 @@ function parseLedger(ledger) {

return 'validated';
}

function parseCurrencyAmount(currencyAmount) {
if (typeof currencyAmount === 'string') {
return {
currency: 'XRP',
counterparty: '',
value: dropsToXrp(currencyAmount)
};
} else {
return {
currency: currencyAmount.currency,
counterparty: currencyAmount.issuer,
value: currencyAmount.value
};
}
}
110 changes: 42 additions & 68 deletions test/fixtures/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,232 +743,206 @@ module.exports.RESTAccountOrdersResponse = function(options) {
validated: options.validated,
orders: [
{
"flags": 0,
"seq": 719930,
"taker_gets": {
"currency": "EUR",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "17.70155237781915"
},
"taker_pays": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "1122.990930900328"
}
},
{
"flags": 0,
"seq": 757002,
"taker_gets": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "18.46856867857617"
},
"taker_pays": {
"currency": "USD",
"issuer": "rpDMez6pm6dBve2TJsmDpv7Yae6V5Pyvy2",
"counterparty": "rpDMez6pm6dBve2TJsmDpv7Yae6V5Pyvy2",
"value": "19.50899530491766"
}
},
{
"flags": 0,
"seq": 756999,
"taker_gets": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "19.11697137482289"
},
"taker_pays": {
"currency": "EUR",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "750"
}
},
{
"flags": 0,
"seq": 757003,
"taker_gets": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "14.40727807030772"
},
"taker_pays": {
"currency": "USD",
"issuer": "rpDMez6pm6dBve2TJsmDpv7Yae6V5Pyvy2",
"counterparty": "rpDMez6pm6dBve2TJsmDpv7Yae6V5Pyvy2",
"value": "1445.796633544794"
}
},
{
"flags": 0,
"seq": 782148,
"taker_gets": {
"currency": "NZD",
"issuer": "rsP3mgGb2tcYUrxiLFiHJiQXhsziegtwBc",
"counterparty": "rsP3mgGb2tcYUrxiLFiHJiQXhsziegtwBc",
"value": "9.178557969538755"
},
"taker_pays": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "750"
}
},
{
"flags": 0,
"seq": 787368,
"taker_gets": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "9.94768291869523"
},
"taker_pays": {
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "500"
}
},
{
"flags": 0,
"seq": 787408,
"taker_gets": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "9.994805759894176"
},
"taker_pays": {
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "10000"
}
},
{
"flags": 0,
"seq": 803438,
"taker_gets": {
"currency": "USD",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "11.67691646304319"
},
"taker_pays": {
"currency": "MXN",
"issuer": "rG6FZ31hDHN1K5Dkbma3PSB5uVCuVVRzfn",
"counterparty": "rG6FZ31hDHN1K5Dkbma3PSB5uVCuVVRzfn",
"value": "15834.53653918684"
}
},
{
"flags": 0,
"seq": 807858,
"taker_gets": {
"currency": "XAU",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "0.03206299605333101"
},
"taker_pays": {
"currency": "USD",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "3968.240250979598"
}
},
{
"flags": 0,
"seq": 807896,
"taker_gets": {
"currency": "XAU",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "0.03347459066593226"
},
"taker_pays": {
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "4139.022125516302"
}
},
{
"flags": 0,
"seq": 814018,
"taker_gets": {
"currency": "NZD",
"issuer": "rsP3mgGb2tcYUrxiLFiHJiQXhsziegtwBc",
"counterparty": "rsP3mgGb2tcYUrxiLFiHJiQXhsziegtwBc",
"value": "6.840555705"
},
"taker_pays": "115760190000"
"taker_pays": {
"currency": "XRP",
"counterparty": "",
"value": "115760.19"
}
},
{
"flags": 0,
"seq": 827522,
"taker_gets": {
"currency": "EUR",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "14.40843766044656"
},
"taker_pays": {
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "902.4050961259154"
}
},
{
"flags": 0,
"seq": 833592,
"taker_gets": {
"currency": "XAG",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "1.128432823485991"
},
"taker_pays": {
"currency": "USD",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "1814.887131319799"
}
},
{
"flags": 0,
"seq": 833591,
"taker_gets": {
"currency": "XAG",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "1.128432823485989"
},
"taker_pays": {
"currency": "USD",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "181.4887131319798"
}
},
{
"flags": 0,
"seq": 838954,
"taker_gets": {
"currency": "XAG",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "0.7283371225235964"
},
"taker_pays": {
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "118.6872603846736"
}
},
{
"flags": 0,
"seq": 843730,
"taker_gets": "2229229447",
"taker_gets": {
"currency": "XRP",
"counterparty": "",
"value": "2229.229447"
},
"taker_pays": {
"currency": "XAU",
"issuer": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"counterparty": "r9Dr5xwkeLegBeXq6ujinjSBLQzQ1zQGjH",
"value": "1"
}
},
{
"flags": 0,
"seq": 844068,
"taker_gets": {
"currency": "USD",
"issuer": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"counterparty": "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B",
"value": "17.77537376072202"
},
"taker_pays": {
"currency": "EUR",
"issuer": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"counterparty": "rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q",
"value": "750"
}
}
Expand Down
1 change: 0 additions & 1 deletion test/orders-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ suite('get orders', function() {
});
});


test('/accounts/:account/orders -- with invalid ledger', function(done) {
self.wss.once('request_account_offers', function(message, conn) {
assert.strictEqual(message.command, 'account_offers');
Expand Down
25 changes: 23 additions & 2 deletions test/unit/utils-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var assert = require('assert');
var utils = require('./../../lib/utils.js');

const DEFAULT_LEDGER = 'validated';
suite('unit - utils.parseLedger()', function() {
const DEFAULT_LEDGER = 'validated';

suite('unit - utils', function() {
test('parseLedger() -- ledger (empty string)', function() {
var ledger = '';
assert.strictEqual(utils.parseLedger(ledger), DEFAULT_LEDGER);
Expand Down Expand Up @@ -65,3 +65,24 @@ suite('unit - utils', function() {
assert.strictEqual(utils.parseLedger(ledger), DEFAULT_LEDGER);
});
});

suite('unit - utils.parseCurrencyAmount()', function() {
const nativeAmount = '1000000';
const usdAmount = { currency: 'USD', issuer: 'rMwjYedjc7qqtKYVLiAccJSmCwih4LnE2q', amount: '100' };

test('parseCurrencyAmount() -- XRP', function() {
assert.deepEqual(utils.parseCurrencyAmount(nativeAmount), {
currency: 'XRP',
counterparty: '',
value: utils.dropsToXrp(nativeAmount)
});
});

test('parseCurrencyAmount() -- USD', function() {
assert.deepEqual(utils.parseCurrencyAmount(usdAmount), {
currency: usdAmount.currency,
counterparty: usdAmount.issuer,
value: usdAmount.value
});
});
});

0 comments on commit 4ae2b7e

Please sign in to comment.