Skip to content

Commit

Permalink
clean code + storecodecompat
Browse files Browse the repository at this point in the history
  • Loading branch information
trevormil committed Jan 10, 2024
1 parent ae1ae28 commit 7c59dfd
Show file tree
Hide file tree
Showing 11 changed files with 626 additions and 109 deletions.
2 changes: 2 additions & 0 deletions proto/wasmx/tx.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
syntax = "proto3";
package wasmx;

import "gogoproto/gogo.proto";

option go_package = "github.com/bitbadges/bitbadgeschain/x/wasmx/types";

// Msg defines the wasmx Msg service.
Expand Down
3 changes: 3 additions & 0 deletions x/wasmx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgExecuteContractCompat:
res, err := msgServer.ExecuteContractCompat(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgStoreCodeCompat:
res, err := msgServer.StoreCodeCompat(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
return nil, sdkerrors.Wrap(badgestypes.ErrUnknownRequest,
fmt.Sprintf("Unrecognized wasmx Msg type: %T", msg))
Expand Down
20 changes: 20 additions & 0 deletions x/wasmx/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/hexutil"

"github.com/bitbadges/bitbadgeschain/x/wasmx/types"
)
Expand Down Expand Up @@ -50,3 +51,22 @@ func (m msgServer) ExecuteContractCompat(goCtx context.Context, msg *types.MsgEx
Data: res.Data,
}, nil
}

func (m msgServer) StoreCodeCompat(goCtx context.Context, msg *types.MsgStoreCodeCompat) (*types.MsgStoreCodeCompatResponse, error) {
wasmMsgServer := wasmkeeper.NewMsgServerImpl(&m.wasmKeeper)

oMsg := &wasmtypes.MsgStoreCode{
Sender: msg.Sender,
WASMByteCode: hexutil.MustDecode(msg.HexWasmByteCode),
}

res, err := wasmMsgServer.StoreCode(goCtx, oMsg)
if err != nil {
return nil, err
}

return &types.MsgStoreCodeCompatResponse{
CodeId: sdk.NewUint(res.CodeID),
Checksum: res.Checksum,
}, nil
}
7 changes: 6 additions & 1 deletion x/wasmx/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
// on the provided LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgExecuteContractCompat{}, "wasmx/MsgExecuteContractCompat", nil)
cdc.RegisterConcrete(&MsgStoreCodeCompat{}, "wasmx/MsgStoreCodeCompat", nil)
}

func RegisterInterfaces(registry types.InterfaceRegistry) {

registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgExecuteContractCompat{},
&MsgStoreCodeCompat{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand All @@ -33,10 +35,13 @@ var (
// The actual codec used for serialization should be provided to x/wasmx and
// defined at the application level.
ModuleCdc = codec.NewAminoCodec(amino)

// AminoCdc is a amino codec created to support amino JSON compatible msgs.
AminoCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
amino.Seal()
}
}
38 changes: 0 additions & 38 deletions x/wasmx/types/custom_execution.go

This file was deleted.

25 changes: 0 additions & 25 deletions x/wasmx/types/exec_callback_msgs.go

This file was deleted.

26 changes: 0 additions & 26 deletions x/wasmx/types/exec_msgs.go

This file was deleted.

43 changes: 43 additions & 0 deletions x/wasmx/types/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package types

import (
sdkmath "cosmossdk.io/math"
)

//Needed for custom gogoproto types

type (
Int = sdkmath.Int
Uint = sdkmath.Uint
)

var (
NewInt = sdkmath.NewInt
ZeroInt = sdkmath.ZeroInt
NewUint = sdkmath.NewUint
NewUintFromString = func(s string) Uint {
if s == "" {
return sdkmath.NewUint(0)
}

val := sdkmath.NewUintFromString(s)
return val
}
)

type (
Dec = sdkmath.LegacyDec
)

var (
NewDecWithPrec = sdkmath.LegacyNewDecWithPrec
NewDecFromInt = sdkmath.LegacyNewDecFromInt
NewDecFromStr = sdkmath.LegacyNewDecFromStr
MustNewDecFromStr = sdkmath.LegacyMustNewDecFromStr
)

// var _ CustomProtobufType = (*Dec)(nil)

// func (dp DecProto) String() string {
// return dp.Dec.String()
// }
44 changes: 44 additions & 0 deletions x/wasmx/types/msg_store_code_compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package types

import (
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/hexutil"
)

const (
TypeMsgStoreCodeCompat = "storeCodeCompat"
)

func (msg MsgStoreCodeCompat) Route() string {
return RouterKey
}

func (msg MsgStoreCodeCompat) Type() string {
return TypeMsgStoreCodeCompat
}

func (msg MsgStoreCodeCompat) ValidateBasic() error {
oMsg := &wasmtypes.MsgStoreCode{
Sender: msg.Sender,
WASMByteCode: hexutil.MustDecode(msg.HexWasmByteCode),
}

if err := oMsg.ValidateBasic(); err != nil {
return err
}
return nil
}

// Note ModuleCdc is Amino (see codec.go)
func (msg MsgStoreCodeCompat) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}

func (msg MsgStoreCodeCompat) GetSigners() []sdk.AccAddress {
senderAddr, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil { // should never happen as valid basic rejects invalid addresses
panic(err.Error())
}
return []sdk.AccAddress{senderAddr}
}
2 changes: 1 addition & 1 deletion x/wasmx/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (msg MsgExecuteContractCompat) ValidateBasic() error {

// Note ModuleCdc is Amino (see codec.go)
func (msg MsgExecuteContractCompat) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}

func (msg MsgExecuteContractCompat) GetSigners() []sdk.AccAddress {
Expand Down
Loading

0 comments on commit 7c59dfd

Please sign in to comment.