diff --git a/api/orders.js b/api/orders.js index 42732893..7c08380b 100644 --- a/api/orders.js +++ b/api/orders.js @@ -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 = {}; @@ -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)); }); diff --git a/lib/utils.js b/lib/utils.js index a4c43483..4d8ca605 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -11,7 +11,8 @@ module.exports = { getPackageVersion: getPackageVersion, getApiVersion: getApiVersion, getUrlBase: getUrlBase, - parseLedger: parseLedger + parseLedger: parseLedger, + parseCurrencyAmount: parseCurrencyAmount }; function dropsToXrp(drops) { @@ -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 + }; + } +} diff --git a/test/fixtures/orders.js b/test/fixtures/orders.js index 50ac255a..6edaf2c6 100644 --- a/test/fixtures/orders.js +++ b/test/fixtures/orders.js @@ -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" } } diff --git a/test/orders-test.js b/test/orders-test.js index 1284d73c..02464448 100644 --- a/test/orders-test.js +++ b/test/orders-test.js @@ -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'); diff --git a/test/unit/utils-test.js b/test/unit/utils-test.js index de65bfba..884905c0 100644 --- a/test/unit/utils-test.js +++ b/test/unit/utils-test.js @@ -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); @@ -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 + }); + }); +});