Search Apps Documentation Source Content File Folder Download Copy Actions Download

actions.gno

2.41 Kb ยท 112 lines
  1package daokit
  2
  3import (
  4	"errors"
  5
  6	"gno.land/p/moul/md"
  7	"gno.land/p/nt/ufmt"
  8)
  9
 10type Action interface {
 11	String() string
 12	Type() string
 13}
 14
 15type ActionHandler interface {
 16	Execute(action Action)
 17	Type() string
 18}
 19
 20func NewAction(kind string, payload interface{}) Action {
 21	return &genericAction{kind: kind, payload: payload}
 22}
 23
 24type genericAction struct {
 25	kind    string
 26	payload interface{}
 27}
 28
 29// String implements Action.
 30func (g *genericAction) String() string {
 31	return ufmt.Sprintf("%v", g.payload)
 32}
 33
 34// Type implements Action.
 35func (g *genericAction) Type() string {
 36	return g.kind
 37}
 38
 39func NewActionHandler(kind string, executor func(interface{})) ActionHandler {
 40	return &genericActionHandler{kind: kind, executor: executor}
 41}
 42
 43type genericActionHandler struct {
 44	kind     string
 45	executor func(payload interface{})
 46}
 47
 48// Execute implements ActionHandler.
 49func (g *genericActionHandler) Execute(iaction Action) {
 50	action, ok := iaction.(*genericAction)
 51	if !ok {
 52		panic(errors.New("invalid action type"))
 53	}
 54	g.executor(action.payload)
 55}
 56
 57// Instantiate implements ActionHandler.
 58func (g *genericActionHandler) Instantiate() Action {
 59	return &genericAction{
 60		kind: g.kind,
 61	}
 62}
 63
 64// Type implements ActionHandler.
 65func (g *genericActionHandler) Type() string {
 66	return g.kind
 67}
 68
 69const ActionExecuteLambdaKind = "gno.land/p/samcrew/daokit.ExecuteLambda"
 70
 71func NewExecuteLambdaHandler() ActionHandler {
 72	return NewActionHandler(ActionExecuteLambdaKind, func(i interface{}) {
 73		cb, ok := i.(func())
 74		if !ok {
 75			panic(errors.New("invalid action type"))
 76		}
 77		cb()
 78	})
 79}
 80
 81func NewExecuteLambdaAction(cb func()) Action {
 82	return NewAction(ActionExecuteLambdaKind, cb)
 83}
 84
 85const ActionInstantExecuteKind = "gno.land/p/samcrew/daokit.InstantExecute"
 86
 87type actionInstantExecute struct {
 88	dao DAO
 89	req ProposalRequest
 90}
 91
 92func (a *actionInstantExecute) String() string {
 93	// XXX: find a way to be explicit about the subdao
 94	s := ""
 95	s += md.Paragraph(md.Blockquote(a.req.Action.Type()))
 96	s += md.Paragraph(a.req.Action.String())
 97	return s
 98}
 99
100func NewInstantExecuteHandler() ActionHandler {
101	return NewActionHandler(ActionInstantExecuteKind, func(i interface{}) {
102		action, ok := i.(*actionInstantExecute)
103		if !ok {
104			panic(errors.New("invalid action type"))
105		}
106		InstantExecute(action.dao, action.req)
107	})
108}
109
110func NewInstantExecuteAction(dao DAO, req ProposalRequest) Action {
111	return NewAction(ActionInstantExecuteKind, &actionInstantExecute{dao: dao, req: req})
112}