github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/pausable/pausable.gno (about)

     1  package pausable
     2  
     3  import "gno.land/p/demo/ownable"
     4  
     5  type Pausable struct {
     6  	*ownable.Ownable
     7  	paused bool
     8  }
     9  
    10  // New returns a new Pausable struct with non-paused state as default
    11  func New() *Pausable {
    12  	return &Pausable{
    13  		Ownable: ownable.New(),
    14  		paused:  false,
    15  	}
    16  }
    17  
    18  // NewFromOwnable is the same as New, but with a pre-existing top-level ownable
    19  func NewFromOwnable(ownable *ownable.Ownable) *Pausable {
    20  	return &Pausable{
    21  		Ownable: ownable,
    22  		paused:  false,
    23  	}
    24  }
    25  
    26  // IsPaused checks if Pausable is paused
    27  func (p Pausable) IsPaused() bool {
    28  	return p.paused
    29  }
    30  
    31  // Pause sets the state of Pausable to true, meaning all pausable functions are paused
    32  func (p *Pausable) Pause() error {
    33  	if err := p.CallerIsOwner(); err != nil {
    34  		return err
    35  	}
    36  
    37  	p.paused = true
    38  	return nil
    39  }
    40  
    41  // Unpause sets the state of Pausable to false, meaning all pausable functions are resumed
    42  func (p *Pausable) Unpause() error {
    43  	if err := p.CallerIsOwner(); err != nil {
    44  		return err
    45  	}
    46  
    47  	p.paused = false
    48  	return nil
    49  }