payment.gno
1.46 Kb · 56 lines
1package memba_collections
2
3import (
4 "chain"
5 "chain/banker"
6 "chain/runtime"
7 "chain/runtime/unsafe"
8
9 "gno.land/p/demo/tokens/grc20"
10 "gno.land/r/demo/defi/grc20reg"
11)
12
13func chainHeight() int64 { return runtime.ChainHeight() }
14
15func selfAddr() address { return unsafe.CurrentRealm().Address() }
16
17// denomKey normalizes a payDenom to its proceeds-ledger key (native → "ugnot").
18func denomKey(d string) string {
19 if isNativeDenom(d) {
20 return "ugnot"
21 }
22 return d
23}
24
25// isNativeDenom reports whether a payDenom string means native ugnot.
26func isNativeDenom(d string) bool { return d == "" || d == "ugnot" }
27
28// sumDenom totals the coins of a single denom in a coin set.
29func sumDenom(coins chain.Coins, denom string) int64 {
30 total := int64(0)
31 for _, c := range coins {
32 if c.Denom == denom {
33 total += c.Amount
34 }
35 }
36 return total
37}
38
39// sendNative sends ugnot from the realm to `to` via a realm-send banker.
40func sendNative(cur realm, to address, amount int64) {
41 if amount <= 0 {
42 return
43 }
44 bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
45 bnk.SendCoins(unsafe.CurrentRealm().Address(), to, chain.Coins{chain.NewCoin("ugnot", amount)})
46}
47
48// grc20Teller resolves a registered token by its grc20reg key and returns a
49// RealmTeller (the realm is the spender — frozen B-1). Panics if unknown.
50func grc20Teller(cur realm, denom string) grc20.Teller {
51 tok := grc20reg.Get(denom)
52 if tok == nil {
53 panic("unknown grc20 denom: " + denom)
54 }
55 return tok.RealmTeller(0, cur)
56}