Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

book.gno

3.21 Kb · 139 lines
  1package col3333
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"strconv"
  7
  8	"gno.land/p/g1n4pl5uc4yt5r96m9w6fmdznx3x0jyg8l6arhmt/bazaar/fee/v1"
  9	"gno.land/r/g1n4pl5uc4yt5r96m9w6fmdznx3x0jyg8l6arhmt/bazaar/factory"
 10)
 11
 12const protocolBps = int64(50)
 13
 14func List(cur realm, id int, priceUgnot int64) {
 15	caller := requireUser(cur)
 16	if priceUgnot <= 0 {
 17		panic("col: price")
 18	}
 19	it := mustItem(id)
 20	if it.listed {
 21		panic("col: listed")
 22	}
 23	if it.owner != caller {
 24		panic("col: not owner")
 25	}
 26	grcTransfer(caller, caller, self, id)
 27	it.listed = true
 28	it.seller = caller
 29	it.price = priceUgnot
 30	it.owner = self
 31	items.Set(itemKey(id), it)
 32	chain.Emit("List", "id", strconv.Itoa(id), "seller", caller.String(), "price", strconv.FormatInt(priceUgnot, 10))
 33}
 34
 35func UpdatePrice(cur realm, id int, priceUgnot int64) {
 36	caller := requireUser(cur)
 37	if priceUgnot <= 0 {
 38		panic("col: price")
 39	}
 40	it := mustItem(id)
 41	if !it.listed {
 42		panic("col: not listed")
 43	}
 44	if it.seller != caller {
 45		panic("col: not seller")
 46	}
 47	it.price = priceUgnot
 48	items.Set(itemKey(id), it)
 49	chain.Emit("List", "id", strconv.Itoa(id), "seller", caller.String(), "price", strconv.FormatInt(priceUgnot, 10))
 50}
 51
 52func Cancel(cur realm, id int) {
 53	caller := requireUser(cur)
 54	it := mustItem(id)
 55	if !it.listed {
 56		panic("col: not listed")
 57	}
 58	if caller != it.seller {
 59		panic("col: not seller")
 60	}
 61	grcTransfer(self, self, it.seller, id)
 62	it.listed = false
 63	it.owner = it.seller
 64	it.seller = address("")
 65	it.price = 0
 66	items.Set(itemKey(id), it)
 67	chain.Emit("Cancel", "id", strconv.Itoa(id))
 68}
 69
 70func Buy(cur realm, id int) {
 71	caller := requireUser(cur)
 72	it := mustItem(id)
 73	if !it.listed {
 74		panic("col: not listed")
 75	}
 76	takeExact(it.price)
 77	if caller == it.seller {
 78		panic("col: self buy")
 79	}
 80	price := it.price
 81	proto := fee.ProtocolFee(price, protocolBps)
 82	royal := int64(0)
 83	if royaltyBps > 0 {
 84		royal = fee.ProtocolFee(price, int64(royaltyBps))
 85	}
 86	if proto+royal > price {
 87		panic("col: fee")
 88	}
 89	sellerAmt := price - proto - royal
 90	bk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
 91	if sellerAmt > 0 {
 92		bk.SendCoins(self, it.seller, chain.Coins{{Denom: nativeDenom, Amount: sellerAmt}})
 93	}
 94	if royal > 0 {
 95		bk.SendCoins(self, creator, chain.Coins{{Denom: nativeDenom, Amount: royal}})
 96	}
 97	if proto > 0 {
 98		bk.SendCoins(self, factory.Address(), chain.Coins{{Denom: nativeDenom, Amount: proto}})
 99	}
100	grcTransfer(self, self, caller, id)
101	it.listed = false
102	it.owner = caller
103	it.seller = address("")
104	it.price = 0
105	items.Set(itemKey(id), it)
106	chain.Emit("Buy", "id", strconv.Itoa(id), "buyer", caller.String(), "fee", strconv.FormatInt(proto, 10))
107}
108
109func Transfer(cur realm, to address, id int) {
110	caller := requireUser(cur)
111	if !to.IsValid() {
112		panic("col: invalid address")
113	}
114	if to == caller || to == self {
115		panic("col: invalid address")
116	}
117	it := mustItem(id)
118	if it.listed {
119		panic("col: listed")
120	}
121	if it.owner != caller {
122		panic("col: not owner")
123	}
124	grcTransfer(caller, caller, to, id)
125	it.owner = to
126	items.Set(itemKey(id), it)
127	chain.Emit("Transfer", "id", strconv.Itoa(id), "to", to.String())
128}
129
130func ProtocolBps() int64 {
131	return protocolBps
132}
133
134func grcTransfer(spender, from, to address, id int) {
135	err := ledger.TransferFrom(spender, from, to, itemTid(id))
136	if err != nil {
137		panic("col: transfer")
138	}
139}