Ejemplo n.º 1
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
            }));
        }
Ejemplo n.º 2
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
            }));
        }