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