Exemplo n.º 1
0
        public async Task Update(List <PositionPortfolio> positions)
        {
            var items = positions
                        .Where(e => e.IsOpen)
                        .Select(PositionPortfolioNoSql.Create)
                        .ToList();

            if (items.Any())
            {
                await _dataWriter.BulkInsertOrReplaceAsync(items);
            }

            items = positions
                    .Where(e => !e.IsOpen)
                    .Select(PositionPortfolioNoSql.Create)
                    .ToList();


            var list = items.Select(e => _dataWriter.DeleteAsync(e.PartitionKey, e.RowKey).AsTask()).ToList();

            if (list.Any())
            {
                await Task.WhenAll(list);
            }
        }
        public async Task RemoveBitGoWallet(RemoveBitGoWalletRequest request)
        {
            using var action = MyTelemetry.StartActivity("Remove BitGo wallet");
            request.AddToActivityAsJsonTag("request");
            try
            {
                _logger.LogInformation("Remove BitGo wallet: {jsonText}",
                                       JsonConvert.SerializeObject(request));

                var entity = await _writer.DeleteAsync(BitGoWalletNoSqlEntity.GeneratePartitionKey(request.BrokerId),
                                                       BitGoWalletNoSqlEntity.GenerateRowKey(request.WalletId));

                if (entity != null)
                {
                    _logger.LogInformation("Removed BitGo wallet: {jsonText}",
                                           JsonConvert.SerializeObject(entity, new ApiKeyHiddenJsonConverter(typeof(BitGoWallet))));
                }
                else
                {
                    _logger.LogInformation("Unable to remove BitGo wallet, do not exist: {jsonText}",
                                           JsonConvert.SerializeObject(request));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Cannot remove BitGo wallet: {requestJson}",
                                 JsonConvert.SerializeObject(request));
                ex.FailActivity();
                throw;
            }
        }
Exemplo n.º 3
0
        public override async Task <RemoveVaultConnectionResponse> RemoveVaultConnection(RemoveVaultConnectionRequest request, ServerCallContext context)
        {
            var tenantId = context.GetHttpContext().User.GetClaimOrDefault(Claims.TenantId);
            var apiKeyId = context.GetHttpContext().User.GetClaimOrDefault(Claims.ApiKeyId);

            if (string.IsNullOrEmpty(apiKeyId))
            {
                return(new RemoveVaultConnectionResponse()
                {
                    Error = new ValidatorApiError()
                    {
                        Code = ValidatorApiError.Types.ErrorCodes.InternalServerError,
                        Message = "Incorrect Bearer Token. "
                    }
                });
            }

            var validatorLinkEntity = _validatorLinkReader.Get(
                ValidatorLinkEntity.GeneratePartitionKey(tenantId),
                ValidatorLinkEntity.GenerateRowKey(apiKeyId));

            if (validatorLinkEntity != null)
            {
                await _validatorLinkWriter.DeleteAsync(
                    ValidatorLinkEntity.GeneratePartitionKey(tenantId),
                    ValidatorLinkEntity.GenerateRowKey(apiKeyId));
            }

            return(new RemoveVaultConnectionResponse());
        }
Exemplo n.º 4
0
        public async Task RemoveSpotInstrumentFeesSettings(RemoveSpotInstrumentFeesRequest request)
        {
            using var action = MyTelemetry.StartActivity("Remove Spot Instrument Fees Settings");
            request.AddToActivityAsJsonTag("request");
            try
            {
                _logger.LogInformation("Remove Spot Instrument Fees Setting: {jsonText}",
                                       JsonConvert.SerializeObject(request));

                var entity = await _writer.DeleteAsync(SpotInstrumentFeesNoSqlEntity.GeneratePartitionKey(request.BrokerId),
                                                       SpotInstrumentFeesNoSqlEntity.GenerateRowKey(request.SpotInstrumentId));

                if (entity != null)
                {
                    _logger.LogInformation("Removed Spot Instrument Fees Settings: {jsonText}",
                                           JsonConvert.SerializeObject(entity));
                }
                else
                {
                    _logger.LogInformation("Unable to remove Spot Instrument Fees Setting, do not exist: {jsonText}",
                                           JsonConvert.SerializeObject(request));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Cannot remove ExternalMarketSettings: {requestJson}",
                                 JsonConvert.SerializeObject(request));
                ex.FailActivity();
                throw;
            }
        }
        public async Task Logout(string traderId, Guid rootSessionId)
        {
            var task1 = _sessionWriter.DeleteAsync(RootSessionNoSqlEntity.GeneratePartitionKey(traderId), RootSessionNoSqlEntity.GenerateRowKey(rootSessionId)).AsTask();
            var task2 = _rootSessionWriter.DeleteAsync(RootSessionNoSqlEntity.GeneratePartitionKey(traderId), RootSessionNoSqlEntity.GenerateRowKey(rootSessionId)).AsTask();

            await Task.WhenAll(task1, task2);
        }
        public async ValueTask <bool> DeleteBitgoCoinEntityAsync(string coin)
        {
            if (string.IsNullOrEmpty(coin))
            {
                throw new Exception("Cannot update coin. Coin cannot be empty");
            }

            var entity = await _bitgoCoins.GetAsync(BitgoCoinEntity.GeneratePartitionKey(),
                                                    BitgoCoinEntity.GenerateRowKey(coin));

            if (entity != null)
            {
                var assets = await _assetMap.GetAsync();

                var existAssets = assets.Any(e => e.BitgoCoin == coin);
                if (existAssets)
                {
                    throw new Exception("Cannot delete coin. Asset used it as coin");
                }

                await _bitgoCoins.DeleteAsync(BitgoCoinEntity.GeneratePartitionKey(),
                                              BitgoCoinEntity.GenerateRowKey(coin));
            }

            return(true);
        }
        public async Task DeleteRouteAsync(ProviderRoute newRoute)
        {
            var partitionKey = ProviderRouteMyNoSqlEntity.GeneratePartitionKey();
            var rowKey       = ProviderRouteMyNoSqlEntity.GenerateRowKey(newRoute.Id);

            await _routeWriter.DeleteAsync(partitionKey, rowKey);

            _logger.LogInformation("Deleted provider route {routeJson}", JsonConvert.SerializeObject(newRoute));
        }
        public async Task RemoveWalletAsync(string name)
        {
            await _noSqlDataWriter.DeleteAsync(LpWalletNoSql.GeneratePartitionKey(), LpWalletNoSql.GenerateRowKey(name));

            lock (_sync)
            {
                _data.Remove(name);
            }

            _logger.LogInformation("Deleted Wallet {name}", name);
        }
Exemplo n.º 9
0
        public async Task DeleteCountry(string countryCode)
        {
            var country = await GetCountry(countryCode);

            await using var context = new DatabaseContext(_dbContextOptionsBuilder.Options);
            context.Countries.Remove(country);
            await context.SaveChangesAsync();

            await _cache.DeleteAsync(KycCountryNoSqlEntity.GeneratePartitionKey(),
                                     KycCountryNoSqlEntity.GenerateRowKey(countryCode));
        }
Exemplo n.º 10
0
        private async Task ProcessMessageAsync(ExecutionEvent message)
        {
            var orders = new List <OrderEntity>();
            var trades = new List <TradeEntity>();

            foreach (var order in message.Orders)
            {
                var orderEntity = _mapper.Map <OrderEntity>(order);
                orders.Add(orderEntity);

                if (order.Trades == null)
                {
                    continue;
                }

                foreach (var trade in order.Trades)
                {
                    var tradeEntity = _mapper.Map <TradeEntity>(trade);
                    tradeEntity.AssetPairId  = orderEntity.AssetPairId;
                    tradeEntity.OrderId      = orderEntity.Id;
                    tradeEntity.PartitionKey = orderEntity.WalletId;
                    tradeEntity.WalletId     = orderEntity.WalletId;
                    trades.Add(tradeEntity);
                }
            }

            await _orderWriter.BulkInsertOrReplaceAsync(orders);

            await _tradeWriter.BulkInsertOrReplaceAsync(trades);

            Task.Run(async() =>
            {
                var ordersToRemove = orders
                                     .Where(x => x.Status == OrderStatus.Matched.ToString() ||
                                            x.Status == OrderStatus.Cancelled.ToString() ||
                                            x.Status == OrderStatus.Rejected.ToString()).ToList();

                foreach (var order in ordersToRemove)
                {
                    await _orderWriter.DeleteAsync(order.WalletId, order.Id);
                }

                var walletIds = trades.Select(x => x.WalletId).Distinct().ToList();

                foreach (var walletId in walletIds)
                {
                    await _tradeWriter.CleanAndKeepMaxRecords(walletId, 0);
                }
            });
        }
Exemplo n.º 11
0
        public async Task <bool> TryDeleteAsync(string partitionKey, string rowKey)
        {
            try
            {
                await _writer.DeleteAsync(partitionKey, rowKey);

                return(true);
            }
            catch (Exception e)
            {
                _log.Error(e, $"Cannot execute DeleteAsync to MyNoSql with entity {typeof(TEntity).Name}", $"partitionKey: {partitionKey}; rowKey: {rowKey}");
                return(false);
            }
        }
Exemplo n.º 12
0
        public override async Task <AcknowledgeResultResponse> AcknowledgeResult(AcknowledgeResultRequest request, ServerCallContext context)
        {
            var vaultId = context.GetVaultId();

            var item = _dataReader.Get(ApprovalRequestMyNoSqlEntity.GeneratePartitionKey(request.ValidatorId),
                                       ApprovalRequestMyNoSqlEntity.GenerateRowKey(request.TransferSigningRequestId));

            if (item != null && item.VaultId == vaultId)
            {
                await _dataWriter.DeleteAsync(ApprovalRequestMyNoSqlEntity.GeneratePartitionKey(request.ValidatorId),
                                              ApprovalRequestMyNoSqlEntity.GenerateRowKey(request.TransferSigningRequestId));

                _logger.LogInformation("Acknowledge ApprovalResults. TransferSigningRequestId={TransferSigningRequestId}; TenantId={TenantId}; VaultId={VaultId}; ValidatorId={ValidatorId}", item.TransferSigningRequestId, item.TenantId, item.VaultId, item.ValidatorId);
            }

            return(new AcknowledgeResultResponse());
        }
Exemplo n.º 13
0
        public override async Task <PingResponse> GetPing(PingRequest request, ServerCallContext context)
        {
            var validatorId = context.GetHttpContext().User.GetClaimOrDefault(Claims.KeyKeeperId);
            var publicKey   = context.GetHttpContext().User.GetClaimOrDefault(Claims.PublicKeyPem);

            if (string.IsNullOrEmpty(publicKey))
            {
                return(new PingResponse()
                {
                    Error = new ValidatorApiError()
                    {
                        Code = ValidatorApiError.Types.ErrorCodes.InternalServerError,
                        Message = "Incorrect Bearer Token."
                    }
                });
            }

            var message = _pingMessageReader.Get(PingMessageMyNoSqlEntity.GeneratePartitionKey(validatorId),
                                                 PingMessageMyNoSqlEntity.GenerateRowKey());

            var response = new PingResponse();

            if (message == null)
            {
                response.MessageEnc       = string.Empty;
                response.SignatureMessage = string.Empty;
            }
            else
            {
                var asynccrypto = new AsymmetricEncryptionService();
                var messageEnc  = asynccrypto.Encrypt(Encoding.UTF8.GetBytes(message.Message), publicKey);

                response.MessageEnc       = Convert.ToBase64String(messageEnc);
                response.SignatureMessage = "not-implemented-please-skip";

                await _pingMessageWriter.DeleteAsync(message.PartitionKey, message.RowKey);
            }

            _logger.LogInformation("GetPing response. ValidatorId='{ValidatorId}'; HasMessage={HasMessage}", validatorId, !string.IsNullOrEmpty(response.MessageEnc));

            return(response);
        }
        public async Task RemoveValidatorApiKeyAsync([FromBody] ValidatorRequest request)
        {
            var(auth, tenantId, adminId, adminEmail) = Authorize();

            if (!auth)
            {
                return;
            }

            var entity = _validationReader.Get(ValidatorLinkEntity.GeneratePartitionKey(tenantId),
                                               ValidatorLinkEntity.GenerateRowKey(request.ApiKeyId));

            if (entity == null)
            {
                return;
            }

            await _validationWriter.DeleteAsync(entity.PartitionKey, entity.RowKey);

            _logger.LogInformation("Removed validator Api Key: {ApiKeyId}; AdminId: {AdminId}; Name: {Name}; Device: {Device}; TenantId: {TenantId}", request.ApiKeyId, adminId, entity.Name, entity.DeviceInfo, tenantId);
        }
        public async Task KillRootSessionAsync(KillRootSessionRequest request)
        {
            using var activity = MyTelemetry.StartActivity("Kill Root Session");

            if (string.IsNullOrEmpty(request.SessionRootId) || string.IsNullOrEmpty(request.ClientId))
            {
                _logger.LogWarning("Cannot kill session, RootSessionId is empty or ClientId is empty");
                activity.SetStatus(Status.Error);
                return;
            }

            await _writer.DeleteAsync(SpotSessionNoSql.GeneratePartitionKey(request.ClientId), SpotSessionNoSql.GenerateRowKey(request.SessionRootId));

            request.ClientId.AddToActivityAsTag("clientId");

            request.SessionRootId.AddToActivityAsTag("sessionRootId");

            await _sessionAuditService.KillSessionAudit(request.SessionRootId, request.SessionId, request.ClientId, request.Reason, request.UserAgent, request.Ip);

            _logger.LogInformation("Session is killed. ClientId: {clientId}, RootSessionId: {rootIdText}", request.ClientId, request.SessionRootId);
        }
Exemplo n.º 16
0
        public async Task <OperationResponse> DeleteWithdrawalProfile(DeleteProfileRequest request)
        {
            try
            {
                var groups = await _profileWriter.GetAsync(FeeProfilesNoSqlEntity.GeneratePartitionKey(),
                                                           FeeProfilesNoSqlEntity.GenerateRowKey());

                var withdrawalList = groups?.Profiles ?? new List <string>();
                var depositList    = groups?.DepositProfiles ?? new List <string>()
                {
                    FeeProfileConsts.DefaultProfile
                };

                var list = withdrawalList.Distinct().ToList();
                list.Remove(request.ProfileId);
                await _profileWriter.InsertOrReplaceAsync(FeeProfilesNoSqlEntity.Create(list, depositList));

                var assets =
                    (await _assetWriter.GetAsync(
                         AssetFeesNoSqlEntity.GeneratePartitionKey(request.BrokerId, request.ProfileId))).ToList();

                foreach (var asset in assets)
                {
                    await _assetWriter.DeleteAsync(asset.PartitionKey, asset.RowKey);
                }

                return(new OperationResponse()
                {
                    IsSuccess = true
                });
            }
            catch (Exception e)
            {
                return(new OperationResponse()
                {
                    IsSuccess = false,
                    ErrorText = e.Message
                });
            }
        }
Exemplo n.º 17
0
        public async ValueTask <bool> DeleteBitgoAssetMapEntityAsync(string brokerId, string assetSymbol)
        {
            if (string.IsNullOrEmpty(brokerId))
            {
                throw new Exception("Cannot delete asset map. BrokerId cannot be empty");
            }
            if (string.IsNullOrEmpty(assetSymbol))
            {
                throw new Exception("Cannot delete asset map. AssetSymbol cannot be empty");
            }

            var entity = await _assetMap.GetAsync(BitgoAssetMapEntity.GeneratePartitionKey(brokerId),
                                                  BitgoAssetMapEntity.GenerateRowKey(assetSymbol));

            if (entity != null)
            {
                await _assetMap.DeleteAsync(BitgoAssetMapEntity.GeneratePartitionKey(brokerId),
                                            BitgoAssetMapEntity.GenerateRowKey(assetSymbol));
            }

            return(true);
        }
Exemplo n.º 18
0
        public async Task LogoutAsync(SessionEntity session)
        {
            await _sessionsWriter.DeleteAsync(session.PartitionKey, session.RowKey);

            await _clientSessionsClient.DeleteSessionIfExistsAsync(session.Token);
        }
        private async Task <GetDepositAddressResponse> GetBitgoDepositAddressAsync(GetDepositAddressRequest request)
        {
            try
            {
                var(bitgoCoin, bitgoWalletId) =
                    _assetMapper.AssetToBitgoCoinAndWallet(request.BrokerId, request.AssetSymbol);

                if (string.IsNullOrEmpty(bitgoWalletId) || string.IsNullOrEmpty(bitgoCoin))
                {
                    _logger.LogError(
                        "Cannot process GetDepositAddress. Asset do not mapped to bitgo. Request: {jsonText}",
                        JsonConvert.SerializeObject(request));
                    return(new GetDepositAddressResponse
                    {
                        Error = GetDepositAddressResponse.ErrorCode.AssetDoNotSupported
                    });
                }

                Error error = null;

                var label = _walletMapper.WalletToBitgoLabel(new JetWalletIdentity
                {
                    BrokerId = request.BrokerId,
                    ClientId = request.ClientId,
                    WalletId = request.WalletId
                });

                await using var ctx = DatabaseContext.Create(_dbContextOptionsBuilder);
                var addressEntity = await ctx.Addresses.Where(t =>
                                                              t.BrokerId == request.BrokerId && t.ClientId == request.ClientId &&
                                                              t.WalletId == request.WalletId && t.Blockchain == request.Blockchain &&
                                                              t.AssetSymbol == request.AssetSymbol).FirstOrDefaultAsync();

                var address = addressEntity?.Address;

                if (string.IsNullOrEmpty(address))
                {
                    (address, error) =
                        await _depositAddressGeneratorService.GetAddressAsync(bitgoCoin, bitgoWalletId, label);
                }

                if (string.IsNullOrEmpty(address))
                {
                    var preGeneratedAddresses = (await _generatedAddressDataWriter.GetAsync(
                                                     GeneratedDepositAddressEntity.GeneratePartitionKey(request.BrokerId, bitgoCoin,
                                                                                                        bitgoWalletId, request.Blockchain)))
                                                .ToList();

                    if (preGeneratedAddresses.Count > 0)
                    {
                        var preGeneratedAddress = preGeneratedAddresses.First();
                        (address, error) = await _depositAddressGeneratorService.UpdateAddressLabelAsync(bitgoCoin,
                                                                                                         bitgoWalletId,
                                                                                                         preGeneratedAddress.Address.BitGoAddressId, label);

                        if (error == null)
                        {
                            await _generatedAddressDataWriter.DeleteAsync(preGeneratedAddress.PartitionKey,
                                                                          preGeneratedAddress.RowKey);
                        }
                    }
                    else
                    {
                        (address, error) =
                            await _depositAddressGeneratorService.GenerateAddressAsync(bitgoCoin, bitgoWalletId, label);
                    }
                }

                if (string.IsNullOrEmpty(address) || error != null)
                {
                    _logger.LogError(
                        "Cannot process GetDepositAddress. Unable to generate address. Request: {jsonText}. Error: {error}",
                        JsonConvert.SerializeObject(request), error);
                    return(new GetDepositAddressResponse
                    {
                        Error = GetDepositAddressResponse.ErrorCode.AddressNotGenerated
                    });
                }

                if (addressEntity == null)
                {
                    await ctx.InsertAsync(new DepositAddressSqlEntity
                    {
                        BrokerId    = request.BrokerId,
                        ClientId    = request.ClientId,
                        WalletId    = request.WalletId,
                        Integration = "BitGo",
                        AssetSymbol = request.AssetSymbol,
                        Blockchain  = request.Blockchain,
                        Address     = address,
                        CreatedDate = DateTime.Now
                    });
                }

                await _addressDataWriter.InsertOrReplaceAsync(DepositAddressEntity.Create(request.WalletId,
                                                                                          request.AssetSymbol, request.Blockchain, address));

                await _addressDataWriter.CleanAndKeepMaxPartitions(Program.Settings.MaxClientInCache);

                _logger.LogInformation("Handle GetDepositAddress, request: {jsonText}, address: {address}",
                                       JsonConvert.SerializeObject(request), address);

                return(new GetDepositAddressResponse
                {
                    Address = address,
                    Error = GetDepositAddressResponse.ErrorCode.Ok
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception on GetDepositAddress with request: {jsonText}",
                                 JsonConvert.SerializeObject(request));
                throw;
            }
        }
 public async Task DeleteAsync(string id)
 {
     await _myNoSqlServerDataWriter.DeleteAsync(NotificationChannelNoSql.GeneratePartitionKey(),
                                                NotificationChannelNoSql.GenerateRowKey(id));
 }
Exemplo n.º 21
0
 public async Task CloseSession(SessionEntity session, string reason)
 {
     await _sessionsWriter.DeleteAsync(session.PartitionKey, session.RowKey);
 }