Skip to content

Commit

Permalink
ignite scaffold message query-exchangerate chainID --module interchai…
Browse files Browse the repository at this point in the history
…nquery
  • Loading branch information
riley-stride committed Jun 10, 2022
1 parent 0edf336 commit cb06054
Show file tree
Hide file tree
Showing 24 changed files with 727 additions and 72 deletions.
2 changes: 2 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51853,6 +51853,8 @@ definitions:
the connection handshake.
stride.interchainquery.MsgQueryBalanceResponse:
type: object
stride.interchainquery.MsgQueryExchangerateResponse:
type: object
stride.interchainquery.MsgSubmitQueryResponse:
type: object
properties:
Expand Down
11 changes: 10 additions & 1 deletion proto/interchainquery/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ service Msg {
};
};
rpc QueryBalance(MsgQueryBalance) returns (MsgQueryBalanceResponse);
// this line is used by starport scaffolding # proto/tx/rpc
rpc QueryExchangerate(MsgQueryExchangerate) returns (MsgQueryExchangerateResponse);
// this line is used by starport scaffolding # proto/tx/rpc
}

// MsgSubmitQueryResponse represents a message type to fulfil a query request.
Expand Down Expand Up @@ -61,4 +62,12 @@ message MsgQueryBalance {

message MsgQueryBalanceResponse {}

message MsgQueryExchangerate {
string creator = 1;
string chainID = 2;
}

message MsgQueryExchangerateResponse {
}

// this line is used by starport scaffolding # proto/tx/message
1 change: 1 addition & 0 deletions x/interchainquery/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func GetTxCmd() *cobra.Command {
QueryBalanceCmd(),
SubmitQueryResponse(),
)
cmd.AddCommand(CmdQueryExchangerate())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
42 changes: 42 additions & 0 deletions x/interchainquery/client/cli/tx_query_exchangerate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cli

import (
"strconv"

"github.com/Stride-Labs/stride/x/interchainquery/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdQueryExchangerate() *cobra.Command {
cmd := &cobra.Command{
Use: "query-exchangerate [chain-id]",
Short: "Broadcast message query-exchangerate",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argChainID := args[0]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgQueryExchangerate(
clientCtx.GetFromAddress().String(),
argChainID,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
3 changes: 3 additions & 0 deletions x/interchainquery/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgSubmitQueryResponse:
res, err := msgServer.SubmitQueryResponse(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgQueryExchangerate:
res, err := msgServer.QueryExchangerate(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
// this line is used by starport scaffolding # 1
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
Expand Down
17 changes: 17 additions & 0 deletions x/interchainquery/keeper/msg_server_query_exchangerate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"context"

"github.com/Stride-Labs/stride/x/interchainquery/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k msgServer) QueryExchangerate(goCtx context.Context, msg *types.MsgQueryExchangerate) (*types.MsgQueryExchangerateResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx

return &types.MsgQueryExchangerateResponse{}, nil
}
79 changes: 79 additions & 0 deletions x/interchainquery/module_simulation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package interchainquery

import (
"math/rand"

"github.com/Stride-Labs/stride/testutil/sample"
interchainquerysimulation "github.com/Stride-Labs/stride/x/interchainquery/simulation"
"github.com/Stride-Labs/stride/x/interchainquery/types"
"github.com/cosmos/cosmos-sdk/baseapp"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
)

// avoid unused import issue
var (
_ = sample.AccAddress
_ = interchainquerysimulation.FindAccount
_ = simappparams.StakePerAccount
_ = simulation.MsgEntryKind
_ = baseapp.Paramspace
)

const (
opWeightMsgQueryExchangerate = "op_weight_msg_query_exchangerate"
// TODO: Determine the simulation weight value
defaultWeightMsgQueryExchangerate int = 100

// this line is used by starport scaffolding # simapp/module/const
)

// GenerateGenesisState creates a randomized GenState of the module
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
accs := make([]string, len(simState.Accounts))
for i, acc := range simState.Accounts {
accs[i] = acc.Address.String()
}
interchainqueryGenesis := types.GenesisState{
Params: types.DefaultParams(),
// this line is used by starport scaffolding # simapp/module/genesisState
}
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(&interchainqueryGenesis)
}

// ProposalContents doesn't return any content functions for governance proposals
func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent {
return nil
}

// RandomizedParams creates randomized param changes for the simulator
func (am AppModule) RandomizedParams(_ *rand.Rand) []simtypes.ParamChange {

return []simtypes.ParamChange{}
}

// RegisterStoreDecoder registers a decoder
func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}

// WeightedOperations returns the all the gov module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
operations := make([]simtypes.WeightedOperation, 0)

var weightMsgQueryExchangerate int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgQueryExchangerate, &weightMsgQueryExchangerate, nil,
func(_ *rand.Rand) {
weightMsgQueryExchangerate = defaultWeightMsgQueryExchangerate
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgQueryExchangerate,
interchainquerysimulation.SimulateMsgQueryExchangerate(am.accountKeeper, am.bankKeeper, am.keeper),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
}
29 changes: 29 additions & 0 deletions x/interchainquery/simulation/query_exchangerate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simulation

import (
"math/rand"

"github.com/Stride-Labs/stride/x/interchainquery/keeper"
"github.com/Stride-Labs/stride/x/interchainquery/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)

func SimulateMsgQueryExchangerate(
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
msg := &types.MsgQueryExchangerate{
Creator: simAccount.Address.String(),
}

// TODO: Handling the QueryExchangerate simulation

return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "QueryExchangerate simulation not implemented"), nil, nil
}
}
15 changes: 15 additions & 0 deletions x/interchainquery/simulation/simap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package simulation

import (
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)

// FindAccount find a specific address from an account list
func FindAccount(accs []simtypes.Account, address string) (simtypes.Account, bool) {
creator, err := sdk.AccAddressFromBech32(address)
if err != nil {
panic(err)
}
return simtypes.FindAccount(accs, creator)
}
4 changes: 4 additions & 0 deletions x/interchainquery/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (

func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgSubmitQueryResponse{}, "/stride.interchainquery.MsgSubmitQueryResponse", nil)
cdc.RegisterConcrete(&MsgQueryExchangerate{}, "interchainquery/QueryExchangerate", nil)
// this line is used by starport scaffolding # 2
}

Expand All @@ -23,6 +24,9 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
&MsgSubmitQueryResponse{},
&MsgQueryBalance{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgQueryExchangerate{},
)
// this line is used by starport scaffolding # 3

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
46 changes: 46 additions & 0 deletions x/interchainquery/types/message_query_exchangerate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const TypeMsgQueryExchangerate = "query_exchangerate"

var _ sdk.Msg = &MsgQueryExchangerate{}

func NewMsgQueryExchangerate(creator string, chainID string) *MsgQueryExchangerate {
return &MsgQueryExchangerate{
Creator: creator,
ChainID: chainID,
}
}

func (msg *MsgQueryExchangerate) Route() string {
return RouterKey
}

func (msg *MsgQueryExchangerate) Type() string {
return TypeMsgQueryExchangerate
}

func (msg *MsgQueryExchangerate) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}

func (msg *MsgQueryExchangerate) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgQueryExchangerate) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
return nil
}
40 changes: 40 additions & 0 deletions x/interchainquery/types/message_query_exchangerate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types

import (
"testing"

"github.com/Stride-Labs/stride/testutil/sample"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
)

func TestMsgQueryExchangerate_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgQueryExchangerate
err error
}{
{
name: "invalid address",
msg: MsgQueryExchangerate{
Creator: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
msg: MsgQueryExchangerate{
Creator: sample.AccAddress(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}
Loading

0 comments on commit cb06054

Please sign in to comment.