from typing_extensions import AsyncIterable
from dataclasses import dataclass
from datetime import datetime

from .api.get_funding_payments import GetFundingPayments, FundingPayment

@dataclass
class GetFundingPaymentsPaged(GetFundingPayments):
  async def get_funding_payments_paged(
    self,
    *,
    address: str,
    subaccount_number: int,
    ticker: str | None = None,
    after_or_at: datetime | None = None,
    limit: int | None = None,
    validate: bool | None = None,
  ) -> AsyncIterable[list[FundingPayment]]:
    """Retrieves funding payment history for a specific subaccount. Funding payments are periodic settlements that occur between long and short positions based on the funding rate.

    - `address`: The wallet address that owns the account.
    - `subaccount_number`: The identifier for the specific subaccount within the wallet address.
    - `ticker`: The market ticker (e.g. `'BTC-USD'`).
    - `after_or_at`: If given, fetches funding payments starting from the given timestamp.
    - `limit`: The max. number of funding payments to retrieve.
    - `validate`: Whether to validate the response against the expected schema.

    > [dYdX API docs](https://docs.dydx.xyz/indexer-client/http#get-funding-payments)
    """
    page = 1
    while True:
      response = await self.get_funding_payments(
        address=address,
        subaccount_number=subaccount_number,
        ticker=ticker,
        after_or_at=after_or_at,
        limit=limit,
        page=page,
        validate=validate,
      )
      funding_payments = response['fundingPayments']
      if not funding_payments:
        break
      yield funding_payments
      page += 1
