NUT-04 and NUT-05: Add state field to quotes (#560)

* wip adding states, tests failing

* add state field to quotes

* responses from quotes

* store correct state

* cleaner test

* fix swap check

* oops
This commit is contained in:
callebtc
2024-06-26 03:06:01 +02:00
committed by GitHub
parent e846acf946
commit 6b38ef6c29
26 changed files with 330 additions and 84 deletions

View File

@@ -48,7 +48,7 @@ class DLEQWallet(BaseModel):
# ------- PROOFS -------
class SpentState(Enum):
class ProofSpentState(Enum):
unspent = "UNSPENT"
spent = "SPENT"
pending = "PENDING"
@@ -59,13 +59,13 @@ class SpentState(Enum):
class ProofState(LedgerEvent):
Y: str
state: SpentState
state: ProofSpentState
witness: Optional[str] = None
@root_validator()
def check_witness(cls, values):
state, witness = values.get("state"), values.get("witness")
if witness is not None and state != SpentState.spent:
if witness is not None and state != ProofSpentState.spent:
raise ValueError('Witness can only be set if the spent state is "SPENT"')
return values
@@ -268,6 +268,15 @@ class Invoice(BaseModel):
time_paid: Union[None, str, int, float] = ""
class MeltQuoteState(Enum):
unpaid = "UNPAID"
pending = "PENDING"
paid = "PAID"
def __str__(self):
return self.name
class MeltQuote(LedgerEvent):
quote: str
method: str
@@ -277,6 +286,7 @@ class MeltQuote(LedgerEvent):
amount: int
fee_reserve: int
paid: bool
state: MeltQuoteState
created_time: Union[int, None] = None
paid_time: Union[int, None] = None
fee_paid: int = 0
@@ -303,6 +313,7 @@ class MeltQuote(LedgerEvent):
amount=row["amount"],
fee_reserve=row["fee_reserve"],
paid=row["paid"],
state=MeltQuoteState[row["state"]],
created_time=created_time,
paid_time=paid_time,
fee_paid=row["fee_paid"],
@@ -318,6 +329,27 @@ class MeltQuote(LedgerEvent):
def kind(self) -> JSONRPCSubscriptionKinds:
return JSONRPCSubscriptionKinds.BOLT11_MELT_QUOTE
# method that is invoked when the `state` attribute is changed. to protect the state from being set to anything else if the current state is paid
def __setattr__(self, name, value):
# an unpaid quote can only be set to pending or paid
if name == "state" and self.state == MeltQuoteState.unpaid:
if value != MeltQuoteState.pending and value != MeltQuoteState.paid:
raise Exception("Cannot change state of an unpaid quote.")
# a paid quote can not be changed
if name == "state" and self.state == MeltQuoteState.paid:
raise Exception("Cannot change state of a paid quote.")
super().__setattr__(name, value)
class MintQuoteState(Enum):
unpaid = "UNPAID"
paid = "PAID"
pending = "PENDING"
issued = "ISSUED"
def __str__(self):
return self.name
class MintQuote(LedgerEvent):
quote: str
@@ -328,6 +360,7 @@ class MintQuote(LedgerEvent):
amount: int
paid: bool
issued: bool
state: MintQuoteState
created_time: Union[int, None] = None
paid_time: Union[int, None] = None
expiry: Optional[int] = None
@@ -353,6 +386,7 @@ class MintQuote(LedgerEvent):
amount=row["amount"],
paid=row["paid"],
issued=row["issued"],
state=MintQuoteState[row["state"]],
created_time=created_time,
paid_time=paid_time,
)
@@ -366,6 +400,24 @@ class MintQuote(LedgerEvent):
def kind(self) -> JSONRPCSubscriptionKinds:
return JSONRPCSubscriptionKinds.BOLT11_MINT_QUOTE
def __setattr__(self, name, value):
# un unpaid quote can only be set to paid
if name == "state" and self.state == MintQuoteState.unpaid:
if value != MintQuoteState.paid:
raise Exception("Cannot change state of an unpaid quote.")
# a paid quote can only be set to pending or issued
if name == "state" and self.state == MintQuoteState.paid:
if value != MintQuoteState.pending and value != MintQuoteState.issued:
raise Exception(f"Cannot change state of a paid quote to {value}.")
# a pending quote can only be set to paid or issued
if name == "state" and self.state == MintQuoteState.pending:
if value not in [MintQuoteState.paid, MintQuoteState.issued]:
raise Exception("Cannot change state of a pending quote.")
# an issued quote cannot be changed
if name == "state" and self.state == MintQuoteState.issued:
raise Exception("Cannot change state of an issued quote.")
super().__setattr__(name, value)
# ------- KEYSETS -------