|
| 1 | +"""Model for AMMClawback transaction type.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass, field |
| 6 | +from enum import Enum |
| 7 | +from typing import Dict, Optional |
| 8 | + |
| 9 | +from typing_extensions import Self |
| 10 | + |
| 11 | +from xrpl.models.amounts import IssuedCurrencyAmount |
| 12 | +from xrpl.models.currencies import Currency |
| 13 | +from xrpl.models.currencies.issued_currency import IssuedCurrency |
| 14 | +from xrpl.models.flags import FlagInterface |
| 15 | +from xrpl.models.required import REQUIRED |
| 16 | +from xrpl.models.transactions.transaction import Transaction |
| 17 | +from xrpl.models.transactions.types import TransactionType |
| 18 | +from xrpl.models.utils import KW_ONLY_DATACLASS, require_kwargs_on_init |
| 19 | + |
| 20 | + |
| 21 | +class AMMClawbackFlag(int, Enum): |
| 22 | + """ |
| 23 | + Claw back the specified amount of Asset, and a corresponding amount of Asset2 based |
| 24 | + on the AMM pool's asset proportion; both assets must be issued by the issuer in the |
| 25 | + Account field. If this flag isn't enabled, the issuer claws back the specified |
| 26 | + amount of Asset, while a corresponding proportion of Asset2 goes back to the Holder. |
| 27 | + """ |
| 28 | + |
| 29 | + TF_CLAW_TWO_ASSETS = 0x00000001 |
| 30 | + |
| 31 | + |
| 32 | +class AMMClawbackFlagInterface(FlagInterface): |
| 33 | + """ |
| 34 | + Claw back the specified amount of Asset, and a corresponding amount of Asset2 based |
| 35 | + on the AMM pool's asset proportion; both assets must be issued by the issuer in the |
| 36 | + Account field. If this flag isn't enabled, the issuer claws back the specified |
| 37 | + amount of Asset, while a corresponding proportion of Asset2 goes back to the Holder. |
| 38 | + """ |
| 39 | + |
| 40 | + TF_CLAW_TWO_ASSETS: bool |
| 41 | + |
| 42 | + |
| 43 | +@require_kwargs_on_init |
| 44 | +@dataclass(frozen=True, **KW_ONLY_DATACLASS) |
| 45 | +class AMMClawback(Transaction): |
| 46 | + """ |
| 47 | + Claw back tokens from a holder who has deposited your issued tokens into an AMM |
| 48 | + pool. |
| 49 | + """ |
| 50 | + |
| 51 | + holder: str = REQUIRED # type: ignore |
| 52 | + """The account holding the asset to be clawed back.""" |
| 53 | + |
| 54 | + asset: IssuedCurrency = REQUIRED # type: ignore |
| 55 | + """ |
| 56 | + Specifies the asset that the issuer wants to claw back from the AMM pool. In JSON, |
| 57 | + this is an object with currency and issuer fields. The issuer field must match with |
| 58 | + Account. |
| 59 | + """ |
| 60 | + |
| 61 | + asset2: Currency = REQUIRED # type: ignore |
| 62 | + """ |
| 63 | + Specifies the other asset in the AMM's pool. In JSON, this is an object with |
| 64 | + currency and issuer fields (omit issuer for XRP). |
| 65 | + """ |
| 66 | + |
| 67 | + amount: Optional[IssuedCurrencyAmount] = None |
| 68 | + """ |
| 69 | + The maximum amount to claw back from the AMM account. The currency and issuer |
| 70 | + subfields should match the Asset subfields. If this field isn't specified, or the |
| 71 | + value subfield exceeds the holder's available tokens in the AMM, all of the |
| 72 | + holder's tokens are clawed back. |
| 73 | + """ |
| 74 | + |
| 75 | + transaction_type: TransactionType = field( |
| 76 | + default=TransactionType.AMM_CLAWBACK, |
| 77 | + init=False, |
| 78 | + ) |
| 79 | + |
| 80 | + def _get_errors(self: Self) -> Dict[str, str]: |
| 81 | + return { |
| 82 | + key: value |
| 83 | + for key, value in { |
| 84 | + **super()._get_errors(), |
| 85 | + "AMMClawback": self._validate_wallet_and_amount_fields(), |
| 86 | + }.items() |
| 87 | + if value is not None |
| 88 | + } |
| 89 | + |
| 90 | + def _validate_wallet_and_amount_fields(self: Self) -> Optional[str]: |
| 91 | + errors = "" |
| 92 | + if self.account == self.holder: |
| 93 | + errors += "Issuer and holder wallets must be distinct." |
| 94 | + |
| 95 | + if self.account != self.asset.issuer: |
| 96 | + errors += ( |
| 97 | + "Asset.issuer and AMMClawback transaction sender must be identical." |
| 98 | + ) |
| 99 | + |
| 100 | + if self.amount is not None and ( |
| 101 | + self.amount.issuer != self.asset.issuer |
| 102 | + or self.amount.currency != self.asset.currency |
| 103 | + ): |
| 104 | + errors += ( |
| 105 | + "Amount.issuer and Amount.currency must match corresponding Asset " |
| 106 | + + "fields." |
| 107 | + ) |
| 108 | + |
| 109 | + return errors if errors else None |
0 commit comments