Playground
gnomod.toml
module = "gno.land/r/gov/dao/treasury/v0" gno = "0.9" [addpkg] creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"
treasury.gno
package treasury
import (
"chain/banker"
"gno.land/p/nt/grc20/v0"
t "gno.land/p/nt/treasury/v0"
"gno.land/r/gov/dao"
"gno.land/r/nt/grc20reg/v0"
)
var (
treasury *t.Treasury
tokenKeys = []string{
// TODO: Add the default GRC20 tokens we want to support here.
}
)
func init(cur realm) {
// Define a token lister for the GRC20Banker.
// For now, GovDAO uses a static list of tokens.
grc20Lister := func() map[string]*grc20.Token {
// Get the GRC20 tokens from the registry.
tokens := map[string]*grc20.Token{}
for _, key := range tokenKeys {
// Get the token by its key.
token := grc20reg.Get(key)
if token != nil {
tokens[key] = token
}
}
return tokens
}
// Init the treasury bankers.
coinsBanker, err := t.NewCoinsBankerWithOwner(cur.Address(), banker.NewBanker(banker.BankerTypeRealmSend, cur))
if err != nil {
panic("failed to create CoinsBanker: " + err.Error())
}
grc20Banker, err := t.NewGRC20BankerWithOwner(cur.Address(), grc20Lister)
if err != nil {
panic("failed to create GRC20Banker: " + err.Error())
}
bankers := []t.Banker{
coinsBanker,
grc20Banker,
}
// Create the treasury instance with the bankers. cur.PkgPath() is
// captured for render-link construction (See full history → /r/gov/dao/treasury/v0:.../history).
treasury, err = t.New(bankers, cur.PkgPath())
if err != nil {
panic("failed to create treasury: " + err.Error())
}
}
// SetTokenKeys sets the GRC20 token registry keys that the treasury will use.
func SetTokenKeys(cur realm, keys []string) {
// Crossing function, so cur is a live minted cur and this is redundant
// today; kept to match the documented rule that caller identity is
// derived from cur.Previous() only under an IsCurrent() guard.
if !cur.IsCurrent() {
panic("realm value is not the caller's live cur")
}
caller := cur.Previous().PkgPath()
// Check if the caller realm is allowed to set token keys.
if !dao.InAllowedDAOs(caller) {
panic("this Realm is not allowed to send payment: " + caller)
}
// Copied, not aliased: the grc20Lister closure re-reads tokenKeys on every
// Balances() and every GRC20 payment, so storing the caller's array as
// given would leave it a permanent write handle on this realm's token set.
tokenKeys = make([]string, len(keys))
copy(tokenKeys, keys)
}
// Send sends a payment using the treasury instance.
func Send(cur realm, payment t.Payment) {
// See SetTokenKeys: redundant under the crossing-function guarantee, kept
// for conformance with the IsCurrent()-before-Previous() rule.
if !cur.IsCurrent() {
panic("realm value is not the caller's live cur")
}
caller := cur.Previous().PkgPath()
// Check if the caller realm is allowed to send payments.
if !dao.InAllowedDAOs(caller) {
panic("this Realm is not allowed to send payment: " + caller)
}
// Send the payment using the treasury instance. cur is this realm's
// captured cur — passes IsCurrent inside Banker.Send and matches the
// banker's owner (this realm's address) registered at init.
if err := treasury.Send(0, cur, payment); err != nil {
panic(err)
}
}
// History returns the payment history sent by the banker with the given ID.
// Payments are paginated, with the most recent payments first.
func History(bankerID string, pageNumber int, pageSize int) []t.Payment {
history, err := treasury.History(bankerID, pageNumber, pageSize)
if err != nil {
panic("failed to get history: " + err.Error())
}
return history
}
// Balances returns the balances of the banker with the given ID.
func Balances(bankerID string) []t.Balance {
balances, err := treasury.Balances(bankerID)
if err != nil {
panic("failed to get balances: " + err.Error())
}
return balances
}
// Address returns the address of the banker with the given ID.
func Address(bankerID string) string {
addr, err := treasury.Address(bankerID)
if err != nil {
panic("failed to get address: " + err.Error())
}
return addr
}
// HasBanker checks if a banker with the given ID is registered.
func HasBanker(bankerID string) bool {
return treasury.HasBanker(bankerID)
}
// ListBankerIDs returns a list of all registered banker IDs.
func ListBankerIDs() []string {
return treasury.ListBankerIDs()
}
func Render(path string) string {
return treasury.Render(path)
}