Skip to content

Commit

Permalink
ignite scaffold message liquidate-loan id:uint
Browse files Browse the repository at this point in the history
  • Loading branch information
outsmartchad committed May 16, 2024
1 parent e213c42 commit 871f307
Show file tree
Hide file tree
Showing 12 changed files with 1,631 additions and 96 deletions.
1,040 changes: 986 additions & 54 deletions api/loan/loan/tx.pulsar.go

Large diffs are not rendered by default.

45 changes: 41 additions & 4 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/LiquidateLoan:
post:
operationId: LoanLoanMsg_LiquidateLoan
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/RepayLoan:
post:
operationId: LoanLoanMsg_RepayLoan
Expand Down Expand Up @@ -23468,6 +23508,16 @@ definitions:
format: uint64
loan.loan.MsgApproveLoanResponse:
type: object
loan.loan.MsgLiquidateLoan:
type: object
properties:
creator:
type: string
id:
type: string
format: uint64
loan.loan.MsgLiquidateLoanResponse:
type: object
loan.loan.MsgRepayLoan:
type: object
properties:
Expand Down
17 changes: 13 additions & 4 deletions proto/loan/loan/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ service Msg {

// UpdateParams defines a (governance) operation for updating the module
// parameters. The authority defaults to the x/gov module account.
rpc UpdateParams (MsgUpdateParams) returns (MsgUpdateParamsResponse);
rpc RequestLoan (MsgRequestLoan ) returns (MsgRequestLoanResponse );
rpc ApproveLoan (MsgApproveLoan ) returns (MsgApproveLoanResponse );
rpc RepayLoan (MsgRepayLoan ) returns (MsgRepayLoanResponse );
rpc UpdateParams (MsgUpdateParams ) returns (MsgUpdateParamsResponse );
rpc RequestLoan (MsgRequestLoan ) returns (MsgRequestLoanResponse );
rpc ApproveLoan (MsgApproveLoan ) returns (MsgApproveLoanResponse );
rpc RepayLoan (MsgRepayLoan ) returns (MsgRepayLoanResponse );
rpc LiquidateLoan (MsgLiquidateLoan) returns (MsgLiquidateLoanResponse);
}
// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
Expand Down Expand Up @@ -66,3 +67,11 @@ message MsgRepayLoan {

message MsgRepayLoanResponse {}

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

message MsgLiquidateLoanResponse {}

18 changes: 18 additions & 0 deletions x/loan/keeper/msg_server_liquidate_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) LiquidateLoan(goCtx context.Context, msg *types.MsgLiquidateLoan) (*types.MsgLiquidateLoanResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx

return &types.MsgLiquidateLoanResponse{}, nil
}
6 changes: 6 additions & 0 deletions x/loan/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Short: "Send a repay-loan tx",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "id"}},
},
{
RpcMethod: "LiquidateLoan",
Use: "liquidate-loan [id]",
Short: "Send a liquidate-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 @@ -35,6 +35,10 @@ const (
// TODO: Determine the simulation weight value
defaultWeightMsgRepayLoan int = 100

opWeightMsgLiquidateLoan = "op_weight_msg_liquidate_loan"
// TODO: Determine the simulation weight value
defaultWeightMsgLiquidateLoan int = 100

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

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

var weightMsgLiquidateLoan int
simState.AppParams.GetOrGenerate(opWeightMsgLiquidateLoan, &weightMsgLiquidateLoan, nil,
func(_ *rand.Rand) {
weightMsgLiquidateLoan = defaultWeightMsgLiquidateLoan
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgLiquidateLoan,
loansimulation.SimulateMsgLiquidateLoan(am.accountKeeper, am.bankKeeper, am.keeper),
))

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

return operations
Expand Down Expand Up @@ -123,6 +138,14 @@ func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.Wei
return nil
},
),
simulation.NewWeightedProposalMsg(
opWeightMsgLiquidateLoan,
defaultWeightMsgLiquidateLoan,
func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg {
loansimulation.SimulateMsgLiquidateLoan(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/liquidate_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 SimulateMsgLiquidateLoan(
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.MsgLiquidateLoan{
Creator: simAccount.Address.String(),
}

// TODO: Handling the LiquidateLoan simulation

return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "LiquidateLoan 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 @@ -17,6 +17,9 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgRepayLoan{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgLiquidateLoan{},
)
// 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_liquidate_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 = &MsgLiquidateLoan{}

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

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

Please sign in to comment.