voteweight.gno
1.71 Kb · 55 lines
1package kourt
2func votingWeight(c *Court, who address, at uint32) (w, snapshot int64) {
3 snapshot = c.coin.PastVotes(who, at)
4 w = snapshot
5 if held := c.coin.BalanceOf(who); held < w {
6 w = held
7 }
8 if w < 0 {
9 w = 0
10 }
11 return w, snapshot
12}
13func voteCap(c *Court, who address) int64 { return c.coin.BalanceOf(who) }
14func ClaimVoteWeightOf(courtSlug string, who address, claimID uint64) (verdict, quality int64) {
15 c := mustCourt(courtSlug)
16 cs := mustClaim(c, claimID)
17 if cs.disputeOpen {
18 verdict, _ = votingWeight(c, who, c.gov.EpochOf(cs.proposalID))
19 }
20 return verdict, quality
21}
22func ElectionVoteWeightOf(courtSlug string, who address) int64 {
23 c := mustCourt(courtSlug)
24 if c.mod == nil || c.mod.election == nil || c.mod.election.resolved {
25 return 0
26 }
27 e := c.mod.election
28 if prior := e.voters.Get(who.String()); prior != nil {
29 return prior.(int64)
30 }
31 w, _ := votingWeight(c, who, e.epoch)
32 return w
33}
34func VoteWeightWhy(courtSlug string, who address, claimID uint64) (verdict, quality string) {
35 c := mustCourt(courtSlug)
36 cs := mustClaim(c, claimID)
37 verdict = "no verdict vote is open on this claim"
38 if cs.disputeOpen {
39 verdict = whyWeighed(c, who, c.gov.EpochOf(cs.proposalID))
40 }
41 quality = "the quality vote no longer exists"
42 return verdict, quality
43}
44func whyWeighed(c *Court, who address, at uint32) string {
45 w, snapshot := votingWeight(c, who, at)
46 switch {
47 case snapshot <= 0:
48 return "you had none when this vote started; coin bought since counts in later votes"
49 case w <= 0:
50 return "you had some when this vote started but have none now, and your say is the smaller of the two"
51 case w < snapshot:
52 return "limited to what you still hold, which is less than when this vote started"
53 }
54 return "counted in full"
55}