private async ValueTask HandleSignal(FireblocksDepositSignal signal)
 {
     using var activity = MyTelemetry.StartActivity("Handle Event FireblocksDepositSignal");
     try
     {
         await _service.HandledDepositAsync(signal);
     }
     catch (Exception ex)
     {
         ex.FailActivity();
         throw;
     }
 }
Exemple #2
0
        public async Task HandledDepositAsync(FireblocksDepositSignal deposit)
        {
            deposit.AddToActivityAsJsonTag("fireblocks-deposit");

            _logger.LogInformation("Request to handle deposit from fireblocks: {transferJson}",
                                   JsonConvert.SerializeObject(deposit));


            deposit.BrokerId.AddToActivityAsTag("brokerId");
            deposit.WalletId.AddToActivityAsTag("walletId");
            deposit.ClientId.AddToActivityAsTag("clientId");

            var accuracy = _assetsDictionary.GetAssetById(new AssetIdentity
            {
                Symbol   = deposit.AssetSymbol,
                BrokerId = deposit.BrokerId
            }).Accuracy;

            var amount = deposit.Amount;
            //var feeAmount = double.Parse(string.IsNullOrEmpty(deposit.PaymentInfo?.Fees?.Amount)
            //    ? "0.0"
            //    : deposit.PaymentInfo.Fees.Amount);

            var roundedAmount = Math.Round(amount, accuracy, MidpointRounding.ToNegativeInfinity);

            try
            {
                await using var ctx = DatabaseContext.Create(_dbContextOptionsBuilder);
                var existingEntity =
                    await ctx.Deposits.Where(e => e.TransactionId == deposit.TransactionId).FirstOrDefaultAsync();

                if (existingEntity == null)
                {
                    _logger.LogInformation("Got deposit from fireblocks, adding entry");
                    existingEntity = new DepositEntity
                    {
                        BrokerId      = deposit.BrokerId,
                        WalletId      = deposit.WalletId,
                        ClientId      = deposit.ClientId,
                        TransactionId = deposit.TransactionId,
                        Amount        = amount,
                        AssetSymbol   = deposit.AssetSymbol,
                        Comment       = "",
                        Integration   = "Fireblocks",
                        Txid          = deposit.TransactionId,
                        Status        = DepositStatus.New,
                        EventDate     = deposit.EventDate.ToUniversalTime(),
                        UpdateDate    = DateTime.UtcNow,
                        //FeeAmount = feeAmount,
                        FeeAssetSymbol = deposit.FeeAssetSymbol,
                        //    ? deposit.PaymentInfo.Amount.Currency
                        //    : deposit.PaymentInfo.Fees.Currency,
                        Network          = deposit.Network,
                        MatchingEngineId = "Deposit:" + Guid.NewGuid(),
                    };
                }
                else
                {
                    existingEntity.Amount     = amount;
                    existingEntity.UpdateDate = DateTime.UtcNow;
                    //existingEntity.FeeAmount = feeAmount;
                    existingEntity.FeeAssetSymbol = deposit.FeeAssetSymbol;
                }

                await ctx.UpsertAsync(existingEntity);

                await _depositPublisher.PublishAsync(new Deposit(existingEntity));
            }
            catch (Exception)
            {
                _logger.LogError("Unable to update deposit entry");
                throw;
            }

            _logger.LogInformation("Deposit request from Circle {transferIdString} is handled",
                                   deposit.TransactionId);
        }