vs.gno
8.07 Kb · 267 lines
1// Package vs is Mimir's VS (duel) market, settled by an AI oracle: the
2// gno.land port of contracts/MimirV3Erc20.sol. Stakes are mUSD.
3//
4// A creator opens a claim with a stake on their position. Challengers take the
5// other side, each once, up to the claim's maxChallengers (at most 100):
6// - pool odds: challengers share the creator's stake pro rata, and together
7// may stake at most MaxPoolMultiple times the creator's stake;
8// - fixed odds: each challenger is paid challengerPayoutBps of their stake
9// (2x by default, at most 10x), and the creator's unreserved stake must
10// cover every challenger's profit when they join.
11//
12// After the deadline the oracle proposes CREATOR, CHALLENGERS, DRAW or
13// UNRESOLVABLE with a confidence and the hash of its evidence. A participant
14// may dispute within the dispute window by posting DisputeBond; the arbiter
15// rules. An undisputed proposal is finalized by anyone. Settlement pays every
16// participant in the same transaction. Draws, unresolvable questions and
17// claims nobody challenged before the cutoff refund every stake.
18//
19// Fees: an entry fee on every stake that opens or joins a claim
20// (fees.EntryBps, 0.5% by default), kept on refunds. No fee on winnings,
21// except copy trades: ReferrerFeeBps of the profit to the referrer and
22// CopyFeeBps to the platform.
23//
24// Private claims: only wallets the creator invites may challenge. Market data
25// and transactions stay public.
26//
27// Differences from the EVM contract come from the transport, as in the pool
28// realm: mUSD with an approve in the same transaction (no permit variants),
29// payouts sent directly (mUSD cannot refuse or re-enter), block-time seconds.
30// SetInvited takes a comma-separated address list.
31package vs
32
33import (
34 "chain"
35 "chain/runtime"
36 "strconv"
37 "time"
38
39 mimir "gno.land/p/g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz/mimir"
40 "gno.land/p/nt/avl/v0"
41 "gno.land/r/g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz/fees"
42 "gno.land/r/g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz/musd"
43)
44
45// Claim states.
46const (
47 StOpen = 0
48 StActive = 1
49 StResolved = 2
50 StCancelled = 3
51 StProposed = 4 // the oracle has proposed a verdict; disputable until the window closes
52 StDisputed = 5 // a participant disputed; the arbiter decides
53)
54
55// Winner sides.
56const (
57 SideNone = 0
58 SideCreator = 1
59 SideChallengers = 2
60 SideDraw = 3
61 SideUnresolvable = 4
62)
63
64const (
65 MaxChallengers = 100
66 MaxPoolMultiple = int64(5)
67 // MinStake is the smallest gross stake that opens or joins a claim: 0.1 mUSD.
68 MinStake = int64(100_000)
69 DisputeBond = int64(2_000_000)
70 DefaultPayoutBps = int64(20_000) // 2x
71 // Fixed odds pay at most 10x.
72 MaxPayoutBps = int64(100_000)
73 MaxDeadlineAhead = int64(365 * 24 * 60 * 60)
74 MaxEntryBps = int64(100)
75 // Anti-sniping: no new challenges in the final seconds before the deadline.
76 ChallengeLockSeconds = int64(60)
77 ReferrerFeeBps = int64(100)
78 CopyFeeBps = int64(100)
79 ResolutionGraceSeconds = int64(7 * 24 * 60 * 60)
80 MinLiveDisputeWindow = int64(24 * 60 * 60)
81 TestnetDisputeWindow = int64(10 * 60)
82)
83
84const (
85 Deployer = address("g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz")
86 InitialOracle = address("g18pnec3atge4msr5hhanm790tljtdyd87s0wguh")
87 InitialArbiter = address("g1af78r3tuv84mf3cd0kxrssmw6g2v2mvvc6phpv")
88)
89
90type Challenger struct {
91 Addr address
92 Stake int64 // net
93 At int64
94 Referrer address
95}
96
97type Proposal struct {
98 WinnerSide int
99 Confidence int
100 ProposedAt int64
101 DisputedAt int64
102 Disputer address
103 Bond int64
104 EvidenceHash string
105 Summary string
106}
107
108type Claim struct {
109 ID int64
110 Creator address
111 Question string
112 CreatorPosition string
113 CounterPosition string
114 ResolutionURL string
115 CreatorStake int64
116 TotalChallengerStake int64
117 ReservedCreatorLiability int64
118 Deadline int64
119 State int
120 WinnerSide int
121 ResolutionSummary string
122 Confidence int
123 Category string
124 ParentID int64
125 CreatedAt int64
126 MarketType string // binary | moneyline | spread | total | prop | custom
127 OddsMode string // pool | fixed
128 ChallengerPayoutBps int64 // fixed odds, e.g. 20000 = 2x
129 HandicapLine string
130 SettlementRule string
131 MaxChallengers int
132 IsPrivate bool
133 EvidenceHash string
134 Referrer address // the creator's referrer
135 EntryFees int64
136 SettlementCutoff int64
137 LockAt int64
138 Proposal Proposal
139
140 challengers []*Challenger
141 joined avl.Tree // address -> true
142 invited avl.Tree // address -> bool
143}
144
145var (
146 claims avl.Tree // padded id -> *Claim
147 claimCount int64
148 totalResolved int64
149
150 wins avl.Tree // address -> int64
151 losses avl.Tree
152
153 accruedFees avl.Tree // address -> int64
154 lifetimeFeesAccrued int64
155 lifetimeFeesClaimed int64
156
157 roles *mimir.Roles
158 disputeWindow int64
159)
160
161func init(cur realm) {
162 roles = mimir.NewRoles(Deployer, InitialArbiter, InitialOracle, Deployer)
163 disputeWindow = TestnetDisputeWindow
164 if runtime.ChainID() == "gnoland-1" {
165 disputeWindow = MinLiveDisputeWindow
166 }
167}
168
169func now() int64 { return time.Now().Unix() }
170func emit(typ string, attrs ...string) { chain.Emit(typ, attrs...) }
171func itoa(n int64) string { return strconv.FormatInt(n, 10) }
172func caller(cur realm) address { return cur.Previous().Address() }
173
174func key(id int64) string {
175 s := itoa(id)
176 for len(s) < 12 {
177 s = "0" + s
178 }
179 return s
180}
181
182func getClaim(id int64) *Claim {
183 v := claims.Get(key(id))
184 if v == nil {
185 panic("ClaimNotFound")
186 }
187 return v.(*Claim)
188}
189
190func (c *Claim) hasJoined(a address) bool { return c.joined.Has(a.String()) }
191
192func (c *Claim) isInvited(a address) bool {
193 v := c.invited.Get(a.String())
194 return v != nil && v.(bool)
195}
196
197func counter(t *avl.Tree, a address) int64 {
198 if v := t.Get(a.String()); v != nil {
199 return v.(int64)
200 }
201 return 0
202}
203
204func bump(t *avl.Tree, a address) { t.Set(a.String(), counter(t, a)+1) }
205
206func accrue(id int64, to address, amount int64) {
207 accruedFees.Set(to.String(), mimir.Add(counter(&accruedFees, to), amount))
208 lifetimeFeesAccrued = mimir.Add(lifetimeFeesAccrued, amount)
209 emit("FeeAccrued", "id", itoa(id), "recipient", to.String(), "amount", itoa(amount))
210}
211
212func pull(cur realm, from address, amount int64) {
213 musd.TransferFrom(cross(cur), from, cur.Address(), amount)
214}
215
216func push(cur realm, to address, amount int64) {
217 if amount == 0 {
218 return
219 }
220 musd.Transfer(cross(cur), to, amount)
221}
222
223// takeEntry pulls `amount` from the caller, keeps the caller's entry fee and
224// returns the rest: the stake that is recorded and can be won or refunded.
225func takeEntry(cur realm, c *Claim, user address, amount int64) int64 {
226 pull(cur, user, amount)
227 bps := fees.EntryBps(user)
228 if bps > MaxEntryBps {
229 panic("EntryFeeTooHigh")
230 }
231 fee := mimir.MulDiv(amount, bps, 10_000)
232 if fee != 0 {
233 accrue(c.ID, roles.FeeRecipient, fee)
234 c.EntryFees += fee
235 }
236 return amount - fee
237}
238
239func grossPayout(stake, bps int64) int64 { return mimir.MulDiv(stake, bps, 10_000) }
240
241// payWinner pays a winner. Only a copy trade pays a fee on winnings:
242// ReferrerFeeBps of the profit to the referrer and CopyFeeBps to the platform.
243// Profit is gross - principal floored at zero, so a refund and a break-even
244// win are free. Nobody pays themselves.
245func payWinner(cur realm, id int64, to address, gross, principal int64, referrer address) (paid, taken int64) {
246 profit := int64(0)
247 if gross > principal {
248 profit = gross - principal
249 }
250 if profit != 0 && referrer != "" && referrer != to {
251 refFee := mimir.MulDiv(profit, ReferrerFeeBps, 10_000)
252 if refFee != 0 {
253 accrue(id, referrer, refFee)
254 }
255 copyFee := int64(0)
256 if roles.FeeRecipient != to {
257 copyFee = mimir.MulDiv(profit, CopyFeeBps, 10_000)
258 }
259 if copyFee != 0 {
260 accrue(id, roles.FeeRecipient, copyFee)
261 }
262 taken = refFee + copyFee
263 }
264 paid = gross - taken
265 push(cur, to, paid)
266 return paid, taken
267}