Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch get_budget to used instead of remaining. #163

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions nwc_backend/event_handlers/__tests__/get_budget_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
"remaining_budget_msats": 1000000,
"total_budget": 1000000,
"used_budget": 0,
"renews_at": round(spending_cycle.end_time.timestamp()),
}

Expand Down Expand Up @@ -104,13 +104,13 @@ 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,
"remaining_budget_msats": estimated_budget_currency_amount * 1000,
"total_budget": estimated_budget_currency_amount * 1000,
"used_budget": 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,
Expand Down
14 changes: 9 additions & 5 deletions nwc_backend/event_handlers/get_budget_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
total_budget=current_spending_limit.amount * 1000,
used_budget=used_budget_sats * 1000,
renews_at=current_cycle_renews_at,
)

Expand All @@ -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,
total_budget=total_budget_sats * 1000,
used_budget=used_budget_sats * 1000,
renews_at=current_cycle_renews_at,
)
8 changes: 4 additions & 4 deletions nwc_backend/models/nip47_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.remaining_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}
Loading