Example #1
0
        /// <summary>
        /// Get only ask offers of a market. 
        /// </summary>
        /// <param name="counterAssetCode">Code of counter asset (must not be XRP)</param>
        /// <param name="counterAssetGateway">Gateway address of counter asset</param>
        internal List<FiatAsk> GetOrderBookAsks(string counterAssetCode, string counterAssetGateway, bool includeAutoBridged = false)
        {
            var command = new MarketDepthRequest
            {
                id = 2,
                taker_pays = new Take { currency = counterAssetCode, issuer = counterAssetGateway },
                taker_gets = new Take { currency = _fiatCurreny, issuer = _issuerAddress }
            };

            string askData = sendToRippleNet(Helpers.SerializeJson(command));
            if (null == askData)
            {
                return null;
            }

            if (!checkError(String.Format("GetOrderBookAsks({0}, {1})", counterAssetCode, counterAssetGateway), askData))
            {
                return null;
            }

            var asks = Helpers.DeserializeJSON<MarketDepthFiatAsksResponse>(askData);
            if (null == asks.result || null == asks.result.offers)
            {
                _logger.Error("askData JSON is " + Environment.NewLine + askData);
                return null;
            }

            if (!includeAutoBridged)
            {
                return asks.result.offers;
            }

            //Check auto-bridged orders by taking asset1/XRP and XRP/asset2 and counting asset1/asset2
            command = new MarketDepthRequest
            {
                id = 4,
                taker_pays = new Take { currency = counterAssetCode, issuer = counterAssetGateway },
                taker_gets = new Take { currency = Const.NATIVE_ASSET }
            };

            string natAskData = sendToRippleNet(Helpers.SerializeJson(command));
            if (null == natAskData)
            {
                return asks.result.offers;
            }

            if (!checkError(String.Format("#2 GetOrderBookAsks: orders for {0}.{1}", counterAssetCode, counterAssetGateway), natAskData))
            {
                return asks.result.offers;
            }

            var natAsks = Helpers.DeserializeJSON<MarketDepthAsksResponse>(natAskData);
            if (null == natAsks.result)
            {
                _logger.Error(String.Format("natAskData JSON is " + Environment.NewLine + natAskData));
                return asks.result.offers;
            }

            command = new MarketDepthRequest
            {
                id = 5,
                taker_pays = new Take { currency = Const.NATIVE_ASSET },
                taker_gets = new Take { currency = _fiatCurreny, issuer = _issuerAddress }
            };

            string natBidData = sendToRippleNet(Helpers.SerializeJson(command));
            if (null == natBidData)
            {
                return asks.result.offers;
            }

            if (!checkError(String.Format("#3 GetOrderBookAsks: orders for {0}.{1}", _fiatCurreny, _issuerAddress), natBidData))
            {
                return asks.result.offers;
            }

            var natBids = Helpers.DeserializeJSON<MarketDepthBidsResponse>(natBidData);
            if (null == natBids.result)
            {
                _logger.Error("natBidData JSON is " + Environment.NewLine + natBidData);
                return asks.result.offers;
            }

            //NOTE: takes only "best" auto-bridged order and only over XRP. Considering other options would make this code
            //      too complicated and the benefits are questionable
            Ask bestAsk = natAsks.result.offers[0];
            Bid bestBid = natBids.result.offers[0];

            double abPrice = bestAsk.Price / bestBid.Price;

            //Inject the auto-bridged order to the resulting list
            for (int i = 0; i < asks.result.offers.Count; i++)
            {
                if (abPrice < asks.result.offers[i].Price)
                {
                    var abOrder = new FiatAsk
                    {
                        Account = "AUTOBRIDGED",
                        TakerPays = new Take { value = abPrice.ToString() },
                        TakerGets = new Take { value = (1.0).ToString() }      //NOTE: ugly workaround for the .Price getter
                        //Don't need anything else for trading purposes
                    };
                    asks.result.offers.Insert(i, abOrder);
                    break;
                }
            }

            return asks.result.offers;
        }
Example #2
0
        internal Market GetMarketDepth()
        {
            //BIDs
            var command = new MarketDepthRequest
            {
                id = 2,
                taker_pays = new Take { currency = Const.NATIVE_ASSET },
                taker_gets = new Take { currency = _fiatCurreny, issuer = _issuerAddress }
            };

            string bidData = sendToRippleNet(Helpers.SerializeJson(command));
            if (null == bidData)
            {
                return null;
            }

            if (!checkError("GetMarketDepth", bidData))
            {
                return null;
            }

            var bids = Helpers.DeserializeJSON<MarketDepthBidsResponse>(bidData);
            if (null == bids.result || null == bids.result.offers)
            {
                _logger.Error("bidData JSON is " + Environment.NewLine + bidData);
                return null;
            }

            //ASKs
            command = new MarketDepthRequest
            {
                id = 3,
                taker_pays = new Take { currency = _fiatCurreny, issuer = _issuerAddress },
                taker_gets = new Take { currency = Const.NATIVE_ASSET }
            };

            string askData = sendToRippleNet(Helpers.SerializeJson(command));
            if (null == askData)
            {
                return null;
            }

            if (!checkError("GetMarketDepth", askData))
            {
                return null;
            }

            var asks = Helpers.DeserializeJSON<MarketDepthAsksResponse>(askData);
            if (null == asks.result || null == asks.result.offers)
            {
                _logger.Error("askData JSON is " + Environment.NewLine + askData);
                return null;
            }

            var market = new Market
            {
                Bids  = bids.result.offers,
                Asks = asks.result.offers
            };

            return market;
        }