From fc1a376fbdcf8fb9a3ab42a2bdf465ff21bb35ea Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 29 Jan 2022 10:38:15 +0000 Subject: [PATCH] Added POST /transaction/fee API endpoint, to return the recommended fee for the supplied transaction data. --- .../api/resource/TransactionsResource.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/main/java/org/qortal/api/resource/TransactionsResource.java b/src/main/java/org/qortal/api/resource/TransactionsResource.java index f8a50ec7..9bc6d497 100644 --- a/src/main/java/org/qortal/api/resource/TransactionsResource.java +++ b/src/main/java/org/qortal/api/resource/TransactionsResource.java @@ -402,6 +402,47 @@ public class TransactionsResource { } } + @POST + @Path("/fee") + @Operation( + summary = "Get recommended fee for supplied transaction data", + requestBody = @RequestBody( + required = true, + content = @Content( + mediaType = MediaType.TEXT_PLAIN, + schema = @Schema( + type = "string" + ) + ) + ) + ) + @ApiErrors({ + ApiError.INVALID_CRITERIA, ApiError.REPOSITORY_ISSUE + }) + public long getRecommendedTransactionFee(String rawInputBytes58) { + byte[] rawInputBytes = Base58.decode(rawInputBytes58); + if (rawInputBytes.length == 0) + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.JSON); + + try (final Repository repository = RepositoryManager.getRepository()) { + + // Append null signature on the end before transformation + byte[] rawBytes = Bytes.concat(rawInputBytes, new byte[TransactionTransformer.SIGNATURE_LENGTH]); + + TransactionData transactionData = TransactionTransformer.fromBytes(rawBytes); + if (transactionData == null) + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA); + + Transaction transaction = Transaction.fromData(repository, transactionData); + return transaction.calcRecommendedFee(); + + } catch (DataException e) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e); + } catch (TransformationException e) { + throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.TRANSFORMATION_ERROR, e); + } + } + @GET @Path("/creator/{publickey}") @Operation(