Skip to content

Models

Assets

Bases: BaseDict

Contains all tokens and quantities.

Source code in src/charli3_dendrite/dataclasses/models.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Assets(BaseDict):
    """Contains all tokens and quantities."""

    root: dict[str, int]

    def unit(self, index: int = 0) -> str:
        """Units of asset at `index`."""
        return list(self.keys())[index]

    def quantity(self, index: int = 0) -> int:
        """Quantity of the asset at `index`."""
        return list(self.values())[index]

    @model_validator(mode="before")
    def _digest_assets(cls, values: dict) -> dict:
        if hasattr(values, "root"):
            root = values.root
        elif "values" in values and isinstance(values["values"], list):
            root = {v.unit: v.quantity for v in values["values"]}
        elif isinstance(values, list) and isinstance(values[0], dict):
            if not all(len(v) == 1 for v in values):
                raise ValueError(
                    "For a list of dictionaries, each dictionary must be of length 1.",
                )
            root = {k: v for d in values for k, v in d.items()}
        else:
            root = dict(values.items())
        return dict(
            sorted(root.items(), key=lambda x: "" if x[0] == "lovelace" else x[0]),
        )

    def __add__(a: "Assets", b: "Assets") -> "Assets":
        """Add two assets."""
        intersection = set(a.keys()) | set(b.keys())

        result = {key: a[key] + b[key] for key in intersection}

        return Assets(**result)

    def __sub__(a: "Assets", b: "Assets") -> "Assets":
        """Subtract two assets."""
        intersection = set(a.keys()) | set(b.keys())

        result = {key: a[key] - b[key] for key in intersection}

        return Assets(**result)

__add__(a: Assets, b: Assets) -> Assets

Add two assets.

Source code in src/charli3_dendrite/dataclasses/models.py
106
107
108
109
110
111
112
def __add__(a: "Assets", b: "Assets") -> "Assets":
    """Add two assets."""
    intersection = set(a.keys()) | set(b.keys())

    result = {key: a[key] + b[key] for key in intersection}

    return Assets(**result)

__sub__(a: Assets, b: Assets) -> Assets

Subtract two assets.

Source code in src/charli3_dendrite/dataclasses/models.py
114
115
116
117
118
119
120
def __sub__(a: "Assets", b: "Assets") -> "Assets":
    """Subtract two assets."""
    intersection = set(a.keys()) | set(b.keys())

    result = {key: a[key] - b[key] for key in intersection}

    return Assets(**result)

quantity(index: int = 0) -> int

Quantity of the asset at index.

Source code in src/charli3_dendrite/dataclasses/models.py
84
85
86
def quantity(self, index: int = 0) -> int:
    """Quantity of the asset at `index`."""
    return list(self.values())[index]

unit(index: int = 0) -> str

Units of asset at index.

Source code in src/charli3_dendrite/dataclasses/models.py
80
81
82
def unit(self, index: int = 0) -> str:
    """Units of asset at `index`."""
    return list(self.keys())[index]

BaseDict

Bases: BaseList

Utility class for dict models.

Source code in src/charli3_dendrite/dataclasses/models.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class BaseDict(BaseList):
    """Utility class for dict models."""

    def items(self):  # noqa: ANN201
        """Return iterable of key-value pairs."""
        return self.root.items()

    def keys(self):  # noqa: ANN201
        """Return iterable of keys."""
        return self.root.keys()

    def values(self):  # noqa: ANN201
        """Return iterable of values."""
        return self.root.values()

    def __getitem__(self, item: str):  # noqa: ANN204
        """Get item by key."""
        return self.root.get(item, 0)

__getitem__(item: str)

Get item by key.

Source code in src/charli3_dendrite/dataclasses/models.py
70
71
72
def __getitem__(self, item: str):  # noqa: ANN204
    """Get item by key."""
    return self.root.get(item, 0)

items()

Return iterable of key-value pairs.

Source code in src/charli3_dendrite/dataclasses/models.py
58
59
60
def items(self):  # noqa: ANN201
    """Return iterable of key-value pairs."""
    return self.root.items()

keys()

Return iterable of keys.

Source code in src/charli3_dendrite/dataclasses/models.py
62
63
64
def keys(self):  # noqa: ANN201
    """Return iterable of keys."""
    return self.root.keys()

values()

Return iterable of values.

Source code in src/charli3_dendrite/dataclasses/models.py
66
67
68
def values(self):  # noqa: ANN201
    """Return iterable of values."""
    return self.root.values()

BaseList

Bases: RootModel

Utility class for list models.

Source code in src/charli3_dendrite/dataclasses/models.py
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class BaseList(RootModel):
    """Utility class for list models."""

    def __hash__(self) -> int:
        return hash(self.model_dump_json())

    def __iter__(self):  # noqa
        return iter(self.root)

    def __getitem__(self, item):  # noqa
        return self.root[item]

    def __len__(self):  # noqa
        return len(self.root)

PoolSelector

Bases: DendriteBaseModel

Pool selection information for dbsync.

Source code in src/charli3_dendrite/dataclasses/models.py
32
33
34
35
36
class PoolSelector(DendriteBaseModel):
    """Pool selection information for dbsync."""

    addresses: list[str]
    assets: list[str] | None = None

PoolSelectorType

Bases: Enum

How to identify a pool.

DEX pools are generally identified by one of two mechanism: 1. An address 2. Presence of one or more NFTs (asset policy, with or without asset name)

Source code in src/charli3_dendrite/dataclasses/models.py
20
21
22
23
24
25
26
27
28
29
class PoolSelectorType(Enum):
    """How to identify a pool.

    DEX pools are generally identified by one of two mechanism:
    1. An address
    2. Presence of one or more NFTs (asset policy, with or without asset name)
    """

    address = "addresses"
    asset = "assets"

TokenSummary

Bases: DendriteBaseModel

Summary of token information.

Source code in src/charli3_dendrite/dataclasses/models.py
264
265
266
267
268
269
270
271
272
273
class TokenSummary(DendriteBaseModel):
    """Summary of token information."""

    ticker: str
    name: str
    policy_id: str
    policy_name: str
    decimals: int
    price_numerator: int = 0
    price_denominator: int = 0