Exemple #1
0
        public async Task <CommandHandlingResult> Handle(StartCashoutCommand command, IEventPublisher eventPublisher)
        {
            var asset = await _assetsService.AssetGetAsync(command.AssetId);

            var amount = EthServiceHelpers.ConvertToContract(command.Amount, asset.MultiplierPower, asset.Accuracy);

            try
            {
                if (asset.Type == AssetType.Erc20Token)
                {
                    var token = await _assetsService.Erc20TokenGetBySpecificationAsync(new Erc20TokenSpecification(new List <string>()
                    {
                        asset.Id
                    }));

                    var tokenAddress = token?.Items?.FirstOrDefault()?.Address;

                    if (string.IsNullOrEmpty(tokenAddress))
                    {
                        _logger.WriteWarning(nameof(CashoutCommandHandler), nameof(Handle), $"Can't perform cashout on empty token, {command.Id}");
                        return(CommandHandlingResult.Ok());
                    }

                    await _hotWalletService.EnqueueCashoutAsync(new Service.EthereumCore.Core.Repositories.HotWalletOperation()
                    {
                        Amount       = amount,
                        OperationId  = command.Id.ToString(),
                        FromAddress  = command.FromAddress,
                        ToAddress    = command.ToAddress,
                        TokenAddress = tokenAddress
                    });
                }
                else
                {
                    await _pendingOperationService.CashOut(command.Id, asset.AssetAddress,
                                                           _addressUtil.ConvertToChecksumAddress(command.FromAddress), _addressUtil.ConvertToChecksumAddress(command.ToAddress), amount, string.Empty);
                }
            }
            catch (ClientSideException ex) when(ex.ExceptionType == ExceptionType.EntityAlreadyExists || ex.ExceptionType == ExceptionType.OperationWithIdAlreadyExists)
            {
                _logger.WriteWarning(nameof(CashoutCommandHandler), nameof(Handle), $"Operation already exists, {command.Id}", ex);
            }

            eventPublisher.PublishEvent(new CashoutCompletedEvent {
                OperationId = command.Id
            });

            return(CommandHandlingResult.Ok());
        }
Exemple #2
0
        private async Task ProcessCashIn(ProcessEthCoinEventCommand queueMessage, IEventPublisher eventPublisher)
        {
            if (queueMessage.CoinEventType != CoinEventType.CashinCompleted)
            {
                return;
            }

            var bcnCreds = await _bcnClientCredentialsRepository.GetByAssetAddressAsync(queueMessage.FromAddress);

            var asset = await _assetsServiceWithCache.TryGetAssetAsync(bcnCreds.AssetId);

            var amount = EthServiceHelpers.ConvertFromContract(queueMessage.Amount, asset.MultiplierPower, asset.Accuracy);

            Guid.TryParse(bcnCreds.ClientId, out var clientId);

            await HandleCashInOperation(asset, amount, clientId, bcnCreds.Address,
                                        queueMessage.TransactionHash, eventPublisher, createPendingActions : true);
        }
        private async Task <bool> ProcessCashIn(CoinEvent queueMessage)
        {
            if (queueMessage.CoinEventType != CoinEventType.CashinCompleted)
            {
                return(true);
            }

            var bcnCreds = await _bcnClientCredentialsRepository.GetByAssetAddressAsync(queueMessage.FromAddress);

            var asset = await _assetsService.TryGetAssetAsync(bcnCreds.AssetId);

            var amount = EthServiceHelpers.ConvertFromContract(queueMessage.Amount, asset.MultiplierPower, asset.Accuracy);

            await HandleCashInOperation(asset, (double)amount, bcnCreds.ClientId, bcnCreds.Address,
                                        queueMessage.TransactionHash);

            return(true);
        }
        public async Task <CommandHandlingResult> Handle(StartTransferCommand command, IEventPublisher eventPublisher)
        {
            var asset = await _assetsService.AssetGetAsync(command.AssetId);

            var amount = EthServiceHelpers.ConvertToContract(command.Amount, asset.MultiplierPower, asset.Accuracy);

            try
            {
                await _pendingOperationService.Transfer(command.Id, asset.AssetAddress,
                                                        _addressUtil.ConvertToChecksumAddress(command.FromAddress), _addressUtil.ConvertToChecksumAddress(command.ToAddress), amount, command.Sign);
            }
            catch (ClientSideException ex) when(ex.ExceptionType == ExceptionType.EntityAlreadyExists || ex.ExceptionType == ExceptionType.OperationWithIdAlreadyExists)
            {
                _logger.WriteWarning(nameof(TransferCommandHandler), nameof(Handle), $"Operation already exists, {command.Id}", ex);
            }

            eventPublisher.PublishEvent(new TransferCompletedEvent {
                OperationId = command.Id
            });

            return(CommandHandlingResult.Ok());
        }
Exemple #5
0
        private async Task ProcessHotWalletCashin(ProcessHotWalletErc20EventCommand queueMessage,
                                                  IEventPublisher eventPublisher)
        {
            var bcnCreds = await _bcnClientCredentialsRepository.GetByAssetAddressAsync(queueMessage.FromAddress);

            string tokenAddress = queueMessage.TokenAddress;
            var    token        = await _assetsService.Erc20TokenGetByAddressAsync(tokenAddress);

            if (token == null)
            {
                _log.Error(new Exception($"Skipping cashin. Unsupported Erc 20 token - {tokenAddress}"),
                           context: queueMessage);

                return;
            }
            var asset = await _assetsServiceWithCache.TryGetAssetAsync(token.AssetId);

            var amount = EthServiceHelpers.ConvertFromContract(queueMessage.Amount, asset.MultiplierPower, asset.Accuracy);

            Guid.TryParse(bcnCreds.ClientId, out var clientId);

            await HandleCashInOperation(asset, amount, clientId,
                                        bcnCreds.Address, queueMessage.TransactionHash, eventPublisher);
        }