Skip to content

Commit

Permalink
Handling Loan request
Browse files Browse the repository at this point in the history
  • Loading branch information
DuyVK23 committed Jun 30, 2024
1 parent 11dc78c commit eb0d681
Show file tree
Hide file tree
Showing 12 changed files with 2,018 additions and 67 deletions.
1,213 changes: 1,190 additions & 23 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.

70 changes: 62 additions & 8 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16447,6 +16447,51 @@ paths:
additionalProperties: {}
tags:
- Query
/loan.loan.Msg/RequestLoan:
post:
operationId: LoanLoanMsg_RequestLoan
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
amount:
type: string
fee:
type: string
collateral:
type: string
deadline:
type: string
tags:
- Msg
/loan.loan.Msg/UpdateParams:
post:
summary: |-
Expand Down Expand Up @@ -16495,10 +16540,7 @@ paths:
authority is the address that controls the module (defaults to
x/gov unless overwritten).
params:
description: |-
params defines the module parameters to update.

NOTE: All parameters must be supplied.
description: 'NOTE: All parameters must be supplied.'
type: object
description: MsgUpdateParams is the Msg/UpdateParams request type.
tags:
Expand Down Expand Up @@ -23336,6 +23378,21 @@ definitions:
description: params holds all the parameters of this module.
type: object
description: QueryParamsResponse is response type for the Query/Params RPC method.
loan.loan.MsgRequestLoan:
type: object
properties:
creator:
type: string
amount:
type: string
fee:
type: string
collateral:
type: string
deadline:
type: string
loan.loan.MsgRequestLoanResponse:
type: object
loan.loan.MsgUpdateParams:
type: object
properties:
Expand All @@ -23345,10 +23402,7 @@ definitions:
authority is the address that controls the module (defaults to x/gov
unless overwritten).
params:
description: |-
params defines the module parameters to update.

NOTE: All parameters must be supplied.
description: 'NOTE: All parameters must be supplied.'
type: object
description: MsgUpdateParams is the Msg/UpdateParams request type.
loan.loan.MsgUpdateParamsResponse:
Expand Down
34 changes: 22 additions & 12 deletions proto/loan/loan/tx.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
syntax = "proto3";

package loan.loan;

import "amino/amino.proto";
Expand All @@ -12,29 +13,38 @@ option go_package = "loan/x/loan/types";
// Msg defines the Msg service.
service Msg {
option (cosmos.msg.v1.service) = true;

// 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 UpdateParams (MsgUpdateParams) returns (MsgUpdateParamsResponse);
rpc RequestLoan (MsgRequestLoan ) returns (MsgRequestLoanResponse );
}

// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "loan/x/loan/MsgUpdateParams";

option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "loan/x/loan/MsgUpdateParams";
// authority is the address that controls the module (defaults to x/gov unless overwritten).
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// params defines the module parameters to update.
//

// NOTE: All parameters must be supplied.
Params params = 2 [
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
Params params = 2 [(gogoproto.nullable) = false, (amino.dont_omitempty) = true];
}

// MsgUpdateParamsResponse defines the response structure for executing a
// MsgUpdateParams message.
message MsgUpdateParamsResponse {}
message MsgUpdateParamsResponse {}

message MsgRequestLoan {
option (cosmos.msg.v1.signer) = "creator";
string creator = 1;
string amount = 2;
string fee = 3;
string collateral = 4;
string deadline = 5;
}

message MsgRequestLoanResponse {}

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

// TODO: Handling the message
_ = ctx

return &types.MsgRequestLoanResponse{}, nil
}
6 changes: 6 additions & 0 deletions x/loan/module/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
},
{
RpcMethod: "RequestLoan",
Use: "request-loan [amount] [fee] [collateral] [deadline]",
Short: "Send a request-loan tx",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "amount"}, {ProtoField: "fee"}, {ProtoField: "collateral"}, {ProtoField: "deadline"}},
},
// this line is used by ignite scaffolding # autocli/tx
},
},
Expand Down
25 changes: 24 additions & 1 deletion x/loan/module/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ var (
)

const (
// this line is used by starport scaffolding # simapp/module/const
opWeightMsgRequestLoan = "op_weight_msg_request_loan"
// TODO: Determine the simulation weight value
defaultWeightMsgRequestLoan int = 100

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

// GenerateGenesisState creates a randomized GenState of the module.
Expand All @@ -46,6 +50,17 @@ func (am AppModule) RegisterStoreDecoder(_ simtypes.StoreDecoderRegistry) {}
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
operations := make([]simtypes.WeightedOperation, 0)

var weightMsgRequestLoan int
simState.AppParams.GetOrGenerate(opWeightMsgRequestLoan, &weightMsgRequestLoan, nil,
func(_ *rand.Rand) {
weightMsgRequestLoan = defaultWeightMsgRequestLoan
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgRequestLoan,
loansimulation.SimulateMsgRequestLoan(am.accountKeeper, am.bankKeeper, am.keeper),
))

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

return operations
Expand All @@ -54,6 +69,14 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
// ProposalMsgs returns msgs used for governance proposals for simulations.
func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg {
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
opWeightMsgRequestLoan,
defaultWeightMsgRequestLoan,
func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg {
loansimulation.SimulateMsgRequestLoan(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/request_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 SimulateMsgRequestLoan(
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.MsgRequestLoan{
Creator: simAccount.Address.String(),
}

// TODO: Handling the RequestLoan simulation

return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "RequestLoan 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 @@ -8,6 +8,9 @@ import (
)

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgRequestLoan{},
)
// this line is used by starport scaffolding # 3

registry.RegisterImplementations((*sdk.Msg)(nil),
Expand Down
27 changes: 27 additions & 0 deletions x/loan/types/message_request_loan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 = &MsgRequestLoan{}

func NewMsgRequestLoan(creator string, amount string, fee string, collateral string, deadline string) *MsgRequestLoan {
return &MsgRequestLoan{
Creator: creator,
Amount: amount,
Fee: fee,
Collateral: collateral,
Deadline: deadline,
}
}

func (msg *MsgRequestLoan) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
return nil
}
Loading

0 comments on commit eb0d681

Please sign in to comment.