guildhall.gno
7.11 Kb · 253 lines
1// Package guildhall is the Guildhall bounty board realm. A poster attaches
2// GNOT to Post and the realm escrows it until the quest is paid to a worker
3// (by Accept or by silence through the review window) or returned to the
4// poster (Refund past the deadline, or Cancel while open).
5package guildhall
6
7import (
8 "strconv"
9
10 "chain"
11 "chain/banker"
12 "chain/runtime"
13 "chain/runtime/unsafe"
14
15 "gno.land/p/nt/avl/v0"
16 "gno.land/p/nt/seqid/v0"
17 "gno.land/p/nt/ufmt/v0"
18 "gno.land/p/nym-ololade001/guild/v0"
19)
20
21var (
22 // quests maps a zero-padded decimal quest id to *guild.Quest. The
23 // fixed-width padding keeps AVL iteration in numeric id order.
24 quests = avl.NewTree()
25 // active holds the same keys for quests that are not in a terminal
26 // state, so the board never walks paid, refunded or cancelled rows.
27 active = avl.NewTree()
28 // profiles maps an address string to *profile.
29 profiles = avl.NewTree()
30
31 questSeq seqid.ID
32
33 escrowTotal int64 // ugnot currently locked in live quests
34 paidTotal int64 // ugnot paid out to workers, all time
35)
36
37// profile aggregates an address's activity as poster and as worker.
38type profile struct {
39 Posted int
40 Paid int
41 Accepted int
42 AutoAccepted int
43 Earned int64
44 Spent int64
45 PostedIDs []uint64
46 PaidIDs []uint64
47}
48
49// Post escrows the attached ugnot as a quest's reward. The send and the
50// direct-user-call check are a pair: OriginSend describes coins that landed
51// at this realm only when the caller is a pure EOA, so anything else
52// (maketx run scripts, intermediary realms) is rejected first.
53func Post(cur realm, title, spec string, leaseBlocks, reviewBlocks, deadline int64) uint64 {
54 if !cur.Previous().IsUserCall() {
55 panic("Post must be called directly by a user (maketx call)")
56 }
57 sent := unsafe.OriginSend()
58 if len(sent) != 1 || sent[0].Denom != "ugnot" || sent[0].Amount <= 0 {
59 panic("attach a positive ugnot reward with -send")
60 }
61 poster := cur.Previous().Address()
62 h := runtime.ChainHeight()
63 id := uint64(questSeq.Next())
64 q, err := guild.New(id, poster, title, spec, sent[0].Amount, leaseBlocks, reviewBlocks, deadline, h)
65 if err != nil {
66 panic(err)
67 }
68 quests.Set(questKey(id), q)
69 active.Set(questKey(id), q)
70 escrowTotal += q.Reward
71 p := getProfile(poster)
72 p.Posted++
73 p.PostedIDs = append(p.PostedIDs, id)
74 chain.Emit("QuestPosted",
75 "id", ufmt.Sprintf("%d", id),
76 "poster", poster.String(),
77 "reward", ufmt.Sprintf("%d", q.Reward))
78 return id
79}
80
81// Claim lets the caller take an open quest under a fresh lease.
82func Claim(cur realm, id uint64) {
83 caller := cur.Previous().Address()
84 q := mustQuest(id)
85 h := runtime.ChainHeight()
86 if err := q.Claim(caller, h); err != nil {
87 panic(err)
88 }
89 chain.Emit("QuestClaimed",
90 "id", ufmt.Sprintf("%d", id),
91 "worker", caller.String(),
92 "lease_end", ufmt.Sprintf("%d", q.LeaseEnd))
93}
94
95// Submit turns in the caller's proof of work while the lease is live.
96func Submit(cur realm, id uint64, proofURL, note string) {
97 caller := cur.Previous().Address()
98 q := mustQuest(id)
99 if err := q.Submit(caller, runtime.ChainHeight(), proofURL, note); err != nil {
100 panic(err)
101 }
102 chain.Emit("QuestSubmitted",
103 "id", ufmt.Sprintf("%d", id),
104 "worker", caller.String(),
105 "proof", proofURL)
106}
107
108// Accept pays the escrowed reward to the worker; poster only.
109func Accept(cur realm, id uint64) {
110 caller := cur.Previous().Address()
111 q := mustQuest(id)
112 if err := q.Accept(caller, runtime.ChainHeight()); err != nil {
113 panic(err)
114 }
115 payQuest(cur, q)
116}
117
118// Reject returns a submitted quest to the board inside the review window;
119// poster only.
120func Reject(cur realm, id uint64, reason string) {
121 caller := cur.Previous().Address()
122 q := mustQuest(id)
123 if err := q.Reject(caller, runtime.ChainHeight(), reason); err != nil {
124 panic(err)
125 }
126 chain.Emit("QuestRejected",
127 "id", ufmt.Sprintf("%d", id),
128 "poster", caller.String(),
129 "reason", reason)
130}
131
132// ClaimPayout pays the worker when the review window closed with no answer
133// from the poster; worker only.
134func ClaimPayout(cur realm, id uint64) {
135 caller := cur.Previous().Address()
136 q := mustQuest(id)
137 if err := q.AutoAccept(caller, runtime.ChainHeight()); err != nil {
138 panic(err)
139 }
140 payQuest(cur, q)
141}
142
143// Refund returns the escrow to the poster once the deadline passed.
144func Refund(cur realm, id uint64) {
145 caller := cur.Previous().Address()
146 q := mustQuest(id)
147 if err := q.Refund(caller, runtime.ChainHeight()); err != nil {
148 panic(err)
149 }
150 settleQuest(q)
151 pay(cur, q.Poster, q.Reward)
152 chain.Emit("QuestRefunded",
153 "id", ufmt.Sprintf("%d", id),
154 "poster", caller.String(),
155 "amount", ufmt.Sprintf("%d", q.Reward))
156}
157
158// Cancel withdraws an open quest and returns its escrow to the poster.
159func Cancel(cur realm, id uint64) {
160 caller := cur.Previous().Address()
161 q := mustQuest(id)
162 if err := q.Cancel(caller, runtime.ChainHeight()); err != nil {
163 panic(err)
164 }
165 settleQuest(q)
166 pay(cur, q.Poster, q.Reward)
167 chain.Emit("QuestCancelled",
168 "id", ufmt.Sprintf("%d", id),
169 "poster", caller.String(),
170 "amount", ufmt.Sprintf("%d", q.Reward))
171}
172
173// settleQuest moves a quest out of the active index and releases its
174// escrowed amount. Every terminal transition goes through here so the
175// index and the totals cannot drift apart.
176func settleQuest(q *guild.Quest) {
177 active.Remove(questKey(q.ID))
178 escrowTotal -= q.Reward
179}
180
181// payQuest settles a Paid quest: state and totals move before coins do.
182func payQuest(cur realm, q *guild.Quest) {
183 settleQuest(q)
184 recordPayout(q)
185 pay(cur, q.Worker, q.Reward)
186 auto := "false"
187 if q.AutoPaid {
188 auto = "true"
189 }
190 chain.Emit("QuestPaid",
191 "id", ufmt.Sprintf("%d", q.ID),
192 "worker", q.Worker.String(),
193 "amount", ufmt.Sprintf("%d", q.Reward),
194 "auto", auto)
195}
196
197// pay sends ugnot out of the realm. The banker is made inside the call and
198// never stored: a stored banker would be a permanent spending grant. cur is
199// always the caller's live realm value, which is what authorizes the send.
200func pay(cur realm, to address, amount int64) {
201 b := banker.NewBanker(banker.BankerTypeRealmSend, cur)
202 b.SendCoins(cur.Address(), to, chain.NewCoins(chain.NewCoin("ugnot", amount)))
203}
204
205func recordPayout(q *guild.Quest) {
206 paidTotal += q.Reward
207 wp := getProfile(q.Worker)
208 wp.Paid++
209 wp.Earned += q.Reward
210 wp.PaidIDs = append(wp.PaidIDs, q.ID)
211 if q.AutoPaid {
212 wp.AutoAccepted++
213 } else {
214 wp.Accepted++
215 }
216 pp := getProfile(q.Poster)
217 pp.Spent += q.Reward
218}
219
220func mustQuest(id uint64) *guild.Quest {
221 q, ok := quests.Get(questKey(id)).(*guild.Quest)
222 if !ok || q == nil {
223 panic("quest not found: " + strconv.FormatUint(id, 10))
224 }
225 return q
226}
227
228func getProfile(addr address) *profile {
229 key := addr.String()
230 if p, ok := profiles.Get(key).(*profile); ok && p != nil {
231 return p
232 }
233 p := &profile{}
234 profiles.Set(key, p)
235 return p
236}
237
238// findProfile reads a profile without creating one, for Render.
239func findProfile(addr address) *profile {
240 p, _ := profiles.Get(addr.String()).(*profile)
241 return p
242}
243
244// questKey zero-pads the id to a fixed 20 digits so AVL keys iterate in
245// numeric order ("2" sorts before "10"). ufmt's width flag is a no-op for
246// ints, so the padding is built by hand.
247func questKey(id uint64) string {
248 s := strconv.FormatUint(id, 10)
249 for len(s) < 20 {
250 s = "0" + s
251 }
252 return s
253}