From 7abc4ad15e63c20b9eb579b9a6f88dc1d7d2eea0 Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Sun, 22 Sep 2024 21:47:02 -0700 Subject: [PATCH 1/2] Switch get_budget to used instead of remaining. --- .../event_handlers/__tests__/get_budget_test.py | 6 +++--- nwc_backend/event_handlers/get_budget_handler.py | 10 +++++++--- nwc_backend/models/nip47_budget.py | 6 +++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/nwc_backend/event_handlers/__tests__/get_budget_test.py b/nwc_backend/event_handlers/__tests__/get_budget_test.py index 1ebc1b0..fe41701 100644 --- a/nwc_backend/event_handlers/__tests__/get_budget_test.py +++ b/nwc_backend/event_handlers/__tests__/get_budget_test.py @@ -65,7 +65,7 @@ async def test_get_budget_success__spending_limit_SAT_enabled( spending_cycle = (await db.session.execute(select(SpendingCycle))).scalar_one() assert exclude_none_values(response.to_dict()) == { "total_budget_msats": 1000000, - "remaining_budget_msats": 1000000, + "used_budget_msats": 0, "renews_at": round(spending_cycle.end_time.timestamp()), } @@ -105,12 +105,12 @@ async def test_pay_invoice_success__spending_limit_USD_enabled( ) assert exclude_none_values(response.to_dict()) == { "total_budget_msats": estimated_budget_currency_amount * 1000, - "remaining_budget_msats": estimated_budget_currency_amount * 1000, + "used_budget_msats": 0, "renews_at": ANY, "currency": { "code": "USD", "total_budget": total_budget_currency_amount, - "remaining_budget": total_budget_currency_amount, + "used_budget": 0, "symbol": "$", "name": "US Dollar", "decimals": 2, diff --git a/nwc_backend/event_handlers/get_budget_handler.py b/nwc_backend/event_handlers/get_budget_handler.py index 76b5bb1..9d31e1b 100644 --- a/nwc_backend/event_handlers/get_budget_handler.py +++ b/nwc_backend/event_handlers/get_budget_handler.py @@ -20,9 +20,12 @@ async def get_budget(access_token: str, request: Nip47Request) -> Nip47BudgetRes round(current_cycle_end_time.timestamp()) if current_cycle_end_time else None ) if budget_currency.code == "SAT": + used_budget_sats = ( + current_spending_limit.amount - current_cycle_remaining_amount + ) return Nip47BudgetResponse( total_budget_msats=current_spending_limit.amount * 1000, - remaining_budget_msats=current_cycle_remaining_amount * 1000, + used_budget_msats=used_budget_sats * 1000, renews_at=current_cycle_renews_at, ) @@ -39,17 +42,18 @@ async def get_budget(access_token: str, request: Nip47Request) -> Nip47BudgetRes / current_spending_limit.amount * current_cycle_remaining_amount ) + used_budget_sats = total_budget_sats - remaining_budget_sats return Nip47BudgetResponse( currency=Nip47BudgetCurrency( code=budget_currency.code, total_budget=current_spending_limit.amount, - remaining_budget=current_cycle_remaining_amount, + used_budget=current_spending_limit.amount - current_cycle_remaining_amount, symbol=budget_currency.symbol, name=budget_currency.name, decimals=budget_currency.decimals, ), total_budget_msats=total_budget_sats * 1000, - remaining_budget_msats=remaining_budget_sats * 1000, + used_budget_msats=used_budget_sats * 1000, renews_at=current_cycle_renews_at, ) diff --git a/nwc_backend/models/nip47_budget.py b/nwc_backend/models/nip47_budget.py index fd2aebd..73e99b0 100644 --- a/nwc_backend/models/nip47_budget.py +++ b/nwc_backend/models/nip47_budget.py @@ -8,18 +8,18 @@ class Nip47BudgetCurrency: symbol: str name: str decimals: int - remaining_budget: int + used_budget: int total_budget: int @dataclass class Nip47BudgetResponse: - remaining_budget_msats: Optional[int] = None + used_budget_msats: Optional[int] = None total_budget_msats: Optional[int] = None renews_at: Optional[int] = None currency: Optional[Nip47BudgetCurrency] = None def to_dict(self): - if self.remaining_budget_msats is None: + if self.used_budget_msats is None: return {} return {k: v for k, v in asdict(self).items() if v is not None} From ebcba3fd0bb64f898a549db15714cf5d73724b8a Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Sun, 22 Sep 2024 22:02:03 -0700 Subject: [PATCH 2/2] Remove msats from field names as per feedback --- nwc_backend/event_handlers/__tests__/get_budget_test.py | 8 ++++---- nwc_backend/event_handlers/get_budget_handler.py | 8 ++++---- nwc_backend/models/nip47_budget.py | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/nwc_backend/event_handlers/__tests__/get_budget_test.py b/nwc_backend/event_handlers/__tests__/get_budget_test.py index fe41701..4f67058 100644 --- a/nwc_backend/event_handlers/__tests__/get_budget_test.py +++ b/nwc_backend/event_handlers/__tests__/get_budget_test.py @@ -64,8 +64,8 @@ async def test_get_budget_success__spending_limit_SAT_enabled( mock_get_budget_estimate.assert_not_called() spending_cycle = (await db.session.execute(select(SpendingCycle))).scalar_one() assert exclude_none_values(response.to_dict()) == { - "total_budget_msats": 1000000, - "used_budget_msats": 0, + "total_budget": 1000000, + "used_budget": 0, "renews_at": round(spending_cycle.end_time.timestamp()), } @@ -104,8 +104,8 @@ async def test_pay_invoice_success__spending_limit_USD_enabled( headers=ANY, ) assert exclude_none_values(response.to_dict()) == { - "total_budget_msats": estimated_budget_currency_amount * 1000, - "used_budget_msats": 0, + "total_budget": estimated_budget_currency_amount * 1000, + "used_budget": 0, "renews_at": ANY, "currency": { "code": "USD", diff --git a/nwc_backend/event_handlers/get_budget_handler.py b/nwc_backend/event_handlers/get_budget_handler.py index 9d31e1b..50bd6c2 100644 --- a/nwc_backend/event_handlers/get_budget_handler.py +++ b/nwc_backend/event_handlers/get_budget_handler.py @@ -24,8 +24,8 @@ async def get_budget(access_token: str, request: Nip47Request) -> Nip47BudgetRes current_spending_limit.amount - current_cycle_remaining_amount ) return Nip47BudgetResponse( - total_budget_msats=current_spending_limit.amount * 1000, - used_budget_msats=used_budget_sats * 1000, + total_budget=current_spending_limit.amount * 1000, + used_budget=used_budget_sats * 1000, renews_at=current_cycle_renews_at, ) @@ -53,7 +53,7 @@ async def get_budget(access_token: str, request: Nip47Request) -> Nip47BudgetRes name=budget_currency.name, decimals=budget_currency.decimals, ), - total_budget_msats=total_budget_sats * 1000, - used_budget_msats=used_budget_sats * 1000, + total_budget=total_budget_sats * 1000, + used_budget=used_budget_sats * 1000, renews_at=current_cycle_renews_at, ) diff --git a/nwc_backend/models/nip47_budget.py b/nwc_backend/models/nip47_budget.py index 73e99b0..522c178 100644 --- a/nwc_backend/models/nip47_budget.py +++ b/nwc_backend/models/nip47_budget.py @@ -14,12 +14,12 @@ class Nip47BudgetCurrency: @dataclass class Nip47BudgetResponse: - used_budget_msats: Optional[int] = None - total_budget_msats: Optional[int] = None + used_budget: Optional[int] = None + total_budget: Optional[int] = None renews_at: Optional[int] = None currency: Optional[Nip47BudgetCurrency] = None def to_dict(self): - if self.used_budget_msats is None: + if self.used_budget is None: return {} return {k: v for k, v in asdict(self).items() if v is not None}