Skip to content

Commit

Permalink
ignite scaffold message cancel-loan id:uint
Browse files Browse the repository at this point in the history
  • Loading branch information
outsmartchad committed May 16, 2024
1 parent 871f307 commit a24f890
Show file tree
Hide file tree
Showing 12 changed files with 1,616 additions and 81 deletions.
1,022 changes: 977 additions & 45 deletions api/loan/loan/tx.pulsar.go

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions api/loan/loan/tx_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16487,6 +16487,46 @@ paths:
format: uint64
tags:
- Msg
/loan.loan.Msg/CancelLoan:
post:
operationId: LoanLoanMsg_CancelLoan
responses:
'200':
description: A successful response.
schema:
type: object
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
parameters:
- name: body
in: body
required: true
schema:
type: object
properties:
creator:
type: string
id:
type: string
format: uint64
tags:
- Msg
/loan.loan.Msg/LiquidateLoan:
post:
operationId: LoanLoanMsg_LiquidateLoan
Expand Down Expand Up @@ -23508,6 +23548,16 @@ definitions:
format: uint64
loan.loan.MsgApproveLoanResponse:
type: object
loan.loan.MsgCancelLoan:
type: object
properties:
creator:
type: string
id:
type: string
format: uint64
loan.loan.MsgCancelLoanResponse:
type: object
loan.loan.MsgLiquidateLoan:
type: object
properties:
Expand Down
9 changes: 9 additions & 0 deletions proto/loan/loan/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ service Msg {
rpc ApproveLoan (MsgApproveLoan ) returns (MsgApproveLoanResponse );
rpc RepayLoan (MsgRepayLoan ) returns (MsgRepayLoanResponse );
rpc LiquidateLoan (MsgLiquidateLoan) returns (MsgLiquidateLoanResponse);
rpc CancelLoan (MsgCancelLoan ) returns (MsgCancelLoanResponse );
}
// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
Expand Down Expand Up @@ -75,3 +76,11 @@ message MsgLiquidateLoan {

message MsgLiquidateLoanResponse {}

message MsgCancelLoan {
option (cosmos.msg.v1.signer) = "creator";
string creator = 1;
uint64 id = 2;
}

message MsgCancelLoanResponse {}

18 changes: 18 additions & 0 deletions x/loan/keeper/msg_server_cancel_loan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package keeper

import (
"context"

"loan/x/loan/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k msgServer) CancelLoan(goCtx context.Context, msg *types.MsgCancelLoan) (*types.MsgCancelLoanResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx

return &types.MsgCancelLoanResponse{}, nil
}
6 changes: 6 additions & 0 deletions x/loan/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Short: "Send a liquidate-loan tx",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "id"}},
},
{
RpcMethod: "CancelLoan",
Use: "cancel-loan [id]",
Short: "Send a cancel-loan tx",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "id"}},
},
// this line is used by ignite scaffolding # autocli/tx
},
},
Expand Down
23 changes: 23 additions & 0 deletions x/loan/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const (
// TODO: Determine the simulation weight value
defaultWeightMsgLiquidateLoan int = 100

opWeightMsgCancelLoan = "op_weight_msg_cancel_loan"
// TODO: Determine the simulation weight value
defaultWeightMsgCancelLoan int = 100

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

Expand Down Expand Up @@ -106,6 +110,17 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
loansimulation.SimulateMsgLiquidateLoan(am.accountKeeper, am.bankKeeper, am.keeper),
))

var weightMsgCancelLoan int
simState.AppParams.GetOrGenerate(opWeightMsgCancelLoan, &weightMsgCancelLoan, nil,
func(_ *rand.Rand) {
weightMsgCancelLoan = defaultWeightMsgCancelLoan
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgCancelLoan,
loansimulation.SimulateMsgCancelLoan(am.accountKeeper, am.bankKeeper, am.keeper),
))

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

return operations
Expand Down Expand Up @@ -146,6 +161,14 @@ func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.Wei
return nil
},
),
simulation.NewWeightedProposalMsg(
opWeightMsgCancelLoan,
defaultWeightMsgCancelLoan,
func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg {
loansimulation.SimulateMsgCancelLoan(am.accountKeeper, am.bankKeeper, am.keeper)
return nil
},
),
// this line is used by starport scaffolding # simapp/module/OpMsg
}
}
30 changes: 30 additions & 0 deletions x/loan/simulation/cancel_loan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package simulation

import (
"math/rand"

"loan/x/loan/keeper"
"loan/x/loan/types"

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

func SimulateMsgCancelLoan(
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.MsgCancelLoan{
Creator: simAccount.Address.String(),
}

// TODO: Handling the CancelLoan simulation

return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "CancelLoan simulation not implemented"), nil, nil
}
}
3 changes: 3 additions & 0 deletions x/loan/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgLiquidateLoan{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgCancelLoan{},
)
// this line is used by starport scaffolding # 3

registry.RegisterImplementations((*sdk.Msg)(nil),
Expand Down
24 changes: 24 additions & 0 deletions x/loan/types/message_cancel_loan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package types

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

var _ sdk.Msg = &MsgCancelLoan{}

func NewMsgCancelLoan(creator string, id uint64) *MsgCancelLoan {
return &MsgCancelLoan{
Creator: creator,
Id: id,
}
}

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

import (
"testing"

"loan/testutil/sample"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
)

func TestMsgCancelLoan_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgCancelLoan
err error
}{
{
name: "invalid address",
msg: MsgCancelLoan{
Creator: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
msg: MsgCancelLoan{
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 a24f890

Please sign in to comment.