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.

settle.gno

6.47 Kb · 221 lines
  1package pool
  2
  3func validOutcome(outcome int) {
  4	if outcome < SideA || outcome > Unresolvable {
  5		panic("BadSide")
  6	}
  7}
  8
  9func validHash(h string) {
 10	if h == "" {
 11		return
 12	}
 13	if len(h) != 64 {
 14		panic("BadEvidenceHash")
 15	}
 16	for _, c := range h {
 17		if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
 18			panic("BadEvidenceHash")
 19		}
 20	}
 21}
 22
 23// refundAt is when RefundExpired opens: grace from the latest deadline, proposal or dispute.
 24func refundAt(m *Market) int64 {
 25	start := m.Deadline
 26	if m.ProposedAt > start {
 27		start = m.ProposedAt
 28	}
 29	if m.DisputedAt > start {
 30		start = m.DisputedAt
 31	}
 32	return start + ResolutionGraceSeconds
 33}
 34
 35func setCutoff(m *Market, cutoff int64) {
 36	if cutoff > now() || cutoff < 0 {
 37		panic("BadCutoff")
 38	}
 39	m.SettlementCutoff = cutoff
 40	emit("SettlementCutoffSet", "id", itoa(m.ID), "cutoff", itoa(cutoff))
 41}
 42
 43// Resolve is the oracle's verdict: final at once with no dispute window, else a proposal.
 44func Resolve(cur realm, id int64, outcome int, summary, evidenceHash string) {
 45	resolve(cur, id, outcome, summary, evidenceHash, 0)
 46}
 47
 48// ResolveWithCutoff also sets the settlement cutoff: only deposits strictly
 49// before it compete; later ones get their net principal back.
 50func ResolveWithCutoff(cur realm, id int64, outcome int, summary, evidenceHash string, cutoff int64) {
 51	resolve(cur, id, outcome, summary, evidenceHash, cutoff)
 52}
 53
 54func resolve(cur realm, id int64, outcome int, summary, evidenceHash string, cutoff int64) {
 55	assertNotPaused()
 56	roles.AssertOracle(caller(cur))
 57	m := getMarket(id)
 58	if m.State != StOpen {
 59		panic("NotOpen")
 60	}
 61	if now() < m.Deadline {
 62		panic("NotExpired")
 63	}
 64	if now() >= refundAt(m) {
 65		panic("GraceOver")
 66	}
 67	validOutcome(outcome)
 68	validHash(evidenceHash)
 69	setCutoff(m, cutoff)
 70	if disputeWindow == 0 {
 71		settle(m, outcome, summary, evidenceHash)
 72		return
 73	}
 74	m.State = StProposed
 75	m.Proposed = outcome
 76	m.ProposedAt = now()
 77	m.EvidenceHash = evidenceHash
 78	m.Summary = summary
 79	emit("ResolutionProposed", "id", itoa(id), "outcome", itoa(int64(outcome)),
 80		"evidenceHash", evidenceHash, "disputableUntil", itoa(now()+disputeWindow))
 81}
 82
 83// Dispute escalates the proposal to the arbiter, pulling DisputeBond mUSD.
 84// Open to the market's participants, the audit agent and the arbiter.
 85func Dispute(cur realm, id int64) {
 86	m := getMarket(id)
 87	if m.State != StProposed {
 88		panic("NotProposed")
 89	}
 90	if now() >= m.ProposedAt+disputeWindow {
 91		panic("WindowClosed")
 92	}
 93	c := caller(cur)
 94	p := m.position(c)
 95	staked := p != nil && (p.A != 0 || p.B != 0)
 96	if !staked && (roles.AuditAgent == "" || c != roles.AuditAgent) && c != roles.Arbiter {
 97		panic("NotParticipant")
 98	}
 99	pull(cur, c, DisputeBond)
100	m.State = StDisputed
101	m.Disputer = c
102	m.DisputedAt = now()
103	m.Bond = DisputeBond
104	emit("ResolutionDisputed", "id", itoa(id), "disputer", c.String(), "bond", itoa(DisputeBond))
105}
106
107// VetoProposal lets the owner send a proposal it believes wrong to the
108// arbiter, inside the window. No bond; if the arbiter never rules,
109// RefundExpired refunds every stake rather than honouring the vetoed proposal.
110func VetoProposal(cur realm, id int64) {
111	assertOwner(cur)
112	m := getMarket(id)
113	if m.State != StProposed {
114		panic("NotProposed")
115	}
116	if now() >= m.ProposedAt+disputeWindow {
117		panic("WindowClosed")
118	}
119	m.State = StDisputed
120	m.DisputedAt = now()
121	emit("ResolutionDisputed", "id", itoa(id), "disputer", "", "bond", "0")
122}
123
124// Finalize settles an undisputed proposal once the window has closed. Anyone may call it.
125func Finalize(cur realm, id int64) {
126	assertNotPaused()
127	m := getMarket(id)
128	if m.State != StProposed {
129		panic("NotProposed")
130	}
131	if now() < m.ProposedAt+disputeWindow {
132		panic("WindowOpen")
133	}
134	settle(m, m.Proposed, m.Summary, m.EvidenceHash)
135}
136
137// ResolveDispute is the arbiter's final word on a disputed market.
138func ResolveDispute(cur realm, id int64, outcome int, summary, evidenceHash string) {
139	m := getMarket(id)
140	resolveDispute(cur, m, outcome, summary, evidenceHash, m.SettlementCutoff)
141}
142
143func ResolveDisputeWithCutoff(cur realm, id int64, outcome int, summary, evidenceHash string, cutoff int64) {
144	resolveDispute(cur, getMarket(id), outcome, summary, evidenceHash, cutoff)
145}
146
147func resolveDispute(cur realm, m *Market, outcome int, summary, evidenceHash string, cutoff int64) {
148	assertNotPaused()
149	roles.AssertArbiter(caller(cur))
150	if m.State != StDisputed {
151		panic("NotDisputed")
152	}
153	if now() >= refundAt(m) {
154		panic("GraceOver")
155	}
156	validOutcome(outcome)
157	validHash(evidenceHash)
158	disputerRight := outcome != m.Proposed || cutoff != m.SettlementCutoff
159	setCutoff(m, cutoff)
160	emit("DisputeResolved", "id", itoa(m.ID), "outcome", itoa(int64(outcome)), "disputerRight", boolStr(disputerRight))
161	// Settled before the bond moves.
162	settle(m, outcome, summary, evidenceHash)
163	settleBond(cur, m, disputerRight)
164}
165
166// RefundExpired is the escape hatch, open to anyone once the grace has passed.
167// A market the oracle never resolved is refunded in full; a dispute the
168// arbiter never ruled on settles to the oracle's proposal and the bond is
169// forfeited, so a cheap dispute cannot turn a loss into a free refund. A
170// vetoed proposal, or any proposal while paused, refunds instead.
171func RefundExpired(cur realm, id int64) {
172	m := getMarket(id)
173	disputed := m.State == StDisputed
174	if !disputed && m.State != StOpen && !(roles.Paused && m.State == StProposed) {
175		panic("NotOpen")
176	}
177	if now() < refundAt(m) {
178		panic("GraceNotOver")
179	}
180	switch {
181	case disputed && !roles.Paused && m.Disputer != "":
182		emit("DisputeResolved", "id", itoa(id), "outcome", itoa(int64(m.Proposed)), "disputerRight", "false")
183		settle(m, m.Proposed, m.Summary, m.EvidenceHash)
184		settleBond(cur, m, false)
185	case disputed:
186		emit("DisputeResolved", "id", itoa(id), "outcome", itoa(Unresolvable), "disputerRight", "true")
187		settle(m, Unresolvable, "Refunded: vetoed or paused", "")
188		settleBond(cur, m, true)
189	default:
190		emit("MarketExpiredRefund", "id", itoa(id), "caller", caller(cur).String())
191		settle(m, Unresolvable, "Refunded: not resolved within the grace period", "")
192	}
193}
194
195func settle(m *Market, outcome int, summary, evidenceHash string) {
196	m.State = StResolved
197	m.Outcome = outcome
198	m.Summary = summary
199	m.EvidenceHash = evidenceHash
200	emit("MarketResolved", "id", itoa(m.ID), "outcome", itoa(int64(outcome)), "summary", summary, "evidenceHash", evidenceHash)
201}
202
203func settleBond(cur realm, m *Market, returnIt bool) {
204	bond := m.Bond
205	if bond == 0 {
206		return
207	}
208	m.Bond = 0
209	if returnIt {
210		push(cur, m.Disputer, bond)
211	} else {
212		accrue(m.ID, roles.FeeRecipient, bond)
213	}
214}
215
216func boolStr(b bool) string {
217	if b {
218		return "true"
219	}
220	return "false"
221}