-
Notifications
You must be signed in to change notification settings - Fork 22
Add balance system #72
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8f42858
Add balance system
lingtonglu b90e508
fix the alembic migrations
lingtonglu 60c26bf
rebase the alembic ddl order
lingtonglu 2970ab1
more optimization
lingtonglu 4b108dc
fix the formatting
lingtonglu 3a652fc
undo the tensorblock billing
lingtonglu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| """add balance system | ||
|
|
||
| Revision ID: a58395ea1b22 | ||
| Revises: c9f3e548adef | ||
| Create Date: 2025-08-20 22:00:45.743308 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = 'a58395ea1b22' | ||
| down_revision = 'c9f3e548adef' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| def upgrade() -> None: | ||
| op.create_table( | ||
| 'wallets', | ||
| sa.Column('account_id', sa.BigInteger(), nullable=False), | ||
| sa.Column('currency', sa.CHAR(length=3), nullable=False, server_default='USD'), | ||
| sa.Column('balance', sa.DECIMAL(precision=20, scale=6), nullable=False, server_default='0'), | ||
| sa.Column('blocked', sa.Boolean(), nullable=False, server_default='FALSE'), | ||
| sa.Column('version', sa.BigInteger(), nullable=False, server_default='0'), | ||
| sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')), | ||
| sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('now()')), | ||
| sa.PrimaryKeyConstraint('account_id'), | ||
| sa.ForeignKeyConstraint(['account_id'], ['users.id'], ondelete='CASCADE') | ||
| ) | ||
| op.add_column('provider_keys', sa.Column('billable', sa.Boolean(), nullable=False, server_default='FALSE')) | ||
|
|
||
| def downgrade() -> None: | ||
| op.drop_table('wallets') | ||
| op.drop_column('provider_keys', 'billable') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from decimal import Decimal | ||
| from fastapi import APIRouter, Depends | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
| from pydantic import BaseModel | ||
|
|
||
| from app.api.dependencies import get_current_active_user, get_current_active_user_from_clerk | ||
| from app.core.database import get_async_db | ||
| from app.models.user import User | ||
| from app.services.wallet_service import WalletService | ||
|
|
||
| router = APIRouter() | ||
|
|
||
| class WalletResponse(BaseModel): | ||
| balance: Decimal | ||
| blocked: bool | ||
| currency: str | ||
|
|
||
| @router.get("/balance", response_model=WalletResponse) | ||
| async def get_wallet_balance( | ||
| user: User = Depends(get_current_active_user), | ||
| db: AsyncSession = Depends(get_async_db) | ||
| ): | ||
| """Get current wallet balance""" | ||
| wallet = await WalletService.get(db, user.id) | ||
|
|
||
| if not wallet: | ||
| await WalletService.ensure_wallet(db, user.id) | ||
| return WalletResponse(balance=Decimal("0"), blocked=False, currency="USD") | ||
|
|
||
| return WalletResponse(**wallet) | ||
|
|
||
| @router.get("/balance/clerk", response_model=WalletResponse) | ||
| async def get_wallet_balance_clerk( | ||
| user: User = Depends(get_current_active_user_from_clerk), | ||
| db: AsyncSession = Depends(get_async_db) | ||
| ): | ||
| return await get_wallet_balance(user, db) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from datetime import datetime, UTC | ||
| from sqlalchemy import Column, BigInteger, CHAR, DECIMAL, Boolean, DateTime, ForeignKey | ||
| from sqlalchemy.orm import relationship | ||
| from .base import Base | ||
|
|
||
| class Wallet(Base): | ||
| __tablename__ = "wallets" | ||
|
|
||
| account_id = Column(BigInteger, ForeignKey("users.id", ondelete="CASCADE"), primary_key=True) | ||
| currency = Column(CHAR(3), nullable=False, default='USD') | ||
| balance = Column(DECIMAL(20, 6), nullable=False, default=0) | ||
| blocked = Column(Boolean, nullable=False, default=False) | ||
| version = Column(BigInteger, nullable=False, default=0) | ||
| created_at = Column(DateTime(timezone=True), nullable=False, default=datetime.now(UTC)) | ||
| updated_at = Column(DateTime(timezone=True), nullable=False, default=datetime.now(UTC)) | ||
|
|
||
| user = relationship("User", back_populates="wallet") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we add a "billable" annotation at the model level instead of the provider level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an open discussion. My current implementation is based on the assumption that the billing object is at "provider" level. You could have "free model" by setting the price to be zero.