Skip to content

Datums

Dataclasses for the different datums used in the Charli3 Dendrite project.

AssetClass dataclass

Bases: PlutusData

An asset class. Separates out token policy and asset name.

Source code in src/charli3_dendrite/dataclasses/datums.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
@dataclass
class AssetClass(PlutusData):
    """An asset class. Separates out token policy and asset name."""

    CONSTR_ID = 0

    policy: bytes
    asset_name: bytes

    @classmethod
    def from_assets(cls, asset: Assets) -> "AssetClass":
        """Parse an Assets object into an AssetClass object."""
        error_msg = "Only one asset may be supplied."
        if len(asset) != 1:
            raise ValueError(error_msg)

        if asset.unit() == "lovelace":
            policy = b""
            asset_name = b""
        else:
            policy = bytes.fromhex(asset.unit()[:56])
            asset_name = bytes.fromhex(asset.unit()[56:])

        return AssetClass(policy=policy, asset_name=asset_name)

    @property
    def assets(self) -> Assets:
        """Convert back to assets."""
        if self.policy.hex() == "":
            asset = "lovelace"
        else:
            asset = self.policy.hex() + self.asset_name.hex()

        return Assets(root={asset: 0})

assets: Assets property

Convert back to assets.

from_assets(asset: Assets) -> AssetClass classmethod

Parse an Assets object into an AssetClass object.

Source code in src/charli3_dendrite/dataclasses/datums.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@classmethod
def from_assets(cls, asset: Assets) -> "AssetClass":
    """Parse an Assets object into an AssetClass object."""
    error_msg = "Only one asset may be supplied."
    if len(asset) != 1:
        raise ValueError(error_msg)

    if asset.unit() == "lovelace":
        policy = b""
        asset_name = b""
    else:
        policy = bytes.fromhex(asset.unit()[:56])
        asset_name = bytes.fromhex(asset.unit()[56:])

    return AssetClass(policy=policy, asset_name=asset_name)

CancelRedeemer dataclass

Bases: PlutusData

Cancel datum.

Source code in src/charli3_dendrite/dataclasses/datums.py
135
136
137
138
139
@dataclass
class CancelRedeemer(PlutusData):
    """Cancel datum."""

    CONSTR_ID = 1

OrderDatum

Bases: PlutusData, ABC

Abstract base class for all order datum types.

Source code in src/charli3_dendrite/dataclasses/datums.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
class OrderDatum(PlutusData, ABC):
    """Abstract base class for all order datum types."""

    CONSTR_ID: int = 0

    @abstractmethod
    def address_source(self) -> Address:
        """This method should return the source address associated with the order."""
        pass

    @abstractmethod
    def requested_amount(self) -> Assets:
        """This method should return the amount requested in the order."""
        pass

    @abstractmethod
    def order_type(self) -> OrderType:
        """This method should return the type of the order."""
        pass

address_source() -> Address abstractmethod

This method should return the source address associated with the order.

Source code in src/charli3_dendrite/dataclasses/datums.py
158
159
160
161
@abstractmethod
def address_source(self) -> Address:
    """This method should return the source address associated with the order."""
    pass

order_type() -> OrderType abstractmethod

This method should return the type of the order.

Source code in src/charli3_dendrite/dataclasses/datums.py
168
169
170
171
@abstractmethod
def order_type(self) -> OrderType:
    """This method should return the type of the order."""
    pass

requested_amount() -> Assets abstractmethod

This method should return the amount requested in the order.

Source code in src/charli3_dendrite/dataclasses/datums.py
163
164
165
166
@abstractmethod
def requested_amount(self) -> Assets:
    """This method should return the amount requested in the order."""
    pass

PlutusFullAddress dataclass

Bases: PlutusData

A full address, including payment and staking keys.

Source code in src/charli3_dendrite/dataclasses/datums.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@dataclass
class PlutusFullAddress(PlutusData):
    """A full address, including payment and staking keys."""

    CONSTR_ID = 0
    payment: Union[PlutusPartAddress, PlutusScriptPartAddress]
    stake: Union[_PlutusConstrWrapper, PlutusNone, None] = None

    @classmethod
    def from_address(cls, address: Address) -> "PlutusFullAddress":
        """Parse an Address object to a PlutusFullAddress."""
        error_msg = "Only addresses with staking and payment parts are accepted."
        if None in [address.staking_part, address.payment_part]:
            raise ValueError(error_msg)
        if address.staking_part is not None:
            stake = _PlutusConstrWrapper(
                _PlutusConstrWrapper(
                    PlutusPartAddress(bytes.fromhex(str(address.staking_part))),
                ),
            )
        else:
            stake = PlutusNone
        return PlutusFullAddress(
            PlutusPartAddress(bytes.fromhex(str(address.payment_part))),
            stake=stake,
        )

    def to_address(self) -> Address:
        """Convert back to an address."""
        payment_part = VerificationKeyHash(self.payment.address[:28])
        if isinstance(self.stake, PlutusNone):
            stake_part = None
        else:
            stake_part = VerificationKeyHash(self.stake.wrapped.wrapped.address[:28])
        return Address(payment_part=payment_part, staking_part=stake_part)

from_address(address: Address) -> PlutusFullAddress classmethod

Parse an Address object to a PlutusFullAddress.

Source code in src/charli3_dendrite/dataclasses/datums.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
@classmethod
def from_address(cls, address: Address) -> "PlutusFullAddress":
    """Parse an Address object to a PlutusFullAddress."""
    error_msg = "Only addresses with staking and payment parts are accepted."
    if None in [address.staking_part, address.payment_part]:
        raise ValueError(error_msg)
    if address.staking_part is not None:
        stake = _PlutusConstrWrapper(
            _PlutusConstrWrapper(
                PlutusPartAddress(bytes.fromhex(str(address.staking_part))),
            ),
        )
    else:
        stake = PlutusNone
    return PlutusFullAddress(
        PlutusPartAddress(bytes.fromhex(str(address.payment_part))),
        stake=stake,
    )

to_address() -> Address

Convert back to an address.

Source code in src/charli3_dendrite/dataclasses/datums.py
82
83
84
85
86
87
88
89
def to_address(self) -> Address:
    """Convert back to an address."""
    payment_part = VerificationKeyHash(self.payment.address[:28])
    if isinstance(self.stake, PlutusNone):
        stake_part = None
    else:
        stake_part = VerificationKeyHash(self.stake.wrapped.wrapped.address[:28])
    return Address(payment_part=payment_part, staking_part=stake_part)

PlutusNone dataclass

Bases: PlutusData

Placeholder for a receiver datum.

Source code in src/charli3_dendrite/dataclasses/datums.py
40
41
42
43
44
@dataclass
class PlutusNone(PlutusData):
    """Placeholder for a receiver datum."""

    CONSTR_ID = 1

PlutusPartAddress dataclass

Bases: PlutusData

Encode a plutus address part (i.e. payment, stake, etc).

Source code in src/charli3_dendrite/dataclasses/datums.py
25
26
27
28
29
30
@dataclass
class PlutusPartAddress(PlutusData):
    """Encode a plutus address part (i.e. payment, stake, etc)."""

    CONSTR_ID = 0
    address: bytes

PlutusScriptAddress dataclass

Bases: PlutusFullAddress

A full address, including payment and staking keys.

Source code in src/charli3_dendrite/dataclasses/datums.py
92
93
94
95
96
@dataclass
class PlutusScriptAddress(PlutusFullAddress):
    """A full address, including payment and staking keys."""

    payment: PlutusScriptPartAddress

PlutusScriptPartAddress dataclass

Bases: PlutusPartAddress

Encode a plutus address part (i.e. payment, stake, etc).

Source code in src/charli3_dendrite/dataclasses/datums.py
33
34
35
36
37
@dataclass
class PlutusScriptPartAddress(PlutusPartAddress):
    """Encode a plutus address part (i.e. payment, stake, etc)."""

    CONSTR_ID = 1

PoolDatum

Bases: PlutusData, ABC

Abstract base class for all pool datum types.

Source code in src/charli3_dendrite/dataclasses/datums.py
142
143
144
145
146
147
148
149
150
class PoolDatum(PlutusData, ABC):
    """Abstract base class for all pool datum types."""

    CONSTR_ID = 0

    @abstractmethod
    def pool_pair(self) -> Union[Assets, None]:
        """Return the asset pair associated with the pool."""
        pass

pool_pair() -> Union[Assets, None] abstractmethod

Return the asset pair associated with the pool.

Source code in src/charli3_dendrite/dataclasses/datums.py
147
148
149
150
@abstractmethod
def pool_pair(self) -> Union[Assets, None]:
    """Return the asset pair associated with the pool."""
    pass

ReceiverDatum dataclass

Bases: PlutusData

The receiver address.

Source code in src/charli3_dendrite/dataclasses/datums.py
17
18
19
20
21
22
@dataclass
class ReceiverDatum(PlutusData):
    """The receiver address."""

    CONSTR_ID = 0
    datum_hash: Union[DatumHash, None] = None