roles.gno
7.29 Kb · 259 lines
1package mimir
2
3import (
4 "chain"
5 "strconv"
6)
7
8// TimelockSeconds delays ownership, oracle and fee-recipient changes.
9const TimelockSeconds = int64(2 * 24 * 60 * 60)
10
11// Roles holds a market realm's roles, pause flag and guarded-launch caps, and
12// enforces the same rules the EVM contracts do:
13// - two-step ownership with a timelock;
14// - a timelocked oracle change the owner can revoke at once;
15// - the oracle is never also the owner, the arbiter or the fee recipient;
16// - the audit agent may only dispute, and never holds another role;
17// - a timelocked fee-recipient change;
18// - queued changes are executed by anyone once due.
19//
20// Every method takes the caller and the block time from the realm, which
21// owns the Roles value.
22type Roles struct {
23 Owner address
24 Arbiter address
25 AuditAgent address
26 Oracle address
27 FeeRecipient address
28 PendingOwner address
29 PendingOwnerEta int64
30 PendingOracle address
31 PendingOracleEta int64
32 PendingFeeRecipient address
33 PendingFeeEta int64
34 Paused bool
35 MaxAccountStake int64
36 MaxMarketStake int64
37}
38
39// NewRoles validates the initial roles as the EVM constructors did.
40func NewRoles(owner, arbiter, oracle, feeRecipient address) *Roles {
41 if !owner.IsValid() || !arbiter.IsValid() || !oracle.IsValid() || !feeRecipient.IsValid() {
42 panic("ZeroAddress")
43 }
44 if oracle == owner || oracle == arbiter || oracle == feeRecipient {
45 panic("RoleCollision")
46 }
47 chain.Emit("OwnershipTransferred", "previous", "", "next", owner.String())
48 chain.Emit("ArbiterChanged", "previous", "", "next", arbiter.String())
49 chain.Emit("OracleChanged", "previous", "", "next", oracle.String())
50 chain.Emit("FeeRecipientChanged", "previous", "", "next", feeRecipient.String())
51 return &Roles{Owner: owner, Arbiter: arbiter, Oracle: oracle, FeeRecipient: feeRecipient}
52}
53
54func i64(n int64) string { return strconv.FormatInt(n, 10) }
55
56func (r *Roles) AssertOwner(caller address) {
57 if caller != r.Owner {
58 panic("NotOwner")
59 }
60}
61
62func (r *Roles) AssertOracle(caller address) {
63 if r.Oracle == "" || caller != r.Oracle {
64 panic("NotOracle")
65 }
66}
67
68func (r *Roles) AssertArbiter(caller address) {
69 if caller != r.Arbiter {
70 panic("NotArbiter")
71 }
72}
73
74func (r *Roles) AssertNotPaused() {
75 if r.Paused {
76 panic("IsPaused")
77 }
78}
79
80func (r *Roles) checkAuditCollision(next address) {
81 if next != "" && next == r.AuditAgent {
82 panic("RoleCollision")
83 }
84}
85
86func (r *Roles) TransferOwnership(caller, next address, now int64) {
87 r.AssertOwner(caller)
88 if !next.IsValid() {
89 panic("ZeroAddress")
90 }
91 r.PendingOwner = next
92 r.PendingOwnerEta = now + TimelockSeconds
93 chain.Emit("OwnershipTransferStarted", "previous", r.Owner.String(), "next", next.String(), "eta", i64(r.PendingOwnerEta))
94}
95
96func (r *Roles) CancelOwnershipTransfer(caller address) {
97 r.AssertOwner(caller)
98 if r.PendingOwnerEta == 0 {
99 panic("NothingQueued")
100 }
101 chain.Emit("OwnershipTransferCancelled", "pending", r.PendingOwner.String())
102 r.PendingOwner = ""
103 r.PendingOwnerEta = 0
104}
105
106func (r *Roles) AcceptOwnership(caller address, now int64) {
107 if r.PendingOwner == "" || caller != r.PendingOwner {
108 panic("NotPendingOwner")
109 }
110 if now < r.PendingOwnerEta {
111 panic("Timelocked")
112 }
113 r.checkAuditCollision(caller)
114 chain.Emit("OwnershipTransferred", "previous", r.Owner.String(), "next", caller.String())
115 r.Owner = caller
116 r.PendingOwner = ""
117 r.PendingOwnerEta = 0
118}
119
120func (r *Roles) QueueOracle(caller, next address, now int64) {
121 r.AssertOwner(caller)
122 if !next.IsValid() {
123 panic("ZeroAddress")
124 }
125 if next == r.Owner || next == r.Arbiter || next == r.FeeRecipient {
126 panic("RoleCollision")
127 }
128 r.PendingOracle = next
129 r.PendingOracleEta = now + TimelockSeconds
130 chain.Emit("OracleChangeQueued", "next", next.String(), "eta", i64(r.PendingOracleEta))
131}
132
133// RevokeOracle is the emergency stop: the oracle loses its power at once.
134func (r *Roles) RevokeOracle(caller address) {
135 r.AssertOwner(caller)
136 chain.Emit("OracleChanged", "previous", r.Oracle.String(), "next", "")
137 r.Oracle = ""
138}
139
140func (r *Roles) CancelOracle(caller address) {
141 r.AssertOwner(caller)
142 if r.PendingOracleEta == 0 {
143 panic("NothingQueued")
144 }
145 chain.Emit("OracleChangeCancelled", "next", r.PendingOracle.String())
146 r.PendingOracle = ""
147 r.PendingOracleEta = 0
148}
149
150func (r *Roles) ExecuteOracle(now int64) {
151 if r.PendingOracleEta == 0 {
152 panic("NothingQueued")
153 }
154 if now < r.PendingOracleEta {
155 panic("Timelocked")
156 }
157 r.checkAuditCollision(r.PendingOracle)
158 chain.Emit("OracleChanged", "previous", r.Oracle.String(), "next", r.PendingOracle.String())
159 r.Oracle = r.PendingOracle
160 r.PendingOracle = ""
161 r.PendingOracleEta = 0
162}
163
164// SetAuditAgent appoints the reviewer that may dispute with a bond; "" disables it.
165func (r *Roles) SetAuditAgent(caller, next address) {
166 r.AssertOwner(caller)
167 if next != "" && (next == r.Owner || next == r.Arbiter || next == r.Oracle || next == r.FeeRecipient) {
168 panic("RoleCollision")
169 }
170 chain.Emit("AuditAgentChanged", "previous", r.AuditAgent.String(), "next", next.String())
171 r.AuditAgent = next
172}
173
174func (r *Roles) SetArbiter(caller, next address) {
175 r.AssertOwner(caller)
176 if !next.IsValid() {
177 panic("ZeroAddress")
178 }
179 if next == r.Oracle {
180 panic("RoleCollision")
181 }
182 r.checkAuditCollision(next)
183 chain.Emit("ArbiterChanged", "previous", r.Arbiter.String(), "next", next.String())
184 r.Arbiter = next
185}
186
187// SetCaps sets guarded launch caps in mUSD units (0 = none). They only gate new positions.
188func (r *Roles) SetCaps(caller address, market, account int64) {
189 r.AssertOwner(caller)
190 if market < 0 || account < 0 {
191 panic("BadCap")
192 }
193 r.MaxMarketStake = market
194 r.MaxAccountStake = account
195 chain.Emit("CapsChanged", "maxMarketStake", i64(market), "maxAccountStake", i64(account))
196}
197
198// CheckCaps panics when an account's stake or a market's total is over its cap.
199func (r *Roles) CheckCaps(mine, total int64) {
200 if (r.MaxAccountStake != 0 && mine > r.MaxAccountStake) || (r.MaxMarketStake != 0 && total > r.MaxMarketStake) {
201 panic("CapExceeded")
202 }
203}
204
205func (r *Roles) QueueFeeRecipient(caller, next address, now int64) {
206 r.AssertOwner(caller)
207 if !next.IsValid() {
208 panic("ZeroAddress")
209 }
210 r.PendingFeeRecipient = next
211 r.PendingFeeEta = now + TimelockSeconds
212 chain.Emit("FeeRecipientQueued", "next", next.String(), "eta", i64(r.PendingFeeEta))
213}
214
215func (r *Roles) CancelFeeRecipient(caller address) {
216 r.AssertOwner(caller)
217 if r.PendingFeeEta == 0 {
218 panic("NothingQueued")
219 }
220 chain.Emit("FeeRecipientCancelled", "next", r.PendingFeeRecipient.String())
221 r.PendingFeeRecipient = ""
222 r.PendingFeeEta = 0
223}
224
225func (r *Roles) ExecuteFeeRecipient(now int64) {
226 if r.PendingFeeEta == 0 {
227 panic("NothingQueued")
228 }
229 if now < r.PendingFeeEta {
230 panic("Timelocked")
231 }
232 r.checkAuditCollision(r.PendingFeeRecipient)
233 chain.Emit("FeeRecipientChanged", "previous", r.FeeRecipient.String(), "next", r.PendingFeeRecipient.String())
234 r.FeeRecipient = r.PendingFeeRecipient
235 r.PendingFeeRecipient = ""
236 r.PendingFeeEta = 0
237}
238
239func (r *Roles) SetPaused(caller address, p bool) {
240 r.AssertOwner(caller)
241 r.Paused = p
242 if p {
243 chain.Emit("Paused", "paused", "true")
244 } else {
245 chain.Emit("Paused", "paused", "false")
246 }
247}
248
249// ParseOptional reads an optional address argument: "" means none.
250func ParseOptional(s string) address {
251 if s == "" {
252 return ""
253 }
254 a := address(s)
255 if !a.IsValid() {
256 panic("BadAddress")
257 }
258 return a
259}