Skip to content

Commit 6ec3bed

Browse files
authored
Simplify the Example Code
simplify the example code
1 parent 4b3f68d commit 6ec3bed

File tree

1 file changed

+6
-29
lines changed
  • not-so-smart-contracts/cosmos/missing_error_handler

1 file changed

+6
-29
lines changed

not-so-smart-contracts/cosmos/missing_error_handler/README.md

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,13 @@ The idiomatic way of handling errors in `Go` is to compare the returned error to
44

55

66
## Example
7-
In the following code, `k.bankKeeper.SendCoins(ctx, lender, borrower, amount)` does not have any return values being used, including `err`. This results in `SendCoin` not being able to prevent the transaction from executing even if there is an `error` due to insufficient balance in `SendCoin`.
7+
In the following code, `k.bankKeeper.SendCoins(ctx, sender, receiver, amount)` does not have any return values being used, including `err`. This results in `SendCoin` not being able to prevent the transaction from executing even if there is an `error` due to insufficient balance in `SendCoin`.
88
```golang
9-
func (k msgServer) ApproveLoan(goCtx context.Context, msg *types.MsgApproveLoan) (*types.MsgApproveLoanResponse, error) {
10-
ctx := sdk.UnwrapSDKContext(goCtx)
11-
12-
loan, found := k.GetLoans(ctx, msg.Id)
13-
if !found {
14-
return nil, sdkerrors.Wrapf(sdkerrors.ErrKeyNotFound, "key %d doesn't exist", msg.Id)
15-
}
16-
17-
// TODO: for some reason the error doesn't get printed to the terminal
18-
if loan.State != "requested" {
19-
return nil, sdkerrors.Wrapf(types.ErrWrongLoanState, "%v", loan.State)
20-
}
21-
22-
lender, _ := sdk.AccAddressFromBech32(msg.Creator)
23-
borrower, _ := sdk.AccAddressFromBech32(loan.Borrower)
24-
amount, err := sdk.ParseCoinsNormalized(loan.Amount)
25-
if err != nil {
26-
return nil, sdkerrors.Wrap(types.ErrWrongLoanState, "Cannot parse coins in loan amount")
27-
}
28-
29-
k.bankKeeper.SendCoins(ctx, lender, borrower, amount)
30-
31-
loan.Lender = msg.Creator
32-
loan.State = "approved"
33-
34-
k.SetLoans(ctx, loan)
35-
36-
return &types.MsgApproveLoanResponse{}, nil
9+
func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*types.MsgTransferResponse, error) {
10+
...
11+
k.bankKeeper.SendCoins(ctx, sender, receiver, amount)
12+
...
13+
return &types.MsgTransferResponse{}, nil
3714
}
3815
```
3916
## Mitigations

0 commit comments

Comments
 (0)