Path: blob/main/docs/versioned_docs/version-v0.27/02-guide/05-loan/03-request.md
1007 views
Request a loan
Implement RequestLoan
keeper method that will be called whenever a user requests a loan. RequestLoan
creates a new loan with the provided data, sends the collateral from the borrower's account to a module account, and adds the loan to the blockchain's store.
Keeper method
The function takes in two arguments: a context.Context
object and a pointer to a types.MsgRequestLoan
struct. It returns a pointer to a types.MsgRequestLoanResponse
struct and an error
object.
The first thing the function does is create a new types.Loan
struct with the data from the input types.MsgRequestLoan
struct. It sets the State
field of the types.Loan
struct to "requested".
Next, the function gets the borrower's address from the msg.Creator
field of the input types.MsgRequestLoan
struct. It then parses the loan.Collateral
field (which is a string) into sdk.Coins
using the sdk.ParseCoinsNormalized
function.
The function then sends the collateral from the borrower's account to a module account using the k.bankKeeper.SendCoinsFromAccountToModule
function. Finally, it adds the new loan to a keeper using the k.AppendLoan
function. The function returns a types.MsgRequestLoanResponse
struct and a nil
error if all goes well.
Basic message validation
When a loan is created, a certain message input validation is required. You want to throw error messages in case the end user tries impossible inputs.