Plutus

Plutus related classes and functions.

class pycardano.plutus.CostModels(*args, **kwargs)

Bases: DictCBORSerializable

KEY_TYPE

alias of int

VALUE_TYPE

alias of dict

to_shallow_primitive() dict

Convert the instance to a CBOR primitive. If the primitive is a container, e.g. list, dict, the type of its elements could be either a Primitive or a CBORSerializable.

Returns:

A CBOR primitive.

Return type:

Primitive

Raises:

SerializeException – When the object could not be converted to CBOR primitive types.

classmethod from_primitive(value: dict) CostModels

Restore a primitive value to its original class type.

Parameters:
  • cls (DictBase) – The original class type.

  • value (Primitive) – A CBOR primitive.

Returns:

Restored object.

Return type:

DictBase

Raises:

DeserializeException – When the object could not be restored from primitives.

pycardano.plutus.COST_MODELS = {0: {'sha2_256-memory-arguments': 4, 'equalsString-cpu-arguments-constant': 1000, 'cekDelayCost-exBudgetMemory': 100, 'lessThanEqualsByteString-cpu-arguments-intercept': 103599, 'divideInteger-memory-arguments-minimum': 1, 'appendByteString-cpu-arguments-slope': 621, 'blake2b-cpu-arguments-slope': 29175, 'iData-cpu-arguments': 150000, 'encodeUtf8-cpu-arguments-slope': 1000, 'unBData-cpu-arguments': 150000, 'multiplyInteger-cpu-arguments-intercept': 61516, 'cekConstCost-exBudgetMemory': 100, 'nullList-cpu-arguments': 150000, 'equalsString-cpu-arguments-intercept': 150000, 'trace-cpu-arguments': 150000, 'mkNilData-memory-arguments': 32, 'lengthOfByteString-cpu-arguments': 150000, 'cekBuiltinCost-exBudgetCPU': 29773, 'bData-cpu-arguments': 150000, 'subtractInteger-cpu-arguments-slope': 0, 'unIData-cpu-arguments': 150000, 'consByteString-memory-arguments-intercept': 0, 'divideInteger-memory-arguments-slope': 1, 'divideInteger-cpu-arguments-model-arguments-slope': 118, 'listData-cpu-arguments': 150000, 'headList-cpu-arguments': 150000, 'chooseData-memory-arguments': 32, 'equalsInteger-cpu-arguments-intercept': 136542, 'sha3_256-cpu-arguments-slope': 82363, 'sliceByteString-cpu-arguments-slope': 5000, 'unMapData-cpu-arguments': 150000, 'lessThanInteger-cpu-arguments-intercept': 179690, 'mkCons-cpu-arguments': 150000, 'appendString-memory-arguments-intercept': 0, 'modInteger-cpu-arguments-model-arguments-slope': 118, 'ifThenElse-cpu-arguments': 1, 'mkNilPairData-cpu-arguments': 150000, 'lessThanEqualsInteger-cpu-arguments-intercept': 145276, 'addInteger-memory-arguments-slope': 1, 'chooseList-memory-arguments': 32, 'constrData-memory-arguments': 32, 'decodeUtf8-cpu-arguments-intercept': 150000, 'equalsData-memory-arguments': 1, 'subtractInteger-memory-arguments-slope': 1, 'appendByteString-memory-arguments-intercept': 0, 'lengthOfByteString-memory-arguments': 4, 'headList-memory-arguments': 32, 'listData-memory-arguments': 32, 'consByteString-cpu-arguments-intercept': 150000, 'unIData-memory-arguments': 32, 'remainderInteger-memory-arguments-minimum': 1, 'bData-memory-arguments': 32, 'lessThanByteString-cpu-arguments-slope': 248, 'encodeUtf8-memory-arguments-intercept': 0, 'cekStartupCost-exBudgetCPU': 100, 'multiplyInteger-memory-arguments-intercept': 0, 'unListData-memory-arguments': 32, 'remainderInteger-cpu-arguments-model-arguments-slope': 118, 'cekVarCost-exBudgetCPU': 29773, 'remainderInteger-memory-arguments-slope': 1, 'cekForceCost-exBudgetCPU': 29773, 'sha2_256-cpu-arguments-slope': 29175, 'equalsInteger-memory-arguments': 1, 'indexByteString-memory-arguments': 1, 'addInteger-memory-arguments-intercept': 1, 'chooseUnit-cpu-arguments': 150000, 'sndPair-cpu-arguments': 150000, 'cekLamCost-exBudgetCPU': 29773, 'fstPair-cpu-arguments': 150000, 'quotientInteger-memory-arguments-minimum': 1, 'decodeUtf8-cpu-arguments-slope': 1000, 'lessThanInteger-memory-arguments': 1, 'lessThanEqualsInteger-cpu-arguments-slope': 1366, 'fstPair-memory-arguments': 32, 'modInteger-memory-arguments-intercept': 0, 'unConstrData-cpu-arguments': 150000, 'lessThanEqualsInteger-memory-arguments': 1, 'chooseUnit-memory-arguments': 32, 'sndPair-memory-arguments': 32, 'addInteger-cpu-arguments-intercept': 197209, 'decodeUtf8-memory-arguments-slope': 8, 'equalsData-cpu-arguments-intercept': 150000, 'mapData-cpu-arguments': 150000, 'mkPairData-cpu-arguments': 150000, 'quotientInteger-cpu-arguments-constant': 148000, 'consByteString-memory-arguments-slope': 1, 'cekVarCost-exBudgetMemory': 100, 'indexByteString-cpu-arguments': 150000, 'unListData-cpu-arguments': 150000, 'equalsInteger-cpu-arguments-slope': 1326, 'cekStartupCost-exBudgetMemory': 100, 'subtractInteger-cpu-arguments-intercept': 197209, 'divideInteger-cpu-arguments-model-arguments-intercept': 425507, 'divideInteger-memory-arguments-intercept': 0, 'cekForceCost-exBudgetMemory': 100, 'blake2b-cpu-arguments-intercept': 2477736, 'remainderInteger-cpu-arguments-constant': 148000, 'tailList-cpu-arguments': 150000, 'encodeUtf8-cpu-arguments-intercept': 150000, 'equalsString-cpu-arguments-slope': 1000, 'lessThanByteString-memory-arguments': 1, 'multiplyInteger-cpu-arguments-slope': 11218, 'appendByteString-cpu-arguments-intercept': 396231, 'lessThanEqualsByteString-cpu-arguments-slope': 248, 'modInteger-memory-arguments-slope': 1, 'addInteger-cpu-arguments-slope': 0, 'equalsData-cpu-arguments-slope': 10000, 'decodeUtf8-memory-arguments-intercept': 0, 'chooseList-cpu-arguments': 150000, 'constrData-cpu-arguments': 150000, 'equalsByteString-memory-arguments': 1, 'cekApplyCost-exBudgetCPU': 29773, 'quotientInteger-memory-arguments-slope': 1, 'verifySignature-cpu-arguments-intercept': 3345831, 'unMapData-memory-arguments': 32, 'mkCons-memory-arguments': 32, 'sliceByteString-memory-arguments-slope': 1, 'sha3_256-memory-arguments': 4, 'ifThenElse-memory-arguments': 1, 'mkNilPairData-memory-arguments': 32, 'equalsByteString-cpu-arguments-slope': 247, 'appendString-cpu-arguments-intercept': 150000, 'quotientInteger-cpu-arguments-model-arguments-slope': 118, 'cekApplyCost-exBudgetMemory': 100, 'equalsString-memory-arguments': 1, 'multiplyInteger-memory-arguments-slope': 1, 'cekBuiltinCost-exBudgetMemory': 100, 'remainderInteger-memory-arguments-intercept': 0, 'sha2_256-cpu-arguments-intercept': 2477736, 'remainderInteger-cpu-arguments-model-arguments-intercept': 425507, 'lessThanEqualsByteString-memory-arguments': 1, 'tailList-memory-arguments': 32, 'mkNilData-cpu-arguments': 150000, 'chooseData-cpu-arguments': 150000, 'unBData-memory-arguments': 32, 'blake2b-memory-arguments': 4, 'iData-memory-arguments': 32, 'nullList-memory-arguments': 32, 'cekDelayCost-exBudgetCPU': 29773, 'subtractInteger-memory-arguments-intercept': 1, 'lessThanByteString-cpu-arguments-intercept': 103599, 'consByteString-cpu-arguments-slope': 1000, 'appendByteString-memory-arguments-slope': 1, 'trace-memory-arguments': 32, 'divideInteger-cpu-arguments-constant': 148000, 'cekConstCost-exBudgetCPU': 29773, 'encodeUtf8-memory-arguments-slope': 8, 'quotientInteger-cpu-arguments-model-arguments-intercept': 425507, 'mapData-memory-arguments': 32, 'appendString-cpu-arguments-slope': 1000, 'modInteger-cpu-arguments-constant': 148000, 'verifySignature-cpu-arguments-slope': 1, 'unConstrData-memory-arguments': 32, 'quotientInteger-memory-arguments-intercept': 0, 'equalsByteString-cpu-arguments-constant': 150000, 'sliceByteString-memory-arguments-intercept': 0, 'mkPairData-memory-arguments': 32, 'equalsByteString-cpu-arguments-intercept': 112536, 'appendString-memory-arguments-slope': 1, 'lessThanInteger-cpu-arguments-slope': 497, 'modInteger-cpu-arguments-model-arguments-intercept': 425507, 'modInteger-memory-arguments-minimum': 1, 'sha3_256-cpu-arguments-intercept': 0, 'verifySignature-memory-arguments': 1, 'cekLamCost-exBudgetMemory': 100, 'sliceByteString-cpu-arguments-intercept': 150000}}

A dictionary of current cost models, which could be used to calculate script data hash.

class pycardano.plutus.PlutusData

Bases: ArrayCBORSerializable

PlutusData is a helper class that can serialize itself into a CBOR format, which could be intepreted as a data structure in Plutus scripts. It is not required to use this class to interact with Plutus scripts. However, wrapping datum in PlutusData class will reduce the complexity of serialization and deserialization tremendously.

Examples

>>> @dataclass
... class Test(PlutusData):
...     CONSTR_ID = 1
...     a: int
...     b: bytes
>>> test = Test(123, b"321")
>>> test.to_cbor_hex()
'd87a9f187b43333231ff'
>>> assert test == Test.from_cbor("d87a9f187b43333231ff")
MAX_BYTES_SIZE = 64
CONSTR_ID = 3577940042
to_shallow_primitive() CBORTag
Returns:

A CBOR primitive.

Return type:

Primitive

Raises:

SerializeException – When the object could not be converted to CBOR primitive types.

classmethod from_primitive(value: CBORTag) PlutusData

Restore a primitive value to its original class type.

Parameters:
  • cls (ArrayBase) – The original class type.

  • values (List[Primitive]) – A list whose elements are CBOR primitives.

Returns:

Restored object.

Return type:

ArrayBase

Raises:

DeserializeException – When the object could not be restored from primitives.

hash() DatumHash
to_dict() dict

Convert to a dictionary.

Returns:

a dict PlutusData that can be JSON encoded.

Return type:

str

to_json(**kwargs) str

Convert to a json string

Parameters:

**kwargs – Extra key word arguments to be passed to json.dumps()

Returns:

a JSON encoded PlutusData.

Return type:

str

classmethod from_dict(data: dict) PlutusData

Convert a dictionary to PlutusData

Parameters:

data (dict) – A dictionary.

Returns:

Restored PlutusData.

Return type:

PlutusData

classmethod from_json(data: str) PlutusData

Restore a json encoded string to a PlutusData.

Parameters:

data (str) – An encoded json string.

Returns:

The restored PlutusData.

Return type:

PlutusData

pycardano.plutus.Datum

Plutus Datum type. A Union type that contains all valid datum types.

alias of PlutusData | dict | int | bytes | IndefiniteList | RawCBOR | RawPlutusData

class pycardano.plutus.RedeemerTag(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: CBORSerializable, Enum

Redeemer tag, which indicates the type of redeemer.

SPEND = 0
MINT = 1
CERTIFICATE = 2
WITHDRAWAL = 3
VOTING = 4
PROPOSING = 5
to_primitive() int

Convert the instance and its elements to CBOR primitives recursively.

Returns:

A CBOR primitive.

Return type:

Primitive

Raises:

SerializeException – When the object or its elements could not be converted to CBOR primitive types.

classmethod from_primitive(value: int) RedeemerTag

Turn a CBOR primitive to its original class type.

Parameters:
  • cls (CBORBase) – The original class type.

  • value (Primitive) – A CBOR primitive.

  • type_args (Optional[tuple]) – Type arguments for the class.

Returns:

A CBOR serializable object.

Return type:

CBORBase

Raises:

DeserializeException – When the object could not be restored from primitives.

class pycardano.plutus.ExecutionUnits(mem: 'int', steps: 'int')

Bases: ArrayCBORSerializable

mem: int
steps: int
is_empty() bool
class pycardano.plutus.PlutusScript

Bases: CBORSerializable, bytes

Plutus script class.

This class is a base class for all Plutus script versions.

Example - Load a Plutus script from test/resources/scriptV2.plutus and get its address: # noqa: E501

>>> from pycardano import Address, Network
>>> script = PlutusV2Script.load("test/resources/scriptV2.plutus")
>>> Address(plutus_script_hash(script), network=Network.TESTNET).encode()
'addr_test1wrmz3pjz4dmfxj0fc0a0eyw69tp6h7mpndzf9g3kttq9cqqqw47ym'
property version: int
to_shallow_primitive() bytes

Convert the instance to a CBOR primitive. If the primitive is a container, e.g. list, dict, the type of its elements could be either a Primitive or a CBORSerializable.

Returns:

A CBOR primitive.

Return type:

Primitive

Raises:

SerializeException – When the object could not be converted to CBOR primitive types.

classmethod from_primitive(value: Any, type_args: tuple | None = None) PlutusScript

Turn a CBOR primitive to its original class type.

Parameters:
  • cls (CBORBase) – The original class type.

  • value (Primitive) – A CBOR primitive.

  • type_args (Optional[tuple]) – Type arguments for the class.

Returns:

A CBOR serializable object.

Return type:

CBORBase

Raises:

DeserializeException – When the object could not be restored from primitives.

classmethod from_version(version: int, script_data: bytes) PlutusScript
get_script_hash_prefix() bytes
class pycardano.plutus.PlutusV1Script

Bases: PlutusScript

get_script_hash_prefix() bytes
property version: int
class pycardano.plutus.PlutusV2Script

Bases: PlutusScript

get_script_hash_prefix() bytes
property version: int
class pycardano.plutus.PlutusV3Script

Bases: PlutusScript

get_script_hash_prefix() bytes
property version: int
class pycardano.plutus.RawPlutusData(data: 'RawDatum')

Bases: CBORSerializable

data: PlutusData | dict | int | bytes | IndefiniteList | RawCBOR | CBORTag
to_primitive() bytes | bytearray | str | int | float | Decimal | bool | None | tuple | list | IndefiniteList | dict | defaultdict | OrderedDict | datetime | Pattern | CBORSimpleValue | CBORTag | set | Fraction | frozenset | FrozenDict | FrozenList | IndefiniteFrozenList | ByteString

Convert the instance and its elements to CBOR primitives recursively.

Returns:

A CBOR primitive.

Return type:

Primitive

Raises:

SerializeException – When the object or its elements could not be converted to CBOR primitive types.

to_dict() dict

Convert to a dictionary.

Returns:

a dict RawPlutusData that can be JSON encoded.

Return type:

str

to_json(**kwargs) str

Convert to a json string

Parameters:

**kwargs – Extra key word arguments to be passed to json.dumps()

Returns:

a JSON encoded RawPlutusData.

Return type:

str

classmethod from_primitive(value: PlutusData | dict | int | bytes | IndefiniteList | RawCBOR | CBORTag) RawPlutusData

Turn a CBOR primitive to its original class type.

Parameters:
  • cls (CBORBase) – The original class type.

  • value (Primitive) – A CBOR primitive.

  • type_args (Optional[tuple]) – Type arguments for the class.

Returns:

A CBOR serializable object.

Return type:

CBORBase

Raises:

DeserializeException – When the object could not be restored from primitives.

classmethod from_dict(data: dict) RawPlutusData

Convert a dictionary to RawPlutusData

Parameters:

data (dict) – A dictionary.

Returns:

Restored RawPlutusData.

Return type:

RawPlutusData

classmethod from_json(data: str) RawPlutusData

Restore a json encoded string to a RawPlutusData.

Parameters:

data (str) – An encoded json string.

Returns:

The restored RawPlutusData.

Return type:

RawPlutusData

class pycardano.plutus.Redeemer(data: 'Any', ex_units: 'Optional[ExecutionUnits]' = None)

Bases: ArrayCBORSerializable

tag: RedeemerTag | None = None
index: int = 0
data: Any
ex_units: ExecutionUnits | None = None
classmethod from_primitive(values: list) Redeemer

Restore a primitive value to its original class type.

Parameters:
  • cls (ArrayBase) – The original class type.

  • values (List[Primitive]) – A list whose elements are CBOR primitives.

Returns:

Restored object.

Return type:

ArrayBase

Raises:

DeserializeException – When the object could not be restored from primitives.

class pycardano.plutus.RedeemerKey(tag: 'RedeemerTag', index: 'int' = 0)

Bases: ArrayCBORSerializable

tag: RedeemerTag
index: int = 0
classmethod from_primitive(values: list) RedeemerKey

Restore a primitive value to its original class type.

Parameters:
  • cls (ArrayBase) – The original class type.

  • values (List[Primitive]) – A list whose elements are CBOR primitives.

Returns:

Restored object.

Return type:

ArrayBase

Raises:

DeserializeException – When the object could not be restored from primitives.

class pycardano.plutus.RedeemerValue(data: 'Any', ex_units: 'ExecutionUnits')

Bases: ArrayCBORSerializable

data: Any
ex_units: ExecutionUnits
classmethod from_primitive(values: list) RedeemerValue

Restore a primitive value to its original class type.

Parameters:
  • cls (ArrayBase) – The original class type.

  • values (List[Primitive]) – A list whose elements are CBOR primitives.

Returns:

Restored object.

Return type:

ArrayBase

Raises:

DeserializeException – When the object could not be restored from primitives.

class pycardano.plutus.RedeemerMap(*args, **kwargs)

Bases: DictCBORSerializable

KEY_TYPE

alias of RedeemerKey

VALUE_TYPE

alias of RedeemerValue

pycardano.plutus.ScriptType

Script type. A Union type that contains all valid script types.

alias of NativeScript | PlutusScript

pycardano.plutus.datum_hash(datum: PlutusData | dict | int | bytes | IndefiniteList | RawCBOR | RawPlutusData) DatumHash
pycardano.plutus.plutus_script_hash(script: NativeScript | PlutusScript) ScriptHash

Calculates the hash of a Plutus script.

Parameters:

script (Union[bytes, PlutusScript]) – A plutus script.

Returns:

blake2b hash of the script.

Return type:

ScriptHash

pycardano.plutus.script_hash(script: NativeScript | PlutusScript) ScriptHash

Calculates the hash of a script, which could be either native script or plutus script.

Parameters:

script (ScriptType) – A script.

Returns:

blake2b hash of the script.

Return type:

ScriptHash

class pycardano.plutus.Unit

Bases: PlutusData

The default “Unit type” with a 0 constructor ID

CONSTR_ID = 0