mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-26 23:25:49 +00:00
57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import BigNumber from 'bignumber.js'
|
|
import parseAmount from './amount'
|
|
import {parseTimestamp, adjustQualityForXRP} from './utils'
|
|
import {removeUndefined} from '../../common'
|
|
import {orderFlags} from './flags'
|
|
import {FormattedOrderSpecification} from '../../common/types/objects'
|
|
|
|
export type FormattedAccountOrder = {
|
|
specification: FormattedOrderSpecification,
|
|
properties: {
|
|
maker: string,
|
|
sequence: number,
|
|
makerExchangeRate: string
|
|
}
|
|
}
|
|
|
|
// TODO: remove this function once rippled provides quality directly
|
|
function computeQuality(takerGets, takerPays) {
|
|
const quotient = new BigNumber(takerPays.value).dividedBy(takerGets.value)
|
|
return quotient.precision(16, BigNumber.ROUND_HALF_UP).toString()
|
|
}
|
|
|
|
// rippled 'account_offers' returns a different format for orders than 'tx'
|
|
// the flags are also different
|
|
export function parseAccountOrder(
|
|
address: string, order: any
|
|
): FormattedAccountOrder {
|
|
const direction = (order.flags & orderFlags.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
|
|
|
|
// note: immediateOrCancel and fillOrKill orders cannot enter the order book
|
|
// so we can omit those flags here
|
|
const specification = removeUndefined({
|
|
direction: direction,
|
|
quantity: quantity,
|
|
totalPrice: totalPrice,
|
|
passive: ((order.flags & orderFlags.Passive) !== 0) || undefined,
|
|
// rippled currently does not provide "expiration" in account_offers
|
|
expirationTime: parseTimestamp(order.expiration)
|
|
})
|
|
|
|
const makerExchangeRate = order.quality ?
|
|
adjustQualityForXRP(order.quality.toString(),
|
|
takerGetsAmount.currency, takerPaysAmount.currency) :
|
|
computeQuality(takerGetsAmount, takerPaysAmount)
|
|
const properties = {
|
|
maker: address,
|
|
sequence: order.seq,
|
|
makerExchangeRate: makerExchangeRate
|
|
}
|
|
|
|
return {specification, properties}
|
|
}
|