Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

dao_sponsor.gno

2.42 Kb · 68 lines
 1// Package dao_sponsor shows how an authorized DAO executor can sponsor a budget.
 2package dao_sponsor
 3
 4import (
 5	"chain/banker"
 6	"chain/runtime/unsafe"
 7
 8	"gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/maintenance/v4"
 9	"gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v4"
10)
11
12const lane = "maintenance"
13
14var (
15	realmPath = unsafe.CurrentRealm().PkgPath()
16	// The deployer acts as the governance executor in this example.
17	executor = unsafe.PreviousRealm().Address()
18)
19
20// CreateAndOpen assembles the budget and funds it from this realm's native balance.
21// Send coins to the realm's address before calling it.
22func CreateAndOpen(cur realm, title, policyPath string, coordinator address, reviewersCSV string, quorum int, amount int64) int64 {
23	requireExecutor(cur)
24	id := treasury.CreateProgram(cross(cur), title, "", 0, 100, 1000, 80, 0)
25	treasury.AddPolicy(cross(cur), id, lane, policyPath, amount, amount, false)
26	maintenance.Configure(cross(cur), id, lane, coordinator, reviewersCSV, quorum)
27	funds := banker.NewBanker(banker.BankerTypeRealmSend, cur)
28	treasury.OpenFromBanker(cross(cur), id, funds, amount)
29	return id
30}
31
32// Contribute funds any active native program from this realm's balance.
33// The executor can deposit coins into this realm in the same call.
34func Contribute(cur realm, programID, amount int64) {
35	requireExecutor(cur)
36	if cur.Previous().IsUserCall() {
37		// An attachment credits this realm before the banker sends the requested amount.
38		unsafe.OriginSend()
39	}
40	funds := banker.NewBanker(banker.BankerTypeRealmSend, cur)
41	treasury.FundFromBanker(cross(cur), programID, funds, amount)
42}
43
44func RotateCoordinator(cur realm, programID int64, newCoordinator address) {
45	requireExecutor(cur)
46	maintenance.RotateCoordinator(cross(cur), programID, lane, newCoordinator)
47}
48
49func CloseIssuance(cur realm, programID int64) {
50	requireExecutor(cur)
51	treasury.CloseIssuance(cross(cur), programID)
52}
53
54// Withdraw collects an approved claim payable to this realm.
55// For a sponsor refund, pass lane "refund" and item ID 0 after finalization.
56func Withdraw(cur realm, programID int64, lane string, itemID int64) {
57	requireExecutor(cur)
58	treasury.Withdraw(cross(cur), programID, lane, itemID)
59}
60
61func requireExecutor(cur realm) {
62	if !cur.IsCurrent() || cur.PkgPath() != realmPath {
63		panic("invalid DAO sponsor context")
64	}
65	if cur.Previous().Address() != executor {
66		panic("only the DAO executor can authorize this action")
67	}
68}