示例#1
0
        /// <summary>
        /// This endpoint creates an order which can be executed through Broadcast Order.
        /// Orders can only be created after sufficient funds have been deposited into the user's contract balance.
        /// A successful order will have zero or one make and/or zero or more fills.
        /// </summary>
        /// <param name="apiParams"></param>
        /// <returns></returns>
        public async Task <OrderResponse> CreateOrder(string apiParams)
        {
            var httpContent = new StringContent(apiParams, Encoding.UTF8, "application/json");
            var result      = await ExecuteCall(orders, httpContent);

            return(OrderResponse.FromJson(result));
        }
示例#2
0
        private static void MyCustomCallback(MessageType messageType, string data)
        {
            // this is our custom callback we can do whatever we want with the data here
            OutputConsole.WriteLine($"{DateTime.UtcNow}: Received {messageType} message in private channel callback", ConsoleColor.Green);

            switch (messageType)
            {
            // if you want to deserialize into one of the premade classes you can do so quite easily by checking the MessageType and deserializing into the apropriate response class
            case MessageType.TradeData:
                try
                {
                    var deserializedClass = TradeResponse.FromJson(_client.Options.Serializer, data);
                    OutputConsole.WriteLine($"{DateTime.UtcNow}: We have deserialized a trade in our custom callback and the price in that trade is {deserializedClass.TradeData.Trade.Price}", ConsoleColor.Green);
                }
                catch (Exception ex)
                {
                    OutputConsole.WriteLine(ex.Message);
                }

                break;

            case MessageType.OrderData:
                try
                {
                    var deserializedClass = OrderResponse.FromJson(_client.Options.Serializer, data);
                    OutputConsole.WriteLine($"{DateTime.UtcNow}: We have deserialized an orderbook in our custom callback and the first price in that data is {deserializedClass.OrderData.Orders[0].Price}", ConsoleColor.Green);
                }
                catch (Exception ex)
                {
                    OutputConsole.WriteLine(ex.Message);
                }

                break;

            case MessageType.NewsData:
                break;

            case MessageType.BlockData:
                break;

            case MessageType.FavoriteData:
                break;

            case MessageType.NewMarket:
                break;

            case MessageType.NotificationData:
                break;

            case MessageType.Unknown:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(messageType), messageType, null);
            }
        }
示例#3
0
        /// <summary>
        /// Retrieves orders from a specific address filtered by the given parameters.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="contractHash"></param>
        /// <param name="pair"></param>
        /// <param name="from"></param>
        /// <param name="orderStatus"></param>
        /// <param name="beforeId"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task <OrderResponse> GetOrders(string address, string contractHash = null, string pair = null,
                                                    int from = -1, string orderStatus = null, string beforeId = null, int limit = -1)
        {
            if (string.IsNullOrEmpty(contractHash))
            {
                contractHash = ContractHash;
            }

            var query = HttpUtility.ParseQueryString(string.Empty);

            query["address"]       = address;
            query["contract_hash"] = contractHash;

            //optional args
            if (!string.IsNullOrEmpty(pair))
            {
                query["pair"] = pair;
            }
            if (!string.IsNullOrEmpty(orderStatus))
            {
                query["orderStatus"] = orderStatus;
            }
            if (!string.IsNullOrEmpty(beforeId))
            {
                query["beforeId"] = beforeId;
            }
            if (from >= 0)
            {
                query["from"] = from.ToString();
            }
            if (limit >= 0)
            {
                query["limit"] = limit.ToString();
            }

            string queryString = query.ToString().Insert(0, "?");
            var    result      = await ExecuteCall(Utils.ComposeUrl(orders, queryString));

            return(OrderResponse.FromJson(result));
        }
示例#4
0
        private void _scClient_OnMessage(string message)
        {
            Debug.WriteLine(message);
            // check if this is a publish message
            var m = Regex.Match(message, @"^{""event""*.:*.""#publish""");

            if (!m.Success)
            {
                return;
            }

            // get the channel

            var jobj = Options.Serializer.Deserialize <dynamic>(message);

            string channel = jobj["data"]["channel"].ToString();

            string reqtype;

            if (channel.Contains('-') && !Guid.TryParse(channel, out _))
            {
                reqtype = channel.Substring(0, channel.IndexOf('-'));
            }
            else
            {
                reqtype = channel;
                if (Guid.TryParse(channel, out var cguid))
                {
                    if (string.Equals(cguid.ToString(), channel, StringComparison.OrdinalIgnoreCase))
                    {
                        // ok we have a private channel so lets find out what data we have
                        reqtype = jobj["data"]["data"]["MessageType"].ToString();
                    }
                }
            }

            var hascb = (from c in _subscribeCallbacs where c.Key == channel select c).Any();

            if (hascb)
            {
                var cbs = from c in _subscribeCallbacs where c.Key == channel select c.Value;
                foreach (var cb in cbs)
                {
                    cb.Invoke(GetMessageType(reqtype), message);
                }
            }

            switch (reqtype.ToUpper())
            {
            case "ORDER":
                var ominfo = ParseMarketInfo(channel);
                var orders = OrderResponse.FromJson(Options.Serializer, message);
                OnOrderMessage?.Invoke(ominfo.Exchange, ominfo.Curr1, ominfo.Curr2, orders.OrderData.Orders);
                OnMessage?.Invoke(MessageType.OrderData, message);
                break;

            case "TRADE":
                var tminfo = ParseMarketInfo(channel);
                var trade  = TradeResponse.FromJson(Options.Serializer, message);
                OnTradeMessage?.Invoke(tminfo.Exchange, tminfo.Curr1, tminfo.Curr2, trade.TradeData.Trade);
                OnMessage?.Invoke(MessageType.TradeData, message);
                break;

            case "BLOCK":
                OnBlockMessage?.Invoke(ParseBlockInfo(channel), new BlockItem());
                OnMessage?.Invoke(MessageType.BlockData, message);
                break;

            case "FAVORITE":
                OnFavoriteMessage?.Invoke(FavoriteResponse.FromJson(Options.Serializer, message).Data.FavoritesDataData.Favorites);
                OnMessage?.Invoke(MessageType.FavoriteData, message);
                break;

            case "NOTIFICATION":
                OnNotificationMessage?.Invoke(NotificationResponse.FromJson(Options.Serializer, message).NotificationData.NotificationDataData.NotificationDataItem);
                OnMessage?.Invoke(MessageType.NotificationData, message);
                break;

            case "NEWS":
                OnNewsMessage?.Invoke(NewsResponse.FromJson(Options.Serializer, message).NewsData.NewsDataItem);
                OnMessage?.Invoke(MessageType.NewsData, message);
                break;

            case "NEWMARKET":
                OnNewMarketMessage?.Invoke(NewMarketResponse.FromJson(Options.Serializer, message).NewMarketData.NewMarketDataData);
                OnMessage?.Invoke(MessageType.NewMarket, message);
                break;

            default:
                OnMessage?.Invoke(MessageType.Unknown, message);
                break;
            }
        }
示例#5
0
        /// <summary>
        /// Finds the expected response object derived from the URI of the call.
        /// </summary>
        /// <returns></returns>
        private object ApiResponseDispatcher()
        {
            // Json is empty > Shit...
            if (String.IsNullOrEmpty(Json))
            {
                //log.Error("Json == null or empty");
                return(null);
            }

            // Uri is empty > Shit...
            if (this.Uri == null || this.Uri.AbsolutePath == "")
            {
                //log.Error("Uri.AbsolutePath null or empty");
                return(null);
            }

            object o;

            // Return the correct type based on the Uri
            switch (this.Uri.AbsolutePath.ToLower())
            {
            case "/api/v1/order":
                if (this.Json.Substring(0, 1) == "[")
                {
                    o = OrdersResponse.FromJson(this.Json);
                }
                else
                {
                    o = OrderResponse.FromJson(this.Json);
                }
                break;

            case "/api/v1/order/all":
            case "/api/v1/order/bulk":
                o = OrdersResponse.FromJson(this.Json);
                break;

            case "/api/v1/position/leverage":
                o = PositionResponse.FromJson(this.Json);
                break;

            case "/api/v1/position":
                o = PositionsResponse.FromJson(this.Json);
                break;

            case "/api/v1/order/closeposition":
                o = OrderResponse.FromJson(this.Json);
                break;

            case "/api/v1/user/wallet":
                o = WalletResponse.FromJson(this.Json);
                break;

            case "/api/v1/order/cancelallafter":
            default:
                o = null;
                //log.Error("Uri unknown. Please add [" + this.Uri.AbsolutePath + "] to the evaluated Uri's.");
                break;
            }

            return(o);
        }