royalties.gno
1.68 Kb · 51 lines
1package core
2
3import (
4 "chain"
5 "chain/banker"
6
7 "gno.land/p/g1jz6j9hj6rdf5ltl2kcrpa4jwtdh3w30z0yul06/sinopia4/safe/v2"
8)
9
10// Each collection's royalty receiver is its own sub-treasury
11// DerivePkgSubAddr(core, "roy/<cid>"), written into the official royalty
12// extension at creation. Any market can pay it through RoyaltyInfo; the
13// creator can re-split it later (SetPayees) without touching the receiver.
14// Only core can spend it: a banker for a sub-address needs core's live
15// cur.Sub (interrealm v2 §5.5).
16
17func royaltyAddress(cid string) address {
18 return chain.DerivePkgSubAddr(selfPath, "roy/"+cid)
19}
20
21// RoyaltyAddress is the collection's royalty sub-treasury.
22func RoyaltyAddress(cid string) address {
23 return royaltyAddress(mustCollection(cid).id)
24}
25
26// DistributeRoyalties pushes the whole sub-treasury balance to the royalty
27// payees. Permissionless and never paused (I-2); market/v1 calls it after
28// every sale. Returns the amount distributed. The sub-realm banker spend
29// is verified by spike/subtoken, core and market tests on gno v1.7.0.
30func DistributeRoyalties(cur realm, cid string) int64 {
31 assertNoDeposit(cur)
32 return distribute(cur, mustCollection(cid))
33}
34
35func distribute(cur realm, c *collection) int64 {
36 cid := c.id
37 sub := cur.Sub("roy/" + cid)
38 bal := banker.NewReadonlyBanker().GetCoin(sub.Address(), "ugnot")
39 if bal <= 0 {
40 return 0
41 }
42 shares := safe.SplitByBps(bal, c.royaltyPayees)
43 bk := banker.NewBanker(banker.BankerTypeRealmSend, sub)
44 for i, p := range c.royaltyPayees {
45 if shares[i] > 0 {
46 bk.SendCoins(sub.Address(), p.Addr, chain.NewCoins(chain.NewCoin("ugnot", shares[i])))
47 }
48 }
49 chain.Emit("RoyaltiesDistributed", "cid", cid, "amount", itoa(bal))
50 return bal
51}