public PoloniexBalanceResponse GetBalances(PoloniexBalanceRequest request, string key, string secret) { string command = "command=" + request.Command + "&nonce=" + request.Nonce; string signature = CreateSignature(secret, command); RestRequest restRequest = new RestRequest("tradingApi", Method.POST); restRequest.AddHeader("Key", key); restRequest.AddHeader("Sign", signature); restRequest.AddParameter("command", request.Command); restRequest.AddParameter("nonce", request.Nonce); IRestResponse <Dictionary <string, decimal> > restResponse = _client.Execute <Dictionary <string, decimal> >(restRequest); return(new PoloniexBalanceResponse { Balances = restResponse.Data }); }
public SynchronizePortfolioResponse SynchronizePortfolio(SynchronizePortfolioRequest request) { SynchronizePortfolioResponse response = new SynchronizePortfolioResponse { Success = true, Error = "", PortfolioData = new List <PortfolioData>() }; List <User_Keys> userKeys = db.User_Keys.Where(k => k.UserNr == request.UserNr).ToList(); if (userKeys.Count == 0) { response.Success = false; response.Error = "User does not have any authorization keys"; return(response); } foreach (User_Keys key in userKeys) { if (key.ExchangeType == (int)ExchangeTypeEnum.Cex) { CexBalanceRequest cexBalanceRequest = new CexBalanceRequest { Key = key.KeyValue, Nonce = (154264078495300 + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()).ToString(), Secret = key.SecretValue }; CexBalanceResponse cexBalanceResponse = _cexManager.GetBalance(cexBalanceRequest); List <User_Balances> balances = db.User_Balances.Where(b => b.UserNr == request.UserNr && b.ExchangeType == (int)ExchangeTypeEnum.Cex).ToList(); if (balances.Count > 0) { db.User_Balances.RemoveRange(balances); db.SaveChanges(); } if (cexBalanceResponse.BTC != null) { User_Balances btcBalances = new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.BTC, ExchangeType = (int)ExchangeTypeEnum.Cex, Value = cexBalanceResponse.BTC.Available }; db.User_Balances.Add(btcBalances); } if (cexBalanceResponse.ETH != null) { User_Balances ethBalances = new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.ETH, ExchangeType = (int)ExchangeTypeEnum.Cex, Value = cexBalanceResponse.ETH.Available }; db.User_Balances.Add(ethBalances); } if (cexBalanceResponse.EUR != null) { User_Balances eurBalances = new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.EUR, ExchangeType = (int)ExchangeTypeEnum.Cex, Value = cexBalanceResponse.EUR.Available }; db.User_Balances.Add(eurBalances); } if (cexBalanceResponse.USD != null) { User_Balances usdBalances = new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.USD, ExchangeType = (int)ExchangeTypeEnum.Cex, Value = cexBalanceResponse.USD.Available }; db.User_Balances.Add(usdBalances); } db.SaveChanges(); } else { PoloniexBalanceRequest poloniexBalanceRequest = new PoloniexBalanceRequest { Nonce = (154264078495300 + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()).ToString() }; PoloniexBalanceResponse poloniexBalanceResponse = _poloniexManager.GetBalances(poloniexBalanceRequest, key.KeyValue, key.SecretValue); List <User_Balances> balances = db.User_Balances.Where(b => b.UserNr == request.UserNr && b.ExchangeType == (int)ExchangeTypeEnum.Poloniex).ToList(); if (balances.Count > 0) { db.User_Balances.RemoveRange(balances); db.SaveChanges(); } foreach (var balance in poloniexBalanceResponse.Balances) { if (balance.Key == "BTC") { db.User_Balances.Add(new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.BTC, ExchangeType = (int)ExchangeTypeEnum.Poloniex, Value = balance.Value }); } if (balance.Key == "ETH") { db.User_Balances.Add(new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.ETH, ExchangeType = (int)ExchangeTypeEnum.Poloniex, Value = balance.Value }); } if (balance.Key == "USD") { db.User_Balances.Add(new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.USD, ExchangeType = (int)ExchangeTypeEnum.Poloniex, Value = balance.Value }); } if (balance.Key == "EUR") { db.User_Balances.Add(new User_Balances { UserNr = request.UserNr, CurrencyType = (int)CurrencyType.EUR, ExchangeType = (int)ExchangeTypeEnum.Poloniex, Value = balance.Value }); } } db.SaveChanges(); } } #region CEX Balances List <User_Balances> allBalances = db.User_Balances.Where(b => b.ExchangeType == (int)ExchangeTypeEnum.Cex && b.UserNr == request.UserNr).ToList(); List <PortfolioData> portfolioDatas = new List <PortfolioData> { new PortfolioData { ExchangeType = (int)ExchangeTypeEnum.Cex, CurrencyDatas = new List <CurrencyData>() } }; foreach (var balance in allBalances) { PortfolioData portfolioData = portfolioDatas.FirstOrDefault(p => p.ExchangeType == (int)ExchangeTypeEnum.Cex); portfolioData.CurrencyDatas.Add(new CurrencyData { Symbol = CurrencyTypeEnumToString(balance.CurrencyType), Value = balance.Value }); } #endregion #region Poloniex Balances allBalances = db.User_Balances.Where(b => b.ExchangeType == (int)ExchangeTypeEnum.Poloniex && b.UserNr == request.UserNr).ToList(); portfolioDatas.Add(new PortfolioData { ExchangeType = (int)ExchangeTypeEnum.Poloniex, CurrencyDatas = new List <CurrencyData>() }); foreach (var balance in allBalances) { PortfolioData portfolioData = portfolioDatas.FirstOrDefault(p => p.ExchangeType == (int)ExchangeTypeEnum.Poloniex); portfolioData.CurrencyDatas.Add(new CurrencyData { Symbol = CurrencyTypeEnumToString(balance.CurrencyType), Value = balance.Value }); } #endregion response.PortfolioData = portfolioDatas; return(response); }