private async Task UpdateUserTransferWallet(Erc20DepositContractTransaction contractTransferTr)
        {
            string formattedAddress =
                _userTransferWalletRepository.FormatAddressForErc20(contractTransferTr.ContractAddress, contractTransferTr.TokenAddress);

            await _userTransferWalletRepository.ReplaceAsync(new UserTransferWallet()
            {
                LastBalance             = "",
                TransferContractAddress = formattedAddress,
                UpdateDate  = DateTime.UtcNow,
                UserAddress = contractTransferTr.UserAddress
            });
        }
        public async Task TransferToCoinContract(Erc20DepositContractTransaction contractTransferTr)
        {
            try
            {
                var userAddress = contractTransferTr.UserAddress;

                if (string.IsNullOrEmpty(userAddress) || userAddress == Constants.EmptyEthereumAddress)
                {
                    await UpdateUserTransferWallet(contractTransferTr);

                    await _logger.WriteInfoAsync("TransferContractTransactionService", "TransferToCoinContract", "",
                                                 $"Can't cashin: there is no user assigned to the transfer contract {contractTransferTr.ContractAddress}", DateTime.UtcNow);

                    return;
                }

                var tokenAddress    = contractTransferTr.TokenAddress;
                var contractAddress = await _erc20DepositContractService.GetContractAddress(contractTransferTr.UserAddress);

                var balance = await _ercInterfaceService.GetBalanceForExternalTokenAsync(contractTransferTr.ContractAddress, contractTransferTr.TokenAddress);

                if (balance == 0)
                {
                    await UpdateUserTransferWallet(contractTransferTr);

                    await _logger.WriteInfoAsync("TransferContractTransactionService", "TransferToCoinContract", "",
                                                 $"Can't cashin: there is no funds on the transfer contract {contractTransferTr.ContractAddress}", DateTime.UtcNow);

                    return;
                }

                var    opId            = $"HotWalletCashin-{Guid.NewGuid().ToString()}";
                string transactionHash = null;

                try
                {
                    transactionHash = await _hotWalletService.StartCashinAsync(new HotWalletOperation()
                    {
                        Amount        = balance,
                        FromAddress   = contractAddress,
                        OperationId   = opId,
                        ToAddress     = _hotWalletAddress,
                        TokenAddress  = tokenAddress,
                        OperationType = HotWalletOperationType.Cashin,
                    });
                }
                catch (Exception exc)
                {
                    await UpdateUserTransferWallet(contractTransferTr);

                    return;
                }

                await _userPaymentHistoryRepository.SaveAsync(new UserPaymentHistory()
                {
                    Amount          = balance.ToString(),
                    ToAddress       = contractAddress,
                    AdapterAddress  = $"HotWallet-Token-{tokenAddress}",
                    CreatedDate     = DateTime.UtcNow,
                    Note            = $"Cashin from erc20 deposit contract {contractAddress}",
                    TransactionHash = transactionHash,
                    UserAddress     = contractTransferTr.UserAddress
                });

                //await UpdateUserTransferWallet(contractTransferTr);
                await _logger.WriteInfoAsync(nameof(Erc20DepositTransactionService), nameof(TransferToCoinContract), "",
                                             $"Transfered {balance} from erc 20 deposit contract to {_hotWalletAddress} by transaction {transactionHash}. " +
                                             $"Receiver = {userAddress}");
            }
            catch (Exception e)
            {
                await _logger.WriteErrorAsync(nameof(Erc20DepositTransactionService), nameof(TransferToCoinContract),
                                              $"{contractTransferTr.ContractAddress} - erc20 - {contractTransferTr.TokenAddress} - {contractTransferTr.Amount}", e);

                throw;
            }
        }
 public async Task PutContractTransferTransaction(Erc20DepositContractTransaction tr)
 {
     await _queue.PutRawMessageAsync(JsonConvert.SerializeObject(tr));
 }