Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

views.gno

4.67 Kb · 143 lines
  1package pool
  2
  3import (
  4	"strings"
  5
  6	mimir "gno.land/p/g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz/mimir"
  7)
  8
  9func MarketCount() int64          { return marketCount }
 10func Owner() address              { return roles.Owner }
 11func Arbiter() address            { return roles.Arbiter }
 12func Oracle() address             { return roles.Oracle }
 13func AuditAgent() address         { return roles.AuditAgent }
 14func FeeRecipient() address       { return roles.FeeRecipient }
 15func Paused() bool                { return roles.Paused }
 16func DisputeWindow() int64        { return disputeWindow }
 17func AccruedFees(a address) int64 { return feesOf(a) }
 18
 19// SideTotals are the pool sizes: the implied probability of A is totalA / (totalA + totalB).
 20func SideTotals(id int64) (int64, int64) {
 21	m := getMarket(id)
 22	return m.TotalA, m.TotalB
 23}
 24
 25func StakeOf(id int64, user address) (int64, int64) {
 26	p := getMarket(id).position(user)
 27	if p == nil {
 28		return 0, 0
 29	}
 30	return p.A, p.B
 31}
 32
 33// EligibleTotals are the side totals that compete under the settlement cutoff.
 34func EligibleTotals(id int64) (int64, int64) {
 35	m := getMarket(id)
 36	t := m.history.Before(m.SettlementCutoff)
 37	return t.A, t.B
 38}
 39
 40// Claimable is what ClaimFor would pay `user` now (net) and the copy fees it
 41// would take. 0 if unresolved, already claimed or nothing owed.
 42func Claimable(id int64, user address) (payout, fee int64) {
 43	m := getMarket(id)
 44	p := m.position(user)
 45	if m.State != StResolved || p == nil || p.Claimed {
 46		return 0, 0
 47	}
 48	gross, feeBase := owed(m, p)
 49	toRef, toPlatform := copyFees(user, p, feeBase)
 50	fee = toRef + toPlatform
 51	return gross - fee, fee
 52}
 53
 54func ConfigJSON() string {
 55	return "{" + strings.Join([]string{
 56		kv("owner", mimir.Quote(roles.Owner.String())),
 57		kv("arbiter", mimir.Quote(roles.Arbiter.String())),
 58		kv("oracle", mimir.Quote(roles.Oracle.String())),
 59		kv("auditAgent", mimir.Quote(roles.AuditAgent.String())),
 60		kv("feeRecipient", mimir.Quote(roles.FeeRecipient.String())),
 61		kv("paused", boolStr(roles.Paused)),
 62		kv("disputeWindow", itoa(disputeWindow)),
 63		kv("minStake", itoa(MinStake)),
 64		kv("disputeBond", itoa(DisputeBond)),
 65		kv("lockSeconds", itoa(LockSeconds)),
 66		kv("graceSeconds", itoa(ResolutionGraceSeconds)),
 67		kv("maxMarketStake", itoa(roles.MaxMarketStake)),
 68		kv("maxAccountStake", itoa(roles.MaxAccountStake)),
 69		kv("marketCount", itoa(marketCount)),
 70		kv("lifetimeFeesAccrued", itoa(lifetimeFeesAccrued)),
 71		kv("lifetimeFeesClaimed", itoa(lifetimeFeesClaimed)),
 72		kv("now", itoa(now())),
 73	}, ",") + "}"
 74}
 75
 76func MarketJSON(id int64) string { return marketJSON(getMarket(id)) }
 77
 78// MarketsJSON lists markets newest first: `offset` skips that many from the newest.
 79func MarketsJSON(offset, limit int) string {
 80	if limit <= 0 || limit > 100 {
 81		limit = 100
 82	}
 83	if offset < 0 {
 84		offset = 0
 85	}
 86	var out []string
 87	markets.ReverseIterateByOffset(offset, limit, func(_ string, v any) bool {
 88		out = append(out, marketJSON(v.(*Market)))
 89		return false
 90	})
 91	return "[" + strings.Join(out, ",") + "]"
 92}
 93
 94// PositionJSON is one user's stake, claim state and claimable amount in a market.
 95func PositionJSON(id int64, user address) string {
 96	m := getMarket(id)
 97	p := m.position(user)
 98	if p == nil {
 99		p = &Position{}
100	}
101	payout, fee := Claimable(id, user)
102	return "{" + strings.Join([]string{
103		kv("a", itoa(p.A)), kv("b", itoa(p.B)),
104		kv("copyA", itoa(p.CopyA)), kv("copyB", itoa(p.CopyB)),
105		kv("referrer", mimir.Quote(p.Referrer.String())),
106		kv("claimed", boolStr(p.Claimed)),
107		kv("claimable", itoa(payout)), kv("claimFee", itoa(fee)),
108	}, ",") + "}"
109}
110
111func marketJSON(m *Market) string {
112	ea, eb := EligibleTotals(m.ID)
113	return "{" + strings.Join([]string{
114		kv("id", itoa(m.ID)),
115		kv("creator", mimir.Quote(m.Creator.String())),
116		kv("deadline", itoa(m.Deadline)),
117		kv("createdAt", itoa(m.CreatedAt)),
118		kv("state", itoa(int64(m.State))),
119		kv("outcome", itoa(int64(m.Outcome))),
120		kv("proposed", itoa(int64(m.Proposed))),
121		kv("proposedAt", itoa(m.ProposedAt)),
122		kv("disputedAt", itoa(m.DisputedAt)),
123		kv("disputer", mimir.Quote(m.Disputer.String())),
124		kv("bond", itoa(m.Bond)),
125		kv("totalA", itoa(m.TotalA)),
126		kv("totalB", itoa(m.TotalB)),
127		kv("eligibleA", itoa(ea)),
128		kv("eligibleB", itoa(eb)),
129		kv("entryFees", itoa(m.EntryFees)),
130		kv("settlementCutoff", itoa(m.SettlementCutoff)),
131		kv("lockAt", itoa(m.LockAt)),
132		kv("participants", itoa(int64(m.positions.Size()))),
133		kv("evidenceHash", mimir.Quote(m.EvidenceHash)),
134		kv("question", mimir.Quote(m.Question)),
135		kv("labelA", mimir.Quote(m.LabelA)),
136		kv("labelB", mimir.Quote(m.LabelB)),
137		kv("resolutionUrl", mimir.Quote(m.ResolutionURL)),
138		kv("category", mimir.Quote(m.Category)),
139		kv("summary", mimir.Quote(m.Summary)),
140	}, ",") + "}"
141}
142
143func kv(k, v string) string { return mimir.Quote(k) + ":" + v }