Beispiel #1
0
//        let signature = `/api/${apiPath}${nonce}${JSON.stringify(body)}`
//const sig = CryptoJS.HmacSHA384(signature, apiSecret).toString()

        public int PostOrder(IOrder order)
        {
            if (Username == null || Password == null)
            {
                return(-1);
            }
            WebConector wc       = new WebConector();
            string      api_name = "auth/w/order/submit";
            List <Tuple <string, string> > heder = new List <Tuple <string, string> >();
            string nonce        = ((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds).ToString();
            string body_jsonstr = "{\"type\":\"" + order.Type.Value + "\",\"symbol\":\"" + order.Pair + "\",\"price\":\"" + order.Price + "\",\"amount\":\"" + order.Amount + "\",\"flags\":\"0\"}";
            //string body_jsonstr = "type=" + order.Type.Value + "&symbol=" + order.Pair + "&price=" + order.Price + "&amount=" + order.Amount + "&flags=0";
            string data_for_encript = "/api/" + api_name + nonce + body_jsonstr;

            heder.Add(new Tuple <string, string>("bfx-apikey", Encoding.UTF8.GetString(Encoding.Default.GetBytes(Username))));
            heder.Add(new Tuple <string, string>("bfx-signature", Encoding.UTF8.GetString(Encoding.Default.GetBytes(SignatureHelper.Sign(Password, data_for_encript, 384)))));
            heder.Add(new Tuple <string, string>("bfx-nonce", Encoding.UTF8.GetString(Encoding.Default.GetBytes(nonce))));
            var    _url_url        = string.Format(abase_url, api_name);
            string jsonRezalt      = wc.ReqwestPostAsync(string.Format(abase_url, api_name), heder, body_jsonstr, "application/x-www-form-urlencoded").Result;
            var    jsonRezaltArray = JObject.Parse(jsonRezalt);

            if (jsonRezaltArray["error"] != null)
            {
                LastErrorInfo = "";
                return(Int32.Parse(jsonRezaltArray["order_id"].ToString()));
            }
            else
            {
                LastErrorInfo = jsonRezaltArray["error"].ToString();
                return(-1);
            }
        }
        public List <BaseHistoryRecord> GetHistoryRecords(DateTime dateTime)
        {
            if (Username == null || Password == null)
            {
                LastErrorInfo = "Not Autorizated";
                return(new List <BaseHistoryRecord>());
            }
            WebConector wc       = new WebConector();
            string      api_name = "wapi/v3/depositHistory.html";
            List <Tuple <string, string> > heder = new List <Tuple <string, string> >();
            var    jsontimestamp = wc.ReqwestGetAsync(string.Format("{0}/api/v3/time", base_url), new List <Tuple <string, string> >(), "").Result;
            string timestamp     = (JObject.Parse(jsontimestamp))["serverTime"].ToString();

            heder.Add(new Tuple <string, string>("X-MBX-APIKEY", Username));
            string data_for_encript = "&startTime=" + dateTime.ToUnixTimestamp() + "&recvWindow=" + "50000" + "&timestamp=" + timestamp;

            heder.Add(new Tuple <string, string>("signature", SignatureHelper.Sign(Password, data_for_encript, 256)));
            data_for_encript += "&signature=" + SignatureHelper.Sign(Password, data_for_encript, 256);
            string jsonRezalt   = wc.ReqwestPostAsync(string.Format("{0}{1}", base_url, api_name), heder, data_for_encript).Result;
            var    jarrayRezalt = JObject.Parse(jsonRezalt);
            var    rezalt       = new List <BaseHistoryRecord>();

            if (jarrayRezalt["msg"] == null)
            {
                var jsonArrayRecords = JArray.Parse(jarrayRezalt["depositList"].ToString());
                foreach (var record in jsonArrayRecords)
                {
                    BaseHistoryRecord bhr = new BaseHistoryRecord();
                    bhr.Id        = 0;
                    bhr.Provaider = record["txId"].ToString();
                    bhr.Status    = record["status"].ToString();
                    bhr.Time      = (new DateTime()).FromUnixTimestamp(record["insertTime"].ToObject <Int64>());
                    bhr.Txit      = record["txId"].ToString();
                    bhr.Type      = "deposit";
                    bhr.Account   = record["address"].ToString();;
                    bhr.Amount    = record["amount"].ToString();;
                    bhr.Currency  = record["asset"].ToString();;
                    rezalt.Add(bhr);
                }
            }
            else
            {
                LastErrorInfo = jarrayRezalt["msg"].ToString();
                return(new List <BaseHistoryRecord>());
            }
            api_name      = "/wapi/v3/withdrawHistory.html";
            heder         = new List <Tuple <string, string> >();
            jsontimestamp = wc.ReqwestGetAsync(string.Format("{0}/api/v3/time", base_url), new List <Tuple <string, string> >(), "").Result;
            timestamp     = (JObject.Parse(jsontimestamp))["serverTime"].ToString();
            heder.Add(new Tuple <string, string>("X-MBX-APIKEY", Username));
            data_for_encript = "&startTime=" + dateTime.ToUnixTimestamp() + "&recvWindow=" + "50000" + "&timestamp=" + timestamp;
            heder.Add(new Tuple <string, string>("signature", SignatureHelper.Sign(Password, data_for_encript, 256)));
            data_for_encript += "&signature=" + SignatureHelper.Sign(Password, data_for_encript, 256);
            jsonRezalt        = wc.ReqwestPostAsync(string.Format("{0}{1}", base_url, api_name), heder, data_for_encript).Result;
            jarrayRezalt      = JObject.Parse(jsonRezalt);
            if (jarrayRezalt["msg"] == null)
            {
                var jsonArrayRecords = JArray.Parse(jarrayRezalt["withdrawList"].ToString());
                foreach (var record in jsonArrayRecords)
                {
                    BaseHistoryRecord bhr = new BaseHistoryRecord();
                    bhr.Id        = 0;
                    bhr.Provaider = record["txId"].ToString();
                    bhr.Status    = record["status"].ToString();
                    bhr.Time      = (new DateTime()).FromUnixTimestamp(record["insertTime"].ToObject <Int64>());
                    bhr.Txit      = record["txId"].ToString();
                    bhr.Type      = "withdraw";
                    bhr.Account   = record["address"].ToString();;
                    bhr.Amount    = record["amount"].ToString();;
                    bhr.Currency  = record["asset"].ToString();;
                    rezalt.Add(bhr);
                }
            }
            else
            {
                LastErrorInfo += jarrayRezalt["msg"].ToString();
                return(new List <BaseHistoryRecord>());
            }
            throw new NotImplementedException();
        }