book.gno
5.40 Kb · 183 lines
1package tavern_offers
2
3import (
4 "chain"
5 "chain/banker"
6 "chain/runtime"
7 "chain/runtime/unsafe"
8 "strconv"
9 "strings"
10)
11
12type itemKey struct{ collection, id string }
13type offer struct {
14 buyer address
15 amount, nonce int64
16}
17type listing struct {
18 seller address
19 price, nonce int64
20}
21
22var offers = make(map[itemKey]offer)
23var listings = make(map[itemKey]listing)
24var sequence int64
25var bookAddress address
26
27func init(cur realm) { bookAddress = cur.Address() }
28func Address() address { return bookAddress }
29func fresh() int64 { sequence++; return sequence }
30
31// Limit also keeps basis-point arithmetic within int64 for collection adapters.
32const maxAmount = int64(900000000000000)
33
34func caller(cur realm) address {
35 if !cur.Previous().IsUserCall() {
36 panic("direct wallet call required")
37 }
38 return cur.Previous().Address()
39}
40func unpaid() {
41 if len(unsafe.OriginSend()) != 0 {
42 panic("unexpected payment")
43 }
44}
45func paid(amount int64) {
46 coins := unsafe.OriginSend()
47 if amount < 1 || amount > maxAmount || len(coins) != 1 || coins[0].Denom != "ugnot" || coins[0].Amount != amount {
48 panic("exact GNOT payment required")
49 }
50}
51func pay(cur realm, to address, amount int64) {
52 if amount > 0 {
53 banker.NewBanker(banker.BankerTypeRealmSend, cur).SendCoins(Address(), to, chain.NewCoins(chain.NewCoin("ugnot", amount)))
54 }
55}
56
57func Bid(cur realm, collection, id string, amount, expected int64) {
58 runtime.AssertOriginCall()
59 buyer := caller(cur)
60 paid(amount)
61 key := itemKey{collection, id}
62 owner := ownerOf(collection, id)
63 if owner == buyer || (owner == Address() && listings[key].seller == buyer) {
64 panic("cannot bid on own NFT")
65 }
66 old := offers[key]
67 if expected != old.nonce || amount <= old.amount {
68 panic("offer changed or too low")
69 }
70 offers[key] = offer{buyer, amount, fresh()}
71 pay(cur, old.buyer, old.amount)
72 chain.Emit("OfferPlaced", "collection", collection, "token", id, "amount", strconv.FormatInt(amount, 10))
73}
74
75func Cancel(cur realm, collection, id string, expected int64) {
76 runtime.AssertOriginCall()
77 buyer := caller(cur)
78 unpaid()
79 key := itemKey{collection, id}
80 old := offers[key]
81 if old.buyer != buyer || old.nonce != expected {
82 panic("offer changed or not yours")
83 }
84 offers[key] = offer{nonce: fresh()}
85 pay(cur, buyer, old.amount)
86 chain.Emit("OfferCancelled", "collection", collection, "token", id)
87}
88
89// Wallets always bundle Prepare and the owner's direct NFT transfer in one transaction.
90// A new owner may replace an abandoned preparation while the NFT is in their wallet.
91func Prepare(cur realm, collection, id string) {
92 runtime.AssertOriginCall()
93 seller := caller(cur)
94 unpaid()
95 if seller != ownerOf(collection, id) {
96 panic("token owner required")
97 }
98 listings[itemKey{collection, id}] = listing{seller, 0, fresh()}
99}
100func custody(cur realm, key itemKey) listing {
101 l := listings[key]
102 if l.seller != caller(cur) || ownerOf(key.collection, key.id) != Address() {
103 panic("seller deposit required")
104 }
105 return l
106}
107
108func List(cur realm, collection, id string, price int64) {
109 runtime.AssertOriginCall()
110 unpaid()
111 key := itemKey{collection, id}
112 l := custody(cur, key)
113 if price < 1 || price > maxAmount {
114 panic("invalid price")
115 }
116 listings[key] = listing{l.seller, price, fresh()}
117}
118
119func Withdraw(cur realm, collection, id string) {
120 runtime.AssertOriginCall()
121 unpaid()
122 key := itemKey{collection, id}
123 l := custody(cur, key)
124 delete(listings, key)
125 deliver(cur, collection, id, l.seller)
126}
127
128func settle(cur realm, key itemKey, buyer, seller address, gross, minNet int64) {
129 if buyer == seller {
130 panic("cannot buy own NFT")
131 }
132 recipient, fee := royalty(key.collection, key.id, gross)
133 if fee < 0 || fee > gross || (fee > 0 && !recipient.IsValid()) || gross-fee < minNet {
134 panic("invalid settlement terms")
135 }
136 delete(listings, key)
137 deliver(cur, key.collection, key.id, buyer)
138 pay(cur, recipient, fee)
139 pay(cur, seller, gross-fee)
140 chain.Emit("Sale", "collection", key.collection, "token", key.id, "buyer", buyer.String(), "seller", seller.String(), "gross", strconv.FormatInt(gross, 10), "royalty", strconv.FormatInt(fee, 10))
141}
142
143func Accept(cur realm, collection, id string, expected, minNet int64) {
144 runtime.AssertOriginCall()
145 unpaid()
146 key := itemKey{collection, id}
147 l := custody(cur, key)
148 b := offers[key]
149 if b.amount == 0 || b.nonce != expected {
150 panic("offer changed")
151 }
152 offers[key] = offer{nonce: fresh()}
153 settle(cur, key, b.buyer, l.seller, b.amount, minNet)
154}
155
156func Buy(cur realm, collection, id string, expected, price int64) {
157 runtime.AssertOriginCall()
158 buyer := caller(cur)
159 paid(price)
160 key := itemKey{collection, id}
161 l := listings[key]
162 if l.price != price || l.nonce != expected || ownerOf(collection, id) != Address() {
163 panic("listing changed")
164 }
165 b := offers[key]
166 offers[key] = offer{nonce: fresh()}
167 pay(cur, b.buyer, b.amount)
168 settle(cur, key, buyer, l.seller, price, 0)
169}
170
171// Numeric values are decimal strings to preserve integer precision in clients.
172func Snapshot(collection, id string) string {
173 key := itemKey{collection, id}
174 owner := ownerOf(collection, id)
175 b, l := offers[key], listings[key]
176 _, fee := royalty(collection, id, b.amount)
177 _, listFee := royalty(collection, id, l.price)
178 return strings.Join([]string{
179 owner.String(), l.seller.String(), strconv.FormatInt(l.price, 10), strconv.FormatInt(l.nonce, 10),
180 b.buyer.String(), strconv.FormatInt(b.amount, 10), strconv.FormatInt(b.nonce, 10),
181 strconv.FormatInt(fee, 10), Address().String(), strconv.FormatInt(listFee, 10),
182 }, "|")
183}