pausable.gno
1.36 Kb ยท 60 lines
1// Package pausable provides a mechanism to programmatically pause and unpause
2// functionality. This package allows an owner, defined via an Ownable object,
3// to restrict operations or methods when the contract is in a "paused" state.
4package pausable
5
6import (
7 "errors"
8 "std"
9
10 "gno.land/p/demo/ownable"
11)
12
13type Pausable struct {
14 o *ownable.Ownable
15 paused bool
16}
17
18var ErrPaused = errors.New("pausable: realm is currently paused")
19
20// NewFromOwnable is the same as New, but with a pre-existing top-level ownable
21func NewFromOwnable(ownable *ownable.Ownable) *Pausable {
22 return &Pausable{
23 o: ownable,
24 paused: false,
25 }
26}
27
28// IsPaused checks if Pausable is paused
29func (p Pausable) IsPaused() bool {
30 return p.paused
31}
32
33// Pause sets the state of Pausable to true, meaning all pausable functions are paused
34func (p *Pausable) Pause() error {
35 if !p.o.OwnedByCurrent() {
36 return ownable.ErrUnauthorized
37 }
38
39 p.paused = true
40 std.Emit("Paused", "by", p.o.Owner().String())
41
42 return nil
43}
44
45// Unpause sets the state of Pausable to false, meaning all pausable functions are resumed
46func (p *Pausable) Unpause() error {
47 if !p.o.OwnedByCurrent() {
48 return ownable.ErrUnauthorized
49 }
50
51 p.paused = false
52 std.Emit("Unpaused", "by", p.o.Owner().String())
53
54 return nil
55}
56
57// Ownable returns the underlying ownable
58func (p *Pausable) Ownable() *ownable.Ownable {
59 return p.o
60}