Defi Loan

- 2 mins

title: 开发一个loan模块的过程 & 代码规范 —

开发一个loan模块的过程 & 代码规范

增加loan模块, 依赖bank模块

ignite scaffold module loan --dep bank

list是什么命令?

“list”脚手架命令用于生成文件,实现在区块链状态中存储和与存储为列表的数据交互的逻辑。

ignite scaffold list loan amount fee collateral deadline state borrower lender --no-message

消息处理: 请求,批准,偿还,清算,取消贷款

申请贷款, 输入参数: 金额, 手续费, 保证金, 到期时间

ignite scaffold message request-loan amount fee collateral deadline

批准贷款, 贷款id

ignite scaffold message approve-loan id:uint

取消贷款, 贷款id

ignite scaffold message cancel-loan id:uint

偿还贷款, 贷款id

ignite scaffold message repay-loan id:uint

清算贷款, 贷款id

ignite scaffold message liquidate-loan id:uint

添加expected_keepers代码

添加接口函数, 以及返回错误

添加keeper/msg_server_xxx代码

(处理CRUD)

添加types/message validateBasic代码

什么时候添加panic错误?

在提供的代码示例中,panic 错误主要用于处理 不可恢复的错误,例如:

borrower, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
    panic(err)
}
collateral, err := sdk.ParseCoinsNormalized(loan.Collateral)
if err != nil {
    panic(err)
}
deadline, err := strconv.ParseInt(loan.Deadline, 10, 64)
if err != nil {
    panic(err)
}

使用 panic 错误的理由:

需要注意的是:

建议:

什么时候返回types自定义错误?

在提供的代码示例中,自定义错误主要用于处理 业务逻辑相关的错误,例如:

if loan.State != "requested" {
    return nil, errorsmod.Wrapf(types.ErrWrongLoanState, "%v", loan.State)
}
if ctx.BlockHeight() < deadline {
    return nil, errorsmod.Wrap(types.ErrDeadline, "Cannot liquidate before deadline")
}

使用自定义错误的理由:

建议:

为什么有的验证代码写在types里面, 有的写在keeper里面?