fee.gno
2.80 Kb · 109 lines
1package rooms
2
3import (
4 "chain"
5 "math"
6 "strconv"
7
8 "gno.land/p/nt/avl/v0"
9)
10
11// The service fee. When a room settles, the realm keeps FeeBps basis points
12// of every coin and token that crosses, from each side, and sends it to the
13// treasury; the rest goes to the other party. NFTs cross whole and pay
14// nothing. Withdrawing, cancelling and expiring cost nothing.
15//
16// A room keeps the rate it was opened with, so the admin can never raise
17// the fee on a trade already under way, and no rate can exceed MaxFeeBps.
18
19const (
20 // DefaultFeeBps is 0.5%.
21 DefaultFeeBps = 50
22 // MaxFeeBps caps the fee at 1%, whatever the admin sets.
23 MaxFeeBps = 100
24)
25
26var (
27 feeBps uint64 = DefaultFeeBps
28 treasury address // set to the deployer in init; fees are off while it is empty
29
30 fees avl.Tree // kind + ":" + denom -> int64 fees collected
31)
32
33// SetFee sets the rate for rooms opened from now on, in basis points
34// (50 is 0.5%), at most MaxFeeBps. Rooms already open keep their rate.
35func SetFee(cur realm, bps uint64) {
36 assertAdmin(cur)
37 if bps > MaxFeeBps {
38 panic("the fee can be at most " + strconv.Itoa(MaxFeeBps) + " basis points")
39 }
40 feeBps = bps
41 chain.Emit("FeeChanged", "bps", strconv.FormatUint(bps, 10))
42}
43
44// SetTreasury names the account that receives fees from now on.
45func SetTreasury(cur realm, to string) {
46 assertAdmin(cur)
47 addr := address(to)
48 if !addr.IsValid() {
49 panic("treasury is not a valid address")
50 }
51 treasury = addr
52 chain.Emit("TreasuryChanged", "treasury", addr.String())
53}
54
55// FeeBps returns the rate a room opened now would get.
56func FeeBps() uint64 {
57 return openingFee()
58}
59
60// Treasury returns the account that receives fees, or "" when fees are off.
61func Treasury() string {
62 return treasury.String()
63}
64
65// openingFee is the rate a new room keeps for its whole life.
66func openingFee() uint64 {
67 if treasury == "" {
68 return 0
69 }
70 return feeBps
71}
72
73// feeOf is the fee on amount at bps basis points, rounded down. It never
74// overflows: bps is at most MaxFeeBps.
75func feeOf(amount int64, bps uint64) int64 {
76 if bps == 0 || amount <= 0 {
77 return 0
78 }
79 b := int64(bps)
80 return amount/10000*b + amount%10000*b/10000
81}
82
83// pay sends one asset of a settling room to the party receiving it, less
84// the room's fee on coins and tokens.
85func (r *room) pay(cur realm, to address, a asset) {
86 fee := int64(0)
87 if a.kind != KindNFT {
88 fee = feeOf(a.amount, r.feeBps)
89 }
90 if fee > 0 {
91 send(cur, treasury, asset{kind: a.kind, denom: a.denom, amount: fee})
92 addFee(a.kind, a.denom, fee)
93 }
94 send(cur, to, asset{kind: a.kind, denom: a.denom, amount: a.amount - fee})
95}
96
97func addFee(kind, denom string, amount int64) {
98 key := kind + ":" + denom
99 total := int64(0)
100 if v := fees.Get(key); v != nil {
101 total = v.(int64)
102 }
103 if amount > math.MaxInt64-total {
104 total = math.MaxInt64
105 } else {
106 total += amount
107 }
108 fees.Set(key, total)
109}