Repository: ovrclk/akash (go)

Issue:
akashctl events: show module, action

`akashctl events` currently shows:

```
id:
  owner: akash1uaamq2yk53y0pkykp9h8dq2u3sa2y5g5ec8242
  dseq: 100971
  gseq: 1
  oseq: 1
  provider: akash1sz43rlut99r8l7rfkej8cqxa66arqkhra2v4cp
price:
  denom: akash
  amount: "1000"

id:
  owner: akash1uaamq2yk53y0pkykp9h8dq2u3sa2y5g5ec8242
  dseq: 100971
  gseq: 1
  oseq: 1
  provider: akash1sz43rlut99r8l7rfkej8cqxa66arqkhra2v4cp
price:
  denom: akash
  amount: "1000"
```

we also want to show the `module` and `action` of each event.

Pull request:
feat: events - expose name, module
fixes #718



Patch:
diff --git a/sdkutil/event.go b/sdkutil/event.go
index 96656a8a..e445d7ea 100644
--- a/sdkutil/event.go
+++ b/sdkutil/event.go
@@ -25,6 +25,11 @@ var (
 	ErrUnknownAction = errors.New("Unknown action")
 )
 
+type BaseModuleEvent struct {
+	Module string `json:"module"`
+	Action string `json:"action"`
+}
+
 type ModuleEvent interface {
 	ToSDKEvent() sdk.Event
 }
diff --git a/x/deployment/keeper/keeper.go b/x/deployment/keeper/keeper.go
index ca12ed13..869c0826 100644
--- a/x/deployment/keeper/keeper.go
+++ b/x/deployment/keeper/keeper.go
@@ -105,7 +105,8 @@ func (k Keeper) Create(ctx sdk.Context, deployment types.Deployment, groups []ty
 	}
 
 	ctx.EventManager().EmitEvent(
-		types.EventDeploymentCreate{ID: deployment.ID()}.ToSDKEvent(),
+		types.NewEventDeploymentCreate(deployment.ID()).
+			ToSDKEvent(),
 	)
 
 	return nil
@@ -121,7 +122,8 @@ func (k Keeper) UpdateDeployment(ctx sdk.Context, deployment types.Deployment) e
 	}
 
 	ctx.EventManager().EmitEvent(
-		types.EventDeploymentUpdate{ID: deployment.ID()}.ToSDKEvent(),
+		types.NewEventDeploymentUpdate(deployment.ID()).
+			ToSDKEvent(),
 	)
 
 	store.Set(key, k.cdc.MustMarshalBinaryBare(deployment))
@@ -139,7 +141,8 @@ func (k Keeper) OnCloseGroup(ctx sdk.Context, group types.Group) error {
 	group.State = types.GroupClosed
 
 	ctx.EventManager().EmitEvent(
-		types.EventGroupClose{ID: group.ID()}.ToSDKEvent(),
+		types.NewEventGroupClose(group.ID()).
+			ToSDKEvent(),
 	)
 
 	store.Set(key, k.cdc.MustMarshalBinaryBare(group))
diff --git a/x/deployment/types/event.go b/x/deployment/types/event.go
index 1fb04ea9..ee652db6 100644
--- a/x/deployment/types/event.go
+++ b/x/deployment/types/event.go
@@ -19,7 +19,18 @@ const (
 
 // EventDeploymentCreate struct
 type EventDeploymentCreate struct {
-	ID DeploymentID
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      DeploymentID            `json:"id"`
+}
+
+func NewEventDeploymentCreate(id DeploymentID) EventDeploymentCreate {
+	return EventDeploymentCreate{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionDeploymentCreate,
+		},
+		ID: id,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventDeploymentCreate struct
@@ -34,8 +45,19 @@ func (ev EventDeploymentCreate) ToSDKEvent() sdk.Event {
 
 // EventDeploymentUpdate struct
 type EventDeploymentUpdate struct {
-	ID      DeploymentID
-	Version []byte // TODO
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      DeploymentID            `json:"id"`
+	Version []byte                  `json:"version,omitempty"` // TODO: #565
+}
+
+func NewEventDeploymentUpdate(id DeploymentID) EventDeploymentUpdate {
+	return EventDeploymentUpdate{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionDeploymentUpdate,
+		},
+		ID: id,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventDeploymentUpdate struct
@@ -50,7 +72,18 @@ func (ev EventDeploymentUpdate) ToSDKEvent() sdk.Event {
 
 // EventDeploymentClose struct
 type EventDeploymentClose struct {
-	ID DeploymentID
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      DeploymentID            `json:"id"`
+}
+
+func NewEventDeploymentClose(id DeploymentID) EventDeploymentClose {
+	return EventDeploymentClose{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionDeploymentClose,
+		},
+		ID: id,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventDeploymentClose struct
@@ -90,7 +123,18 @@ func ParseEVDeploymentID(attrs []sdk.Attribute) (DeploymentID, error) {
 
 // EventGroupClose provides SDK event to signal group termination
 type EventGroupClose struct {
-	ID GroupID
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      GroupID                 `json:"id"`
+}
+
+func NewEventGroupClose(id GroupID) EventGroupClose {
+	return EventGroupClose{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionGroupClose,
+		},
+		ID: id,
+	}
 }
 
 // ToSDKEvent produces the SDK notification for Event
@@ -143,25 +187,25 @@ func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 		if err != nil {
 			return nil, err
 		}
-		return EventDeploymentCreate{ID: did}, nil
+		return NewEventDeploymentCreate(did), nil
 	case evActionDeploymentUpdate:
 		did, err := ParseEVDeploymentID(ev.Attributes)
 		if err != nil {
 			return nil, err
 		}
-		return EventDeploymentUpdate{ID: did}, nil
+		return NewEventDeploymentUpdate(did), nil
 	case evActionDeploymentClose:
 		did, err := ParseEVDeploymentID(ev.Attributes)
 		if err != nil {
 			return nil, err
 		}
-		return EventDeploymentClose{ID: did}, nil
+		return NewEventDeploymentClose(did), nil
 	case evActionGroupClose:
 		gid, err := ParseEVGroupID(ev.Attributes)
 		if err != nil {
 			return nil, err
 		}
-		return EventGroupClose{ID: gid}, nil
+		return NewEventGroupClose(gid), nil
 	default:
 		return nil, sdkutil.ErrUnknownAction
 	}
diff --git a/x/market/keeper/keeper.go b/x/market/keeper/keeper.go
index d9e8a1d1..3b10229b 100644
--- a/x/market/keeper/keeper.go
+++ b/x/market/keeper/keeper.go
@@ -62,7 +62,8 @@ func (k Keeper) CreateOrder(ctx sdk.Context, gid dtypes.GroupID, spec dtypes.Gro
 
 	ctx.Logger().Info("created order", "order", order.ID())
 	ctx.EventManager().EmitEvent(
-		types.EventOrderCreated{ID: order.ID()}.ToSDKEvent(),
+		types.NewEventOrderCreated(order.ID()).
+			ToSDKEvent(),
 	)
 	return order, nil
 }
@@ -87,10 +88,8 @@ func (k Keeper) CreateBid(ctx sdk.Context, oid types.OrderID, provider sdk.AccAd
 	store.Set(key, k.cdc.MustMarshalBinaryBare(bid))
 
 	ctx.EventManager().EmitEvent(
-		types.EventBidCreated{
-			ID:    bid.ID(),
-			Price: price,
-		}.ToSDKEvent(),
+		types.NewEventBidCreated(bid.ID(), price).
+			ToSDKEvent(),
 	)
 
 	return bid, nil
@@ -111,10 +110,8 @@ func (k Keeper) CreateLease(ctx sdk.Context, bid types.Bid) {
 	store.Set(key, k.cdc.MustMarshalBinaryBare(lease))
 	ctx.Logger().Info("created lease", "lease", lease.ID())
 	ctx.EventManager().EmitEvent(
-		types.EventLeaseCreated{
-			ID:    lease.ID(),
-			Price: lease.Price,
-		}.ToSDKEvent(),
+		types.NewEventLeaseCreated(lease.ID(), lease.Price).
+			ToSDKEvent(),
 	)
 }
 
@@ -149,10 +146,8 @@ func (k Keeper) OnBidClosed(ctx sdk.Context, bid types.Bid) {
 	bid.State = types.BidClosed
 	k.updateBid(ctx, bid)
 	ctx.EventManager().EmitEvent(
-		types.EventBidClosed{
-			ID:    bid.ID(),
-			Price: bid.Price,
-		}.ToSDKEvent(),
+		types.NewEventBidClosed(bid.ID(), bid.Price).
+			ToSDKEvent(),
 	)
 }
 
@@ -166,7 +161,8 @@ func (k Keeper) OnOrderClosed(ctx sdk.Context, order types.Order) {
 	order.State = types.OrderClosed
 	k.updateOrder(ctx, order)
 	ctx.EventManager().EmitEvent(
-		types.EventOrderClosed{ID: order.ID()}.ToSDKEvent(),
+		types.NewEventOrderClosed(order.ID()).
+			ToSDKEvent(),
 	)
 }
 
@@ -180,10 +176,8 @@ func (k Keeper) OnInsufficientFunds(ctx sdk.Context, lease types.Lease) {
 	lease.State = types.LeaseInsufficientFunds
 	k.updateLease(ctx, lease)
 	ctx.EventManager().EmitEvent(
-		types.EventLeaseClosed{
-			ID:    lease.ID(),
-			Price: lease.Price,
-		}.ToSDKEvent(),
+		types.NewEventLeaseClosed(lease.ID(), lease.Price).
+			ToSDKEvent(),
 	)
 }
 
@@ -198,10 +192,8 @@ func (k Keeper) OnLeaseClosed(ctx sdk.Context, lease types.Lease) {
 	k.updateLease(ctx, lease)
 	ctx.Logger().Info("closed lease", "lease", lease.ID())
 	ctx.EventManager().EmitEvent(
-		types.EventLeaseClosed{
-			ID:    lease.ID(),
-			Price: lease.Price,
-		}.ToSDKEvent(),
+		types.NewEventLeaseClosed(lease.ID(), lease.Price).
+			ToSDKEvent(),
 	)
 }
 
diff --git a/x/market/types/event.go b/x/market/types/event.go
index e474b56a..ecdb1281 100644
--- a/x/market/types/event.go
+++ b/x/market/types/event.go
@@ -30,7 +30,18 @@ var (
 
 // EventOrderCreated struct
 type EventOrderCreated struct {
-	ID OrderID
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      OrderID                 `json:"id"`
+}
+
+func NewEventOrderCreated(id OrderID) EventOrderCreated {
+	return EventOrderCreated{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionOrderCreated,
+		},
+		ID: id,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventOrderCreated struct
@@ -45,7 +56,18 @@ func (e EventOrderCreated) ToSDKEvent() sdk.Event {
 
 // EventOrderClosed struct
 type EventOrderClosed struct {
-	ID OrderID
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      OrderID                 `json:"id"`
+}
+
+func NewEventOrderClosed(id OrderID) EventOrderClosed {
+	return EventOrderClosed{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionOrderClosed,
+		},
+		ID: id,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventOrderClosed struct
@@ -60,8 +82,20 @@ func (e EventOrderClosed) ToSDKEvent() sdk.Event {
 
 // EventBidCreated struct
 type EventBidCreated struct {
-	ID    BidID
-	Price sdk.Coin
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      BidID                   `json:"id"`
+	Price   sdk.Coin                `json:"price"`
+}
+
+func NewEventBidCreated(id BidID, price sdk.Coin) EventBidCreated {
+	return EventBidCreated{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionBidCreated,
+		},
+		ID:    id,
+		Price: price,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventBidCreated struct
@@ -78,8 +112,20 @@ func (e EventBidCreated) ToSDKEvent() sdk.Event {
 
 // EventBidClosed struct
 type EventBidClosed struct {
-	ID    BidID
-	Price sdk.Coin
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      BidID                   `json:"id"`
+	Price   sdk.Coin                `json:"price"`
+}
+
+func NewEventBidClosed(id BidID, price sdk.Coin) EventBidClosed {
+	return EventBidClosed{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionBidClosed,
+		},
+		ID:    id,
+		Price: price,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventBidClosed struct
@@ -96,8 +142,20 @@ func (e EventBidClosed) ToSDKEvent() sdk.Event {
 
 // EventLeaseCreated struct
 type EventLeaseCreated struct {
-	ID    LeaseID
-	Price sdk.Coin
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      LeaseID                 `json:"id"`
+	Price   sdk.Coin                `json:"price"`
+}
+
+func NewEventLeaseCreated(id LeaseID, price sdk.Coin) EventLeaseCreated {
+	return EventLeaseCreated{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionLeaseCreated,
+		},
+		ID:    id,
+		Price: price,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventLeaseCreated struct
@@ -113,8 +171,20 @@ func (e EventLeaseCreated) ToSDKEvent() sdk.Event {
 
 // EventLeaseClosed struct
 type EventLeaseClosed struct {
-	ID    LeaseID
-	Price sdk.Coin
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	ID      LeaseID                 `json:"id"`
+	Price   sdk.Coin                `json:"price"`
+}
+
+func NewEventLeaseClosed(id LeaseID, price sdk.Coin) EventLeaseClosed {
+	return EventLeaseClosed{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionLeaseClosed,
+		},
+		ID:    id,
+		Price: price,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventLeaseClosed struct
@@ -237,13 +307,13 @@ func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 		if err != nil {
 			return nil, err
 		}
-		return EventOrderCreated{ID: id}, nil
+		return NewEventOrderCreated(id), nil
 	case evActionOrderClosed:
 		id, err := parseEVOrderID(ev.Attributes)
 		if err != nil {
 			return nil, err
 		}
-		return EventOrderClosed{ID: id}, nil
+		return NewEventOrderClosed(id), nil
 
 	case evActionBidCreated:
 		id, err := parseEVBidID(ev.Attributes)
@@ -254,7 +324,7 @@ func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 		if err != nil {
 			return nil, err
 		}
-		return EventBidCreated{ID: id, Price: price}, nil
+		return NewEventBidCreated(id, price), nil
 	case evActionBidClosed:
 		id, err := parseEVBidID(ev.Attributes)
 		if err != nil {
@@ -262,7 +332,7 @@ func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 		}
 		// optional price
 		price, _ := parseEVPriceAttributes(ev.Attributes)
-		return EventBidClosed{ID: id, Price: price}, nil
+		return NewEventBidClosed(id, price), nil
 
 	case evActionLeaseCreated:
 		id, err := parseEVLeaseID(ev.Attributes)
@@ -273,7 +343,7 @@ func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 		if err != nil {
 			return nil, err
 		}
-		return EventLeaseCreated{ID: id, Price: price}, nil
+		return NewEventLeaseCreated(id, price), nil
 	case evActionLeaseClosed:
 		id, err := parseEVLeaseID(ev.Attributes)
 		if err != nil {
@@ -281,7 +351,7 @@ func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 		}
 		// optional price
 		price, _ := parseEVPriceAttributes(ev.Attributes)
-		return EventLeaseClosed{ID: id, Price: price}, nil
+		return NewEventLeaseClosed(id, price), nil
 
 	default:
 		return nil, sdkutil.ErrUnknownAction
diff --git a/x/provider/types/event.go b/x/provider/types/event.go
index 573f49b8..68b9e1bc 100644
--- a/x/provider/types/event.go
+++ b/x/provider/types/event.go
@@ -14,7 +14,18 @@ const (
 
 // EventProviderCreate struct
 type EventProviderCreate struct {
-	Owner sdk.AccAddress
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	Owner   sdk.AccAddress          `json:"owner"`
+}
+
+func NewEventProviderCreate(owner sdk.AccAddress) EventProviderCreate {
+	return EventProviderCreate{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionProviderCreate,
+		},
+		Owner: owner,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventProviderCreate struct
@@ -29,7 +40,18 @@ func (ev EventProviderCreate) ToSDKEvent() sdk.Event {
 
 // EventProviderUpdate struct
 type EventProviderUpdate struct {
-	Owner sdk.AccAddress
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	Owner   sdk.AccAddress          `json:"owner"`
+}
+
+func NewEventProviderUpdate(owner sdk.AccAddress) EventProviderUpdate {
+	return EventProviderUpdate{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionProviderUpdate,
+		},
+		Owner: owner,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventProviderUpdate struct
@@ -44,7 +66,18 @@ func (ev EventProviderUpdate) ToSDKEvent() sdk.Event {
 
 // EventProviderDelete struct
 type EventProviderDelete struct {
-	Owner sdk.AccAddress
+	Context sdkutil.BaseModuleEvent `json:"context"`
+	Owner   sdk.AccAddress          `json:"owner"`
+}
+
+func NewEventProviderDelete(owner sdk.AccAddress) EventProviderDelete {
+	return EventProviderDelete{
+		Context: sdkutil.BaseModuleEvent{
+			Module: ModuleName,
+			Action: evActionProviderDelete,
+		},
+		Owner: owner,
+	}
 }
 
 // ToSDKEvent method creates new sdk event for EventProviderDelete struct
@@ -89,19 +122,19 @@ func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 		if err != nil {
 			return nil, err
 		}
-		return EventProviderCreate{Owner: owner}, nil
+		return NewEventProviderCreate(owner), nil
 	case evActionProviderUpdate:
 		owner, err := ParseEVProvider(ev.Attributes)
 		if err != nil {
 			return nil, err
 		}
-		return EventProviderUpdate{Owner: owner}, nil
+		return NewEventProviderUpdate(owner), nil
 	case evActionProviderDelete:
 		owner, err := ParseEVProvider(ev.Attributes)
 		if err != nil {
 			return nil, err
 		}
-		return EventProviderDelete{Owner: owner}, nil
+		return NewEventProviderDelete(owner), nil
 	default:
 		return nil, sdkutil.ErrUnknownAction
 	}


Test patch:
diff --git a/events/publish_test.go b/events/publish_test.go
index 2da67bd1..9306e7fb 100644
--- a/events/publish_test.go
+++ b/events/publish_test.go
@@ -15,23 +15,23 @@ import (
 func Test_processEvent(t *testing.T) {
 	tests := []sdkutil.ModuleEvent{
 		// x/deployment events
-		dtypes.EventDeploymentCreate{ID: testutil.DeploymentID(t)},
-		dtypes.EventDeploymentUpdate{ID: testutil.DeploymentID(t)},
-		dtypes.EventDeploymentClose{ID: testutil.DeploymentID(t)},
-		dtypes.EventGroupClose{ID: testutil.GroupID(t)},
+		dtypes.NewEventDeploymentCreate(testutil.DeploymentID(t)),
+		dtypes.NewEventDeploymentUpdate(testutil.DeploymentID(t)),
+		dtypes.NewEventDeploymentClose(testutil.DeploymentID(t)),
+		dtypes.NewEventGroupClose(testutil.GroupID(t)),
 
 		// x/market events
-		mtypes.EventOrderCreated{ID: testutil.OrderID(t)},
-		mtypes.EventOrderClosed{ID: testutil.OrderID(t)},
-		mtypes.EventBidCreated{ID: testutil.BidID(t), Price: testutil.Coin(t)},
-		mtypes.EventBidClosed{ID: testutil.BidID(t), Price: testutil.Coin(t)},
-		mtypes.EventLeaseCreated{ID: testutil.LeaseID(t), Price: testutil.Coin(t)},
-		mtypes.EventLeaseClosed{ID: testutil.LeaseID(t), Price: testutil.Coin(t)},
+		mtypes.NewEventOrderCreated(testutil.OrderID(t)),
+		mtypes.NewEventOrderClosed(testutil.OrderID(t)),
+		mtypes.NewEventBidCreated(testutil.BidID(t), testutil.Coin(t)),
+		mtypes.NewEventBidClosed(testutil.BidID(t), testutil.Coin(t)),
+		mtypes.NewEventLeaseCreated(testutil.LeaseID(t), testutil.Coin(t)),
+		mtypes.NewEventLeaseClosed(testutil.LeaseID(t), testutil.Coin(t)),
 
 		// x/provider events
-		ptypes.EventProviderCreate{Owner: testutil.AccAddress(t)},
-		ptypes.EventProviderUpdate{Owner: testutil.AccAddress(t)},
-		ptypes.EventProviderDelete{Owner: testutil.AccAddress(t)},
+		ptypes.NewEventProviderCreate(testutil.AccAddress(t)),
+		ptypes.NewEventProviderUpdate(testutil.AccAddress(t)),
+		ptypes.NewEventProviderDelete(testutil.AccAddress(t)),
 	}
 
 	for _, test := range tests {