Skip to content

Commit

Permalink
updates new type MsgInit
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Dec 2, 2024
1 parent 5ead3d3 commit f89848f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 32 deletions.
17 changes: 1 addition & 16 deletions x/accounts/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,10 @@ func GetTxInitCmd() *cobra.Command {
return err
}

// we need to convert the message from json to a protobuf message
// to know which message to use, we need to know the account type
// init message schema.
accClient := v1.NewQueryClient(clientCtx)
schema, err := accClient.Schema(cmd.Context(), &v1.SchemaRequest{
AccountType: args[0],
})
if err != nil {
return err
}

msgBytes, err := encodeJSONToProto(schema.InitSchema.Request, args[1])
if err != nil {
return err
}
msg := v1.MsgInit{
Sender: sender,
AccountType: args[0],
Message: msgBytes,
JsonMessage: args[1],
}

isGenesis, err := cmd.Flags().GetBool("genesis")
Expand Down
4 changes: 1 addition & 3 deletions x/accounts/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ func TestGenesis(t *testing.T) {
return testAccountType, acc, err
})
// add to state a genesis account init msg.
initMsg, err := implementation.PackAny(&types.Empty{})
require.NoError(t, err)
state.InitAccountMsgs = append(state.InitAccountMsgs, &v1.MsgInit{
Sender: "sender-2",
AccountType: testAccountType,
Message: initMsg,
JsonMessage: `{}`,
Funds: nil,
})
err = k.ImportState(ctx, state)
Expand Down
4 changes: 3 additions & 1 deletion x/accounts/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ func (k Keeper) initFromMsg(ctx context.Context, initMsg *v1.MsgInit) (transacti
return nil, nil, err
}

schema := v1.MakeAccountSchema(k.accounts[initMsg.AccountType])

// decode message bytes into the concrete boxed message type
msg, err := implementation.UnpackAnyRaw(initMsg.Message)
msg, err := implementation.EncodeMsgJSONToProto(schema.InitSchema.Request, initMsg.JsonMessage)
if err != nil {
return nil, nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions x/accounts/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/emptypb"
// "google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/wrapperspb"

"cosmossdk.io/x/accounts/accountstd"
Expand All @@ -17,13 +17,13 @@ func TestMsgServer(t *testing.T) {
s := NewMsgServer(k)

// create
initMsg, err := implementation.PackAny(&emptypb.Empty{})
require.NoError(t, err)
// initMsg, err := implementation.PackAny(&emptypb.Empty{})
// require.NoError(t, err)

initResp, err := s.Init(ctx, &v1.MsgInit{
Sender: "sender",
AccountType: "test",
Message: initMsg,
JsonMessage: `{}`,
})
require.NoError(t, err)
require.NotNil(t, initResp)
Expand Down
8 changes: 2 additions & 6 deletions x/accounts/query_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/cosmos/gogoproto/types"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/emptypb"
// "google.golang.org/protobuf/types/known/emptypb"

"cosmossdk.io/x/accounts/accountstd"
"cosmossdk.io/x/accounts/internal/implementation"
Expand All @@ -18,14 +18,10 @@ func TestQueryServer(t *testing.T) {
ms := NewMsgServer(k)
qs := NewQueryServer(k)

// create account
initMsg, err := implementation.PackAny(&emptypb.Empty{})
require.NoError(t, err)

initResp, err := ms.Init(ctx, &v1.MsgInit{
Sender: "sender",
AccountType: "test",
Message: initMsg,
JsonMessage: `{}`,
})
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions x/accounts/v1/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
func MakeAccountsSchemas(impls map[string]implementation.Implementation) map[string]*SchemaResponse {
m := make(map[string]*SchemaResponse, len(impls))
for name, impl := range impls {
m[name] = makeAccountSchema(impl)
m[name] = MakeAccountSchema(impl)
}
return m
}

func makeAccountSchema(impl implementation.Implementation) *SchemaResponse {
func MakeAccountSchema(impl implementation.Implementation) *SchemaResponse {
return &SchemaResponse{
InitSchema: &SchemaResponse_Handler{
Request: impl.InitHandlerSchema.RequestSchema.Name,
Expand Down

0 comments on commit f89848f

Please sign in to comment.