Skip to content

Commit

Permalink
Store contract response in event (cosmos#535)
Browse files Browse the repository at this point in the history
* Store contract response in event

* Review feedback
  • Loading branch information
alpe authored Jun 14, 2021
1 parent f8e39bf commit 1ada14e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions x/wasm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"encoding/hex"
"fmt"
"github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -66,6 +67,7 @@ func (m msgServer) InstantiateContract(goCtx context.Context, msg *types.MsgInst
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", msg.CodeID)),
sdk.NewAttribute(types.AttributeKeyContract, contractAddr.String()),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
))

return &types.MsgInstantiateContractResponse{
Expand Down Expand Up @@ -95,6 +97,7 @@ func (m msgServer) ExecuteContract(goCtx context.Context, msg *types.MsgExecuteC
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyContract, msg.Contract),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
))

return &types.MsgExecuteContractResponse{
Expand Down Expand Up @@ -122,7 +125,9 @@ func (m msgServer) MigrateContract(goCtx context.Context, msg *types.MsgMigrateC
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", msg.CodeID)),
sdk.NewAttribute(types.AttributeKeyContract, msg.Contract),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
))

return &types.MsgMigrateContractResponse{
Expand Down
8 changes: 6 additions & 2 deletions x/wasm/keeper/proposal_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"encoding/hex"
"fmt"
"github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -84,7 +85,7 @@ func handleInstantiateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p typ
return sdkerrors.Wrap(err, "admin")
}

contractAddr, _, err := k.Instantiate(ctx, p.CodeID, runAsAddr, adminAddr, p.InitMsg, p.Label, p.Funds)
contractAddr, data, err := k.Instantiate(ctx, p.CodeID, runAsAddr, adminAddr, p.InitMsg, p.Label, p.Funds)
if err != nil {
return err
}
Expand All @@ -94,6 +95,7 @@ func handleInstantiateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p typ
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", p.CodeID)),
sdk.NewAttribute(types.AttributeKeyContract, contractAddr.String()),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
)
ctx.EventManager().EmitEvent(ourEvent)
return nil
Expand All @@ -112,15 +114,17 @@ func handleMigrateProposal(ctx sdk.Context, k types.ContractOpsKeeper, p types.M
if err != nil {
return sdkerrors.Wrap(err, "run as address")
}
_, err = k.Migrate(ctx, contractAddr, runAsAddr, p.CodeID, p.MigrateMsg)
data, err := k.Migrate(ctx, contractAddr, runAsAddr, p.CodeID, p.MigrateMsg)
if err != nil {
return err
}

ourEvent := sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", p.CodeID)),
sdk.NewAttribute(types.AttributeKeyContract, p.Contract),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
)
ctx.EventManager().EmitEvent(ourEvent)
return nil
Expand Down
14 changes: 11 additions & 3 deletions x/wasm/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ func TestInstantiateProposal(t *testing.T) {
p.Admin = otherAddress.String()
p.Label = "testing"
})
em := sdk.NewEventManager()

// when stored
storedProposal, err := govKeeper.SubmitProposal(ctx, src)
require.NoError(t, err)

// and proposal execute
handler := govKeeper.Router().GetRoute(storedProposal.ProposalRoute())
err = handler(ctx, storedProposal.GetContent())
err = handler(ctx.WithEventManager(em), storedProposal.GetContent())
require.NoError(t, err)

// then
Expand All @@ -113,6 +114,9 @@ func TestInstantiateProposal(t *testing.T) {
Msg: src.InitMsg,
}}
assert.Equal(t, expHistory, wasmKeeper.GetContractHistory(ctx, contractAddr))
// and event
require.Len(t, em.Events(), 2, "%#v", em.Events())
require.Len(t, em.Events()[1].Attributes, 4)
}

func TestMigrateProposal(t *testing.T) {
Expand Down Expand Up @@ -161,13 +165,15 @@ func TestMigrateProposal(t *testing.T) {
RunAs: otherAddress.String(),
}

em := sdk.NewEventManager()

// when stored
storedProposal, err := govKeeper.SubmitProposal(ctx, &src)
require.NoError(t, err)

// and proposal execute
handler := govKeeper.Router().GetRoute(storedProposal.ProposalRoute())
err = handler(ctx, storedProposal.GetContent())
err = handler(ctx.WithEventManager(em), storedProposal.GetContent())
require.NoError(t, err)

// then
Expand All @@ -188,7 +194,9 @@ func TestMigrateProposal(t *testing.T) {
Msg: src.MigrateMsg,
}}
assert.Equal(t, expHistory, wasmKeeper.GetContractHistory(ctx, contractAddr))

// and events emitted
require.Len(t, em.Events(), 2)
require.Len(t, em.Events()[1].Attributes, 4)
}

func TestAdminProposals(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions x/wasm/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const (
EventTypeUnpinCode = "unpin_code"
)
const ( // event attributes
AttributeKeyContract = "contract_address"
AttributeKeyCodeID = "code_id"
AttributeKeySigner = "signer"
AttributeKeyContract = "contract_address"
AttributeKeyCodeID = "code_id"
AttributeKeySigner = "signer"
AttributeResultDataHex = "result"
)

0 comments on commit 1ada14e

Please sign in to comment.