feat: complete remaining phase 1 tasks - multi-chain wallet, atomic swaps, and multi-region deployment
This commit is contained in:
62
apps/coordinator-api/src/app/domain/atomic_swap.py
Normal file
62
apps/coordinator-api/src/app/domain/atomic_swap.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""
|
||||
Atomic Swap Domain Models
|
||||
|
||||
Domain models for managing trustless cross-chain atomic swaps between agents.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
|
||||
class SwapStatus(str, Enum):
|
||||
CREATED = "created" # Order created but not initiated on-chain
|
||||
INITIATED = "initiated" # Hashlock created and funds locked on source chain
|
||||
PARTICIPATING = "participating" # Hashlock matched and funds locked on target chain
|
||||
COMPLETED = "completed" # Secret revealed and funds claimed
|
||||
REFUNDED = "refunded" # Timelock expired, funds returned
|
||||
FAILED = "failed" # General error state
|
||||
|
||||
class AtomicSwapOrder(SQLModel, table=True):
|
||||
"""Represents a cross-chain atomic swap order between two parties"""
|
||||
__tablename__ = "atomic_swap_order"
|
||||
|
||||
id: str = Field(default_factory=lambda: uuid4().hex, primary_key=True)
|
||||
|
||||
# Initiator details (Party A)
|
||||
initiator_agent_id: str = Field(index=True)
|
||||
initiator_address: str = Field()
|
||||
source_chain_id: int = Field(index=True)
|
||||
source_token: str = Field() # "native" or ERC20 address
|
||||
source_amount: float = Field()
|
||||
|
||||
# Participant details (Party B)
|
||||
participant_agent_id: str = Field(index=True)
|
||||
participant_address: str = Field()
|
||||
target_chain_id: int = Field(index=True)
|
||||
target_token: str = Field() # "native" or ERC20 address
|
||||
target_amount: float = Field()
|
||||
|
||||
# Cryptographic elements
|
||||
hashlock: str = Field(index=True) # sha256 hash of the secret
|
||||
secret: Optional[str] = Field(default=None) # The secret (revealed upon completion)
|
||||
|
||||
# Timelocks (Unix timestamps)
|
||||
source_timelock: int = Field() # Party A's timelock (longer)
|
||||
target_timelock: int = Field() # Party B's timelock (shorter)
|
||||
|
||||
# Transaction tracking
|
||||
source_initiate_tx: Optional[str] = Field(default=None)
|
||||
target_participate_tx: Optional[str] = Field(default=None)
|
||||
target_complete_tx: Optional[str] = Field(default=None)
|
||||
source_complete_tx: Optional[str] = Field(default=None)
|
||||
refund_tx: Optional[str] = Field(default=None)
|
||||
|
||||
status: SwapStatus = Field(default=SwapStatus.CREATED, index=True)
|
||||
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
435
apps/coordinator-api/src/app/domain/global_marketplace.py
Normal file
435
apps/coordinator-api/src/app/domain/global_marketplace.py
Normal file
@@ -0,0 +1,435 @@
|
||||
"""
|
||||
Global Marketplace Domain Models
|
||||
Domain models for global marketplace operations, multi-region support, and cross-chain integration
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Dict, List, Optional, Any
|
||||
from uuid import uuid4
|
||||
from enum import Enum
|
||||
|
||||
from sqlmodel import SQLModel, Field, Column, JSON, Index, Relationship
|
||||
from sqlalchemy import DateTime, func
|
||||
|
||||
from .marketplace import MarketplaceOffer, MarketplaceBid
|
||||
from .agent_identity import AgentIdentity
|
||||
|
||||
|
||||
class MarketplaceStatus(str, Enum):
|
||||
"""Global marketplace offer status"""
|
||||
ACTIVE = "active"
|
||||
INACTIVE = "inactive"
|
||||
PENDING = "pending"
|
||||
COMPLETED = "completed"
|
||||
CANCELLED = "cancelled"
|
||||
EXPIRED = "expired"
|
||||
|
||||
|
||||
class RegionStatus(str, Enum):
|
||||
"""Global marketplace region status"""
|
||||
ACTIVE = "active"
|
||||
INACTIVE = "inactive"
|
||||
MAINTENANCE = "maintenance"
|
||||
DEPRECATED = "deprecated"
|
||||
|
||||
|
||||
class MarketplaceRegion(SQLModel, table=True):
|
||||
"""Global marketplace region configuration"""
|
||||
|
||||
__tablename__ = "marketplace_regions"
|
||||
__table_args__ = {"extend_existing": True}
|
||||
|
||||
id: str = Field(default_factory=lambda: f"region_{uuid4().hex[:8]}", primary_key=True)
|
||||
region_code: str = Field(index=True, unique=True) # us-east-1, eu-west-1, etc.
|
||||
region_name: str = Field(index=True)
|
||||
geographic_area: str = Field(default="global")
|
||||
|
||||
# Configuration
|
||||
base_currency: str = Field(default="USD")
|
||||
timezone: str = Field(default="UTC")
|
||||
language: str = Field(default="en")
|
||||
|
||||
# Load balancing
|
||||
load_factor: float = Field(default=1.0, ge=0.1, le=10.0)
|
||||
max_concurrent_requests: int = Field(default=1000)
|
||||
priority_weight: float = Field(default=1.0, ge=0.1, le=10.0)
|
||||
|
||||
# Status and health
|
||||
status: RegionStatus = Field(default=RegionStatus.ACTIVE)
|
||||
health_score: float = Field(default=1.0, ge=0.0, le=1.0)
|
||||
last_health_check: Optional[datetime] = Field(default=None)
|
||||
|
||||
# API endpoints
|
||||
api_endpoint: str = Field(default="")
|
||||
websocket_endpoint: str = Field(default="")
|
||||
blockchain_rpc_endpoints: Dict[str, str] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Performance metrics
|
||||
average_response_time: float = Field(default=0.0)
|
||||
request_rate: float = Field(default=0.0)
|
||||
error_rate: float = Field(default=0.0)
|
||||
|
||||
# Timestamps
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_marketplace_region_code', 'region_code'),
|
||||
Index('idx_marketplace_region_status', 'status'),
|
||||
Index('idx_marketplace_region_health', 'health_score'),
|
||||
)
|
||||
|
||||
|
||||
class GlobalMarketplaceConfig(SQLModel, table=True):
|
||||
"""Global marketplace configuration settings"""
|
||||
|
||||
__tablename__ = "global_marketplace_configs"
|
||||
__table_args__ = {"extend_existing": True}
|
||||
|
||||
id: str = Field(default_factory=lambda: f"config_{uuid4().hex[:8]}", primary_key=True)
|
||||
config_key: str = Field(index=True, unique=True)
|
||||
config_value: str = Field(default="") # Changed from Any to str
|
||||
config_type: str = Field(default="string") # string, number, boolean, json
|
||||
|
||||
# Configuration metadata
|
||||
description: str = Field(default="")
|
||||
category: str = Field(default="general")
|
||||
is_public: bool = Field(default=False)
|
||||
is_encrypted: bool = Field(default=False)
|
||||
|
||||
# Validation rules
|
||||
min_value: Optional[float] = Field(default=None)
|
||||
max_value: Optional[float] = Field(default=None)
|
||||
allowed_values: List[str] = Field(default_factory=list, sa_column=Column(JSON))
|
||||
|
||||
# Timestamps
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
last_modified_by: Optional[str] = Field(default=None)
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_global_config_key', 'config_key'),
|
||||
Index('idx_global_config_category', 'category'),
|
||||
)
|
||||
|
||||
|
||||
class GlobalMarketplaceOffer(SQLModel, table=True):
|
||||
"""Global marketplace offer with multi-region support"""
|
||||
|
||||
__tablename__ = "global_marketplace_offers"
|
||||
__table_args__ = {"extend_existing": True}
|
||||
|
||||
id: str = Field(default_factory=lambda: f"offer_{uuid4().hex[:8]}", primary_key=True)
|
||||
original_offer_id: str = Field(index=True) # Reference to original marketplace offer
|
||||
|
||||
# Global offer data
|
||||
agent_id: str = Field(index=True)
|
||||
service_type: str = Field(index=True) # gpu, compute, storage, etc.
|
||||
resource_specification: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Pricing (multi-currency support)
|
||||
base_price: float = Field(default=0.0)
|
||||
currency: str = Field(default="USD")
|
||||
price_per_region: Dict[str, float] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
dynamic_pricing_enabled: bool = Field(default=False)
|
||||
|
||||
# Availability
|
||||
total_capacity: int = Field(default=0)
|
||||
available_capacity: int = Field(default=0)
|
||||
regions_available: List[str] = Field(default_factory=list, sa_column=Column(JSON))
|
||||
|
||||
# Global status
|
||||
global_status: MarketplaceStatus = Field(default=MarketplaceStatus.ACTIVE)
|
||||
region_statuses: Dict[str, MarketplaceStatus] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Quality metrics
|
||||
global_rating: float = Field(default=0.0, ge=0.0, le=5.0)
|
||||
total_transactions: int = Field(default=0)
|
||||
success_rate: float = Field(default=0.0, ge=0.0, le=1.0)
|
||||
|
||||
# Cross-chain support
|
||||
supported_chains: List[int] = Field(default_factory=list, sa_column=Column(JSON))
|
||||
cross_chain_pricing: Dict[int, float] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Timestamps
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
expires_at: Optional[datetime] = Field(default=None)
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_global_offer_agent', 'agent_id'),
|
||||
Index('idx_global_offer_service', 'service_type'),
|
||||
Index('idx_global_offer_status', 'global_status'),
|
||||
Index('idx_global_offer_created', 'created_at'),
|
||||
)
|
||||
|
||||
|
||||
class GlobalMarketplaceTransaction(SQLModel, table=True):
|
||||
"""Global marketplace transaction with cross-chain support"""
|
||||
|
||||
__tablename__ = "global_marketplace_transactions"
|
||||
__table_args__ = {"extend_existing": True}
|
||||
|
||||
id: str = Field(default_factory=lambda: f"tx_{uuid4().hex[:8]}", primary_key=True)
|
||||
transaction_hash: Optional[str] = Field(index=True)
|
||||
|
||||
# Transaction participants
|
||||
buyer_id: str = Field(index=True)
|
||||
seller_id: str = Field(index=True)
|
||||
offer_id: str = Field(index=True)
|
||||
|
||||
# Transaction details
|
||||
service_type: str = Field(index=True)
|
||||
quantity: int = Field(default=1)
|
||||
unit_price: float = Field(default=0.0)
|
||||
total_amount: float = Field(default=0.0)
|
||||
currency: str = Field(default="USD")
|
||||
|
||||
# Cross-chain information
|
||||
source_chain: Optional[int] = Field(default=None)
|
||||
target_chain: Optional[int] = Field(default=None)
|
||||
bridge_transaction_id: Optional[str] = Field(default=None)
|
||||
cross_chain_fee: float = Field(default=0.0)
|
||||
|
||||
# Regional information
|
||||
source_region: str = Field(default="global")
|
||||
target_region: str = Field(default="global")
|
||||
regional_fees: Dict[str, float] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Transaction status
|
||||
status: str = Field(default="pending") # pending, confirmed, completed, failed, cancelled
|
||||
payment_status: str = Field(default="pending") # pending, paid, refunded
|
||||
delivery_status: str = Field(default="pending") # pending, delivered, failed
|
||||
|
||||
# Timestamps
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
confirmed_at: Optional[datetime] = Field(default=None)
|
||||
completed_at: Optional[datetime] = Field(default=None)
|
||||
|
||||
# Transaction metadata
|
||||
transaction_data: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_global_tx_buyer', 'buyer_id'),
|
||||
Index('idx_global_tx_seller', 'seller_id'),
|
||||
Index('idx_global_tx_offer', 'offer_id'),
|
||||
Index('idx_global_tx_status', 'status'),
|
||||
Index('idx_global_tx_created', 'created_at'),
|
||||
Index('idx_global_tx_chain', 'source_chain', 'target_chain'),
|
||||
)
|
||||
|
||||
|
||||
class GlobalMarketplaceAnalytics(SQLModel, table=True):
|
||||
"""Global marketplace analytics and metrics"""
|
||||
|
||||
__tablename__ = "global_marketplace_analytics"
|
||||
__table_args__ = {"extend_existing": True}
|
||||
|
||||
id: str = Field(default_factory=lambda: f"analytics_{uuid4().hex[:8]}", primary_key=True)
|
||||
|
||||
# Analytics period
|
||||
period_type: str = Field(default="hourly") # hourly, daily, weekly, monthly
|
||||
period_start: datetime = Field(index=True)
|
||||
period_end: datetime = Field(index=True)
|
||||
region: Optional[str] = Field(default="global", index=True)
|
||||
|
||||
# Marketplace metrics
|
||||
total_offers: int = Field(default=0)
|
||||
total_transactions: int = Field(default=0)
|
||||
total_volume: float = Field(default=0.0)
|
||||
average_price: float = Field(default=0.0)
|
||||
|
||||
# Performance metrics
|
||||
average_response_time: float = Field(default=0.0)
|
||||
success_rate: float = Field(default=0.0)
|
||||
error_rate: float = Field(default=0.0)
|
||||
|
||||
# User metrics
|
||||
active_buyers: int = Field(default=0)
|
||||
active_sellers: int = Field(default=0)
|
||||
new_users: int = Field(default=0)
|
||||
|
||||
# Cross-chain metrics
|
||||
cross_chain_transactions: int = Field(default=0)
|
||||
cross_chain_volume: float = Field(default=0.0)
|
||||
supported_chains: List[int] = Field(default_factory=list, sa_column=Column(JSON))
|
||||
|
||||
# Regional metrics
|
||||
regional_distribution: Dict[str, int] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
regional_performance: Dict[str, float] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Additional analytics data
|
||||
analytics_data: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Timestamps
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_global_analytics_period', 'period_type', 'period_start'),
|
||||
Index('idx_global_analytics_region', 'region'),
|
||||
Index('idx_global_analytics_created', 'created_at'),
|
||||
)
|
||||
|
||||
|
||||
class GlobalMarketplaceGovernance(SQLModel, table=True):
|
||||
"""Global marketplace governance and rules"""
|
||||
|
||||
__tablename__ = "global_marketplace_governance"
|
||||
__table_args__ = {"extend_existing": True}
|
||||
|
||||
id: str = Field(default_factory=lambda: f"gov_{uuid4().hex[:8]}", primary_key=True)
|
||||
|
||||
# Governance rule
|
||||
rule_type: str = Field(index=True) # pricing, security, compliance, quality
|
||||
rule_name: str = Field(index=True)
|
||||
rule_description: str = Field(default="")
|
||||
|
||||
# Rule configuration
|
||||
rule_parameters: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
conditions: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Scope and applicability
|
||||
global_scope: bool = Field(default=True)
|
||||
applicable_regions: List[str] = Field(default_factory=list, sa_column=Column(JSON))
|
||||
applicable_services: List[str] = Field(default_factory=list, sa_column=Column(JSON))
|
||||
|
||||
# Enforcement
|
||||
is_active: bool = Field(default=True)
|
||||
enforcement_level: str = Field(default="warning") # warning, restriction, ban
|
||||
penalty_parameters: Dict[str, Any] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
|
||||
# Governance metadata
|
||||
created_by: str = Field(default="")
|
||||
approved_by: Optional[str] = Field(default=None)
|
||||
version: int = Field(default=1)
|
||||
|
||||
# Timestamps
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
effective_from: datetime = Field(default_factory=datetime.utcnow)
|
||||
expires_at: Optional[datetime] = Field(default=None)
|
||||
|
||||
# Indexes
|
||||
__table_args__ = (
|
||||
Index('idx_global_gov_rule_type', 'rule_type'),
|
||||
Index('idx_global_gov_active', 'is_active'),
|
||||
Index('idx_global_gov_effective', 'effective_from', 'expires_at'),
|
||||
)
|
||||
|
||||
|
||||
# Request/Response Models for API
|
||||
class GlobalMarketplaceOfferRequest(SQLModel):
|
||||
"""Request model for creating global marketplace offers"""
|
||||
agent_id: str
|
||||
service_type: str
|
||||
resource_specification: Dict[str, Any]
|
||||
base_price: float
|
||||
currency: str = "USD"
|
||||
total_capacity: int
|
||||
regions_available: List[str] = []
|
||||
supported_chains: List[int] = []
|
||||
dynamic_pricing_enabled: bool = False
|
||||
expires_at: Optional[datetime] = None
|
||||
|
||||
|
||||
class GlobalMarketplaceTransactionRequest(SQLModel):
|
||||
"""Request model for creating global marketplace transactions"""
|
||||
buyer_id: str
|
||||
offer_id: str
|
||||
quantity: int = 1
|
||||
source_region: str = "global"
|
||||
target_region: str = "global"
|
||||
payment_method: str = "crypto"
|
||||
source_chain: Optional[int] = None
|
||||
target_chain: Optional[int] = None
|
||||
|
||||
|
||||
class GlobalMarketplaceAnalyticsRequest(SQLModel):
|
||||
"""Request model for global marketplace analytics"""
|
||||
period_type: str = "daily"
|
||||
start_date: datetime
|
||||
end_date: datetime
|
||||
region: Optional[str] = "global"
|
||||
metrics: List[str] = []
|
||||
include_cross_chain: bool = False
|
||||
include_regional: bool = False
|
||||
|
||||
|
||||
# Response Models
|
||||
class GlobalMarketplaceOfferResponse(SQLModel):
|
||||
"""Response model for global marketplace offers"""
|
||||
id: str
|
||||
agent_id: str
|
||||
service_type: str
|
||||
resource_specification: Dict[str, Any]
|
||||
base_price: float
|
||||
currency: str
|
||||
price_per_region: Dict[str, float]
|
||||
total_capacity: int
|
||||
available_capacity: int
|
||||
regions_available: List[str]
|
||||
global_status: MarketplaceStatus
|
||||
global_rating: float
|
||||
total_transactions: int
|
||||
success_rate: float
|
||||
supported_chains: List[int]
|
||||
cross_chain_pricing: Dict[int, float]
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
expires_at: Optional[datetime]
|
||||
|
||||
|
||||
class GlobalMarketplaceTransactionResponse(SQLModel):
|
||||
"""Response model for global marketplace transactions"""
|
||||
id: str
|
||||
transaction_hash: Optional[str]
|
||||
buyer_id: str
|
||||
seller_id: str
|
||||
offer_id: str
|
||||
service_type: str
|
||||
quantity: int
|
||||
unit_price: float
|
||||
total_amount: float
|
||||
currency: str
|
||||
source_chain: Optional[int]
|
||||
target_chain: Optional[int]
|
||||
cross_chain_fee: float
|
||||
source_region: str
|
||||
target_region: str
|
||||
status: str
|
||||
payment_status: str
|
||||
delivery_status: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
confirmed_at: Optional[datetime]
|
||||
completed_at: Optional[datetime]
|
||||
|
||||
|
||||
class GlobalMarketplaceAnalyticsResponse(SQLModel):
|
||||
"""Response model for global marketplace analytics"""
|
||||
period_type: str
|
||||
period_start: datetime
|
||||
period_end: datetime
|
||||
region: str
|
||||
total_offers: int
|
||||
total_transactions: int
|
||||
total_volume: float
|
||||
average_price: float
|
||||
average_response_time: float
|
||||
success_rate: float
|
||||
active_buyers: int
|
||||
active_sellers: int
|
||||
cross_chain_transactions: int
|
||||
cross_chain_volume: float
|
||||
regional_distribution: Dict[str, int]
|
||||
regional_performance: Dict[str, float]
|
||||
generated_at: datetime
|
||||
107
apps/coordinator-api/src/app/domain/wallet.py
Normal file
107
apps/coordinator-api/src/app/domain/wallet.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""
|
||||
Multi-Chain Wallet Integration Domain Models
|
||||
|
||||
Domain models for managing agent wallets across multiple blockchain networks.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Dict, List, Optional
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import Column, JSON
|
||||
from sqlmodel import Field, SQLModel, Relationship
|
||||
|
||||
class WalletType(str, Enum):
|
||||
EOA = "eoa" # Externally Owned Account
|
||||
SMART_CONTRACT = "smart_contract" # Smart Contract Wallet (e.g. Safe)
|
||||
MULTI_SIG = "multi_sig" # Multi-Signature Wallet
|
||||
MPC = "mpc" # Multi-Party Computation Wallet
|
||||
|
||||
class NetworkType(str, Enum):
|
||||
EVM = "evm"
|
||||
SOLANA = "solana"
|
||||
APTOS = "aptos"
|
||||
SUI = "sui"
|
||||
|
||||
class AgentWallet(SQLModel, table=True):
|
||||
"""Represents a wallet owned by an AI agent"""
|
||||
__tablename__ = "agent_wallet"
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
agent_id: str = Field(index=True)
|
||||
address: str = Field(index=True)
|
||||
public_key: str = Field()
|
||||
wallet_type: WalletType = Field(default=WalletType.EOA, index=True)
|
||||
is_active: bool = Field(default=True)
|
||||
encrypted_private_key: Optional[str] = Field(default=None) # Only if managed internally
|
||||
kms_key_id: Optional[str] = Field(default=None) # Reference to external KMS
|
||||
metadata: Dict[str, str] = Field(default_factory=dict, sa_column=Column(JSON))
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
balances: List["TokenBalance"] = Relationship(back_populates="wallet")
|
||||
transactions: List["WalletTransaction"] = Relationship(back_populates="wallet")
|
||||
|
||||
class NetworkConfig(SQLModel, table=True):
|
||||
"""Configuration for supported blockchain networks"""
|
||||
__tablename__ = "wallet_network_config"
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
chain_id: int = Field(index=True, unique=True)
|
||||
name: str = Field(index=True)
|
||||
network_type: NetworkType = Field(default=NetworkType.EVM)
|
||||
rpc_url: str = Field()
|
||||
ws_url: Optional[str] = Field(default=None)
|
||||
explorer_url: str = Field()
|
||||
native_currency_symbol: str = Field()
|
||||
native_currency_decimals: int = Field(default=18)
|
||||
is_testnet: bool = Field(default=False, index=True)
|
||||
is_active: bool = Field(default=True)
|
||||
|
||||
class TokenBalance(SQLModel, table=True):
|
||||
"""Tracks token balances for agent wallets across networks"""
|
||||
__tablename__ = "token_balance"
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
wallet_id: int = Field(foreign_key="agent_wallet.id", index=True)
|
||||
chain_id: int = Field(foreign_key="wallet_network_config.chain_id", index=True)
|
||||
token_address: str = Field(index=True) # "native" for native currency
|
||||
token_symbol: str = Field()
|
||||
balance: float = Field(default=0.0)
|
||||
last_updated: datetime = Field(default_factory=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
wallet: AgentWallet = Relationship(back_populates="balances")
|
||||
|
||||
class TransactionStatus(str, Enum):
|
||||
PENDING = "pending"
|
||||
SUBMITTED = "submitted"
|
||||
CONFIRMED = "confirmed"
|
||||
FAILED = "failed"
|
||||
DROPPED = "dropped"
|
||||
|
||||
class WalletTransaction(SQLModel, table=True):
|
||||
"""Record of transactions executed by agent wallets"""
|
||||
__tablename__ = "wallet_transaction"
|
||||
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
wallet_id: int = Field(foreign_key="agent_wallet.id", index=True)
|
||||
chain_id: int = Field(foreign_key="wallet_network_config.chain_id", index=True)
|
||||
tx_hash: Optional[str] = Field(default=None, index=True)
|
||||
to_address: str = Field(index=True)
|
||||
value: float = Field(default=0.0)
|
||||
data: Optional[str] = Field(default=None)
|
||||
gas_limit: Optional[int] = Field(default=None)
|
||||
gas_price: Optional[float] = Field(default=None)
|
||||
nonce: Optional[int] = Field(default=None)
|
||||
status: TransactionStatus = Field(default=TransactionStatus.PENDING, index=True)
|
||||
error_message: Optional[str] = Field(default=None)
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
updated_at: datetime = Field(default_factory=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
wallet: AgentWallet = Relationship(back_populates="transactions")
|
||||
Reference in New Issue
Block a user