Skip to content

Commit

Permalink
change event and modify the doc
Browse files Browse the repository at this point in the history
  • Loading branch information
DIGIX666 committed Nov 1, 2024
1 parent 000c615 commit 3b4c63a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 54 deletions.
45 changes: 23 additions & 22 deletions examples/gno.land/p/demo/accesscontrol/accesscontrol.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

const (
RoleName = "roleName"
Sender = "sender"
Account = "account"
NewAdminRole = "newAdminRole"
RoleCreatedEvent = "RoleCreated"
RoleGrantedEvent = "RoleGranted"
RoleRevokedEvent = "RoleRevoked"
RoleRenouncedEvent = "RoleRenounced"
RoleSetEvent = "RoleSet"
)

// Role struct to store role information
Expand Down Expand Up @@ -72,9 +73,9 @@ func (rs *Roles) CreateRole(name string) (*Role, error) {
rs.AllRoles = append(rs.AllRoles, role)

std.Emit(
"RoleCreated",
RoleName, name,
Sender, rs.Ownable.Owner().String(),
RoleCreatedEvent,
"roleName", name,
"sender", rs.Ownable.Owner().String(),
)

return role, nil
Expand Down Expand Up @@ -111,10 +112,10 @@ func (rs *Roles) GrantRole(name string, account std.Address) error {
r.Holders.Set(account.String(), struct{}{})

std.Emit(
"RoleGranted",
RoleName, r.Name,
Account, account.String(),
Sender, std.PrevRealm().Addr().String(),
RoleGrantedEvent,
"roleName", r.Name,
"account", account.String(),
"sender", std.PrevRealm().Addr().String(),
)

return nil
Expand All @@ -135,10 +136,10 @@ func (rs *Roles) RevokeRole(name string, account std.Address) error {
r.Holders.Remove(account.String())

std.Emit(
"RoleRevoked",
RoleName, r.Name,
Account, account.String(),
Sender, std.PrevRealm().Addr().String(),
RoleRevokedEvent,
"roleName", r.Name,
"account", account.String(),
"sender", std.PrevRealm().Addr().String(),
)

return nil
Expand All @@ -160,10 +161,10 @@ func (rs *Roles) RenounceRole(name string) error {
r.Holders.Remove(caller.String())

std.Emit(
"RoleRenounced",
RoleName, r.Name,
Account, caller.String(),
Sender, caller.String(),
RoleRenouncedEvent,
"roleName", r.Name,
"account", caller.String(),
"sender", caller.String(),
)

return nil
Expand All @@ -176,9 +177,9 @@ func (r *Role) SetRoleAdmin(admin std.Address) error {
}

std.Emit(
"RoleSet",
RoleName, r.Name,
NewAdminRole, r.Ownable.Owner().String(),
RoleSetEvent,
"roleName", r.Name,
"newAdminRole", r.Ownable.Owner().String(),
)

return nil
Expand Down
29 changes: 29 additions & 0 deletions examples/gno.land/p/demo/accesscontrol/doc.gno
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,33 @@
// Role ownership can be transferred using `SetRoleAdmin`.
//
// SetRoleAdmin(newAdminAddress)
//
// Key events
// - `RoleCreatedEvent`: Triggered when a new role is created
// Key includes:
// `roleName` (name of the role)
// `sender` (address of the sender)
//
// - `RoleGrantedEvent`: Triggered when a role is granted to an account
// Key includes:
// `roleName` (name of the role)
// `account` (address of the account)
// `sender` (address of the sender)
//
// - `RoleRevokedEvent`: Triggered when a role is revoked from an account
// Key includes:
// `roleName` (name of the role)
// `account` (address of the account)
// `sender` (address of the sender)
//
// - `RoleRenouncedEvent`: Triggered when a role is renounced by an account
// Key includes:
// `roleName` (name of the role)
// `account` (address of the account)
//
// - `RoleSetEvent`: Triggered when a role's administrator is set or changed
// Key includes:
// `roleName` (name of the role)
// `newAdmin` (address of the new administrator)
// `sender` (address of the sender)
package accesscontrol
32 changes: 32 additions & 0 deletions examples/gno.land/p/demo/timelock/doc.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Package timelock provides a library for scheduling, cancelling, and
// executing time-locked operations in Gno. It ensures that
// operations are only carried out after a specified delay and offers
// mechanisms for managing and verifying the status of these operations.
// This package leverages an AVL tree for efficient management of timestamps
// and integrates role-based access control for administrative tasks.
//
// # Usage:
//
// import "gno.land/p/demo/timelock"
// import "gno.land/p/demo/accesscontrol"
//
// Initialize timelock utility with an AVL tree and access control.
// timestamps := avl.NewTree()
// adminRole := accesscontrol.NewRole("admin", std.Address("admin-address"))
// timeLockUtil := timelock.NewTimeLockUtil(timestamps, adminRole, 30)
//
// Schedule an operation with a delay of 60 seconds.
// id := seqid.ID()
// timeLockUtil.Schedule(id, 60)
//
// Check if an operation is pending.
// isPending := timeLockUtil.IsPending(id)
//
// Execute the operation when it is pending.
// if timeLockUtil.IsPending(id) {
// timeLockUtil.Execute(id)
// }
//
// Update the minimum delay for future operations.
// timeLockUtil.UpdateDelay(45)
package timelock
32 changes: 0 additions & 32 deletions examples/gno.land/p/demo/timelock/timelock.gno
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
// Package timelock provides a library for scheduling, cancelling, and
// executing time-locked operations in Gno. It ensures that
// operations are only carried out after a specified delay and offers
// mechanisms for managing and verifying the status of these operations.
// This package leverages an AVL tree for efficient management of timestamps
// and integrates role-based access control for administrative tasks.
//
// Example Usage:
//
// import "gno.land/p/demo/timelock"
// import "gno.land/p/demo/accesscontrol"
//
// Initialize timelock utility with an AVL tree and access control.
// timestamps := avl.NewTree()
// adminRole := accesscontrol.NewRole("admin", std.Address("admin-address"))
// timeLockUtil := timelock.NewTimeLockUtil(timestamps, adminRole, 30)
//
// Schedule an operation with a delay of 60 seconds.
// id := seqid.ID()
// timeLockUtil.Schedule(id, 60)
//
// Check if an operation is pending.
// isPending := timeLockUtil.IsPending(id)
//
// Execute the operation when it is pending.
// if timeLockUtil.IsPending(id) {
// timeLockUtil.Execute(id)
// }
//
// Update the minimum delay for future operations.
// timeLockUtil.UpdateDelay(45)

package timelock

import (
Expand Down

0 comments on commit 3b4c63a

Please sign in to comment.