Skip to content

Commit

Permalink
some bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
quasisamurai committed Sep 15, 2022
1 parent b13ed80 commit b633408
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
8 changes: 8 additions & 0 deletions wasmbinding/bindings/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type NeutronMsg struct {
RegisterInterchainQuery *RegisterInterchainQuery `json:"register_interchain_query,omitempty"`
UpdateInterchainQuery *UpdateInterchainQuery `json:"update_interchain_query,omitempty"`
RemoveInterchainQuery *RemoveInterchainQuery `json:"remove_interchain_query,omitempty"`
AddAdmin *AddAdmin `json:"add_admin,omitempty"`
}

// SubmitTx submits interchain transaction on a remote chain.
Expand Down Expand Up @@ -54,6 +55,13 @@ type RegisterInterchainQuery struct {
UpdatePeriod uint64 `json:"update_period"`
}

type AddAdmin struct {
Admin string `protobuf:"bytes,2,opt,name=admin,proto3" json:"admin,omitempty"`
}

type AddAdminResponse struct {
}

// RegisterInterchainQueryResponse holds response for RegisterInterchainQuery
type RegisterInterchainQueryResponse struct {
Id uint64 `json:"id"`
Expand Down
58 changes: 57 additions & 1 deletion wasmbinding/message_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/neutron-org/neutron/wasmbinding/bindings"
adminkeeper "github.com/neutron-org/neutron/x/adminmodule/keeper"
admintypes "github.com/neutron-org/neutron/x/adminmodule/types"
icqkeeper "github.com/neutron-org/neutron/x/interchainqueries/keeper"
icqtypes "github.com/neutron-org/neutron/x/interchainqueries/types"
ictxkeeper "github.com/neutron-org/neutron/x/interchaintxs/keeper"
ictxtypes "github.com/neutron-org/neutron/x/interchaintxs/types"
)

func CustomMessageDecorator(ictx *ictxkeeper.Keeper, icq *icqkeeper.Keeper) func(messenger wasmkeeper.Messenger) wasmkeeper.Messenger {
func CustomMessageDecorator(ictx *ictxkeeper.Keeper, icq *icqkeeper.Keeper, admKeeper *adminkeeper.Keeper) func(messenger wasmkeeper.Messenger) wasmkeeper.Messenger {
return func(old wasmkeeper.Messenger) wasmkeeper.Messenger {
return &CustomMessenger{
Keeper: *ictx,
Wrapped: old,
Ictxmsgserver: ictxkeeper.NewMsgServerImpl(*ictx),
Icqmsgserver: icqkeeper.NewMsgServerImpl(*icq),
Adminserver: adminkeeper.NewMsgServerImpl(*admKeeper),
}
}
}
Expand All @@ -32,6 +35,7 @@ type CustomMessenger struct {
Wrapped wasmkeeper.Messenger
Ictxmsgserver ictxtypes.MsgServer
Icqmsgserver icqtypes.MsgServer
Adminserver admintypes.MsgServer
}

var _ wasmkeeper.Messenger = (*CustomMessenger)(nil)
Expand Down Expand Up @@ -63,6 +67,9 @@ func (m *CustomMessenger) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddre
if contractMsg.RemoveInterchainQuery != nil {
return m.removeInterchainQuery(ctx, contractAddr, contractMsg.RemoveInterchainQuery)
}
if contractMsg.AddAdmin != nil {
return m.addAmin(ctx, contractAddr, contractMsg.AddAdmin)
}
}

return m.Wrapped.DispatchMsg(ctx, contractAddr, contractIBCPortID, msg)
Expand Down Expand Up @@ -193,6 +200,55 @@ func (m *CustomMessenger) submitTx(ctx sdk.Context, contractAddr sdk.AccAddress,
return nil, [][]byte{data}, nil
}

func (m *CustomMessenger) addAmin(ctx sdk.Context, contractAddr sdk.AccAddress, addAdmin *bindings.AddAdmin) ([]sdk.Event, [][]byte, error) {
response, err := m.PerformAddAmin(ctx, contractAddr, addAdmin)
if err != nil {
ctx.Logger().Debug("PerformSubmitTx: failed to addAdmin",
"from_address", contractAddr.String(),
"admin", addAdmin.Admin,
"creator", contractAddr.String(),
"error", err,
)
return nil, nil, sdkerrors.Wrap(err, "failed to submit add admin message")
}

data, err := json.Marshal(response)
if err != nil {
ctx.Logger().Error("json.Marshal: failed to marshal addAdmin response to JSON",
"from_address", contractAddr.String(),
"admin", addAdmin.Admin,
"creator", contractAddr.String(),
"error", err,
)
return nil, nil, sdkerrors.Wrap(err, "marshal json failed")
}

ctx.Logger().Debug("add admin message submitted",
"from_address", contractAddr.String(),
"admin", addAdmin.Admin,
"creator", contractAddr.String(),
)
return nil, [][]byte{data}, nil
}

func (m *CustomMessenger) PerformAddAmin(ctx sdk.Context, contractAddr sdk.AccAddress, addAdmin *bindings.AddAdmin) (*bindings.AddAdminResponse, error) {
tx := admintypes.MsgAddAdmin{
Creator: contractAddr.String(),
Admin: addAdmin.Admin,
}

if err := tx.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrap(err, "failed to validate incoming SubmitTx message")
}

response, err := m.Adminserver.AddAdmin(sdk.WrapSDKContext(ctx), &tx)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to submit interchain transaction")
}

return (*bindings.AddAdminResponse)(response), nil
}

func (m *CustomMessenger) PerformSubmitTx(ctx sdk.Context, contractAddr sdk.AccAddress, submitTx *bindings.SubmitTx) (*bindings.SubmitTxResponse, error) {
tx := ictxtypes.MsgSubmitTx{
FromAddress: contractAddr.String(),
Expand Down

0 comments on commit b633408

Please sign in to comment.