Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add create a deal and query a deal keeper function #525

Merged
merged 16 commits into from
Dec 8, 2022
Merged
30 changes: 25 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ import (
"github.com/medibloc/panacea-core/v2/x/did"
didkeeper "github.com/medibloc/panacea-core/v2/x/did/keeper"
didtypes "github.com/medibloc/panacea-core/v2/x/did/types"

"github.com/medibloc/panacea-core/v2/x/datadeal"
datadealkeeper "github.com/medibloc/panacea-core/v2/x/datadeal/keeper"
datadealtypes "github.com/medibloc/panacea-core/v2/x/datadeal/types"
)

const Name = "panacea"
Expand Down Expand Up @@ -195,6 +199,7 @@ var (
wasm.AppModuleBasic{},
feegrantmodule.AppModuleBasic{},
authzmodule.AppModuleBasic{},
datadeal.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -265,11 +270,12 @@ type App struct {
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedWasmKeeper capabilitykeeper.ScopedKeeper

aolKeeper aolkeeper.Keeper
didKeeper didkeeper.Keeper
burnKeeper burnkeeper.Keeper
wasmKeeper wasm.Keeper
oracleKeeper oraclekeeper.Keeper
aolKeeper aolkeeper.Keeper
didKeeper didkeeper.Keeper
burnKeeper burnkeeper.Keeper
wasmKeeper wasm.Keeper
oracleKeeper oraclekeeper.Keeper
datadealKeeper datadealkeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -315,6 +321,7 @@ func New(
oracletypes.StoreKey,
wasm.StoreKey,
feegrant.StoreKey,
datadealtypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -427,6 +434,14 @@ func New(
app.BankKeeper,
)

app.datadealKeeper = *datadealkeeper.NewKeeper(
appCodec,
keys[datadealtypes.StoreKey],
keys[datadealtypes.MemStoreKey],
app.AccountKeeper,
app.BankKeeper,
)

app.oracleKeeper = *oraclekeeper.NewKeeper(
appCodec,
keys[oracletypes.StoreKey],
Expand Down Expand Up @@ -514,6 +529,7 @@ func New(
burn.NewAppModule(appCodec, app.burnKeeper),
oracle.NewAppModule(appCodec, app.oracleKeeper),
wasm.NewAppModule(appCodec, &app.wasmKeeper, app.StakingKeeper),
datadeal.NewAppModule(appCodec, app.datadealKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -544,6 +560,7 @@ func New(
burntypes.ModuleName,
genutiltypes.ModuleName,
authz.ModuleName,
datadealtypes.ModuleName,
)

app.mm.SetOrderEndBlockers(
Expand All @@ -570,6 +587,7 @@ func New(
wasm.ModuleName,
paramstypes.ModuleName,
authz.ModuleName,
datadealtypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -601,6 +619,7 @@ func New(
upgradetypes.ModuleName,
vestingtypes.ModuleName,
authz.ModuleName,
datadealtypes.ModuleName,
)

app.mm.RegisterInvariants(&app.CrisisKeeper)
Expand Down Expand Up @@ -826,6 +845,7 @@ func initParamsKeeper(appCodec codec.Codec, legacyAmino *codec.LegacyAmino, key,
paramsKeeper.Subspace(burntypes.ModuleName)
paramsKeeper.Subspace(oracletypes.ModuleName)
paramsKeeper.Subspace(wasm.ModuleName)
paramsKeeper.Subspace(datadealtypes.ModuleName)

return paramsKeeper
}
Expand Down
1 change: 0 additions & 1 deletion proto/panacea/datadeal/v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ service Query {
option (google.api.http).get = "/panacea/datadeal/v2/deals/{deal_id}/certificates/{data_hash}";
}


}

// QueryDealsRequest defines the request type for the Query/Deals RPC method.
Expand Down
48 changes: 32 additions & 16 deletions types/testsuite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import (
dbm "github.com/tendermint/tm-db"

"github.com/medibloc/panacea-core/v2/app/params"
datadealkeeper "github.com/medibloc/panacea-core/v2/x/datadeal/keeper"
datadealtypes "github.com/medibloc/panacea-core/v2/x/datadeal/types"
didkeeper "github.com/medibloc/panacea-core/v2/x/did/keeper"
didtypes "github.com/medibloc/panacea-core/v2/x/did/types"
)
Expand All @@ -55,22 +57,24 @@ type TestSuite struct {

Ctx sdk.Context

AccountKeeper authkeeper.AccountKeeper
StakingKeeper stakingkeeper.Keeper
AolKeeper aolkeeper.Keeper
AolMsgServer aoltypes.MsgServer
BankKeeper bankkeeper.Keeper
BurnKeeper burnkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
DistrKeeper distrkeeper.Keeper
IBCKeeper *ibckeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
DIDMsgServer didtypes.MsgServer
DIDKeeper didkeeper.Keeper
OracleKeeper oraclekeeper.Keeper
OracleMsgServer oracletypes.MsgServer
WasmKeeper wasm.Keeper
UpgradeKeeper upgradekeeper.Keeper
AccountKeeper authkeeper.AccountKeeper
StakingKeeper stakingkeeper.Keeper
AolKeeper aolkeeper.Keeper
AolMsgServer aoltypes.MsgServer
BankKeeper bankkeeper.Keeper
BurnKeeper burnkeeper.Keeper
CapabilityKeeper *capabilitykeeper.Keeper
DistrKeeper distrkeeper.Keeper
IBCKeeper *ibckeeper.Keeper
TransferKeeper ibctransferkeeper.Keeper
DIDMsgServer didtypes.MsgServer
DIDKeeper didkeeper.Keeper
DataDealKeeper datadealkeeper.Keeper
DataDealMsgServer datadealtypes.MsgServer
OracleKeeper oraclekeeper.Keeper
OracleMsgServer oracletypes.MsgServer
WasmKeeper wasm.Keeper
UpgradeKeeper upgradekeeper.Keeper
}

func (suite *TestSuite) SetupTest() {
Expand All @@ -80,6 +84,7 @@ func (suite *TestSuite) SetupTest() {
banktypes.StoreKey,
paramstypes.StoreKey,
didtypes.StoreKey,
datadealtypes.StoreKey,
oracletypes.StoreKey,
wasm.StoreKey,
ibchost.StoreKey,
Expand Down Expand Up @@ -208,6 +213,15 @@ func (suite *TestSuite) SetupTest() {
)
suite.DIDMsgServer = didkeeper.NewMsgServerImpl(suite.DIDKeeper)

suite.DataDealKeeper = *datadealkeeper.NewKeeper(
cdc.Marshaler,
keyParams[datadealtypes.StoreKey],
memKeys[datadealtypes.MemStoreKey],
suite.AccountKeeper,
suite.BankKeeper,
)
suite.DataDealMsgServer = datadealkeeper.NewMsgServerImpl(suite.DataDealKeeper)

suite.OracleKeeper = *oraclekeeper.NewKeeper(
cdc.Marshaler,
keyParams[oracletypes.StoreKey],
Expand All @@ -232,6 +246,8 @@ func newTestCodec() params.EncodingConfig {
authtypes.RegisterInterfaces(interfaceRegistry)
banktypes.RegisterInterfaces(interfaceRegistry)
didtypes.RegisterInterfaces(interfaceRegistry)
//TODO: When other Msgs are implemented, I'll remove this comment.
//datadealtypes.RegisterInterfaces(interfaceRegistry)
marshaler := codec.NewProtoCodec(interfaceRegistry)

return params.EncodingConfig{
Expand Down
4 changes: 4 additions & 0 deletions x/datadeal/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdGetDeal())
cmd.AddCommand(CmdGetDeals())

return cmd
}
79 changes: 79 additions & 0 deletions x/datadeal/client/cli/queryDeal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/medibloc/panacea-core/v2/x/datadeal/types"
"github.com/spf13/cobra"
)

func CmdGetDeal() *cobra.Command {
cmd := &cobra.Command{
Use: "deal [deal-id]",
Short: "Query a deal info",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

dealID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}

req := &types.QueryDealRequest{
DealId: dealID,
}
res, err := queryClient.Deal(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdGetDeals() *cobra.Command {
cmd := &cobra.Command{
Use: "deals",
Short: "Query deals",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
req := &types.QueryDealsRequest{
Pagination: pageReq,
}
res, err := queryClient.Deals(cmd.Context(), req)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "all deals")
return cmd
}
1 change: 1 addition & 0 deletions x/datadeal/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdCreateDeal())
return cmd
}
57 changes: 57 additions & 0 deletions x/datadeal/client/cli/txCreateDeal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cli

import (
"encoding/json"
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/medibloc/panacea-core/v2/x/datadeal/types"
"github.com/spf13/cobra"
)

func CmdCreateDeal() *cobra.Command {
cmd := &cobra.Command{
Use: "create-deal [deal-file-path]",
Short: "create a new deal",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg, err := newCreateDealMsg(args[0])
if err != nil {
return err
}

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

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func newCreateDealMsg(file string) (sdk.Msg, error) {
var msg *types.MsgCreateDeal

contents, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}

if err := json.Unmarshal(contents, &msg); err != nil {
return nil, err
}

return msg, nil
}
27 changes: 25 additions & 2 deletions x/datadeal/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,33 @@ import (
// InitGenesis initializes the capability module's state from a provided genesis
// state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
//TODO
for _, deal := range genState.Deals {
if err := k.SetDeal(ctx, &deal); err != nil {
panic(err)
}
}

if err := k.SetNextDealNumber(ctx, genState.NextDealNumber); err != nil {
panic(err)
}

}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
return nil
genesis := types.DefaultGenesis()

deals, err := k.GetAllDeals(ctx)
if err != nil {
panic(err)
}
genesis.Deals = deals

nextDealNum, err := k.GetNextDealNumber(ctx)
if err != nil {
panic(err)
}
genesis.NextDealNumber = nextDealNum

return genesis
}
Loading