Ejemplo n.º 1
0
        public async Task <IActionResult> BuildClaim([FromBody] BuildClaimTransactionRequest request)
        {
            if (request == null)
            {
                return(BadRequest(ErrorResponse.Create("Unable to deserialize request")));
            }

            var addressValid = _addressValidator.IsAddressValid(request.Address);

            if (!addressValid)
            {
                return(BadRequest(ErrorResponse.Create("Invalid address")));
            }

            var built = await _transactionBuilder.BuildClaimTransactions(request.Address);

            var aggregate = await _operationRepository.GetOrInsert(request.OperationId,
                                                                   () => OperationAggregate.StartNew(request.OperationId,
                                                                                                     fromAddress: request.Address,
                                                                                                     toAddress: request.Address,
                                                                                                     amount: built.availiableGas,
                                                                                                     assetId: Constants.Assets.Gas.AssetId,
                                                                                                     fee: 0,
                                                                                                     includeFee: false));

            if (aggregate.IsBroadcasted)
            {
                return(Conflict());
            }

            if (!built.tx.Claims.Any())
            {
                return(Accepted(new BuiltClaimTransactionResponse
                {
                    ClaimedGas = MoneyConversionHelper.ToContract(built.availiableGas, Constants.Assets.Gas.AssetId),
                    AllGas = MoneyConversionHelper.ToContract(built.unclaimedGas, Constants.Assets.Gas.AssetId),
                    TransactionContext = TransactionSerializer.Serialize(built.tx, TransactionType.ClaimTransaction)
                }));
            }

            return(Ok(new BuiltClaimTransactionResponse
            {
                ClaimedGas = MoneyConversionHelper.ToContract(built.availiableGas, Constants.Assets.Gas.AssetId),
                AllGas = MoneyConversionHelper.ToContract(built.unclaimedGas, Constants.Assets.Gas.AssetId),
                TransactionContext = TransactionSerializer.Serialize(built.tx, TransactionType.ClaimTransaction)
            }));
        }