
Features implemented: - User authentication with JWT tokens and role-based access (developer/buyer) - Blockchain wallet linking and management with Ethereum integration - Carbon project creation and management for developers - Marketplace for browsing and purchasing carbon offsets - Transaction tracking with blockchain integration - Database models for users, projects, offsets, and transactions - Comprehensive API with authentication, wallet, project, and trading endpoints - Health check endpoint and platform information - SQLite database with Alembic migrations - Full API documentation with OpenAPI/Swagger Technical stack: - FastAPI with Python - SQLAlchemy ORM with SQLite - Web3.py for blockchain integration - JWT authentication with bcrypt - CORS enabled for frontend integration - Comprehensive error handling and validation Environment variables required: - SECRET_KEY (JWT secret) - BLOCKCHAIN_RPC_URL (optional, defaults to localhost)
56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.db.session import get_db
|
|
from app.core.deps import get_current_user
|
|
from app.models.user import User
|
|
from app.schemas.user import WalletLinkRequest, WalletResponse
|
|
from app.services.wallet import wallet_service
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/link", response_model=WalletResponse)
|
|
def link_wallet(
|
|
wallet_request: WalletLinkRequest,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
result = wallet_service.link_wallet(
|
|
db=db,
|
|
user_id=current_user.id,
|
|
wallet_address=wallet_request.wallet_address
|
|
)
|
|
|
|
if not result["success"]:
|
|
raise HTTPException(status_code=400, detail=result["message"])
|
|
|
|
return WalletResponse(**result)
|
|
|
|
@router.delete("/unlink", response_model=WalletResponse)
|
|
def unlink_wallet(
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
result = wallet_service.unlink_wallet(db=db, user_id=current_user.id)
|
|
|
|
if not result["success"]:
|
|
raise HTTPException(status_code=400, detail=result["message"])
|
|
|
|
return WalletResponse(**result)
|
|
|
|
@router.get("/info", response_model=WalletResponse)
|
|
def get_wallet_info(
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
result = wallet_service.get_wallet_info(db=db, user_id=current_user.id)
|
|
|
|
if not result["success"]:
|
|
raise HTTPException(status_code=400, detail=result["message"])
|
|
|
|
return WalletResponse(**result)
|
|
|
|
@router.post("/generate-test-wallet")
|
|
def generate_test_wallet():
|
|
"""Generate a test wallet for development purposes"""
|
|
result = wallet_service.generate_test_wallet()
|
|
return result |