From 321da1b00e73a7238ad2113c606113d53314230d Mon Sep 17 00:00:00 2001 From: Satya Date: Tue, 25 Jun 2024 23:15:22 +0800 Subject: [PATCH] feat: #402 Add Tx Evaluator implementation to return constant cost --- .../api/impl/StaticTransactionEvaluator.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 core-api/src/main/java/com/bloxbean/cardano/client/api/impl/StaticTransactionEvaluator.java diff --git a/core-api/src/main/java/com/bloxbean/cardano/client/api/impl/StaticTransactionEvaluator.java b/core-api/src/main/java/com/bloxbean/cardano/client/api/impl/StaticTransactionEvaluator.java new file mode 100644 index 00000000..555e1d16 --- /dev/null +++ b/core-api/src/main/java/com/bloxbean/cardano/client/api/impl/StaticTransactionEvaluator.java @@ -0,0 +1,80 @@ +package com.bloxbean.cardano.client.api.impl; + +import com.bloxbean.cardano.client.api.TransactionEvaluator; +import com.bloxbean.cardano.client.api.exception.ApiException; +import com.bloxbean.cardano.client.api.model.EvaluationResult; +import com.bloxbean.cardano.client.api.model.Result; +import com.bloxbean.cardano.client.api.model.Utxo; +import com.bloxbean.cardano.client.exception.CborDeserializationException; +import com.bloxbean.cardano.client.plutus.spec.ExUnits; +import com.bloxbean.cardano.client.transaction.spec.Transaction; +import com.bloxbean.cardano.client.util.JsonUtil; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +/** + * A static implementation of {@link TransactionEvaluator} which returns a static list of {@link EvaluationResult} + */ +public class StaticTransactionEvaluator implements TransactionEvaluator { + private List exUnits; + + /** + * Constructor + * @param exUnits List of {@link ExUnits} to be returned as evaluation result. The number of ExUnits should match the + * number of redeemers in the transaction in the order they appear in the transaction + */ + public StaticTransactionEvaluator(List exUnits) { + this.exUnits = exUnits; + } + + @Override + public Result> evaluateTx(byte[] cbor, Set inputUtxos) throws ApiException { + var evaluationResults = getEvaluationResults(cbor); + return Result.success(JsonUtil.getPrettyJson(evaluationResults)).withValue(evaluationResults); + } + + @Override + public Result> evaluateTx(byte[] cbor) throws ApiException { + var evaluationResults = getEvaluationResults(cbor); + return Result.success(JsonUtil.getPrettyJson(evaluationResults)).withValue(evaluationResults); + } + + @Override + public Result> evaluateTx(Transaction transaction, Set inputUtxos) throws ApiException { + var evaluationResults = getEvaluationResults(transaction); + return Result.success(JsonUtil.getPrettyJson(evaluationResults)).withValue(evaluationResults); + } + + private List getEvaluationResults(byte[] txCbor) { + Transaction transaction = null; + try { + transaction = Transaction.deserialize(txCbor); + } catch (CborDeserializationException e) { + throw new IllegalArgumentException("Invalid transaction cbor. Cost cannot be returned"); + } + return getEvaluationResults(transaction); + } + + private List getEvaluationResults(Transaction transaction) { + if (transaction.getWitnessSet().getRedeemers() == null) + throw new IllegalArgumentException("Transaction doesn't have redeemers. Cost cannot be returned"); + + if (transaction.getWitnessSet().getRedeemers().size() != exUnits.size()) + throw new IllegalArgumentException("Number of redeemers in the transaction doesn't match the number of exUnits provided"); + + List evaluationResults = new ArrayList<>(); + for (int i = 0; i < transaction.getWitnessSet().getRedeemers().size(); i++) { + var redeemer = transaction.getWitnessSet().getRedeemers().get(i); + var evaluationResult = new EvaluationResult(); + evaluationResult.setExUnits(exUnits.get(i)); + evaluationResult.setIndex(redeemer.getIndex().intValue()); + evaluationResult.setRedeemerTag(redeemer.getTag()); + + evaluationResults.add(evaluationResult); + } + + return evaluationResults; + } +}