-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plutus Serialization and contract tests (#60)
* adaToLovelace helper method added * Fix policyId generation for Plutus script and serialization issues * More address tests * Contract transaction tests * ScriptUtxoSelection interface and default impl * Fixed script data hash generation. PlutusObjectConverter interface * New test added for multi token transfer in one transaction
- Loading branch information
Showing
18 changed files
with
1,593 additions
and
350 deletions.
There are no files selected for viewing
1,403 changes: 1,120 additions & 283 deletions
1,403
...ation-test/java/com/bloxbean/cardano/client/backend/api/helper/ContractTransactionIT.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/bloxbean/cardano/client/plutus/api/PlutusObjectConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.bloxbean.cardano.client.plutus.api; | ||
|
||
import com.bloxbean.cardano.client.transaction.spec.ConstrPlutusData; | ||
import com.bloxbean.cardano.client.transaction.spec.PlutusData; | ||
|
||
public interface PlutusObjectConverter { | ||
|
||
public ConstrPlutusData convertToPlutusData(Object o); | ||
|
||
public <T> T convertToObject(PlutusData plutusData, Class<T> tClass); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/bloxbean/cardano/client/plutus/api/ScriptUtxoSelection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.bloxbean.cardano.client.plutus.api; | ||
|
||
import com.bloxbean.cardano.client.backend.exception.ApiException; | ||
import com.bloxbean.cardano.client.backend.model.Utxo; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
public interface ScriptUtxoSelection { | ||
/** | ||
* Find the first utxo matching the predicate | ||
* @param scriptAddress Script address | ||
* @param predicate Predicate to filter utxos | ||
* @return Utxo | ||
* @throws ApiException | ||
*/ | ||
Utxo findFirst(String scriptAddress, Predicate<Utxo> predicate) throws ApiException; | ||
|
||
/** | ||
* Find the first utxo matching the predicate | ||
* @param scriptAddress Script address | ||
* @param predicate Predicate to filter utxos | ||
* @param excludeUtxos Utxos to exclude | ||
* @return Utxo | ||
* @throws ApiException | ||
*/ | ||
Utxo findFirst(String scriptAddress, Predicate<Utxo> predicate, Set<Utxo> excludeUtxos) throws ApiException; | ||
|
||
/** | ||
* Find all utxos matching the predicate | ||
* @param scriptAddress Script address | ||
* @param predicate Predicate to filter utxos | ||
* @return List of Utxos | ||
* @throws ApiException | ||
*/ | ||
List<Utxo> findAll(String scriptAddress, Predicate<Utxo> predicate) throws ApiException; | ||
|
||
/** | ||
* Find all utxos matching the predicate | ||
* @param scriptAddress ScriptAddress Script address | ||
* @param predicate Predicate Predicate to filter utxos | ||
* @param excludeUtxos Utxos to exclude | ||
* @return List of Utxos | ||
* @throws ApiException | ||
*/ | ||
List<Utxo> findAll(String scriptAddress, Predicate<Utxo> predicate, Set<Utxo> excludeUtxos) throws ApiException; | ||
} |
96 changes: 96 additions & 0 deletions
96
src/main/java/com/bloxbean/cardano/client/plutus/impl/DefaultScriptUtxoSelection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.bloxbean.cardano.client.plutus.impl; | ||
|
||
import com.bloxbean.cardano.client.backend.api.UtxoService; | ||
import com.bloxbean.cardano.client.backend.common.OrderEnum; | ||
import com.bloxbean.cardano.client.backend.exception.ApiException; | ||
import com.bloxbean.cardano.client.backend.model.Result; | ||
import com.bloxbean.cardano.client.backend.model.Utxo; | ||
import com.bloxbean.cardano.client.plutus.api.ScriptUtxoSelection; | ||
|
||
import java.util.*; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
//TODO -- Unit tests pending | ||
public class DefaultScriptUtxoSelection implements ScriptUtxoSelection { | ||
private UtxoService utxoService; | ||
|
||
public DefaultScriptUtxoSelection(UtxoService utxoService) { | ||
this.utxoService = utxoService; | ||
} | ||
|
||
@Override | ||
public Utxo findFirst(String scriptAddress, Predicate<Utxo> predicate) throws ApiException { | ||
return findFirst(scriptAddress, predicate, Collections.EMPTY_SET); | ||
} | ||
|
||
@Override | ||
public Utxo findFirst(String scriptAddress, Predicate<Utxo> predicate, Set<Utxo> excludeUtxos) throws ApiException { | ||
boolean canContinue = true; | ||
int i = 1; | ||
|
||
while(canContinue) { | ||
Result<List<Utxo>> result = utxoService.getUtxos(scriptAddress, getUtxoFetchSize(), | ||
i++, getUtxoFetchOrder()); | ||
if(result.code() == 200) { | ||
List<Utxo> fetchData = result.getValue(); | ||
|
||
List<Utxo> data = (fetchData); | ||
if(data == null || data.isEmpty()) | ||
canContinue = false; | ||
|
||
Optional<Utxo> option = data.stream().filter(predicate).findFirst(); | ||
if (option.isPresent()) | ||
return option.get(); | ||
|
||
} else { | ||
canContinue = false; | ||
throw new ApiException(String.format("Unable to get enough Utxos for address : %s, reason: %s", scriptAddress, result.getResponse())); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public List<Utxo> findAll(String scriptAddress, Predicate<Utxo> predicate) throws ApiException { | ||
return findAll(scriptAddress, predicate); | ||
} | ||
|
||
@Override | ||
public List<Utxo> findAll(String scriptAddress, Predicate<Utxo> predicate, Set<Utxo> excludeUtxos) throws ApiException { | ||
boolean canContinue = true; | ||
int i = 1; | ||
|
||
List<Utxo> utxoList = new ArrayList<>(); | ||
while(canContinue) { | ||
Result<List<Utxo>> result = utxoService.getUtxos(scriptAddress, getUtxoFetchSize(), | ||
i++, getUtxoFetchOrder()); | ||
if(result.code() == 200) { | ||
List<Utxo> fetchData = result.getValue(); | ||
|
||
List<Utxo> data = (fetchData); | ||
if(data == null || data.isEmpty()) | ||
canContinue = false; | ||
|
||
List<Utxo> filterUtxos = data.stream().filter(predicate).collect(Collectors.toList()); | ||
utxoList.addAll(filterUtxos); | ||
|
||
} else { | ||
canContinue = false; | ||
throw new ApiException(String.format("Unable to get enough Utxos for address : %s, reason: %s", scriptAddress, result.getResponse())); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private OrderEnum getUtxoFetchOrder() { | ||
return OrderEnum.asc; | ||
} | ||
|
||
private int getUtxoFetchSize() { | ||
return 100; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.