mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-24 06:05:51 +00:00
Convert getOrderBook and add unit test
This commit is contained in:
@@ -1,14 +1,27 @@
|
||||
'use strict';
|
||||
const parseOrderBase = require('./order-base');
|
||||
const utils = require('./utils');
|
||||
const flags = utils.core.Remote.flags.offer;
|
||||
const parseAmount = require('./amount');
|
||||
|
||||
// rippled 'account_offers' returns a different format for orders than 'tx'
|
||||
function parseAccountOrder(order) {
|
||||
const specification = parseOrderBase(
|
||||
order.taker_gets, order.taker_pays, order.flags);
|
||||
const state = {
|
||||
// the flags are also different
|
||||
function parseAccountOrder(order: Object): Object {
|
||||
const direction = (order.flags & flags.Sell) === 0 ? 'buy' : 'sell';
|
||||
const takerGetsAmount = parseAmount(order.taker_gets);
|
||||
const takerPaysAmount = parseAmount(order.taker_pays);
|
||||
const quantity = (direction === 'buy') ? takerPaysAmount : takerGetsAmount;
|
||||
const totalPrice = (direction === 'buy') ? takerGetsAmount : takerPaysAmount;
|
||||
|
||||
const specification = utils.removeUndefined({
|
||||
direction: direction,
|
||||
quantity: quantity,
|
||||
totalPrice: totalPrice,
|
||||
passive: ((order.flags & flags.Passive) !== 0) || undefined
|
||||
});
|
||||
const properties = {
|
||||
sequence: order.seq
|
||||
};
|
||||
return {specification, state};
|
||||
return {specification, properties};
|
||||
}
|
||||
|
||||
module.exports = parseAccountOrder;
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
/* @flow */
|
||||
'use strict';
|
||||
const utils = require('./utils');
|
||||
const parseAmount = require('./amount');
|
||||
const orderFlags = utils.core.Transaction.flags.OfferCreate;
|
||||
|
||||
/*:: type Amount = string | {currency: string, issuer: string, value: string} */
|
||||
function parseOrder(takerGets: Amount, takerPays: Amount, flags: number):
|
||||
Object {
|
||||
const direction = (flags & orderFlags.Sell) === 0 ? 'buy' : 'sell';
|
||||
const takerGetsAmount = parseAmount(takerGets);
|
||||
const takerPaysAmount = parseAmount(takerPays);
|
||||
const quantity = (direction === 'buy') ? takerPaysAmount : takerGetsAmount;
|
||||
const totalPrice = (direction === 'buy') ? takerGetsAmount : takerPaysAmount;
|
||||
|
||||
return utils.removeUndefined({
|
||||
direction: direction,
|
||||
quantity: quantity,
|
||||
totalPrice: totalPrice,
|
||||
passive: ((flags & orderFlags.Passive) !== 0) || undefined,
|
||||
immediateOrCancel: ((flags & orderFlags.ImmediateOrCancel) !== 0)
|
||||
|| undefined,
|
||||
fillOrKill: ((flags & orderFlags.FillOrKill) !== 0) || undefined
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = parseOrder;
|
||||
@@ -1,11 +1,28 @@
|
||||
/* @flow */
|
||||
'use strict';
|
||||
const assert = require('assert');
|
||||
const parseOrderBase = require('./order-base');
|
||||
const utils = require('./utils');
|
||||
const parseAmount = require('./amount');
|
||||
const flags = utils.core.Transaction.flags.OfferCreate;
|
||||
|
||||
function parseOrder(tx: Object): Object {
|
||||
assert(tx.TransactionType === 'OfferCreate');
|
||||
return parseOrderBase(tx.TakerGets, tx.TakerPays, tx.Flags);
|
||||
|
||||
const direction = (tx.Flags & flags.Sell) === 0 ? 'buy' : 'sell';
|
||||
const takerGetsAmount = parseAmount(tx.TakerGets);
|
||||
const takerPaysAmount = parseAmount(tx.TakerPays);
|
||||
const quantity = (direction === 'buy') ? takerPaysAmount : takerGetsAmount;
|
||||
const totalPrice = (direction === 'buy') ? takerGetsAmount : takerPaysAmount;
|
||||
|
||||
return utils.removeUndefined({
|
||||
direction: direction,
|
||||
quantity: quantity,
|
||||
totalPrice: totalPrice,
|
||||
passive: ((tx.Flags & flags.Passive) !== 0) || undefined,
|
||||
immediateOrCancel: ((tx.Flags & flags.ImmediateOrCancel) !== 0)
|
||||
|| undefined,
|
||||
fillOrKill: ((tx.Flags & flags.FillOrKill) !== 0) || undefined
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = parseOrder;
|
||||
|
||||
40
src/api/ledger/parse/orderbook-order.js
Normal file
40
src/api/ledger/parse/orderbook-order.js
Normal file
@@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
const _ = require('lodash');
|
||||
const utils = require('./utils');
|
||||
const flags = utils.core.Remote.flags.offer;
|
||||
const parseAmount = require('./amount');
|
||||
|
||||
function parseOrderbookOrder(order: Object): Object {
|
||||
const direction = (order.Flags & flags.Sell) === 0 ? 'buy' : 'sell';
|
||||
const takerGetsAmount = parseAmount(order.TakerGets);
|
||||
const takerPaysAmount = parseAmount(order.TakerPays);
|
||||
const quantity = (direction === 'buy') ? takerPaysAmount : takerGetsAmount;
|
||||
const totalPrice = (direction === 'buy') ? takerGetsAmount : takerPaysAmount;
|
||||
|
||||
const specification = utils.removeUndefined({
|
||||
direction: direction,
|
||||
quantity: quantity,
|
||||
totalPrice: totalPrice,
|
||||
passive: ((order.Flags & flags.Passive) !== 0) || undefined
|
||||
});
|
||||
// "quality" is omitted intentionally as it corresponds to
|
||||
// either price or inverse price, and it is better to avoid
|
||||
// inverting floats where precision issues can arise
|
||||
const properties = {
|
||||
maker: order.Account,
|
||||
sequence: order.Sequence
|
||||
};
|
||||
const takerGetsFunded = order.taker_gets_funded ?
|
||||
parseAmount(order.taker_gets_funded) : undefined;
|
||||
const takerPaysFunded = order.taker_pays_funded ?
|
||||
parseAmount(order.taker_pays_funded) : undefined;
|
||||
const available = utils.removeUndefined({
|
||||
availableQuantity: direction === 'buy' ? takerPaysFunded : takerGetsFunded,
|
||||
priceOfAvailableQuantity: direction === 'buy' ?
|
||||
takerGetsFunded : takerPaysFunded
|
||||
});
|
||||
const state = _.isEmpty(available) ? undefined : available;
|
||||
return utils.removeUndefined({specification, properties, state});
|
||||
}
|
||||
|
||||
module.exports = parseOrderbookOrder;
|
||||
Reference in New Issue
Block a user