Automated Action e122f16dea Build complete blockchain-enabled carbon offset trading platform
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)
2025-06-20 13:45:14 +00:00

118 lines
5.7 KiB
Python

"""Initial migration
Revision ID: 001
Revises:
Create Date: 2024-01-01 12:00:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '001'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
# Create users table
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('hashed_password', sa.String(), nullable=False),
sa.Column('full_name', sa.String(), nullable=False),
sa.Column('user_type', sa.String(), nullable=False),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('wallet_address', sa.String(), nullable=True),
sa.Column('wallet_public_key', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
op.create_index(op.f('ix_users_wallet_address'), 'users', ['wallet_address'], unique=True)
# Create carbon_projects table
op.create_table('carbon_projects',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('title', sa.String(), nullable=False),
sa.Column('description', sa.Text(), nullable=False),
sa.Column('location', sa.String(), nullable=False),
sa.Column('project_type', sa.String(), nullable=False),
sa.Column('methodology', sa.String(), nullable=False),
sa.Column('total_credits_available', sa.Integer(), nullable=False),
sa.Column('credits_sold', sa.Integer(), nullable=True),
sa.Column('price_per_credit', sa.Float(), nullable=False),
sa.Column('start_date', sa.DateTime(), nullable=False),
sa.Column('end_date', sa.DateTime(), nullable=False),
sa.Column('verification_status', sa.String(), nullable=True),
sa.Column('verification_document_url', sa.String(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=True),
sa.Column('contract_address', sa.String(), nullable=True),
sa.Column('token_id', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('developer_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['developer_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_carbon_projects_id'), 'carbon_projects', ['id'], unique=False)
# Create carbon_offsets table
op.create_table('carbon_offsets',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('serial_number', sa.String(), nullable=False),
sa.Column('vintage_year', sa.Integer(), nullable=False),
sa.Column('quantity', sa.Integer(), nullable=False),
sa.Column('status', sa.String(), nullable=True),
sa.Column('token_id', sa.String(), nullable=True),
sa.Column('blockchain_hash', sa.String(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.Column('project_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['project_id'], ['carbon_projects.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_carbon_offsets_id'), 'carbon_offsets', ['id'], unique=False)
op.create_index(op.f('ix_carbon_offsets_serial_number'), 'carbon_offsets', ['serial_number'], unique=True)
op.create_index(op.f('ix_carbon_offsets_token_id'), 'carbon_offsets', ['token_id'], unique=True)
# Create transactions table
op.create_table('transactions',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('transaction_hash', sa.String(), nullable=False),
sa.Column('quantity', sa.Integer(), nullable=False),
sa.Column('price_per_credit', sa.Float(), nullable=False),
sa.Column('total_amount', sa.Float(), nullable=False),
sa.Column('status', sa.String(), nullable=True),
sa.Column('block_number', sa.Integer(), nullable=True),
sa.Column('gas_used', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('confirmed_at', sa.DateTime(), nullable=True),
sa.Column('buyer_id', sa.Integer(), nullable=False),
sa.Column('offset_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['buyer_id'], ['users.id'], ),
sa.ForeignKeyConstraint(['offset_id'], ['carbon_offsets.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_transactions_id'), 'transactions', ['id'], unique=False)
op.create_index(op.f('ix_transactions_transaction_hash'), 'transactions', ['transaction_hash'], unique=True)
def downgrade() -> None:
op.drop_index(op.f('ix_transactions_transaction_hash'), table_name='transactions')
op.drop_index(op.f('ix_transactions_id'), table_name='transactions')
op.drop_table('transactions')
op.drop_index(op.f('ix_carbon_offsets_token_id'), table_name='carbon_offsets')
op.drop_index(op.f('ix_carbon_offsets_serial_number'), table_name='carbon_offsets')
op.drop_index(op.f('ix_carbon_offsets_id'), table_name='carbon_offsets')
op.drop_table('carbon_offsets')
op.drop_index(op.f('ix_carbon_projects_id'), table_name='carbon_projects')
op.drop_table('carbon_projects')
op.drop_index(op.f('ix_users_wallet_address'), table_name='users')
op.drop_index(op.f('ix_users_email'), table_name='users')
op.drop_index(op.f('ix_users_id'), table_name='users')
op.drop_table('users')