Skip to content

Commit bcf06b5

Browse files
committed
Add flexible amount verification using predicates
1 parent d48de7f commit bcf06b5

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

quicktx/src/main/java/com/bloxbean/cardano/client/quicktx/verifiers/OutputAmountVerifier.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.bloxbean.cardano.client.util.Tuple;
99

1010
import java.math.BigInteger;
11+
import java.util.function.Predicate;
1112

1213
import static com.bloxbean.cardano.client.common.CardanoConstants.LOVELACE;
1314

@@ -18,6 +19,8 @@ public class OutputAmountVerifier implements Verifier {
1819

1920
private final String address;
2021
private final Amount amount;
22+
private final Predicate<Amount> predicate;
23+
private final String unit;
2124
private final String customMsg;
2225

2326
/**
@@ -29,19 +32,43 @@ public class OutputAmountVerifier implements Verifier {
2932
public OutputAmountVerifier(String address, Amount amount, String customMsg) {
3033
this.address = address;
3134
this.amount = amount;
35+
this.unit = amount.getUnit();
36+
this.predicate = null;
37+
this.customMsg = customMsg;
38+
}
39+
40+
/**
41+
* Constructor to create an OutputAmountVerifier instance with a specific unit and predicate for validation.
42+
* This verifier is used to verify the output amount for a given address.
43+
*
44+
* @param address the address for which the output amount is to be verified
45+
* @param unit the unit of the amount to be verified
46+
* @param predicate a predicate to define the condition for amount verification
47+
* @param customMsg a custom message to throw in case of verification failure
48+
*/
49+
public OutputAmountVerifier(String address, String unit, Predicate<Amount> predicate, String customMsg) {
50+
this.address = address;
51+
this.amount = null;
52+
this.unit = unit;
53+
this.predicate = predicate;
3254
this.customMsg = customMsg;
3355
}
3456

3557
@Override
3658
public void verify(Transaction txn) throws VerifierException {
37-
if (LOVELACE.equals(amount.getUnit())) {
59+
if (LOVELACE.equals(unit)) {
3860
BigInteger lovelaceAmt = txn.getBody()
3961
.getOutputs().stream()
4062
.filter(o -> o.getAddress().equals(address))
4163
.map(o -> o.getValue().getCoin())
4264
.reduce(BigInteger.ZERO, (amount1, amount2) -> amount1.add(amount2));
4365

44-
if (lovelaceAmt.compareTo(amount.getQuantity()) != 0) {
66+
if (predicate != null) {
67+
if (!predicate.test(Amount.lovelace(lovelaceAmt))) {
68+
String expectedMsg = formatExceptionMessage(customMsg, address, unit, lovelaceAmt);
69+
throw new VerifierException(expectedMsg);
70+
}
71+
} else if (lovelaceAmt.compareTo(amount.getQuantity()) != 0) {
4572
String expectedMsg = formatExceptionMessage(customMsg, address, amount, lovelaceAmt);
4673
throw new VerifierException(expectedMsg);
4774
}
@@ -57,8 +84,13 @@ public void verify(Transaction txn) throws VerifierException {
5784
}).map(assetTuple -> assetTuple._2.getValue())
5885
.reduce(BigInteger.ZERO, (amount1, amount2) -> amount1.add(amount2));
5986

60-
String expectedMsg = formatExceptionMessage(customMsg, address, amount, assetAmount);
61-
if (assetAmount.compareTo(amount.getQuantity()) != 0) {
87+
if (predicate != null) {
88+
if (!predicate.test(Amount.asset(amount.getUnit(), assetAmount))) {
89+
String expectedMsg = formatExceptionMessage(customMsg, address, unit, assetAmount);
90+
throw new VerifierException(expectedMsg);
91+
}
92+
} else if (assetAmount.compareTo(amount.getQuantity()) != 0) {
93+
String expectedMsg = formatExceptionMessage(customMsg, address, amount, assetAmount);
6294
throw new VerifierException(expectedMsg);
6395
}
6496
}
@@ -72,4 +104,13 @@ private String formatExceptionMessage(String customMsg, String address, Amount e
72104

73105
return expectedMsg;
74106
}
107+
108+
private String formatExceptionMessage(String customMsg, String address, String unit, BigInteger actualAmount) {
109+
String expectedMsg = String.format("Amount for address %s and unit %s doesn't match the condition. Actual amount: %s",
110+
address, unit, actualAmount);
111+
if(customMsg != null)
112+
expectedMsg = customMsg + ".\n" + expectedMsg;
113+
114+
return expectedMsg;
115+
}
75116
}

0 commit comments

Comments
 (0)