public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters) { if (connectRequestParameters.CancellationToken.IsCancellationRequested) { return(ConnectionResult.CreateCancelled()); } if (this.client.Connect(connectRequestParameters.CancellationToken, out string error)) { var symbols = this.client.SymbolsProvider.GetSymbols(OKExInstrumentType.Spot, OKExInstrumentType.Swap, OKExInstrumentType.Futures, OKExInstrumentType.Option, OKExInstrumentType.Index).ToList(); foreach (var s in symbols) { this.allSymbolsCache[s.UniqueInstrumentId] = s; } return(ConnectionResult.CreateSuccess()); } else { return(ConnectionResult.CreateFail(error ?? "Unknown error")); } }
public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters) { // Initialize var config = this.HitConfig; this.restApi = new HitRestApi(config); this.socketApi = new HitSocketApi(config); this.socketApi.ConnectionStateChanged += this.SocketApi_ConnectionStateChanged; this.socketApi.Notification += this.SocketApi_Notification; this.currenciesCache = new Dictionary <string, HitCurrency>(); this.symbolsCache = new Dictionary <string, HitSymbol>(); this.ping = new Ping(); this.pingUri = new Uri("https://api.hitbtc.com/api/2"); this.lastsTimeCache = new Dictionary <string, long>(); this.aggressorFlagCalculator = new AggressorFlagCalculator(); // Connect var token = connectRequestParameters.CancellationToken; this.socketApi.ConnectAsync().Wait(token); if (token.IsCancellationRequested) { return(ConnectionResult.CreateCancelled()); } if (this.socketApi.ConnectionState != HitConnectionState.Connected) { return(ConnectionResult.CreateFail("Can't connect to socket API")); } var currencies = this.CheckHitResponse(this.restApi.GetCurrenciesAsync(token).Result, out HitError hitError); if (hitError != null) { return(ConnectionResult.CreateFail(hitError.Format())); } foreach (var currency in currencies) { this.currenciesCache.Add(currency.Id, currency); } var symbols = this.CheckHitResponse(this.restApi.GetSymbolsAsync(token).Result, out hitError); if (hitError != null) { return(ConnectionResult.CreateFail(hitError.Format())); } foreach (var symbol in symbols) { this.symbolsCache.Add(symbol.Id, symbol); } return(ConnectionResult.CreateSuccess()); }
public override ConnectionResult Connect(ConnectRequestParameters connectRequestParameters) { var token = connectRequestParameters.CancellationToken; // read settings string apiKey = null; string secret = null; var settingItem = connectRequestParameters.ConnectionSettings.GetItemByPath(LOGIN_PARAMETER_GROUP, HitBTCVendor.PARAMETER_API_KEY); if (settingItem != null) { apiKey = settingItem.Value?.ToString(); } if (string.IsNullOrEmpty(apiKey)) { return(ConnectionResult.CreateFail("API key is empty")); } settingItem = connectRequestParameters.ConnectionSettings.GetItemByPath(LOGIN_PARAMETER_GROUP, HitBTCVendor.PARAMETER_SECRET_KEY); if (settingItem != null && settingItem.Value is PasswordHolder passwordHolder) { secret = passwordHolder.Password; } if (string.IsNullOrEmpty(secret)) { return(ConnectionResult.CreateFail("Secret key is empty")); } // create config this.hitConfig = new HitConfig { ApiKey = apiKey, Secret = secret }; if (token.IsCancellationRequested) { return(ConnectionResult.CreateCancelled()); } // connect var result = base.Connect(connectRequestParameters); if (result.State != ConnectionState.Connected || result.Cancelled) { return(result); } // login this.CheckHitResponse(this.socketApi.LoginAsync(token).Result, out var error); if (error != null) { return(ConnectionResult.CreateFail(error.Format())); } if (token.IsCancellationRequested) { return(ConnectionResult.CreateCancelled()); } // subscribe reports this.CheckHitResponse(this.socketApi.SubscribeReportsAsync(token).Result, out error); if (error != null) { return(ConnectionResult.CreateFail(error.Format())); } if (token.IsCancellationRequested) { return(ConnectionResult.CreateCancelled()); } // get balances this.UpdateBalances(token, out error); if (error != null) { return(ConnectionResult.CreateFail(error.Format())); } if (token.IsCancellationRequested) { return(ConnectionResult.CreateCancelled()); } return(ConnectionResult.CreateSuccess()); }