| | 1 | | using EF.Blockchain.Domain; |
| | 2 | | using EF.Blockchain.Server.Dtos; |
| | 3 | | using EF.Blockchain.Server.Mappers; |
| | 4 | | using Microsoft.AspNetCore.Mvc; |
| | 5 | |
|
| | 6 | | namespace EF.Blockchain.Server.Endpoints; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Provides endpoints for managing blockchain transactions. |
| | 10 | | /// </summary> |
| | 11 | | public static class TransactionEndpoints |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Maps transaction-related endpoints. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="app">The endpoint route builder.</param> |
| | 17 | | public static void MapTransactionEndpoints(this IEndpointRouteBuilder app) |
| 112 | 18 | | { |
| 112 | 19 | | app.MapPost("/transactions/prepare", PrepareTransaction) |
| 112 | 20 | | .WithName("PrepareTransaction") |
| 112 | 21 | | .WithTags("Transaction") |
| 112 | 22 | | .WithSummary("Prepare a transaction with proper signing and hash generation") |
| 112 | 23 | | .WithDescription("Builds a complete transaction with signed inputs, proper outputs, and generates the transac |
| 112 | 24 | | .Produces<TransactionDto>(StatusCodes.Status200OK) |
| 112 | 25 | | .Produces(StatusCodes.Status400BadRequest) |
| 112 | 26 | | .WithOpenApi(); |
| | 27 | |
|
| 112 | 28 | | app.MapGet("/transactions/{hash?}", GetTransaction) |
| 112 | 29 | | .WithName("GetTransaction") |
| 112 | 30 | | .WithTags("Transaction") |
| 112 | 31 | | .WithSummary("Get a transaction by hash or list pending transactions") |
| 112 | 32 | | .WithDescription("If a hash is provided, returns the transaction. Otherwise, returns the next N pending tran |
| 112 | 33 | | .Produces<TransactionSearchDto?>(StatusCodes.Status200OK) |
| 112 | 34 | | .Produces(StatusCodes.Status404NotFound) |
| 112 | 35 | | .WithOpenApi(); |
| 56 | 36 | |
|
| 112 | 37 | | app.MapPost("/transactions", PostTransaction) |
| 56 | 38 | | .WithName("PostTransaction") |
| 56 | 39 | | .WithTags("Transaction") |
| 56 | 40 | | .WithSummary("Submit a new transaction") |
| 56 | 41 | | .WithDescription("Receives a transaction DTO and attempts to add it to the blockchain mempool.") |
| 56 | 42 | | .Produces<TransactionDto>(StatusCodes.Status201Created) |
| 56 | 43 | | .Produces(StatusCodes.Status400BadRequest) |
| 56 | 44 | | .Produces(StatusCodes.Status422UnprocessableEntity) |
| 60 | 45 | | .WithOpenApi(); |
| 60 | 46 | | } |
| 4 | 47 | |
|
| 4 | 48 | | /// <summary> |
| | 49 | | /// Handles GET /transactions/{hash?} |
| 4 | 50 | | /// </summary> |
| 4 | 51 | | private static IResult GetTransaction( |
| 4 | 52 | | [FromRoute] string? hash, |
| | 53 | | [FromServices] Domain.Blockchain blockchain) |
| 4 | 54 | | { |
| 4 | 55 | | if (!string.IsNullOrEmpty(hash)) |
| 4 | 56 | | { |
| 4 | 57 | | TransactionSearch transactionSearch = blockchain.GetTransaction(hash); |
| 0 | 58 | |
|
| 8 | 59 | | return transactionSearch is not null |
| 4 | 60 | | ? Results.Ok(TransactionSearchMapper.ToDto(transactionSearch)) |
| 4 | 61 | | : Results.NotFound(); |
| | 62 | | } |
| | 63 | |
|
| 0 | 64 | | var next = blockchain.Mempool.Take(Domain.Blockchain.TX_PER_BLOCK).ToList(); |
| 0 | 65 | | var total = blockchain.Mempool.Count; |
| | 66 | |
|
| 4 | 67 | | return Results.Ok(new { next, total }); |
| 8 | 68 | | } |
| 0 | 69 | |
|
| | 70 | | /// <summary> |
| 4 | 71 | | /// Handles POST /transactions |
| 4 | 72 | | /// </summary> |
| | 73 | | private static IResult PostTransaction( |
| 4 | 74 | | [FromBody] TransactionDto transactionDto, |
| 4 | 75 | | [FromServices] Domain.Blockchain blockchain) |
| 8 | 76 | | { |
| 8 | 77 | | if (string.IsNullOrEmpty(transactionDto.Hash)) |
| 0 | 78 | | return Results.StatusCode(StatusCodes.Status422UnprocessableEntity); |
| | 79 | |
|
| 4 | 80 | | var tx = TransactionMapper.ToDomain(transactionDto); |
| 4 | 81 | | var validation = blockchain.AddTransaction(tx); |
| | 82 | |
|
| 4 | 83 | | return validation.Success |
| 4 | 84 | | ? Results.Created($"/transactions/{tx.Hash}", TransactionMapper.ToDto(tx)) |
| 4 | 85 | | : Results.BadRequest(validation); |
| 4 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Handles the POST /transactions/prepare endpoint. |
| | 90 | | /// </summary> |
| | 91 | | private static IResult PrepareTransaction( |
| | 92 | | [FromBody] TransactionDto TransactionDto, |
| | 93 | | [FromServices] Domain.Blockchain blockchain) |
| 0 | 94 | | { |
| | 95 | | try |
| 0 | 96 | | { |
| 0 | 97 | | var fromWalletBalance = blockchain.GetBalance(TransactionDto.FromWalletAddress ?? ""); |
| 0 | 98 | | var fee = blockchain.GetFeePerTx(); |
| 0 | 99 | | var utxos = blockchain.GetUtxo(TransactionDto.FromWalletAddress ?? ""); |
| | 100 | |
|
| 0 | 101 | | if (fromWalletBalance < TransactionDto.Amount + fee) |
| 0 | 102 | | { |
| 0 | 103 | | return Results.BadRequest(new Validation(false, "Insufficient balance")); |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | if (!utxos.Any()) |
| 0 | 107 | | { |
| 0 | 108 | | return Results.BadRequest(new Validation(false, "No unspent transaction outputs available")); |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | var txInputs = utxos |
| 0 | 112 | | .Select(TransactionInput.FromTxo) |
| 0 | 113 | | .ToList(); |
| | 114 | |
|
| 0 | 115 | | txInputs.ForEach(input => input.Sign(TransactionDto.FromWalletPrivateKey ?? "")); |
| | 116 | |
|
| 0 | 117 | | var txOutputs = new List<TransactionOutput> |
| 0 | 118 | | { |
| 0 | 119 | | new TransactionOutput( |
| 0 | 120 | | toAddress: TransactionDto.ToWalletAddress ?? "", |
| 0 | 121 | | amount: TransactionDto.Amount |
| 0 | 122 | | ) |
| 0 | 123 | | }; |
| | 124 | |
|
| 0 | 125 | | var remaining = fromWalletBalance - TransactionDto.Amount - fee; |
| 0 | 126 | | if (remaining > 0) |
| 0 | 127 | | { |
| 0 | 128 | | txOutputs.Add(new TransactionOutput( |
| 0 | 129 | | toAddress: TransactionDto.FromWalletAddress ?? "", |
| 0 | 130 | | amount: remaining |
| 0 | 131 | | )); |
| 0 | 132 | | } |
| | 133 | |
|
| 0 | 134 | | var transaction = new Transaction( |
| 0 | 135 | | type: TransactionType.REGULAR, |
| 0 | 136 | | timestamp: DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), |
| 0 | 137 | | txInputs: txInputs, |
| 0 | 138 | | txOutputs: txOutputs |
| 0 | 139 | | ); |
| | 140 | |
|
| 0 | 141 | | var transactionPrepare = TransactionMapper.ToDto(transaction); |
| | 142 | |
|
| 0 | 143 | | return Results.Ok(transactionPrepare); |
| | 144 | | } |
| 0 | 145 | | catch (Exception ex) |
| 0 | 146 | | { |
| 0 | 147 | | return Results.Problem( |
| 0 | 148 | | title: "Transaction preparation failed", |
| 0 | 149 | | detail: ex.Message, |
| 0 | 150 | | statusCode: StatusCodes.Status500InternalServerError |
| 0 | 151 | | ); |
| | 152 | | } |
| 0 | 153 | | } |
| | 154 | | } |