Skip to content

Commit

Permalink
chore(x/swaprouter): register amino and interfaces (finalizing merge …
Browse files Browse the repository at this point in the history
…to main #1) (#3912)

* refactor(x/swaprouter): register amino and interfaces

* Update x/swaprouter/types/codec.go

* Update x/swaprouter/types/codec.go

* lint

* amino

* Revert "amino"

This reverts commit a946a21.

* remove obsolete comment
  • Loading branch information
p0mvn authored Jan 3, 2023
1 parent b5cad13 commit f6d9001
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 20 deletions.
6 changes: 2 additions & 4 deletions x/swaprouter/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type AppModuleBasic struct{}
func (AppModuleBasic) Name() string { return types.ModuleName }

func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}

func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
Expand Down Expand Up @@ -69,6 +70,7 @@ func (b AppModuleBasic) GetQueryCmd() *cobra.Command {

// RegisterInterfaces registers interfaces and implementations of the gamm module.
func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
}

type AppModule struct {
Expand Down Expand Up @@ -137,8 +139,6 @@ func (AppModule) ConsensusVersion() uint64 { return 1 }

// **** simulation implementation ****
// GenerateGenesisState creates a randomized GenState of the swaprouter module.
// **** simulation implementation ****
// GenerateGenesisState creates a randomized GenState of the gamm module.
func (am AppModule) SimulatorGenesisState(simState *module.SimulationState, s *simtypes.SimCtx) {
swaprouterGen := types.DefaultGenesis()
// change the pool creation fee denom from uosmo to stake
Expand All @@ -148,7 +148,5 @@ func (am AppModule) SimulatorGenesisState(simState *module.SimulationState, s *s
}

func (am AppModule) Actions() []simtypes.Action {
// TODO: uncomment this once simulation is enabled.
// return swaproutersimulation.DefaultActions(am.k, am.gammKeeper)
return nil
}
40 changes: 40 additions & 0 deletions x/swaprouter/types/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
)

// RegisterLegacyAminoCodec registers the necessary x/gamm interfaces and concrete types
// on the provided LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgSwapExactAmountIn{}, "osmosis/swaprouter/swap-exact-amount-in", nil)
cdc.RegisterConcrete(&MsgSwapExactAmountOut{}, "osmosis/swaprouter/swap-exact-amount-out", nil)
}

func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgSwapExactAmountIn{},
&MsgSwapExactAmountOut{},
)
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(amino)
// Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be
// used to properly serialize MsgGrant and MsgExec instances
sdk.RegisterLegacyAminoCodec(amino)
RegisterLegacyAminoCodec(authzcodec.Amino)

amino.Seal()
}
8 changes: 2 additions & 6 deletions x/swaprouter/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ func (msg MsgSwapExactAmountIn) ValidateBasic() error {
return nil
}

// TODO: uncomment when types are registered.
func (msg MsgSwapExactAmountIn) GetSignBytes() []byte {
// return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
panic("not implemented")
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

func (msg MsgSwapExactAmountIn) GetSigners() []sdk.AccAddress {
Expand Down Expand Up @@ -78,10 +76,8 @@ func (msg MsgSwapExactAmountOut) ValidateBasic() error {
return nil
}

// TODO: uncomment when types are registered.
func (msg MsgSwapExactAmountOut) GetSignBytes() []byte {
// return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
panic("not implemented")
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

func (msg MsgSwapExactAmountOut) GetSigners() []sdk.AccAddress {
Expand Down
16 changes: 6 additions & 10 deletions x/swaprouter/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,17 @@ func TestMsgSwapExactAmountOut(t *testing.T) {

// Test authz serialize and de-serializes for swaprouter msg.
func TestAuthzMsg(t *testing.T) {

// TODO: remove when types are registered.
t.SkipNow()

pk1 := ed25519.GenPrivKey().PubKey()
addr1 := sdk.AccAddress(pk1.Address()).String()
coin := sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1))

testCases := []struct {
name string
gammMsg sdk.Msg
name string
msg sdk.Msg
}{
{
name: "MsgJoinSwapShareAmountOut",
gammMsg: &types.MsgSwapExactAmountIn{
name: "MsgSwapExactAmountOut",
msg: &types.MsgSwapExactAmountIn{
Sender: addr1,
Routes: []types.SwapAmountInRoute{{
PoolId: 0,
Expand All @@ -304,7 +300,7 @@ func TestAuthzMsg(t *testing.T) {
},
{
name: "MsgSwapExactAmountOut",
gammMsg: &types.MsgSwapExactAmountOut{
msg: &types.MsgSwapExactAmountOut{
Sender: addr1,
Routes: []types.SwapAmountOutRoute{{
PoolId: 0,
Expand All @@ -320,7 +316,7 @@ func TestAuthzMsg(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
apptesting.TestMessageAuthzSerialization(t, tc.gammMsg)
apptesting.TestMessageAuthzSerialization(t, tc.msg)
})
}
}

0 comments on commit f6d9001

Please sign in to comment.