Path: blob/main/docs/versioned_docs/version-v0.27/02-guide/05-loan/07-cancel.md
1007 views
Cancel a loan
As a borrower, you have the option to cancel a loan you have created if you no longer want to proceed with it. However, this action is only possible if the loan's current status is marked as "requested".
If you decide to cancel the loan, the collateral tokens that were being held as security for the loan will be transferred back to your account from the module account. This means that you will regain possession of the collateral tokens you had originally put up for the loan.
CancelLoan
takes in two arguments: a context.Context
named goCtx
and a pointer to a types.MsgCancelLoan
named msg
. It returns a pointer to a types.MsgCancelLoanResponse
and an error.
The function begins by using the sdk.UnwrapSDKContext
method to get the sdk.Context
from the context.Context
object. It then uses the GetLoan
method of the msgServer
type to retrieve a loan identified by the Id
field of the msg
argument. If the loan is not found, the function returns an error using the sdk.ErrKeyNotFound
error wrapped with the sdk.Wrap
method.
Next, the function checks if the Creator
field of the msg argument is the same as the Borrower
field of the loan. If they are not the same, the function returns an error using the sdk.ErrUnauthorized
error wrapped with the sdk.Wrap
method.
The function then checks if the State
field of the loan is equal to the string "requested"
. If it is not, the function returns an error using the types.ErrWrongLoanState
error wrapped with the sdk.Wrapf
method.
If the loan has the correct state and the creator of the message is the borrower of the loan, the function proceeds to send the collateral coins held in the Collateral
field of the loan back to the borrower's account using the SendCoinsFromModuleToAccount
method of the bankKeeper
. The function then updates the State field of the loan to the string "cancelled" and sets the updated loan using the SetLoan
method. Finally, the function returns a types.MsgCancelLoanResponse
object and a nil error.