clawchain/x/governance/keeper/query_tally.go
caelum0x 9e2746707a feat(governance): complete governance module with veto votes, cancel, queries, and execution log
- Task 1.1/1.2: Add no_with_veto vote option with 33.4% veto threshold
  - New VoteOptionNoWithVeto constant and DefaultVetoThresholdBps (3340)
  - VetoVotes field on Proposal struct (proto field 15)
  - CastVote handles no_with_veto; TallyProposal rejects if veto > 33.4%

- Task 1.3: Add MsgCancelProposal
  - New ErrUnauthorizedCancel (1411) and ErrProposalNotCancellable (1412)
  - ProposalStatusCancelled constant
  - CancelProposal keeper method: checks proposer, voting status, refunds deposit

- Task 1.4: Add QueryTallyResult and QueryVoterVotes
  - QueryTallyResult returns counts, percentages (bps), pass/veto status
  - QueryVoterVotes walks Votes collection filtered by voter address

- Task 1.5: ParamExecutor coverage for marketplace, reputation, messaging, modelregistry
  - All 4 modules tested via mock executor registration + ExecuteProposal

- Task 1.6: Execution log fields
  - ExecutionHeight (proto field 16) and ExecutionError (proto field 17) on Proposal
  - ExecuteProposal sets these on both success and failure paths

All 24 tests pass (5 existing + 19 new), go build ./... clean.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 14:24:34 +03:00

70 lines
2.3 KiB
Go

package keeper
import (
"context"
"cosmossdk.io/math"
"clawchain/x/governance/types"
)
// TallyResult holds the result of tallying a proposal's votes.
type TallyResult struct {
YesVotes math.Int `json:"yes_votes"`
NoVotes math.Int `json:"no_votes"`
AbstainVotes math.Int `json:"abstain_votes"`
VetoVotes math.Int `json:"veto_votes"`
TotalVotes math.Int `json:"total_votes"`
YesPercentBps int64 `json:"yes_percent_bps"` // basis points (0-10000)
NoPercentBps int64 `json:"no_percent_bps"`
AbstainPercentBps int64 `json:"abstain_percent_bps"`
VetoPercentBps int64 `json:"veto_percent_bps"`
Passed bool `json:"passed"`
Vetoed bool `json:"vetoed"`
}
// QueryTallyResult returns the tally result for a proposal including
// yes/no/abstain/veto counts, percentages, and pass/veto status.
func (k Keeper) QueryTallyResult(ctx context.Context, proposalID uint64) (*TallyResult, error) {
proposal, err := k.GetProposal(ctx, proposalID)
if err != nil {
return nil, err
}
totalVotes := proposal.YesVotes.Add(proposal.NoVotes).Add(proposal.AbstainVotes).Add(proposal.VetoVotes)
result := &TallyResult{
YesVotes: proposal.YesVotes,
NoVotes: proposal.NoVotes,
AbstainVotes: proposal.AbstainVotes,
VetoVotes: proposal.VetoVotes,
TotalVotes: totalVotes,
}
if totalVotes.IsPositive() {
bps := math.NewInt(10000)
result.YesPercentBps = proposal.YesVotes.Mul(bps).Quo(totalVotes).Int64()
result.NoPercentBps = proposal.NoVotes.Mul(bps).Quo(totalVotes).Int64()
result.AbstainPercentBps = proposal.AbstainVotes.Mul(bps).Quo(totalVotes).Int64()
result.VetoPercentBps = proposal.VetoVotes.Mul(bps).Quo(totalVotes).Int64()
// Check veto: veto_votes * 10000 > total_votes * DefaultVetoThresholdBps
if proposal.VetoVotes.IsPositive() {
vetoScaled := proposal.VetoVotes.Mul(math.NewInt(10000))
totalScaled := totalVotes.Mul(math.NewInt(types.DefaultVetoThresholdBps))
result.Vetoed = vetoScaled.GT(totalScaled)
}
// Check pass threshold: yes > 50% of (yes + no)
if !result.Vetoed {
yesAndNo := proposal.YesVotes.Add(proposal.NoVotes)
if yesAndNo.IsPositive() {
threshold := yesAndNo.Mul(math.NewInt(types.DefaultThresholdBps)).Quo(bps)
result.Passed = proposal.YesVotes.GT(threshold)
}
}
}
return result, nil
}