Plutus API
The Plutus API provides comprehensive functionality for interacting with Plutus smart contracts on the Cardano blockchain. It includes support for Plutus scripts (V1, V2, V3) and data serialization with type safety.
Key Features
Section titled “Key Features”- Plutus Script Support: Complete support for V1, V2, and V3 script versions
- Data Serialization: Convert between Java objects and PlutusData
- Type Safety: Strong typing for Plutus data structures
Core Classes
Section titled “Core Classes”PlutusScript Classes
Section titled “PlutusScript Classes”PlutusV1Script: Plutus V1 script implementationPlutusV2Script: Plutus V2 script implementationPlutusV3Script: Plutus V3 script implementation
PlutusData Classes
Section titled “PlutusData Classes”PlutusData: Base interface for all Plutus data typesBigIntPlutusData: Integer data representationBytesPlutusData: Byte string data representationListPlutusData: List data representationMapPlutusData: Map data representation
Usage Examples
Section titled “Usage Examples”Creating Plutus Scripts
Section titled “Creating Plutus Scripts”Create different versions of Plutus scripts:
// Create Plutus V2 Script (most common)PlutusV2Script v2Script = PlutusV2Script.builder() .type("PlutusScriptV2") .cborHex("590a4d590a4a0100003333...") .build();
// Create Plutus V3 Script (latest)PlutusV3Script v3Script = PlutusV3Script.builder() .type("PlutusScriptV3") .cborHex("590b5e590b5b0100003333...") .build();
System.out.println("Scripts created successfully");Loading Plutus Scripts from a Blueprint (CIP-57)
Section titled “Loading Plutus Scripts from a Blueprint (CIP-57)”Smart contract compilers like Aiken and Scalus produce a plutus.json blueprint file
following the CIP-57 standard. This file contains a compiledCode field for each validator,
which is single-encoded CBOR hex. CCL’s PlutusScript objects require double-encoded CBOR, and the
PlutusBlueprintUtil class handles this conversion automatically.
A typical plutus.json looks like this:
{ "preamble": { "title": "aiken-lang/hello_world", "version": "1.0.0", "plutusVersion": "v3" }, "validators": [ { "title": "hello_world.spend", "compiledCode": "58ad0100003232322..." } ]}Option A: Direct conversion with PlutusBlueprintUtil
Section titled “Option A: Direct conversion with PlutusBlueprintUtil”If you already have the compiledCode hex string and know the Plutus version, you can convert it directly:
import com.bloxbean.cardano.client.plutus.blueprint.PlutusBlueprintUtil;import com.bloxbean.cardano.client.plutus.blueprint.model.PlutusVersion;
String compiledCode = "58ad0100003232322..."; // from plutus.json validators[].compiledCode
PlutusScript plutusScript = PlutusBlueprintUtil.getPlutusScriptFromCompiledCode( compiledCode, PlutusVersion.v3);This performs double CBOR encoding internally and returns the appropriate PlutusV1Script, PlutusV2Script, or PlutusV3Script instance.
Option B: Load the full blueprint with PlutusBlueprintLoader
Section titled “Option B: Load the full blueprint with PlutusBlueprintLoader”For a more streamlined approach, load the entire plutus.json and extract scripts by validator title:
import com.bloxbean.cardano.client.plutus.blueprint.PlutusBlueprintLoader;import com.bloxbean.cardano.client.plutus.blueprint.model.PlutusContractBlueprint;
// Load the blueprint from classpathInputStream is = getClass().getResourceAsStream("/plutus.json");PlutusContractBlueprint blueprint = PlutusBlueprintLoader.loadBlueprint(is);
// Extract a PlutusScript by validator titlePlutusScript plutusScript = blueprint.getPlutusScript("hello_world.spend");Using the loaded script with QuickTx
Section titled “Using the loaded script with QuickTx”Once you have a PlutusScript, use it with ScriptTx the same way as a manually created script:
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService);
ScriptTx scriptTx = new ScriptTx() .collectFrom(utxo, redeemer) .payToAddress(receiverAddress, Amount.ada(10)) .attachSpendingValidator(plutusScript);
Result<String> result = quickTxBuilder.compose(scriptTx) .feePayer(senderAddress) .withSigner(SignerProviders.signerFrom(senderAccount)) .completeAndWait(System.out::println);See also:
- Plutus Blueprint Code Generation — generate type-safe Java bindings from a blueprint using annotations
- Aiken Integration — Script cost evaluation and parameterized contracts
- Scalus Integration — Script cost evaluation and parameterized contracts
Working with PlutusData
Section titled “Working with PlutusData”Create and manipulate PlutusData structures:
// Create integer dataPlutusData intData = BigIntPlutusData.of(BigInteger.valueOf(42));
// Create string data (as bytes)PlutusData stringData = BytesPlutusData.of("Hello Cardano".getBytes());
// Create list data (varargs)PlutusData listData = ListPlutusData.of(intData, stringData);
// Create map dataMapPlutusData mapData = new MapPlutusData();mapData.put(BytesPlutusData.of("key".getBytes()), intData);
System.out.println("PlutusData structures created");Best Practices
Section titled “Best Practices”Type Safety
Section titled “Type Safety”// Use type-safe PlutusData creationpublic PlutusData createTypeSafePlutusData() { // Prefer specific types over generic PlutusData PlutusData intData = BigIntPlutusData.of(BigInteger.valueOf(100)); PlutusData bytesData = BytesPlutusData.of("data".getBytes());
// Validate data before use if (intData instanceof BigIntPlutusData) { BigInteger value = ((BigIntPlutusData) intData).getValue(); System.out.println("Integer value: " + value); }
return intData;}