Example #1
0
        public async Task <IActionResult> Transfer([FromBody] TransferRequest model)
        {
            if (model.Amount <= 0)
            {
                throw new BackendException("Amount can't be less or equal to zero", ErrorCode.BadInputParameter);
            }

            await ValidateAddress(model.SourceAddress);
            await ValidateAddress(model.DestinationAddress);

            var asset = await _assetRepository.GetItemAsync(model.Asset);

            if (asset == null)
            {
                throw new BackendException("Provided asset is missing in database", ErrorCode.AssetNotFound);
            }

            var transactionId = await _builder.AddTransactionId(model.TransactionId, $"Transfer: {model.ToJson()}");

            await _transactionQueueWriter.AddCommand(transactionId, TransactionCommandType.Transfer, new TransferCommand
            {
                Amount             = model.Amount,
                SourceAddress      = model.SourceAddress,
                Asset              = model.Asset,
                DestinationAddress = model.DestinationAddress
            }.ToJson());

            return(Ok(new TransactionIdResponse
            {
                TransactionId = transactionId
            }));
        }
Example #2
0
        public async Task <CommandHandlingResult> Handle(StartCashinCommand command,
                                                         IEventPublisher eventPublisher)
        {
            try
            {
                var transactionId = await _builder.AddTransactionId(command.Id, $"SegwitTransfer: {command.ToJson()}");

                await _transactionQueueWriter.AddCommand(transactionId, TransactionCommandType.SegwitTransferToHotwallet, new SegwitTransferCommand
                {
                    SourceAddress = command.Address
                }.ToJson());
            }
            catch (BackendException ex) when(ex.Code == ErrorCode.DuplicateTransactionId)
            {
                _logger.WriteWarning(nameof(CashinCommandHandler), nameof(Handle), $"Duplicated id: {command.Id}");
            }

            return(CommandHandlingResult.Ok());
        }
        public async Task <CommandHandlingResult> Handle(StartCashoutCommand command,
                                                         IEventPublisher eventPublisher)
        {
            var address = command.Address.Trim('\n', ' ', '\t');

            try
            {
                var transactionId = await _builder.AddTransactionId(command.Id, $"Cashout: {command.ToJson()}");

                if (OpenAssetsHelper.IsBitcoin(command.AssetId))
                {
                    await _cashoutRequestRepository.CreateCashoutRequest(transactionId, command.Amount, address);
                }
                else
                {
                    var assetSetting = await _assetSettingCache.GetItemAsync(command.AssetId);

                    var hotWallet = !string.IsNullOrEmpty(assetSetting.ChangeWallet)
                        ? assetSetting.ChangeWallet
                        : assetSetting.HotWallet;

                    await _transactionQueueWriter.AddCommand(transactionId, TransactionCommandType.Transfer, new TransferCommand
                    {
                        Amount             = command.Amount,
                        SourceAddress      = hotWallet,
                        Asset              = command.AssetId,
                        DestinationAddress = command.Address
                    }.ToJson());
                }
            }
            catch (BackendException ex) when(ex.Code == ErrorCode.DuplicateTransactionId)
            {
                _logger.WriteWarning(nameof(CashinCommandHandler), nameof(Handle), $"Duplicated id: {command.Id}");
            }

            return(CommandHandlingResult.Ok());
        }
Example #4
0
        public async Task <IActionResult> CreateCashout([FromBody] TransferRequest model)
        {
            if (model.Amount <= 0)
            {
                throw new BackendException("Amount can't be less or equal to zero", ErrorCode.BadInputParameter);
            }

            var sourceAddress = OpenAssetsHelper.ParseAddress(model.SourceAddress);

            if (sourceAddress == null)
            {
                throw new BackendException("Invalid source address provided", ErrorCode.InvalidAddress);
            }

            var destAddress = OpenAssetsHelper.ParseAddress(model.DestinationAddress);

            if (destAddress == null)
            {
                throw new BackendException("Invalid destination address provided", ErrorCode.InvalidAddress);
            }

            var asset = await _assetRepository.GetAssetById(model.Asset);

            if (asset == null)
            {
                throw new BackendException("Provided asset is missing in database", ErrorCode.AssetNotFound);
            }

            if (model.Fee.GetValueOrDefault() < 0)
            {
                throw new BackendException("Fee must be greater than or equal to zero", ErrorCode.BadInputParameter);
            }

            if (model.Amount <= model.Fee.GetValueOrDefault())
            {
                throw new BackendException("Amount is less than fee", ErrorCode.BadInputParameter);
            }

            var transactionId = await _builder.AddTransactionId(model.TransactionId, model.ToJson());

            CreateTransactionResponse createTransactionResponse;

            if (OpenAssetsHelper.IsBitcoin(asset.Id) && model.Fee.HasValue)
            {
                createTransactionResponse = await _builder.GetPrivateTransferTransaction(sourceAddress, destAddress, model.Amount,
                                                                                         model.Fee.Value, transactionId);

                await _transactionSignRequestRepository.DoNotSign(transactionId);
            }
            else
            {
                createTransactionResponse = await _builder.GetTransferTransaction(sourceAddress, destAddress, model.Amount, asset, transactionId, true, true);
            }

            await _transactionBlobStorage.AddOrReplaceTransaction(transactionId, TransactionBlobType.Initial, createTransactionResponse.Transaction);

            return(Ok(new TransactionResponse
            {
                Transaction = createTransactionResponse.Transaction,
                TransactionId = createTransactionResponse.TransactionId,
                Fee = (createTransactionResponse as PrivateTransferResponse)?.Fee ?? 0
            }));
        }