z_example_filetest.gno
2.79 Kb · 103 lines
1// PKGPATH: gno.land/r/example/bounty
2
3// This is the "Using p/escrow in your own realm" example from README.md, kept
4// here so that `gno test` compiles and runs it. The realm part (everything
5// above main) is what the README shows; main plays two users through one
6// quest and prints the result.
7package bounty
8
9import (
10 "chain"
11 "chain/banker"
12 "chain/runtime"
13 "chain/runtime/unsafe"
14 "strconv"
15 "testing"
16
17 "gno.land/p/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/escrow"
18 "gno.land/p/nt/avl/v0"
19)
20
21var (
22 quests avl.Tree // id -> *escrow.Quest
23 lastID uint64
24)
25
26// Post locks the ugnot sent with the call as the reward.
27func Post(cur realm, title string, workBlocks int64) uint64 {
28 if !cur.Previous().IsUserCall() {
29 panic("direct user calls only")
30 }
31 sent := unsafe.OriginSend()
32 if len(sent) != 1 || sent[0].Denom != "ugnot" {
33 panic("send the reward as one ugnot coin")
34 }
35 lastID++
36 q, err := escrow.NewQuest(lastID, cur.Previous().Address(), title, "", sent[0].Amount, workBlocks, runtime.ChainHeight())
37 if err != nil {
38 panic(err.Error())
39 }
40 quests.Set(strconv.FormatUint(lastID, 10), q)
41 return lastID
42}
43
44func Claim(cur realm, id uint64) {
45 check(get(id).Claim(cur.Previous().Address(), runtime.ChainHeight()))
46}
47
48func Submit(cur realm, id uint64, link string) {
49 check(get(id).Submit(cur.Previous().Address(), link, runtime.ChainHeight()))
50}
51
52// Approve applies the move first, then pays exactly what the escrow says.
53func Approve(cur realm, id uint64) {
54 p, err := get(id).Approve(cur.Previous().Address(), runtime.ChainHeight())
55 check(err)
56 pay(p, cur)
57}
58
59func pay(p escrow.Payout, rlm realm) {
60 b := banker.NewBanker(banker.BankerTypeRealmSend, rlm)
61 b.SendCoins(rlm.Address(), p.To, chain.Coins{{Denom: "ugnot", Amount: p.Amount}})
62}
63
64func get(id uint64) *escrow.Quest {
65 v := quests.Get(strconv.FormatUint(id, 10))
66 if v == nil {
67 panic("unknown quest")
68 }
69 return v.(*escrow.Quest)
70}
71
72func check(err error) {
73 if err != nil {
74 panic(err.Error())
75 }
76}
77
78// ---- test driver (not part of the README example)
79
80func main(cur realm) {
81 poster := address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
82 builder := address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
83 reward := chain.Coins{{Denom: "ugnot", Amount: 2_000_000}}
84 testing.IssueCoins(cur.Address(), reward) // what -send does on chain
85
86 testing.SetRealm(testing.NewUserRealm(poster))
87 testing.SetOriginSend(reward)
88 id := Post(cross(cur), "Write a guide", escrow.MinWorkBlocks)
89 testing.SetOriginSend(nil)
90
91 testing.SetRealm(testing.NewUserRealm(builder))
92 Claim(cross(cur), id)
93 Submit(cross(cur), id, "https://example.com/guide")
94
95 testing.SetRealm(testing.NewUserRealm(poster))
96 Approve(cross(cur), id)
97
98 bk := banker.NewReadonlyBanker()
99 println(get(id).State().String(), bk.GetCoin(builder, "ugnot"), bk.GetCoin(cur.Address(), "ugnot"))
100}
101
102// Output:
103// Paid 2000000 0