create.gno
7.48 Kb · 236 lines
1package vs
2
3import (
4 "strings"
5
6 mimir "gno.land/p/g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz/mimir"
7)
8
9type createArgs struct {
10 question, creatorPosition, counterPosition, resolutionURL string
11 deadline, stakeAmount int64
12 category string
13 parentID int64
14 marketType, oddsMode string
15 challengerPayoutBps int64
16 handicapLine, settlementRule string
17 maxChallengers int
18 isPrivate bool
19 referrer address
20}
21
22// CreateClaim opens a claim with `stakeAmount` mUSD (less the entry fee) on
23// the creator's position. oddsMode is "pool" or "fixed"; challengerPayoutBps
24// applies to fixed odds (below 10000 means the 2x default). maxChallengers 0
25// means the maximum. referrer is the copied basket's creator, or "".
26func CreateClaim(cur realm,
27 question, creatorPosition, counterPosition, resolutionURL string,
28 deadline, stakeAmount int64,
29 category string, parentID int64,
30 marketType, oddsMode string, challengerPayoutBps int64,
31 handicapLine, settlementRule string, maxChallengers int,
32 isPrivate bool, referrer string) int64 {
33 roles.AssertNotPaused()
34 return createClaim(cur, createArgs{
35 question, creatorPosition, counterPosition, resolutionURL,
36 deadline, stakeAmount, category, parentID, marketType, oddsMode,
37 challengerPayoutBps, handicapLine, settlementRule, maxChallengers,
38 isPrivate, mimir.ParseOptional(referrer),
39 })
40}
41
42// CreateRematch opens a new claim inheriting its terms from a parent. The
43// caller is the creator and pays the stake. The parent's referrer carries over
44// only to the parent's own creator.
45func CreateRematch(cur realm, parentID, deadline, stakeAmount int64) int64 {
46 roles.AssertNotPaused()
47 p := getClaim(parentID)
48 ref := address("")
49 if caller(cur) == p.Creator {
50 ref = p.Referrer
51 }
52 return createClaim(cur, createArgs{
53 p.Question, p.CreatorPosition, p.CounterPosition, p.ResolutionURL,
54 deadline, stakeAmount, p.Category, parentID, p.MarketType, p.OddsMode,
55 p.ChallengerPayoutBps, p.HandicapLine, p.SettlementRule, p.MaxChallengers,
56 p.IsPrivate, ref,
57 })
58}
59
60func createClaim(cur realm, a createArgs) int64 {
61 if a.stakeAmount < MinStake {
62 panic("StakeTooSmall")
63 }
64 if a.deadline <= now() {
65 panic("DeadlineInPast")
66 }
67 if a.deadline > now()+MaxDeadlineAhead {
68 panic("DeadlineTooFar")
69 }
70 if a.question == "" {
71 panic("EmptyQuestion")
72 }
73 isFixed := a.oddsMode == "fixed"
74 payoutBps := int64(0)
75 if isFixed {
76 payoutBps = DefaultPayoutBps
77 if a.challengerPayoutBps >= 10_000 {
78 payoutBps = a.challengerPayoutBps
79 }
80 }
81 if payoutBps > MaxPayoutBps {
82 panic("PayoutTooHigh")
83 }
84 maxCh := a.maxChallengers
85 if maxCh <= 0 || maxCh > MaxChallengers {
86 maxCh = MaxChallengers
87 }
88 claimCount++
89 c := &Claim{
90 ID: claimCount, Creator: caller(cur), Question: a.question,
91 CreatorPosition: a.creatorPosition, CounterPosition: a.counterPosition,
92 ResolutionURL: a.resolutionURL, Deadline: a.deadline, Category: orDefault(a.category, "custom"),
93 ParentID: a.parentID, CreatedAt: now(), MarketType: orDefault(a.marketType, "binary"),
94 OddsMode: "pool", ChallengerPayoutBps: payoutBps, HandicapLine: a.handicapLine,
95 SettlementRule: a.settlementRule, MaxChallengers: maxCh, IsPrivate: a.isPrivate,
96 Referrer: a.referrer,
97 }
98 if isFixed {
99 c.OddsMode = "fixed"
100 }
101 net := takeEntry(cur, c, c.Creator, a.stakeAmount)
102 roles.CheckCaps(net, net)
103 c.CreatorStake = net
104 claims.Set(key(c.ID), c)
105 if a.referrer != "" {
106 emit("ReferrerSet", "id", itoa(c.ID), "user", c.Creator.String(), "referrer", a.referrer.String())
107 }
108 emit("ClaimCreated", "id", itoa(c.ID), "creator", c.Creator.String(), "category", c.Category)
109 return c.ID
110}
111
112func orDefault(s, d string) string {
113 if s == "" {
114 return d
115 }
116 return s
117}
118
119// SetInvited grants or revokes wallets on a private claim, up to
120// MaxChallengers per call, as a comma-separated list. Revocation only blocks
121// future entry. Rematches have their own empty list.
122func SetInvited(cur realm, claimID int64, accounts string, allowed bool) {
123 c := getClaim(claimID)
124 list := strings.Split(accounts, ",")
125 if caller(cur) != c.Creator || !c.IsPrivate || c.State > StActive || len(list) > MaxChallengers {
126 panic("BadInvite")
127 }
128 for _, s := range list {
129 a := address(strings.TrimSpace(s))
130 if !a.IsValid() || a == c.Creator {
131 panic("BadInvite")
132 }
133 c.invited.Set(a.String(), allowed)
134 emit("InviteUpdated", "id", itoa(claimID), "account", a.String(), "allowed", boolStr(allowed))
135 }
136}
137
138// ChallengeClaim takes the counter position with `stakeAmount` mUSD (less the entry fee).
139func ChallengeClaim(cur realm, claimID, stakeAmount int64, referrer string) {
140 roles.AssertNotPaused()
141 c := getClaim(claimID)
142 user := caller(cur)
143 ref := mimir.ParseOptional(referrer)
144 checkChallenge(c, user, stakeAmount)
145 net := takeEntry(cur, c, user, stakeAmount)
146 roles.CheckCaps(net, c.CreatorStake+c.TotalChallengerStake+net)
147 if c.OddsMode == "fixed" {
148 // The creator's unreserved stake must cover this challenger's profit.
149 gross := grossPayout(net, c.ChallengerPayoutBps)
150 profit := int64(0)
151 if gross > net {
152 profit = gross - net
153 }
154 if c.CreatorStake-c.ReservedCreatorLiability < profit {
155 panic("CreatorHasInsufficientLiquidity")
156 }
157 c.ReservedCreatorLiability += profit
158 } else if c.TotalChallengerStake+net > c.CreatorStake*MaxPoolMultiple {
159 panic("PoolFull")
160 }
161 c.challengers = append(c.challengers, &Challenger{Addr: user, Stake: net, At: now(), Referrer: ref})
162 c.joined.Set(user.String(), true)
163 if ref != "" {
164 emit("ReferrerSet", "id", itoa(claimID), "user", user.String(), "referrer", ref.String())
165 }
166 c.TotalChallengerStake += net
167 c.State = StActive
168 emit("ClaimChallenged", "id", itoa(claimID), "challenger", user.String(), "stake", itoa(net))
169}
170
171func checkChallenge(c *Claim, user address, stakeAmount int64) {
172 if c.State != StOpen && c.State != StActive {
173 panic("NotOpen")
174 }
175 if user == c.Creator {
176 panic("SelfChallenge")
177 }
178 if c.hasJoined(user) {
179 panic("AlreadyChallenged")
180 }
181 if len(c.challengers) >= c.MaxChallengers {
182 panic("Full")
183 }
184 if stakeAmount < MinStake {
185 panic("StakeTooSmall")
186 }
187 if now()+ChallengeLockSeconds > c.Deadline {
188 panic("ChallengeWindowClosed")
189 }
190 if c.LockAt != 0 && now() >= c.LockAt {
191 panic("Locked")
192 }
193 // Authorize the caller's wallet before calling the fee realm or pulling mUSD.
194 if c.IsPrivate && !c.isInvited(user) {
195 panic("NotInvited")
196 }
197}
198
199// SetLockAt closes a claim to new challengers early, by its creator or the
200// owner. Only ever earlier.
201func SetLockAt(cur realm, claimID, at int64) {
202 c := getClaim(claimID)
203 u := caller(cur)
204 if u != c.Creator && u != roles.Owner {
205 panic("BadLock")
206 }
207 if c.State > StActive || at < now() || at+ChallengeLockSeconds > c.Deadline {
208 panic("BadLock")
209 }
210 if c.LockAt != 0 && at > c.LockAt {
211 panic("BadLock")
212 }
213 c.LockAt = at
214 emit("LockSet", "id", itoa(claimID), "lockAt", itoa(at))
215}
216
217// CancelClaim refunds the creator of a claim nobody has challenged yet.
218func CancelClaim(cur realm, claimID int64) {
219 c := getClaim(claimID)
220 if caller(cur) != c.Creator {
221 panic("NotCreator")
222 }
223 if c.State != StOpen {
224 panic("NotOpen")
225 }
226 c.State = StCancelled
227 push(cur, c.Creator, c.CreatorStake)
228 emit("ClaimCancelled", "id", itoa(claimID), "refunded", itoa(c.CreatorStake))
229}
230
231func boolStr(b bool) string {
232 if b {
233 return "true"
234 }
235 return "false"
236}