public async Task <BuildTransactionResponse> BuildSingle([FromBody] BuildSingleTransactionRequest request) { if (request == null) { throw new BusinessException("Unable deserialize request", ErrorCode.BadInputParameter); } var amountSatoshi = MoneyConversionHelper.SatoshiFromContract(request.Amount); if (amountSatoshi <= 0) { throw new BusinessException($"Amount can't be less or equal to zero: {amountSatoshi}", ErrorCode.BadInputParameter); } if (request.AssetId != Constants.Assets.LiteCoin.AssetId) { throw new BusinessException("Invalid assetId", ErrorCode.BadInputParameter); } var toBitcoinAddress = _addressValidator.GetBitcoinAddress(request.ToAddress); if (toBitcoinAddress == null) { throw new BusinessException("Invalid ToAddress ", ErrorCode.BadInputParameter); } var fromBitcoinAddress = _addressValidator.GetBitcoinAddress(request.FromAddress); if (fromBitcoinAddress == null) { throw new BusinessException("Invalid FromAddress", ErrorCode.BadInputParameter); } if (request.OperationId == Guid.Empty) { throw new BusinessException("Invalid operation id (GUID)", ErrorCode.BadInputParameter); } var tx = await _operationService.GetOrBuildTransferTransaction(request.OperationId, fromBitcoinAddress, toBitcoinAddress, request.AssetId, new Money(amountSatoshi), request.IncludeFee); return(new BuildTransactionResponse { TransactionContext = tx.ToHex() }); }
public async Task <IActionResult> BuildSingle([FromBody] BuildSingleTransactionRequest request) { if (request == null) { throw new ValidationApiException("Unable deserialize request"); } var amountSatoshi = MoneyConversionHelper.SatoshiFromContract(request.Amount); if (amountSatoshi <= 0) { throw new ValidationApiException($"Amount can't be less or equal to zero: {amountSatoshi}"); } if (request.AssetId != Constants.Assets.Bitcoin.AssetId) { throw new ValidationApiException("Invalid assetId"); } if (request.OperationId == Guid.Empty) { throw new ValidationApiException("Invalid operation id (GUID)"); } if (await _operationEventRepository.ExistAsync(request.OperationId, OperationEventType.Broadcasted)) { return(Conflict()); } BuiltTransactionInfo tx; try { tx = await _operationService.GetOrBuildTransferTransactionAsync(request.OperationId, new List <OperationInput> { new OperationInput { Address = request.FromAddress, AddressContext = request.FromAddressContext?.DeserializeJson <AddressContextContract>()?.PubKey, Amount = amountSatoshi } }, new List <OperationOutput> { new OperationOutput { Address = request.ToAddress, Amount = amountSatoshi } }, OperationType.Single, request.AssetId, request.IncludeFee); } catch (NotEnoughFundsException) { return(BadRequest(BlockchainErrorResponse.FromKnownError(BlockchainErrorCode.NotEnoughBalance))); } catch (BusinessException e) when(e.Code == ErrorCode.NotEnoughFundsAvailable) { return(BadRequest(BlockchainErrorResponse.FromKnownError(BlockchainErrorCode.NotEnoughBalance))); } return(Ok(new BuildTransactionResponse { TransactionContext = tx.ToJson(_network) })); }
public async Task <IActionResult> BuildSingle([FromBody] BuildSingleTransactionRequest request) { if (request == null) { throw new BusinessException("Unable deserialize request", ErrorCode.BadInputParameter); } var amountSatoshi = MoneyConversionHelper.SatoshiFromContract(request.Amount); if (amountSatoshi <= 0) { throw new BusinessException($"Amount can't be less or equal to zero: {amountSatoshi}", ErrorCode.BadInputParameter); } if (request.AssetId != Constants.Assets.LiteCoin.AssetId) { throw new BusinessException("Invalid assetId", ErrorCode.BadInputParameter); } var toBitcoinAddress = _addressValidator.GetBitcoinAddress(request.ToAddress); if (toBitcoinAddress == null) { throw new BusinessException("Invalid ToAddress ", ErrorCode.BadInputParameter); } var fromBitcoinAddress = _addressValidator.GetBitcoinAddress(request.FromAddress); if (fromBitcoinAddress == null) { throw new BusinessException("Invalid FromAddress", ErrorCode.BadInputParameter); } if (request.OperationId == Guid.Empty) { throw new BusinessException("Invalid operation id (GUID)", ErrorCode.BadInputParameter); } PubKey fromAddressPubkey = null; var pubKeyString = request.FromAddressContext?.DeserializeJson <AddressContextContract>()?.PubKey; if (pubKeyString != null) { if (!_addressValidator.IsPubkeyValid(pubKeyString)) { throw new BusinessException("Invalid pubkey string", ErrorCode.BadInputParameter); } fromAddressPubkey = _addressValidator.GetPubkey(pubKeyString); } if (await _operationEventRepository.Exist(request.OperationId, OperationEventType.Broadcasted)) { return(StatusCode(409)); } var tx = await _operationService.GetOrBuildTransferTransaction(request.OperationId, fromBitcoinAddress, fromAddressPubkey, toBitcoinAddress, request.AssetId, new Money(amountSatoshi), request.IncludeFee); return(Ok(new BuildTransactionResponse { TransactionContext = tx })); }
public async Task <ActionResult> BuildSingle([FromBody] BuildSingleTransactionRequest request) { if (request == null) { throw new BusinessException("Unable deserialize request", ErrorCode.BadInputParameter); } var amountSatoshi = MoneyConversionHelper.SatoshiFromContract(request.Amount); if (amountSatoshi <= 0) { throw new BusinessException($"Amount can't be less or equal to zero: {amountSatoshi}", ErrorCode.BadInputParameter); } if (request.AssetId != (await _assetRepository.GetDefaultAsset()).AssetId) { throw new BusinessException("Invalid assetId", ErrorCode.BadInputParameter); } var toBitcoinAddress = _addressValidator.GetBitcoinAddress(request.ToAddress, _blockChainProvider.Network); if (toBitcoinAddress == null) { throw new BusinessException("Invalid ToAddress ", ErrorCode.BadInputParameter); } var fromBitcoinAddress = _addressValidator.GetBitcoinAddress(request.FromAddress, _blockChainProvider.Network); if (fromBitcoinAddress == null) { throw new BusinessException("Invalid FromAddress", ErrorCode.BadInputParameter); } if (request.OperationId == Guid.Empty) { throw new BusinessException("Invalid operation id (GUID)", ErrorCode.BadInputParameter); } if (await _operationEventRepository.Exist(request.OperationId, OperationEventType.Broadcasted)) { return(Conflict()); } BuildedTransactionInfo tx; try { tx = await _operationService.GetOrBuildTransferTransaction(request.OperationId, fromBitcoinAddress, toBitcoinAddress, request.AssetId, new Money(amountSatoshi), request.IncludeFee); } catch (NotEnoughFundsException) { return(BadRequest(BlockchainErrorResponse.FromKnownError(BlockchainErrorCode.NotEnoughBalance))); } catch (BusinessException e) when(e.Code == ErrorCode.NotEnoughFundsAvailable) { return(BadRequest(BlockchainErrorResponse.FromKnownError(BlockchainErrorCode.NotEnoughBalance))); } return(Ok(new BuildTransactionResponse { TransactionContext = tx.ToJson(_network) })); }