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 DeactivateDeal Tx keeper, and CLI #543

Merged
merged 8 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x/datadeal/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ func GetTxCmd() *cobra.Command {
}

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

import (
"strconv"

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

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

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

requesterAddr := clientCtx.GetFromAddress()

msgDeactivateDeal := types.NewMsgDeactivateDeal(dealID, requesterAddr.String())

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

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgDeactivateDeal)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
40 changes: 40 additions & 0 deletions x/datadeal/keeper/deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,43 @@ func (k Keeper) IncreaseCurNumDataOfDeal(ctx sdk.Context, dealID uint64) error {
}
return nil
}

func (k Keeper) DeactivateDeal(ctx sdk.Context, msg *types.MsgDeactivateDeal) error {
deal, err := k.GetDeal(ctx, msg.DealId)
if err != nil {
return sdkerrors.Wrapf(types.ErrDeactivateDeal, err.Error())
}

if deal.ConsumerAddress != msg.RequesterAddress {
return sdkerrors.Wrapf(types.ErrDeactivateDeal, "only consumer can deactivate the deal")
}

if deal.Status != types.DEAL_STATUS_ACTIVE {
return sdkerrors.Wrapf(types.ErrDeactivateDeal, "deal's status is not 'ACTIVE'")
}

deal.Status = types.DEAL_STATUS_INACTIVE

err = k.SetDeal(ctx, deal)
if err != nil {
return sdkerrors.Wrapf(types.ErrDeactivateDeal, err.Error())
}

dealAccAddr, err := sdk.AccAddressFromBech32(deal.Address)
if err != nil {
return sdkerrors.Wrapf(types.ErrDeactivateDeal, err.Error())
}

consumerAccAddr, err := sdk.AccAddressFromBech32(deal.ConsumerAddress)
if err != nil {
return sdkerrors.Wrapf(types.ErrDeactivateDeal, err.Error())
}

// refund a budget to consumer
err = k.bankKeeper.SendCoins(ctx, dealAccAddr, consumerAccAddr, sdk.NewCoins(*deal.Budget))
if err != nil {
return sdkerrors.Wrapf(types.ErrDeactivateDeal, err.Error())
}

return nil
}
70 changes: 70 additions & 0 deletions x/datadeal/keeper/deal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type dealTestSuite struct {

defaultFunds sdk.Coins
consumerAccAddr sdk.AccAddress
providerAccAddr sdk.AccAddress
}

func TestDealTestSuite(t *testing.T) {
Expand All @@ -24,6 +25,7 @@ func TestDealTestSuite(t *testing.T) {

func (suite *dealTestSuite) BeforeTest(_, _ string) {
suite.consumerAccAddr = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
suite.providerAccAddr = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
suite.defaultFunds = sdk.NewCoins(sdk.NewCoin(assets.MicroMedDenom, sdk.NewInt(10000000000)))

testDeal := suite.MakeTestDeal(1, suite.consumerAccAddr, 100)
Expand Down Expand Up @@ -93,3 +95,71 @@ func (suite *dealTestSuite) TestCheckDealCurNumDataAndIncrement() {
check = updatedDeal.IsCompleted()
suite.Require().Equal(true, check)
}

func (suite *dealTestSuite) TestRequestDeactivateDeal() {
ctx := suite.Ctx

err := suite.FundAccount(ctx, suite.consumerAccAddr, suite.defaultFunds)
suite.Require().NoError(err)

getDeal, err := suite.DataDealKeeper.GetDeal(ctx, 1)
suite.Require().NoError(err)

dealAccAddr, err := sdk.AccAddressFromBech32(getDeal.Address)
suite.Require().NoError(err)

// Sending Budget from buyer to deal
err = suite.BankKeeper.SendCoins(suite.Ctx, suite.consumerAccAddr, dealAccAddr, sdk.NewCoins(*getDeal.Budget))
suite.Require().NoError(err)

msgDeactivateDeal := &types.MsgDeactivateDeal{
DealId: 1,
RequesterAddress: suite.consumerAccAddr.String(),
}

// Consumer Balance = Original Consumer Balance(10000000000umed) - Deal's Budget(1000000000umed) --> 9000000000umed
beforeConsumerBalance := suite.BankKeeper.GetBalance(suite.Ctx, suite.consumerAccAddr, assets.MicroMedDenom)

err = suite.DataDealKeeper.DeactivateDeal(ctx, msgDeactivateDeal)
suite.Require().NoError(err)

getDeal, err = suite.DataDealKeeper.GetDeal(ctx, 1)
suite.Require().NoError(err)
suite.Require().Equal(getDeal.Status, types.DEAL_STATUS_INACTIVE)

// After deactivating a deal, the consumer get the refund from deal.
afterConsumerBalance := suite.BankKeeper.GetBalance(suite.Ctx, suite.consumerAccAddr, assets.MicroMedDenom)
suite.Require().Equal(beforeConsumerBalance.Add(*getDeal.Budget), afterConsumerBalance)
}

func (suite *dealTestSuite) TestRequestDeactivateDealInvalidRequester() {
ctx := suite.Ctx

msgDeactivateDeal := &types.MsgDeactivateDeal{
DealId: 1,
RequesterAddress: suite.providerAccAddr.String(),
}

err := suite.DataDealKeeper.DeactivateDeal(ctx, msgDeactivateDeal)
suite.Require().ErrorIs(err, types.ErrDeactivateDeal)
}

func (suite *dealTestSuite) TestRequestDeactivateDealStatusNotActive() {
ctx := suite.Ctx

getDeal, err := suite.DataDealKeeper.GetDeal(ctx, 1)
suite.Require().NoError(err)

getDeal.Status = types.DEAL_STATUS_COMPLETED

err = suite.DataDealKeeper.SetDeal(ctx, getDeal)
suite.Require().NoError(err)

msgDeactivateDeal := &types.MsgDeactivateDeal{
DealId: 1,
RequesterAddress: suite.consumerAccAddr.String(),
}

err = suite.DataDealKeeper.DeactivateDeal(ctx, msgDeactivateDeal)
suite.Require().ErrorIs(err, types.ErrDeactivateDeal)
}
12 changes: 9 additions & 3 deletions x/datadeal/keeper/msg_server_deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ func (m msgServer) CreateDeal(goCtx context.Context, msg *types.MsgCreateDeal) (
return &types.MsgCreateDealResponse{DealId: newDealID}, nil
}

func (m msgServer) DeactivateDeal(ctx context.Context, deal *types.MsgDeactivateDeal) (*types.MsgDeactivateDealResponse, error) {
//TODO implement me
panic("implement me")
func (m msgServer) DeactivateDeal(goCtx context.Context, msg *types.MsgDeactivateDeal) (*types.MsgDeactivateDealResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

err := m.Keeper.DeactivateDeal(ctx, msg)
if err != nil {
return nil, err
}

return &types.MsgDeactivateDealResponse{}, nil
}

func (m msgServer) SubmitConsent(goCtx context.Context, msg *types.MsgSubmitConsent) (*types.MsgSubmitConsentResponse, error) {
Expand Down
1 change: 1 addition & 0 deletions x/datadeal/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ var (
ErrCertificateNotFound = sdkerrors.Register(ModuleName, 5, "certificate is not found")
ErrGetCertificate = sdkerrors.Register(ModuleName, 6, "error while get certificate")
ErrSubmitConsent = sdkerrors.Register(ModuleName, 7, "error while submit consent")
ErrDeactivateDeal = sdkerrors.Register(ModuleName, 8, "error while deactivate deal")
)
8 changes: 8 additions & 0 deletions x/datadeal/types/message_deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (m *UnsignedCertificate) ValidateBasic() error {

var _ sdk.Msg = &MsgDeactivateDeal{}

func NewMsgDeactivateDeal(dealID uint64, requesterAddress string) *MsgDeactivateDeal {
return &MsgDeactivateDeal{
DealId: dealID,
RequesterAddress: requesterAddress,
}
}

func (m *MsgDeactivateDeal) Route() string {
return RouterKey
}
Expand All @@ -133,6 +140,7 @@ func (m *MsgDeactivateDeal) Type() string {
}

func (m *MsgDeactivateDeal) ValidateBasic() error {

if _, err := sdk.AccAddressFromBech32(m.RequesterAddress); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "requesterAddress is invalid. address: %s, error: %s", m.RequesterAddress, err.Error())
}
Expand Down