Skip to content

Muesliswap

MuesliSwap DEX Module.

MuesliCLPoolDatum dataclass

Bases: MuesliPoolDatum

The pool datum for MuesliSwap constant liquidity pools.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
140
141
142
143
144
145
146
147
@dataclass
class MuesliCLPoolDatum(MuesliPoolDatum):
    """The pool datum for MuesliSwap constant liquidity pools."""

    upper: PreciseFloat
    lower: PreciseFloat
    price_sqrt: PreciseFloat
    unknown: int

MuesliCancelRedeemer dataclass

Bases: PlutusData

The cancel redeemer for MuesliSwap.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
150
151
152
153
154
@dataclass
class MuesliCancelRedeemer(PlutusData):
    """The cancel redeemer for MuesliSwap."""

    CONSTR_ID = 0

MuesliOrderConfig dataclass

Bases: PlutusData

The order configuration for MuesliSwap.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@dataclass
class MuesliOrderConfig(PlutusData):
    """The order configuration for MuesliSwap."""

    CONSTR_ID = 0

    full_address: PlutusFullAddress
    token_out_policy: bytes
    token_out_name: bytes
    token_in_policy: bytes
    token_in_name: bytes
    min_receive: int
    unknown: Union[MuesliSometimesNone, PlutusNone]
    in_amount: int

MuesliOrderDatum dataclass

Bases: OrderDatum

The order datum for MuesliSwap.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
 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
 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
@dataclass
class MuesliOrderDatum(OrderDatum):
    """The order datum for MuesliSwap."""

    value: MuesliOrderConfig

    @classmethod
    def create_datum(
        cls,
        address_source: Address,
        in_assets: Assets,
        out_assets: Assets,
        batcher_fee: Assets,
        deposit: Assets,
        address_target: Address | None = None,
        datum_target: PlutusData | None = None,
    ):
        """Create a MuesliSwap order datum."""
        full_address = PlutusFullAddress.from_address(address_source)

        if in_assets.unit() == "lovelace":
            token_in_policy = b""
            token_in_name = b""
        else:
            token_in_policy = bytes.fromhex(in_assets.unit()[:56])
            token_in_name = bytes.fromhex(in_assets.unit()[56:])

        if out_assets.unit() == "lovelace":
            token_out_policy = b""
            token_out_name = b""
        else:
            token_out_policy = bytes.fromhex(out_assets.unit()[:56])
            token_out_name = bytes.fromhex(out_assets.unit()[56:])

        config = MuesliOrderConfig(
            full_address=full_address,
            token_in_policy=token_in_policy,
            token_in_name=token_in_name,
            token_out_policy=token_out_policy,
            token_out_name=token_out_name,
            min_receive=out_assets.quantity(),
            unknown=PlutusNone(),
            in_amount=batcher_fee.quantity() + deposit.quantity(),
        )

        return cls(value=config)

    def address_source(self) -> str:
        return self.value.full_address.to_address()

    def requested_amount(self) -> Assets:
        token_out = self.value.token_out_policy.hex() + self.value.token_out_name.hex()
        if token_out == "":
            token_out = "lovelace"
        return Assets({token_out: self.value.min_receive})

    def order_type(self) -> OrderType:
        return OrderType.swap

create_datum(address_source: Address, in_assets: Assets, out_assets: Assets, batcher_fee: Assets, deposit: Assets, address_target: Address | None = None, datum_target: PlutusData | None = None) classmethod

Create a MuesliSwap order datum.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
 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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@classmethod
def create_datum(
    cls,
    address_source: Address,
    in_assets: Assets,
    out_assets: Assets,
    batcher_fee: Assets,
    deposit: Assets,
    address_target: Address | None = None,
    datum_target: PlutusData | None = None,
):
    """Create a MuesliSwap order datum."""
    full_address = PlutusFullAddress.from_address(address_source)

    if in_assets.unit() == "lovelace":
        token_in_policy = b""
        token_in_name = b""
    else:
        token_in_policy = bytes.fromhex(in_assets.unit()[:56])
        token_in_name = bytes.fromhex(in_assets.unit()[56:])

    if out_assets.unit() == "lovelace":
        token_out_policy = b""
        token_out_name = b""
    else:
        token_out_policy = bytes.fromhex(out_assets.unit()[:56])
        token_out_name = bytes.fromhex(out_assets.unit()[56:])

    config = MuesliOrderConfig(
        full_address=full_address,
        token_in_policy=token_in_policy,
        token_in_name=token_in_name,
        token_out_policy=token_out_policy,
        token_out_name=token_out_name,
        min_receive=out_assets.quantity(),
        unknown=PlutusNone(),
        in_amount=batcher_fee.quantity() + deposit.quantity(),
    )

    return cls(value=config)

MuesliPoolDatum dataclass

Bases: PoolDatum

The pool datum for MuesliSwap.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
117
118
119
120
121
122
123
124
125
126
127
@dataclass
class MuesliPoolDatum(PoolDatum):
    """The pool datum for MuesliSwap."""

    asset_a: AssetClass
    asset_b: AssetClass
    lp: int
    fee: int

    def pool_pair(self) -> Assets | None:
        return self.asset_a.assets + self.asset_b.assets

MuesliSometimesNone dataclass

Bases: PlutusData

A dataclass that can be None.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
34
35
36
37
38
@dataclass
class MuesliSometimesNone(PlutusData):
    """A dataclass that can be None."""

    CONSTR_ID = 0

MuesliSwapCLPState

Bases: AbstractConstantLiquidityPoolState, MuesliSwapCPPState

The MuesliSwap constant liquidity pool state.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
class MuesliSwapCLPState(AbstractConstantLiquidityPoolState, MuesliSwapCPPState):
    """The MuesliSwap constant liquidity pool state."""

    inactive: bool = True

    @classmethod
    def dex_policy(cls) -> list[str]:
        return [
            # "de9b756719341e79785aa13c164e7fe68c189ed04d61c9876b2fe53f4d7565736c69537761705f414d4d",
            # "ffcdbb9155da0602280c04d8b36efde35e3416567f9241aff09552694d7565736c69537761705f414d4d",
            "f33bf12af1c23d660e29ebb0d3206b0bfc56ffd87ffafe2d36c42a454d7565736c69537761705f634c50",  # constant liquidity pools
            # "a8512101cb1163cc218e616bb4d4070349a1c9395313f1323cc583634d7565736c695377617054657374506f6f6c",  # test pool policy
        ]

    @classmethod
    def pool_datum_class(cls) -> type[MuesliCLPoolDatum]:
        return MuesliCLPoolDatum

MuesliSwapCPPState

Bases: AbstractConstantProductPoolState

The MuesliSwap constant product pool state.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
class MuesliSwapCPPState(AbstractConstantProductPoolState):
    """The MuesliSwap constant product pool state."""

    fee: int = 30
    _batcher = Assets(lovelace=950000)
    _deposit = Assets(lovelace=1700000)
    _test_pool: ClassVar[
        str
    ] = "a8512101cb1163cc218e616bb4d4070349a1c9395313f1323cc583634d7565736c695377617054657374506f6f6c"
    _stake_address: ClassVar[Address] = Address.from_primitive(
        "addr1zyq0kyrml023kwjk8zr86d5gaxrt5w8lxnah8r6m6s4jp4g3r6dxnzml343sx8jweqn4vn3fz2kj8kgu9czghx0jrsyqqktyhv",
    )
    _reference_utxo: ClassVar[UTxO | None] = None

    @classmethod
    def dex(cls) -> str:
        return "MuesliSwap"

    @classmethod
    def order_selector(self) -> list[str]:
        return [self._stake_address.encode()]

    @classmethod
    def pool_selector(cls) -> PoolSelector:
        return PoolSelector(
            addresses=[
                "addr1w9cy2gmar6cpn8yymll93lnd7lw96f27kn2p3eq5d4tjr7qkh3tzd",
                "addr1w85t4tvj3rwf40wqnx6x72kqq6c6stra7jvkupnlqrqyarg2m74rn",
                "addr1w8djr38pct9dpewvv7k67xuh45xpj5f9hyzs3cp939j0csc6nhwme",
            ],
            assets=cls.dex_policy(),
        )

    @property
    def swap_forward(self) -> bool:
        return False

    @classmethod
    def reference_utxo(cls) -> UTxO | None:
        if cls._reference_utxo is None:
            script_reference = get_backend().get_script_from_address(cls._stake_address)

            if script_reference is None:
                return None

            script_bytes = bytes.fromhex(script_reference.script)
            script = cls.default_script_class()(script_bytes)

            cls._reference_utxo = UTxO(
                input=TransactionInput(
                    transaction_id=TransactionId(
                        bytes.fromhex(
                            "7e4142b7a040eae45d14513000adf91ab42da33a1bd5ccffcfe851b3d93e1e5e",
                        ),
                    ),
                    index=1,
                ),
                output=TransactionOutput(
                    address=Address.decode(
                        "addr1v9p0rc57dzkz7gg97dmsns8hngsuxl956xe6myjldaug7hse4elc6",
                    ),
                    amount=Value(coin=24269610),
                    script=script,
                ),
            )

        return cls._reference_utxo

    @property
    def stake_address(self) -> Address:
        return self._stake_address

    @classmethod
    def order_datum_class(cls) -> type[MuesliOrderDatum]:
        return MuesliOrderDatum

    @classmethod
    def pool_datum_class(cls) -> type[MuesliPoolDatum]:
        return MuesliPoolDatum

    @property
    def pool_id(self) -> str:
        """A unique identifier for the pool."""
        return self.pool_nft.unit()

    @classmethod
    def dex_policy(cls) -> list[str]:
        return [
            "de9b756719341e79785aa13c164e7fe68c189ed04d61c9876b2fe53f4d7565736c69537761705f414d4d",
            "ffcdbb9155da0602280c04d8b36efde35e3416567f9241aff09552694d7565736c69537761705f414d4d",
            # "f33bf12af1c23d660e29ebb0d3206b0bfc56ffd87ffafe2d36c42a454d7565736c69537761705f634c50",  # constant liquidity pools
            # "a8512101cb1163cc218e616bb4d4070349a1c9395313f1323cc583634d7565736c695377617054657374506f6f6c",  # test pool policy
        ]

    @classmethod
    def extract_dex_nft(cls, values: dict[str, Any]) -> Optional[Assets]:
        """Extract the dex nft from the UTXO.

        Some DEXs put a DEX nft into the pool UTXO.

        This function checks to see if the DEX nft is in the UTXO if the DEX policy is
        defined.

        If the dex nft is in the values, this value is skipped because it is assumed
        that this utxo has already been parsed.

        Args:
            values: The pool UTXO inputs.

        Returns:
            Assets: None or the dex nft.
        """
        dex_nft = super().extract_dex_nft(values)

        if cls._test_pool in dex_nft:
            raise InvalidPoolError("This is a test pool.")

        return dex_nft

    @classmethod
    def extract_pool_nft(cls, values: dict[str, Any]) -> Optional[Assets]:
        """Extract the dex nft from the UTXO.

        Some DEXs put a DEX nft into the pool UTXO.

        This function checks to see if the DEX nft is in the UTXO if the DEX policy is
        defined.

        If the dex nft is in the values, this value is skipped because it is assumed
        that this utxo has already been parsed.

        Args:
            values: The pool UTXO inputs.

        Returns:
            Assets: None or the dex nft.
        """
        assets = values["assets"]

        if "pool_nft" in values:
            pool_nft = Assets(root=values["pool_nft"])
        else:
            nfts = [asset for asset, quantity in assets.items() if quantity == 1]
            if len(nfts) != 1:
                raise InvalidPoolError(
                    f"MuesliSwap pools must have exactly one pool nft: assets={assets}",
                )
            pool_nft = Assets(**{nfts[0]: assets.root.pop(nfts[0])})
            values["pool_nft"] = pool_nft

        return pool_nft

    @classmethod
    def default_script_class(self) -> type[PlutusV1Script] | type[PlutusV2Script]:
        return PlutusV2Script

    @classmethod
    def cancel_redeemer(cls) -> PlutusData:
        return Redeemer(MuesliCancelRedeemer())

pool_id: str property

A unique identifier for the pool.

extract_dex_nft(values: dict[str, Any]) -> Optional[Assets] classmethod

Extract the dex nft from the UTXO.

Some DEXs put a DEX nft into the pool UTXO.

This function checks to see if the DEX nft is in the UTXO if the DEX policy is defined.

If the dex nft is in the values, this value is skipped because it is assumed that this utxo has already been parsed.

Parameters:

Name Type Description Default
values dict[str, Any]

The pool UTXO inputs.

required

Returns:

Name Type Description
Assets Optional[Assets]

None or the dex nft.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
@classmethod
def extract_dex_nft(cls, values: dict[str, Any]) -> Optional[Assets]:
    """Extract the dex nft from the UTXO.

    Some DEXs put a DEX nft into the pool UTXO.

    This function checks to see if the DEX nft is in the UTXO if the DEX policy is
    defined.

    If the dex nft is in the values, this value is skipped because it is assumed
    that this utxo has already been parsed.

    Args:
        values: The pool UTXO inputs.

    Returns:
        Assets: None or the dex nft.
    """
    dex_nft = super().extract_dex_nft(values)

    if cls._test_pool in dex_nft:
        raise InvalidPoolError("This is a test pool.")

    return dex_nft

extract_pool_nft(values: dict[str, Any]) -> Optional[Assets] classmethod

Extract the dex nft from the UTXO.

Some DEXs put a DEX nft into the pool UTXO.

This function checks to see if the DEX nft is in the UTXO if the DEX policy is defined.

If the dex nft is in the values, this value is skipped because it is assumed that this utxo has already been parsed.

Parameters:

Name Type Description Default
values dict[str, Any]

The pool UTXO inputs.

required

Returns:

Name Type Description
Assets Optional[Assets]

None or the dex nft.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
@classmethod
def extract_pool_nft(cls, values: dict[str, Any]) -> Optional[Assets]:
    """Extract the dex nft from the UTXO.

    Some DEXs put a DEX nft into the pool UTXO.

    This function checks to see if the DEX nft is in the UTXO if the DEX policy is
    defined.

    If the dex nft is in the values, this value is skipped because it is assumed
    that this utxo has already been parsed.

    Args:
        values: The pool UTXO inputs.

    Returns:
        Assets: None or the dex nft.
    """
    assets = values["assets"]

    if "pool_nft" in values:
        pool_nft = Assets(root=values["pool_nft"])
    else:
        nfts = [asset for asset, quantity in assets.items() if quantity == 1]
        if len(nfts) != 1:
            raise InvalidPoolError(
                f"MuesliSwap pools must have exactly one pool nft: assets={assets}",
            )
        pool_nft = Assets(**{nfts[0]: assets.root.pop(nfts[0])})
        values["pool_nft"] = pool_nft

    return pool_nft

PreciseFloat dataclass

Bases: PlutusData

A precise float dataclass.

Source code in src/charli3_dendrite/dexs/amm/muesli.py
130
131
132
133
134
135
136
137
@dataclass
class PreciseFloat(PlutusData):
    """A precise float dataclass."""

    CONSTR_ID = 0

    numerator: int
    denominator: int