example_test.gno

2.43 Kb ยท 90 lines
 1package authz
 2
 3import (
 4	"std"
 5)
 6
 7// Example_basic demonstrates initializing and using a basic member authority
 8func Example_basic() {
 9	// Initialize with contract deployer as authority
10	auth := NewWithOrigin()
11
12	// Use the authority to perform a privileged action
13	auth.DoByCurrent("update_config", func() error {
14		// config = newValue
15		return nil
16	})
17}
18
19// Example_addingMembers demonstrates how to add new members to a member authority
20func Example_addingMembers() {
21	// Initialize with contract deployer as authority
22	addr := std.CurrentRealm().Address()
23	auth := NewWithCurrent()
24
25	// Add a new member to the authority
26	memberAuth := auth.Authority().(*MemberAuthority)
27	memberAuth.AddMember(addr, std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"))
28}
29
30// Example_contractAuthority demonstrates using a contract-based authority
31func Example_contractAuthority() {
32	// Initialize with contract authority (e.g., DAO)
33	auth := NewWithAuthority(
34		NewContractAuthority(
35			"gno.land/r/demo/dao",
36			mockDAOHandler, // defined elsewhere for example
37		),
38	)
39
40	// Privileged actions will be handled by the contract
41	auth.DoByCurrent("update_params", func() error {
42		// Executes after DAO approval
43		return nil
44	})
45}
46
47// Example_restrictedContractAuthority demonstrates a contract authority with member-only proposals
48func Example_restrictedContractAuthority() {
49	// Initialize member authority for proposers
50	proposerAuth := NewMemberAuthority(
51		std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"), // admin1
52		std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj"), // admin2
53	)
54
55	// Create contract authority with restricted proposers
56	auth := NewWithAuthority(
57		NewRestrictedContractAuthority(
58			"gno.land/r/demo/dao",
59			mockDAOHandler,
60			proposerAuth,
61		),
62	)
63
64	// Only members can propose, and contract must approve
65	auth.DoByCurrent("update_params", func() error {
66		// Executes after:
67		// 1. Proposer initiates
68		// 2. DAO approves
69		return nil
70	})
71}
72
73// Example_switchingAuthority demonstrates switching from member to contract authority
74func Example_switchingAuthority() {
75	// Start with member authority (deployer)
76	addr := std.CurrentRealm().Address()
77	auth := NewWithCurrent()
78
79	// Create and switch to contract authority
80	daoAuthority := NewContractAuthority(
81		"gno.land/r/demo/dao",
82		mockDAOHandler,
83	)
84	auth.Transfer(addr, daoAuthority)
85}
86
87// Mock handler for examples
88func mockDAOHandler(title string, action PrivilegedAction) error {
89	return action()
90}