8
8
import com .bloxbean .cardano .client .util .Tuple ;
9
9
10
10
import java .math .BigInteger ;
11
+ import java .util .function .Predicate ;
11
12
12
13
import static com .bloxbean .cardano .client .common .CardanoConstants .LOVELACE ;
13
14
@@ -18,6 +19,8 @@ public class OutputAmountVerifier implements Verifier {
18
19
19
20
private final String address ;
20
21
private final Amount amount ;
22
+ private final Predicate <Amount > predicate ;
23
+ private final String unit ;
21
24
private final String customMsg ;
22
25
23
26
/**
@@ -29,19 +32,43 @@ public class OutputAmountVerifier implements Verifier {
29
32
public OutputAmountVerifier (String address , Amount amount , String customMsg ) {
30
33
this .address = address ;
31
34
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 ;
32
54
this .customMsg = customMsg ;
33
55
}
34
56
35
57
@ Override
36
58
public void verify (Transaction txn ) throws VerifierException {
37
- if (LOVELACE .equals (amount . getUnit () )) {
59
+ if (LOVELACE .equals (unit )) {
38
60
BigInteger lovelaceAmt = txn .getBody ()
39
61
.getOutputs ().stream ()
40
62
.filter (o -> o .getAddress ().equals (address ))
41
63
.map (o -> o .getValue ().getCoin ())
42
64
.reduce (BigInteger .ZERO , (amount1 , amount2 ) -> amount1 .add (amount2 ));
43
65
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 ) {
45
72
String expectedMsg = formatExceptionMessage (customMsg , address , amount , lovelaceAmt );
46
73
throw new VerifierException (expectedMsg );
47
74
}
@@ -57,8 +84,13 @@ public void verify(Transaction txn) throws VerifierException {
57
84
}).map (assetTuple -> assetTuple ._2 .getValue ())
58
85
.reduce (BigInteger .ZERO , (amount1 , amount2 ) -> amount1 .add (amount2 ));
59
86
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 );
62
94
throw new VerifierException (expectedMsg );
63
95
}
64
96
}
@@ -72,4 +104,13 @@ private String formatExceptionMessage(String customMsg, String address, Amount e
72
104
73
105
return expectedMsg ;
74
106
}
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
+ }
75
116
}
0 commit comments