Exemplo n.º 1
0
        public async Task SetEarnProgramByWalletAsync(SetEarnProgramByWalletRequest request)
        {
            using var activity = MyTelemetry.StartActivity("SetEarnProgramByWalletAsync");
            request.AddToActivityAsJsonTag("request");

            _logger.LogWarning("Change EnableEarnProgram. Wallet: {walletId}, EnableEarnProgram: {flag}", request.WalletId, request.EnableEarnProgram);

            await using var ctx = new DatabaseContext(_dbContextOptionsBuilder.Options);

            var walletEntity = ctx.ClientWallet.FirstOrDefault(e => e.WalletId == request.WalletId);

            if (walletEntity != null)
            {
                var oldWallet = (ClientWallet)walletEntity.Clone();

                walletEntity.EnableEarnProgram = request.EnableEarnProgram;
                await ctx.SaveChangesAsync();


                await UpdateCache(
                    walletEntity.ClientId,
                    walletEntity.BrokerId,
                    new List <ClientWalletEntity> {
                    walletEntity
                });

                await _publisher.PublishAsync(new ClientWalletUpdateMessage()
                {
                    OldWallet = ClientWallet.Create(oldWallet),
                    NewWallet = ClientWallet.Create(walletEntity)
                });
            }
        }
Exemplo n.º 2
0
        public async Task <SetBaseAssetResponse> SetBaseAssetAsync(SetBaseAssetRequest request)
        {
            using var activity = MyTelemetry.StartActivity($"Set base asset request");
            request.AddToActivityAsJsonTag("request");
            request.ClientId.ClientId.AddToActivityAsTag("clientId");
            request.ClientId.BrokerId.AddToActivityAsTag("brokerId");
            request.WalletId.AddToActivityAsTag("WalletId");

            if (string.IsNullOrEmpty(request.ClientId?.ClientId) ||
                string.IsNullOrEmpty(request.ClientId?.BrokerId) ||
                string.IsNullOrEmpty(request.WalletId) ||
                string.IsNullOrWhiteSpace(request.BaseAsset))
            {
                var message = "Cannot set base asset. BadRequest.";
                _logger.LogError(message);
                return(new SetBaseAssetResponse()
                {
                    Success = false,
                    ErrorMessage = message
                });
            }
            await using var ctx = new DatabaseContext(_dbContextOptionsBuilder.Options);
            var walletEntity = ctx.ClientWallet.FirstOrDefault(e => e.WalletId == request.WalletId);

            if (walletEntity == null)
            {
                var message = $"Wallet not found. WalletId: {request.WalletId}";
                _logger.LogError(message);
                return(new SetBaseAssetResponse()
                {
                    Success = false,
                    ErrorMessage = message
                });
            }

            var oldWallet = (ClientWallet)walletEntity.Clone();

            walletEntity.BaseAsset = request.BaseAsset;
            await ctx.SaveChangesAsync();

            await UpdateCache(request.ClientId.ClientId, request.ClientId.BrokerId, new List <ClientWalletEntity> {
                walletEntity
            });

            await _publisher.PublishAsync(new ClientWalletUpdateMessage()
            {
                OldWallet = ClientWallet.Create(oldWallet),
                NewWallet = ClientWallet.Create(walletEntity)
            });

            return(new SetBaseAssetResponse()
            {
                Success = true
            });
        }