Search Apps Documentation Source Content File Folder Download Copy Actions Download

treasury.gno

3.45 Kb · 131 lines
  1package treasury
  2
  3import (
  4	"chain/banker"
  5
  6	"gno.land/p/demo/tokens/grc20"
  7	t "gno.land/p/nt/treasury/v0"
  8
  9	"gno.land/r/demo/defi/grc20reg"
 10	"gno.land/r/gov/dao"
 11)
 12
 13var (
 14	treasury  *t.Treasury
 15	tokenKeys = []string{
 16		// TODO: Add the default GRC20 tokens we want to support here.
 17	}
 18)
 19
 20func init(cur realm) {
 21	// Define a token lister for the GRC20Banker.
 22	// For now, GovDAO uses a static list of tokens.
 23	grc20Lister := func() map[string]*grc20.Token {
 24		// Get the GRC20 tokens from the registry.
 25		tokens := map[string]*grc20.Token{}
 26		for _, key := range tokenKeys {
 27			// Get the token by its key.
 28			token := grc20reg.Get(key)
 29			if token != nil {
 30				tokens[key] = token
 31			}
 32		}
 33
 34		return tokens
 35	}
 36
 37	// Init the treasury bankers.
 38	coinsBanker, err := t.NewCoinsBankerWithOwner(cur.Address(), banker.NewBanker(banker.BankerTypeRealmSend, cur))
 39	if err != nil {
 40		panic("failed to create CoinsBanker: " + err.Error())
 41	}
 42	grc20Banker, err := t.NewGRC20BankerWithOwner(cur.Address(), grc20Lister)
 43	if err != nil {
 44		panic("failed to create GRC20Banker: " + err.Error())
 45	}
 46	bankers := []t.Banker{
 47		coinsBanker,
 48		grc20Banker,
 49	}
 50
 51	// Create the treasury instance with the bankers. cur.PkgPath() is
 52	// captured for render-link construction (See full history → /r/gov/dao/v3/treasury:.../history).
 53	treasury, err = t.New(bankers, cur.PkgPath())
 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(cur realm, keys []string) {
 61	caller := cur.Previous().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(cur realm, payment t.Payment) {
 73	caller := cur.Previous().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. cur is this realm's
 81	// captured cur — passes IsCurrent inside Banker.Send and matches the
 82	// banker's owner (this realm's address) registered at init.
 83	if err := treasury.Send(0, cur, payment); err != nil {
 84		panic(err)
 85	}
 86}
 87
 88// History returns the payment history sent by the banker with the given ID.
 89// Payments are paginated, with the most recent payments first.
 90func History(bankerID string, pageNumber int, pageSize int) []t.Payment {
 91	history, err := treasury.History(bankerID, pageNumber, pageSize)
 92	if err != nil {
 93		panic("failed to get history: " + err.Error())
 94	}
 95
 96	return history
 97}
 98
 99// Balances returns the balances of the banker with the given ID.
100func Balances(bankerID string) []t.Balance {
101	balances, err := treasury.Balances(bankerID)
102	if err != nil {
103		panic("failed to get balances: " + err.Error())
104	}
105
106	return balances
107}
108
109// Address returns the address of the banker with the given ID.
110func Address(bankerID string) string {
111	addr, err := treasury.Address(bankerID)
112	if err != nil {
113		panic("failed to get address: " + err.Error())
114	}
115
116	return addr
117}
118
119// HasBanker checks if a banker with the given ID is registered.
120func HasBanker(bankerID string) bool {
121	return treasury.HasBanker(bankerID)
122}
123
124// ListBankerIDs returns a list of all registered banker IDs.
125func ListBankerIDs() []string {
126	return treasury.ListBankerIDs()
127}
128
129func Render(path string) string {
130	return treasury.Render(path)
131}