treasury.gno
3.13 Kb ยท 129 lines
1package treasury
2
3import (
4 "chain/banker"
5 "chain/runtime"
6
7 "gno.land/p/demo/tokens/grc20"
8 t "gno.land/p/nt/treasury"
9
10 "gno.land/r/demo/defi/grc20reg"
11 "gno.land/r/gov/dao"
12)
13
14var (
15 treasury *t.Treasury
16 tokenKeys = []string{
17 // TODO: Add the default GRC20 tokens we want to support here.
18 }
19)
20
21func init() {
22 // Define a token lister for the GRC20Banker.
23 // For now, GovDAO uses a static list of tokens.
24 grc20Lister := func() map[string]*grc20.Token {
25 // Get the GRC20 tokens from the registry.
26 tokens := map[string]*grc20.Token{}
27 for _, key := range tokenKeys {
28 // Get the token by its key.
29 token := grc20reg.Get(key)
30 if token != nil {
31 tokens[key] = token
32 }
33 }
34
35 return tokens
36 }
37
38 // Init the treasury bankers.
39 coinsBanker, err := t.NewCoinsBanker(banker.NewBanker(banker.BankerTypeRealmSend))
40 if err != nil {
41 panic("failed to create CoinsBanker: " + err.Error())
42 }
43 grc20Banker, err := t.NewGRC20Banker(grc20Lister)
44 if err != nil {
45 panic("failed to create GRC20Banker: " + err.Error())
46 }
47 bankers := []t.Banker{
48 coinsBanker,
49 grc20Banker,
50 }
51
52 // Create the treasury instance with the bankers.
53 treasury, err = t.New(bankers)
54 if err != nil {
55 panic("failed to create treasury: " + err.Error())
56 }
57}
58
59// SetTokenKeys sets the GRC20 token registry keys that the treasury will use.
60func SetTokenKeys(_ realm, keys []string) {
61 caller := runtime.PreviousRealm().PkgPath()
62
63 // Check if the caller realm is allowed to set token keys.
64 if !dao.InAllowedDAOs(caller) {
65 panic("this Realm is not allowed to send payment: " + caller)
66 }
67
68 tokenKeys = keys
69}
70
71// Send sends a payment using the treasury instance.
72func Send(_ realm, payment t.Payment) {
73 caller := runtime.PreviousRealm().PkgPath()
74
75 // Check if the caller realm is allowed to send payments.
76 if !dao.InAllowedDAOs(caller) {
77 panic("this Realm is not allowed to send payment: " + caller)
78 }
79
80 // Send the payment using the treasury instance.
81 if err := treasury.Send(payment); err != nil {
82 panic(err)
83 }
84}
85
86// History returns the payment history sent by the banker with the given ID.
87// Payments are paginated, with the most recent payments first.
88func History(bankerID string, pageNumber int, pageSize int) []t.Payment {
89 history, err := treasury.History(bankerID, pageNumber, pageSize)
90 if err != nil {
91 panic("failed to get history: " + err.Error())
92 }
93
94 return history
95}
96
97// Balances returns the balances of the banker with the given ID.
98func Balances(bankerID string) []t.Balance {
99 balances, err := treasury.Balances(bankerID)
100 if err != nil {
101 panic("failed to get balances: " + err.Error())
102 }
103
104 return balances
105}
106
107// Address returns the address of the banker with the given ID.
108func Address(bankerID string) string {
109 addr, err := treasury.Address(bankerID)
110 if err != nil {
111 panic("failed to get address: " + err.Error())
112 }
113
114 return addr
115}
116
117// HasBanker checks if a banker with the given ID is registered.
118func HasBanker(bankerID string) bool {
119 return treasury.HasBanker(bankerID)
120}
121
122// ListBankerIDs returns a list of all registered banker IDs.
123func ListBankerIDs() []string {
124 return treasury.ListBankerIDs()
125}
126
127func Render(path string) string {
128 return treasury.Render(path)
129}