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()); }
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()); }