Skip to content

Commit aa07e29

Browse files
committed
chore: Add reference scripts
1 parent 10befbc commit aa07e29

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

function/src/main/java/com/bloxbean/cardano/client/function/TxBuilderContext.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import com.bloxbean.cardano.client.transaction.spec.MultiAsset;
2020
import com.bloxbean.cardano.client.transaction.spec.Transaction;
2121
import com.bloxbean.cardano.client.util.HexUtil;
22+
import com.bloxbean.cardano.client.util.Tuple;
2223
import lombok.AccessLevel;
2324
import lombok.Data;
2425
import lombok.Setter;
2526
import lombok.SneakyThrows;
2627

2728
import java.util.*;
29+
import java.util.stream.Collectors;
2830

2931
/**
3032
* Provides necessary services which are required to build the transaction
@@ -47,11 +49,12 @@ public class TxBuilderContext {
4749
//Stores utxos used in the transaction.
4850
//This list is cleared after each build() call.
4951
private Set<Utxo> utxos = new HashSet<>();
50-
private Map<String, byte[]> refScripts = new HashMap<>();
52+
53+
@Setter(AccessLevel.NONE)
54+
private Map<String, Tuple<PlutusScript, byte[]>> refScripts = new HashMap<>();
5155

5256
@Setter(AccessLevel.NONE)
5357
private boolean mergeOutputs = true;
54-
private Language referenceInputLanguage = Language.PLUTUS_V2;
5558

5659
public TxBuilderContext(UtxoSupplier utxoSupplier, ProtocolParamsSupplier protocolParamsSupplier) {
5760
this(utxoSupplier, protocolParamsSupplier.getProtocolParams());
@@ -184,15 +187,32 @@ public void clearUtxos() {
184187

185188
@SneakyThrows
186189
public void addRefScripts(PlutusScript plutusScript) {
187-
refScripts.put(HexUtil.encodeHexString(plutusScript.getScriptHash()), plutusScript.scriptRefBytes());
190+
refScripts.put(HexUtil.encodeHexString(plutusScript.getScriptHash()), new Tuple<>(plutusScript, plutusScript.scriptRefBytes()));
188191
}
189192

190-
public byte[] getRefScript(String scriptHash) {
191-
return refScripts.get(scriptHash);
193+
public Optional<byte[]> getRefScript(String scriptHash) {
194+
var tuple = refScripts.get(scriptHash);
195+
return tuple != null ? Optional.of(tuple._2) : Optional.empty();
192196
}
193197

194198
public List<byte[]> getRefScripts() {
195-
return new ArrayList<>(refScripts.values());
199+
return refScripts.values().stream().map(tuple -> tuple._2)
200+
.collect(Collectors.toList());
201+
}
202+
203+
public List<PlutusScript> getRefPlutusScripts() {
204+
return refScripts.values().stream().map(tuple -> tuple._1)
205+
.collect(Collectors.toList());
206+
}
207+
208+
public List<Language> getRefScriptLanguages() {
209+
if (refScripts.size() == 0)
210+
return Collections.emptyList();
211+
212+
return refScripts.values().stream().map(tuple -> tuple._1.getLanguage())
213+
.collect(Collectors.toSet())
214+
.stream().sorted(Comparator.comparing(Language::getKey))
215+
.collect(Collectors.toList());
196216
}
197217

198218
public void clearRefScripts() {

0 commit comments

Comments
 (0)