Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Chris Clark
committed
Jun 25, 2015
1 parent
3960b4e
commit 84bc7dd
Showing
14 changed files
with
662 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,37 @@ | ||
/* eslint-disable valid-jsdoc */ | ||
'use strict'; | ||
const _ = require('lodash'); | ||
const utils = require('./utils'); | ||
const ripple = utils.common.core; | ||
const validate = utils.common.validate; | ||
const composeAsync = utils.common.composeAsync; | ||
const parseAccountOrder = require('./parse/account-order'); | ||
|
||
function requestAccountOffers(remote, address, ledgerVersion, options, | ||
marker, limit, callback) { | ||
remote.requestAccountOffers({ | ||
account: address, | ||
marker: marker, | ||
limit: limit, | ||
ledger: ledgerVersion | ||
}, | ||
composeAsync((data) => ({ | ||
marker: data.marker, | ||
results: data.offers.map(parseAccountOrder) | ||
}), callback)); | ||
} | ||
|
||
const DefaultPageLimit = 200; | ||
|
||
/** | ||
* Get orders from the ripple network | ||
* | ||
* @query | ||
* @param {String} [request.query.limit] | ||
* - Set a limit to the number of results returned | ||
* @param {String} [request.query.marker] | ||
* - Used to paginate results | ||
* @param {String} [request.query.ledger] | ||
* - The ledger index to query against | ||
* - (required if request.query.marker is present) | ||
* | ||
* @url | ||
* @param {RippleAddress} request.params.account | ||
* - The ripple address to query orders | ||
* | ||
*/ | ||
function getOrders(account, options, callback) { | ||
const self = this; | ||
|
||
function getAccountOrders(account, options, callback) { | ||
validate.address(account); | ||
validate.options(options); | ||
|
||
function getAccountOrders(prevResult) { | ||
const isAggregate = options.limit === undefined; | ||
if (prevResult && (!isAggregate || !prevResult.marker)) { | ||
return Promise.resolve(prevResult); | ||
} | ||
|
||
const promise = new Promise(function(resolve, reject) { | ||
let accountOrdersRequest; | ||
let marker; | ||
let ledger; | ||
let limit; | ||
|
||
if (prevResult) { | ||
marker = prevResult.marker; | ||
limit = prevResult.limit; | ||
ledger = prevResult.ledger_index; | ||
} else { | ||
marker = options.marker; | ||
limit = options.limit || DefaultPageLimit; | ||
ledger = utils.parseLedger(options.ledger); | ||
} | ||
|
||
accountOrdersRequest = self.remote.requestAccountOffers({ | ||
account: account, | ||
marker: marker, | ||
limit: limit, | ||
ledger: ledger | ||
}); | ||
|
||
accountOrdersRequest.once('error', reject); | ||
accountOrdersRequest.once('success', function(nextResult) { | ||
nextResult.offers = prevResult ? | ||
nextResult.offers.concat(prevResult.offers) : nextResult.offers; | ||
resolve(nextResult); | ||
}); | ||
accountOrdersRequest.request(); | ||
}); | ||
|
||
return promise.then(getAccountOrders); | ||
} | ||
|
||
function getParsedOrders(offers) { | ||
return _.reduce(offers, function(orders, off) { | ||
const sequence = off.seq; | ||
const type = off.flags & ripple.Remote.flags.offer.Sell ? 'sell' : 'buy'; | ||
const passive = (off.flags & ripple.Remote.flags.offer.Passive) !== 0; | ||
|
||
const taker_gets = utils.parseCurrencyAmount(off.taker_gets); | ||
const taker_pays = utils.parseCurrencyAmount(off.taker_pays); | ||
|
||
orders.push({ | ||
type: type, | ||
taker_gets: taker_gets, | ||
taker_pays: taker_pays, | ||
sequence: sequence, | ||
passive: passive | ||
}); | ||
|
||
return orders; | ||
}, []); | ||
} | ||
|
||
function respondWithOrders(result) { | ||
const promise = new Promise(function(resolve) { | ||
const orders = {}; | ||
|
||
if (result.marker) { | ||
orders.marker = result.marker; | ||
} | ||
|
||
orders.limit = result.limit; | ||
orders.ledger = result.ledger_index; | ||
orders.validated = result.validated; | ||
orders.orders = getParsedOrders(result.offers); | ||
|
||
resolve(callback(null, orders)); | ||
}); | ||
|
||
return promise; | ||
} | ||
|
||
getAccountOrders() | ||
.then(respondWithOrders) | ||
.catch(callback); | ||
const defaultLimit = 100; | ||
const limit = options.limit || defaultLimit; | ||
const ledgerVersion = options.ledgerVersion | ||
|| this.remote.getLedgerSequence(); | ||
const getter = _.partial(requestAccountOffers, this.remote, account, | ||
ledgerVersion, options); | ||
utils.getRecursive(getter, limit, | ||
composeAsync((orders) => _.sortBy(orders, (order) => order.state.sequence), | ||
callback)); | ||
} | ||
|
||
module.exports = getOrders; | ||
module.exports = getAccountOrders; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
const parseOrderBase = require('./order-base'); | ||
|
||
// 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 = { | ||
sequence: order.seq | ||
}; | ||
return {specification, state}; | ||
} | ||
|
||
module.exports = parseAccountOrder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* @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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,11 @@ | ||
/* @flow */ | ||
'use strict'; | ||
const assert = require('assert'); | ||
const utils = require('./utils'); | ||
const parseAmount = require('./amount'); | ||
const flags = utils.core.Transaction.flags.OfferCreate; | ||
const parseOrderBase = require('./order-base'); | ||
|
||
function parseOrder(tx: Object): Object { | ||
assert(tx.TransactionType === 'OfferCreate'); | ||
|
||
const direction = (tx.Flags & flags.Sell) === 0 ? 'buy' : 'sell'; | ||
const takerGets = parseAmount(tx.TakerGets); | ||
const takerPays = parseAmount(tx.TakerPays); | ||
const quantity = (direction === 'buy') ? takerPays : takerGets; | ||
const totalPrice = (direction === 'buy') ? takerGets : takerPays; | ||
|
||
return { | ||
direction: direction, | ||
quantity: quantity, | ||
totalPrice: totalPrice, | ||
passive: (tx.Flags & flags.Passive) !== 0, | ||
immediateOrCancel: (tx.Flags & flags.ImmediateOrCancel) !== 0, | ||
fillOrKill: (tx.Flags & flags.FillOrKill) !== 0 | ||
}; | ||
return parseOrderBase(tx.TakerGets, tx.TakerPays, tx.Flags); | ||
} | ||
|
||
module.exports = parseOrder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.