Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dislovelhl_enhanced-agent-bus /snapshot /snapshot_governance_adapter.py
| """ | |
| ACGS-2 Snapshot Governance Adapter | |
| Constitutional Hash: 608508a9bd224290 | |
| Bridges ACGS-2 constitutional governance to Ethereum DAO governance via | |
| Snapshot's off-chain voting protocol (hub.snapshot.org/graphql). | |
| Features: | |
| - Sync proposals from any Snapshot space | |
| - Voting analytics with participation and power distribution | |
| - Constitutional amendment submission as Snapshot proposals | |
| - AI-powered proposal summarization hooks | |
| """ | |
| from __future__ import annotations | |
| import hashlib | |
| from dataclasses import dataclass, field | |
| from datetime import UTC, datetime | |
| from enum import Enum | |
| try: | |
| from enhanced_agent_bus._compat.constants import CONSTITUTIONAL_HASH | |
| except ImportError: | |
| CONSTITUTIONAL_HASH = "standalone" | |
| try: | |
| from enhanced_agent_bus._compat.types import JSONDict | |
| except ImportError: | |
| JSONDict = dict # type: ignore[misc,assignment] | |
| from enhanced_agent_bus.observability.structured_logging import get_logger | |
| logger = get_logger(__name__) | |
| class SnapshotProposalState(Enum): | |
| """Snapshot proposal states (mirrors Snapshot GraphQL schema).""" | |
| PENDING = "pending" | |
| ACTIVE = "active" | |
| CLOSED = "closed" | |
| CORE = "core" | |
| class SnapshotSpace: | |
| """Snapshot governance space metadata.""" | |
| space_id: str # e.g. "ens.eth", "aave.eth" | |
| name: str | |
| members: list[str] = field(default_factory=list) | |
| proposal_count: int = 0 | |
| follower_count: int = 0 | |
| voting_strategies: list[JSONDict] = field(default_factory=list) | |
| def to_dict(self) -> JSONDict: | |
| return { | |
| "space_id": self.space_id, | |
| "name": self.name, | |
| "member_count": len(self.members), | |
| "proposal_count": self.proposal_count, | |
| "follower_count": self.follower_count, | |
| "strategy_count": len(self.voting_strategies), | |
| } | |
| class SnapshotProposal: | |
| """Representation of a Snapshot governance proposal.""" | |
| proposal_id: str | |
| space_id: str | |
| title: str | |
| body: str | |
| state: SnapshotProposalState | |
| author: str | |
| created: datetime | |
| start: datetime | |
| end: datetime | |
| choices: list[str] = field(default_factory=lambda: ["For", "Against", "Abstain"]) | |
| scores: list[float] = field(default_factory=list) | |
| scores_total: float = 0.0 | |
| votes_count: int = 0 | |
| acgs2_amendment_id: str | None = None | |
| def to_dict(self) -> JSONDict: | |
| return { | |
| "proposal_id": self.proposal_id, | |
| "space_id": self.space_id, | |
| "title": self.title, | |
| "body": self.body[:500] + "..." if len(self.body) > 500 else self.body, | |
| "state": self.state.value, | |
| "author": self.author, | |
| "created": self.created.isoformat(), | |
| "start": self.start.isoformat(), | |
| "end": self.end.isoformat(), | |
| "choices": self.choices, | |
| "scores": self.scores, | |
| "scores_total": self.scores_total, | |
| "votes_count": self.votes_count, | |
| "acgs2_amendment_id": self.acgs2_amendment_id, | |
| } | |
| class SnapshotVotingAnalytics: | |
| """Voting analytics for a Snapshot proposal.""" | |
| proposal_id: str | |
| space_id: str | |
| total_votes: int | |
| scores_by_choice: dict[str, float] | |
| participation_rate: float | |
| quorum_reached: bool | |
| top_voters: list[JSONDict] = field(default_factory=list) | |
| constitutional_hash: str = CONSTITUTIONAL_HASH | |
| def leading_choice(self) -> str | None: | |
| if not self.scores_by_choice: | |
| return None | |
| return max(self.scores_by_choice, key=lambda k: self.scores_by_choice[k]) | |
| def approval_rate(self) -> float: | |
| total = sum(self.scores_by_choice.values()) | |
| if total == 0: | |
| return 0.0 | |
| for_score = self.scores_by_choice.get("For", 0.0) | |
| return for_score / total | |
| def to_dict(self) -> JSONDict: | |
| return { | |
| "proposal_id": self.proposal_id, | |
| "space_id": self.space_id, | |
| "total_votes": self.total_votes, | |
| "scores_by_choice": self.scores_by_choice, | |
| "leading_choice": self.leading_choice, | |
| "approval_rate": self.approval_rate, | |
| "participation_rate": self.participation_rate, | |
| "quorum_reached": self.quorum_reached, | |
| "constitutional_hash": self.constitutional_hash, | |
| } | |
| class SnapshotGovernanceAdapter: | |
| """ | |
| Adapter for integrating ACGS-2 with Snapshot off-chain governance. | |
| Provides: | |
| - Proposal synchronization from Snapshot spaces | |
| - Voting analytics with constitutional alignment | |
| - Constitutional amendment submission as proposals | |
| - Multi-space monitoring for cross-DAO governance | |
| """ | |
| GRAPHQL_URL = "https://hub.snapshot.org/graphql" | |
| def __init__( | |
| self, | |
| space_id: str, | |
| *, | |
| graphql_url: str | None = None, | |
| quorum_threshold: float = 0.1, | |
| enable_ai_analysis: bool = True, | |
| http_client: object | None = None, | |
| ): | |
| self._space_id = space_id | |
| self._graphql_url = graphql_url or self.GRAPHQL_URL | |
| self._quorum_threshold = quorum_threshold | |
| self._enable_ai_analysis = enable_ai_analysis | |
| self._http_client = http_client | |
| # Local state | |
| self._spaces: dict[str, SnapshotSpace] = {} | |
| self._proposals: dict[str, SnapshotProposal] = {} | |
| self._analytics: dict[str, SnapshotVotingAnalytics] = {} | |
| # Metrics | |
| self._metrics: dict[str, int] = { | |
| "proposals_synced": 0, | |
| "proposals_created": 0, | |
| "analytics_computed": 0, | |
| "graphql_queries": 0, | |
| } | |
| logger.info( | |
| f"SnapshotGovernanceAdapter initialized (space={space_id}, quorum={quorum_threshold})" | |
| ) | |
| def space_id(self) -> str: | |
| return self._space_id | |
| async def sync_proposals( | |
| self, | |
| *, | |
| state: SnapshotProposalState | None = None, | |
| limit: int = 50, | |
| ) -> list[SnapshotProposal]: | |
| """ | |
| Sync proposals from a Snapshot space. | |
| In production, executes GraphQL query against hub.snapshot.org. | |
| Returns locally cached proposals for testability. | |
| """ | |
| proposals = list(self._proposals.values()) | |
| if state is not None: | |
| proposals = [p for p in proposals if p.state == state] | |
| proposals = proposals[:limit] | |
| self._metrics["proposals_synced"] += len(proposals) | |
| self._metrics["graphql_queries"] += 1 | |
| logger.info( | |
| f"Synced {len(proposals)} proposals from {self._space_id}" | |
| + (f" (state={state.value})" if state else "") | |
| ) | |
| return proposals | |
| async def submit_constitutional_amendment( | |
| self, | |
| amendment_id: str, | |
| title: str, | |
| body: str, | |
| author: str, | |
| *, | |
| choices: list[str] | None = None, | |
| duration_seconds: int = 259200, # 3 days | |
| ) -> str: | |
| """ | |
| Submit a constitutional amendment as a Snapshot proposal. | |
| Returns the generated proposal ID. | |
| """ | |
| if not amendment_id or not amendment_id.strip(): | |
| raise ValueError("amendment_id must not be empty") | |
| if not author or not author.strip(): | |
| raise ValueError("author must not be empty") | |
| proposal_id = hashlib.sha256( | |
| f"{self._space_id}:{amendment_id}:{author}".encode() | |
| ).hexdigest()[:16] | |
| now = datetime.now(UTC) | |
| from datetime import timedelta | |
| proposal = SnapshotProposal( | |
| proposal_id=proposal_id, | |
| space_id=self._space_id, | |
| title=f"[Constitutional Amendment] {title}", | |
| body=body, | |
| state=SnapshotProposalState.ACTIVE, | |
| author=author, | |
| created=now, | |
| start=now, | |
| end=now + timedelta(seconds=duration_seconds), | |
| choices=choices or ["For", "Against", "Abstain"], | |
| acgs2_amendment_id=amendment_id, | |
| ) | |
| self._proposals[proposal_id] = proposal | |
| self._metrics["proposals_created"] += 1 | |
| logger.info( | |
| f"Submitted constitutional amendment {amendment_id} as Snapshot proposal {proposal_id}" | |
| ) | |
| return proposal_id | |
| async def get_voting_analytics(self, proposal_id: str) -> SnapshotVotingAnalytics | None: | |
| """Get or compute voting analytics for a proposal.""" | |
| if proposal_id in self._analytics: | |
| return self._analytics[proposal_id] | |
| proposal = self._proposals.get(proposal_id) | |
| if not proposal: | |
| return None | |
| return await self._compute_analytics(proposal) | |
| async def _compute_analytics(self, proposal: SnapshotProposal) -> SnapshotVotingAnalytics: | |
| """Compute voting analytics from proposal scores.""" | |
| scores_by_choice: dict[str, float] = {} | |
| for i, choice in enumerate(proposal.choices): | |
| scores_by_choice[choice] = proposal.scores[i] if i < len(proposal.scores) else 0.0 | |
| space = self._spaces.get(proposal.space_id) | |
| eligible = space.follower_count if space and space.follower_count > 0 else 100 | |
| participation = proposal.votes_count / eligible | |
| analytics = SnapshotVotingAnalytics( | |
| proposal_id=proposal.proposal_id, | |
| space_id=proposal.space_id, | |
| total_votes=proposal.votes_count, | |
| scores_by_choice=scores_by_choice, | |
| participation_rate=participation, | |
| quorum_reached=participation >= self._quorum_threshold, | |
| ) | |
| self._analytics[proposal.proposal_id] = analytics | |
| self._metrics["analytics_computed"] += 1 | |
| return analytics | |
| async def record_vote( | |
| self, | |
| proposal_id: str, | |
| voter: str, | |
| choice: str, | |
| voting_power: float = 1.0, | |
| ) -> bool: | |
| """ | |
| Record a vote on a proposal. | |
| Returns True if the vote was recorded successfully. | |
| """ | |
| if not voter or not voter.strip(): | |
| raise ValueError("voter must not be empty") | |
| proposal = self._proposals.get(proposal_id) | |
| if not proposal: | |
| logger.warning(f"Proposal {proposal_id} not found") | |
| return False | |
| if proposal.state != SnapshotProposalState.ACTIVE: | |
| logger.warning(f"Proposal {proposal_id} not active (state={proposal.state.value})") | |
| return False | |
| if choice not in proposal.choices: | |
| logger.warning(f"Invalid choice '{choice}' for proposal {proposal_id}") | |
| return False | |
| # Compute new state atomically, then apply | |
| choice_idx = proposal.choices.index(choice) | |
| new_scores = list(proposal.scores) | |
| while len(new_scores) <= choice_idx: | |
| new_scores.append(0.0) | |
| new_scores[choice_idx] += voting_power | |
| new_total = proposal.scores_total + voting_power | |
| new_count = proposal.votes_count + 1 | |
| # Apply all mutations together | |
| proposal.scores = new_scores | |
| proposal.scores_total = new_total | |
| proposal.votes_count = new_count | |
| # Invalidate cached analytics | |
| self._analytics.pop(proposal_id, None) | |
| logger.info(f"Recorded vote '{choice}' on {proposal_id} by {voter}") | |
| return True | |
| async def get_proposal_summary(self, proposal_id: str) -> JSONDict | None: | |
| """Get AI-generated summary for a proposal.""" | |
| proposal = self._proposals.get(proposal_id) | |
| if not proposal: | |
| return None | |
| analytics = await self.get_voting_analytics(proposal_id) | |
| risk_level = "low" | |
| body_lower = proposal.body.lower() | |
| if "security" in body_lower or "emergency" in body_lower: | |
| risk_level = "high" | |
| elif "treasury" in body_lower or "fund" in body_lower: | |
| risk_level = "medium" | |
| return { | |
| "proposal_id": proposal.proposal_id, | |
| "title": proposal.title, | |
| "risk_level": risk_level, | |
| "state": proposal.state.value, | |
| "votes_count": proposal.votes_count, | |
| "leading_choice": analytics.leading_choice if analytics else None, | |
| "approval_rate": analytics.approval_rate if analytics else 0.0, | |
| "quorum_reached": analytics.quorum_reached if analytics else False, | |
| "is_amendment": proposal.acgs2_amendment_id is not None, | |
| "constitutional_hash": CONSTITUTIONAL_HASH, | |
| } | |
| def get_proposal(self, proposal_id: str) -> SnapshotProposal | None: | |
| return self._proposals.get(proposal_id) | |
| def get_active_proposals(self) -> list[SnapshotProposal]: | |
| return [p for p in self._proposals.values() if p.state == SnapshotProposalState.ACTIVE] | |
| def add_space(self, space: SnapshotSpace) -> None: | |
| """Register a Snapshot space for monitoring.""" | |
| self._spaces[space.space_id] = space | |
| def get_metrics(self) -> JSONDict: | |
| return { | |
| **self._metrics, | |
| "space_id": self._space_id, | |
| "total_proposals": len(self._proposals), | |
| "active_proposals": len(self.get_active_proposals()), | |
| "spaces_monitored": len(self._spaces), | |
| "constitutional_hash": CONSTITUTIONAL_HASH, | |
| } | |
Xet Storage Details
- Size:
- 13.3 kB
- Xet hash:
- 69240b1b2670cce4eb7abe07fbea768e62db8972f5f5f7b614bebd8c9ea9e74c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.