6
6
import com .bloxbean .cardano .client .api .model .Amount ;
7
7
import com .bloxbean .cardano .client .api .model .Result ;
8
8
import com .bloxbean .cardano .client .api .model .Utxo ;
9
- import com .bloxbean .cardano .client .backend .model .TxContentRedeemers ;
10
9
import com .bloxbean .cardano .client .common .model .Networks ;
10
+ import com .bloxbean .cardano .client .exception .CborDeserializationException ;
11
11
import com .bloxbean .cardano .client .function .helper .ScriptUtxoFinders ;
12
12
import com .bloxbean .cardano .client .function .helper .SignerProviders ;
13
13
import com .bloxbean .cardano .client .plutus .spec .*;
14
14
import com .bloxbean .cardano .client .transaction .spec .TransactionInput ;
15
+ import com .bloxbean .cardano .client .util .HexUtil ;
15
16
import com .bloxbean .cardano .client .util .JsonUtil ;
16
17
import org .junit .jupiter .api .Test ;
17
18
@@ -30,7 +31,7 @@ public class ScriptTxV3IT extends TestDataBaseIT {
30
31
private boolean aikenEvaluation = false ;
31
32
32
33
@ Test
33
- void alwaysTrueScript () throws ApiException {
34
+ void alwaysTrueScript () throws ApiException , CborDeserializationException {
34
35
PlutusV3Script plutusScript = PlutusV3Script .builder ()
35
36
.type ("PlutusScriptV3" )
36
37
.cborHex ("46450101002499" )
@@ -60,7 +61,7 @@ void alwaysTrueScript() throws ApiException {
60
61
Optional <Utxo > optionalUtxo = ScriptUtxoFinders .findFirstByInlineDatum (utxoSupplier , scriptAddress , plutusData );
61
62
ScriptTx scriptTx = new ScriptTx ()
62
63
.collectFrom (optionalUtxo .get (), plutusData )
63
- .payToAddress (receiver1 , Amount .lovelace ( scriptAmt ))
64
+ .payToAddress (receiver1 , Amount .ada ( 1 ))
64
65
.attachSpendingValidator (plutusScript )
65
66
.withChangeAddress (scriptAddress , plutusData );
66
67
@@ -81,9 +82,74 @@ void alwaysTrueScript() throws ApiException {
81
82
82
83
checkIfUtxoAvailable (result1 .getValue (), sender2Addr );
83
84
84
- // Example of getting the redeemer datum hash and then getting the datum values.
85
- List <TxContentRedeemers > redeemers = getBackendService ().getTransactionService ()
86
- .getTransactionRedeemers (result1 .getValue ()).getValue ();
85
+ //Validate inline datum in script change output
86
+ var utxoOpt = utxoSupplier .getTxOutput (result1 .getValue (), 1 );
87
+ assertTrue (utxoOpt .isPresent ());
88
+ Utxo scriptChangeUtxo = utxoOpt .get ();
89
+ assertThat (scriptChangeUtxo .getAddress ()).isEqualTo (scriptAddress );
90
+ assertThat (scriptChangeUtxo .getInlineDatum ()).isNotEmpty ();
91
+ assertThat (PlutusData .deserialize (HexUtil .decodeHexString (scriptChangeUtxo .getInlineDatum ()))).isEqualTo (plutusData );
92
+ }
93
+
94
+ @ Test
95
+ void alwaysTrueScript_datumHashInChangeOutput () throws ApiException , CborDeserializationException {
96
+ PlutusV3Script plutusScript = PlutusV3Script .builder ()
97
+ .type ("PlutusScriptV3" )
98
+ .cborHex ("46450101002499" )
99
+ .build ();
100
+
101
+ String scriptAddress = AddressProvider .getEntAddress (plutusScript , Networks .testnet ()).toBech32 ();
102
+ BigInteger scriptAmt = new BigInteger ("2479280" );
103
+
104
+ Random rand = new Random ();
105
+ long randInt = System .currentTimeMillis ();
106
+ BigIntPlutusData plutusData = new BigIntPlutusData (BigInteger .valueOf (randInt )); //any random number
107
+
108
+ Tx tx = new Tx ();
109
+ tx .payToContract (scriptAddress , Amount .lovelace (scriptAmt ), plutusData )
110
+ .from (sender2Addr );
111
+
112
+ QuickTxBuilder quickTxBuilder = new QuickTxBuilder (backendService );
113
+ var result = quickTxBuilder .compose (tx )
114
+ .withSigner (SignerProviders .signerFrom (sender2 ))
115
+ .complete ();
116
+
117
+ assertThat (result .getTxStatus ()).isEqualTo (TxStatus .SUBMITTED );
118
+
119
+ System .out .println (result .getResponse ());
120
+ checkIfUtxoAvailable (result .getValue (), scriptAddress );
121
+
122
+ Optional <Utxo > optionalUtxo = ScriptUtxoFinders .findFirstByInlineDatum (utxoSupplier , scriptAddress , plutusData );
123
+ ScriptTx scriptTx = new ScriptTx ()
124
+ .collectFrom (optionalUtxo .get (), plutusData )
125
+ .payToAddress (receiver1 , Amount .ada (1 ))
126
+ .attachSpendingValidator (plutusScript )
127
+ .withChangeAddress (scriptAddress , plutusData .getDatumHash ());
128
+
129
+ Result <String > result1 = quickTxBuilder .compose (scriptTx )
130
+ .feePayer (sender2Addr )
131
+ .withSigner (SignerProviders .signerFrom (sender2 ))
132
+ .withRequiredSigners (sender2 .getBaseAddress ())
133
+ .withVerifier (txn -> {
134
+ System .out .println (JsonUtil .getPrettyJson (txn ));
135
+ assertThat (txn .getBody ().getRequiredSigners ()).hasSize (1 );
136
+ assertThat (txn .getBody ().getRequiredSigners ().get (0 )) //Verify sender's payment cred hash in required signer
137
+ .isEqualTo (sender2 .getBaseAddress ().getPaymentCredentialHash ().get ());
138
+ })
139
+ .completeAndWait (System .out ::println );
140
+
141
+ System .out .println (result1 );
142
+ assertTrue (result1 .isSuccessful ());
143
+
144
+ checkIfUtxoAvailable (result1 .getValue (), sender2Addr );
145
+
146
+ //Validate inline datum in script change output
147
+ var utxoOpt = utxoSupplier .getTxOutput (result1 .getValue (), 1 );
148
+ assertTrue (utxoOpt .isPresent ());
149
+ Utxo scriptChangeUtxo = utxoOpt .get ();
150
+ assertThat (scriptChangeUtxo .getAddress ()).isEqualTo (scriptAddress );
151
+ assertThat (scriptChangeUtxo .getDataHash ()).isNotEmpty ();
152
+ assertThat (scriptChangeUtxo .getDataHash ()).isEqualTo (plutusData .getDatumHash ());
87
153
}
88
154
89
155
@ Test
0 commit comments