book.gno
2.77 Kb · 125 lines
1package fish
2
3import (
4 "chain"
5 "strconv"
6)
7
8func List(cur realm, id int, priceUgnot int64) {
9 caller := requireUser(cur)
10 if priceUgnot <= 0 || priceUgnot > maxPrice {
11 panic("fish: price")
12 }
13 takeExact(0)
14 body := must(id)
15 if body.owner != caller {
16 panic("fish: owner")
17 }
18 guardMovable(body)
19 if body.mustHeal {
20 panic("fish: heal")
21 }
22 moveToken(caller, caller, self, id)
23 body.listed = true
24 body.seller = caller
25 body.price = priceUgnot
26 body.owner = self
27 chain.Emit("List", "id", strconv.Itoa(id), "seller", caller.String(), "price", strconv.FormatInt(priceUgnot, 10))
28}
29
30func UpdatePrice(cur realm, id int, priceUgnot int64) {
31 caller := requireUser(cur)
32 if priceUgnot <= 0 || priceUgnot > maxPrice {
33 panic("fish: price")
34 }
35 takeExact(0)
36 body := must(id)
37 if !body.listed {
38 panic("fish: listed")
39 }
40 if body.seller != caller {
41 panic("fish: owner")
42 }
43 body.price = priceUgnot
44 chain.Emit("List", "id", strconv.Itoa(id), "seller", caller.String(), "price", strconv.FormatInt(priceUgnot, 10))
45}
46
47func Cancel(cur realm, id int) {
48 caller := requireUser(cur)
49 takeExact(0)
50 body := must(id)
51 if !body.listed {
52 panic("fish: listed")
53 }
54 if body.seller != caller {
55 panic("fish: owner")
56 }
57 moveToken(self, self, body.seller, id)
58 body.owner = body.seller
59 body.seller = address("")
60 body.price = 0
61 body.listed = false
62 chain.Emit("Cancel", "id", strconv.Itoa(id))
63}
64
65func Buy(cur realm, id int) {
66 caller := requireUser(cur)
67 body := must(id)
68 if !body.listed {
69 panic("fish: listed")
70 }
71 if caller == body.seller {
72 panic("fish: self")
73 }
74 takeExact(body.price)
75 proto := body.price * protocolBps / 10000
76 royal := body.price * royaltyBps / 10000
77 if proto+royal > body.price {
78 panic("fish: fee")
79 }
80 sellerAmt := body.price - proto - royal
81 send(body.seller, sellerAmt, cur)
82 send(creator, royal, cur)
83 send(protocolSink, proto, cur)
84 moveToken(self, self, caller, id)
85 body.owner = caller
86 body.seller = address("")
87 body.price = 0
88 body.listed = false
89 chain.Emit("Buy", "id", strconv.Itoa(id), "buyer", caller.String())
90}
91
92func ItemLine(id int) string {
93 body := must(id)
94 return strconv.Itoa(body.id) + "|" + body.name + "|" + addrStr(body.owner) + "|" + addrStr(body.seller) + "|" +
95 strconv.FormatInt(body.price, 10) + "|" + strconv.FormatBool(body.listed) + "|" + body.image + "|fish|" +
96 body.rarity + "|Size:" + body.size + ";Trait:" + body.trait + ";Species:" + body.species + ";HP:" + strconv.Itoa(body.hp) +
97 "|true|-1"
98}
99
100func ListOpen() string {
101 requireInit()
102 out := ""
103 n := 0
104 for _, body := range bodies {
105 if body == nil || !body.listed || body.burned {
106 continue
107 }
108 if n >= listCap {
109 break
110 }
111 if out != "" {
112 out += "\n"
113 }
114 out += ItemLine(body.id)
115 n++
116 }
117 return out
118}
119
120func addrStr(a address) string {
121 if !a.IsValid() {
122 return ""
123 }
124 return a.String()
125}