ripple / rippled-historical-database

SQL database as a canonical source of historical data
99 stars 68 forks source link

Data V2 /orders always returns direction "BUY" #160

Closed ihomp closed 6 years ago

ihomp commented 6 years ago

https://data.ripple.com/v2/accounts/raNf8ibQZECTaiFqkDXKRmM2GfdWK76cSu/orders

shekenahglory commented 6 years ago

The output is from the specification used for rippleAPI https://ripple.com/build/rippleapi/#order, where direction is determined solely by the tfSell flag https://ripple.com/build/transactions/#offercreate-flags - if this flag is not set, its considered a buy. Note that some offers here have XRP as the quantity and totalPrice in USD, while others have the quantity in USD and totalPrice in XRP. One or the other is probably what you are thinking of as a sell, as they'd show up on opposite sides of the orderbook.

professorhantzen commented 6 years ago

In this case, the "direction" should not be included. It is misleading and provides no useful information. The correct implementation of the current behaviour would be to include a "tfSell" boolean in the responses (but it's doubtful many would be interested in the state of this flag anyway).

As I understand it, the tfSell flag does not relate to the users perceived "direction" of a given trade. That is determined by the users arbitrarily-chosen perspective on the two currencies about which they are requesting information. Unfortunately, for a response that may include a variety of currency pairs, returning "correct" results may require some kind of widespread architectural change.

What users would generally expect of a "direction" field would be as @ihomp described - the "buy" or "sell" would be reported as relative to the user-attributed "order" of the base/counter pairing of the supplied currencies. In this case however, a discrepancy between the way rippled internally stores transactions, and the way most users expect to see them would need to be addressed in the historical database (and presumably also the rippleAPI, though I am not familiar with if/how it does that as I don't use it).

What I'm meaning, is something such as an array called "trade_perspective", that contains the two currencies involved in the request, that could be used to determine the "buy" or "sell" direction returned to the user: { trade_perspective: [ "XRP", "USD" ] }

However, a simpler solution that could work for all cases is to simply rely on a canonical "order", and provide a user-requestable "flip" for such default results that do not match user expectation. Eg, resort to "alphabetise" as the system-wide canonical/default "order" for base/currency pairs, and enable the user to specify "flip"'s of either single currencies (to affect many results), or currency pairs (to affect specific results) in request headers to return whichever don't match their expectations to the order they expect.

It's arguable however that almost everyone expects XRP to be the base in most requests, so perhaps pairings involving XRP could be the only exemption from the base/counter alphabetising rule, and "flip" could default to on for XRP paired currencies.

This is an unfortunate situation - but I can't see how a "direction" can be established usefully without involving the user to determine their perspective in order to provide meaningful results.