Skip to content

Commit 5af824f

Browse files
committed
[FIX] fix taker pays funded calculation
When calling `parseInt` on a string with scientific notation, it ignores the exponents.
1 parent 423ec7d commit 5af824f

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/js/ripple/orderbook.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ OrderBook.prototype.setOfferFundedAmount = function(offer) {
640640
);
641641

642642
offer.taker_pays_funded = this._currencyPays.is_native()
643-
? String(parseInt(takerPaysFunded.to_json().value, 10))
643+
? String(Math.floor(takerPaysFunded.to_number()))
644644
: takerPaysFunded.to_json().value;
645645
} else {
646646
offer.taker_gets_funded = '0';

test/fixtures/orderbook.js

+31
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,37 @@ module.exports.QUALITY_OFFERS = [
372372
}
373373
];
374374

375+
// This fixture is to exercise a bug where taker_pays_funded = taker_gets_funded * quality
376+
// has decimal amounts.
377+
module.exports.DECIMAL_TAKER_PAYS_FUNDED_OFFERS = [
378+
{
379+
Account: addresses.ACCOUNT,
380+
BookDirectory: '4627DFFCFF8B5A265EDBD8AE8C14A52325DBFEDAF4F5C32E5D0689673FA9094A',
381+
BookNode: '0000000000000000',
382+
Flags: 0,
383+
LedgerEntryType: 'Offer',
384+
OwnerNode: '0000000000000006',
385+
PreviousTxnID: 'C1BB04CE39E30BF5982B7660793723E9B3A832F5B458DB1C5938F4737E0E9ABF',
386+
PreviousTxnLgrSeq: 11631257,
387+
Sequence: 2936,
388+
TakerGets: {
389+
currency: 'USD',
390+
issuer: addresses.ISSUER,
391+
value: '9280.04'
392+
},
393+
TakerPays: '1707459061637',
394+
index: '89D85BBE91E0F419953EB89CE62E194922ED930EE57BE0C62FCC3B22DDB20852',
395+
owner_funds: '9280.037154029904',
396+
quality: '183992640.2943306',
397+
taker_gets_funded: {
398+
currency: 'USD',
399+
issuer: addresses.ISSUER,
400+
value: '9261.514125778347'
401+
},
402+
taker_pays_funded: '1704050437125'
403+
}
404+
];
405+
375406
module.exports.bookOffersResponse = function (options) {
376407
options = options || {};
377408
_.defaults(options, {

test/orderbook-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,27 @@ describe('OrderBook', function() {
15331533
assert.strictEqual(book.getOwnerFunds(addresses.FOURTH_ACCOUNT).to_text(), '7229.594289344439');
15341534
});
15351535

1536+
it('Set offers - incorrect taker pays funded', function() {
1537+
var remote = new Remote();
1538+
1539+
var book = remote.createOrderBook({
1540+
currency_gets: 'USD',
1541+
issuer_gets: addresses.ISSUER,
1542+
currency_pays: 'XRP'
1543+
});
1544+
1545+
book._issuerTransferRate = 1002000000;
1546+
1547+
var offers = fixtures.DECIMAL_TAKER_PAYS_FUNDED_OFFERS;
1548+
1549+
book.setOffers(offers);
1550+
1551+
assert.strictEqual(book._offers.length, 1);
1552+
1553+
assert.strictEqual(book._offers[0].taker_gets_funded, '9261.514125778347');
1554+
assert.strictEqual(book._offers[0].taker_pays_funded, '1704050437125');
1555+
});
1556+
15361557
it('Notify - created node', function() {
15371558
var remote = new Remote();
15381559

0 commit comments

Comments
 (0)