Skip to content

Commit

Permalink
fix: don't allow self transfer in msg-send
Browse files Browse the repository at this point in the history
  • Loading branch information
aleem1314 committed Dec 9, 2022
1 parent 750875c commit 3258537
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions x/ecocredit/base/keeper/msg_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ import (

// Send sends credits to a recipient.
// Send also retires credits if the amount to retire is specified in the request.
// NOTE: This method will return an error if both sender and recipient are same.
func (k Keeper) Send(ctx context.Context, req *types.MsgSend) (*types.MsgSendResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
sender, _ := sdk.AccAddressFromBech32(req.Sender)
recipient, _ := sdk.AccAddressFromBech32(req.Recipient)

if sender.Equals(recipient) {
return nil, sdkerrors.ErrInvalidRequest.Wrap("sender and recipient cannot be the same")
}

for _, credit := range req.Credits {
err := k.sendEcocredits(sdkCtx, credit, recipient, sender)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions x/ecocredit/base/types/v1/features/msg_send.feature
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,23 @@ Feature: MsgSend
}
}
"""

Scenario: an error is returned if sender and recipient are the same
Given the message
"""
{
"sender":"regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"recipient":"regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"credits": [
{
"batch_denom": "C01-001-20200101-20210101-001",
"tradable_amount": "100",
"retired_amount": "100",
"retirement_jurisdiction": "US-WA",
"retirement_reason": "offsetting electricity consumption"
}
]
}
"""
When the message is validated
Then expect the error "sender and recipient cannot be the same: invalid request"
4 changes: 4 additions & 0 deletions x/ecocredit/base/types/v1/msg_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (m *MsgSend) ValidateBasic() error {
return sdkerrors.ErrInvalidAddress.Wrapf("recipient: %s", err)
}

if m.Sender == m.Recipient {
return sdkerrors.ErrInvalidRequest.Wrap("sender and recipient cannot be the same")
}

if len(m.Credits) == 0 {
return sdkerrors.ErrInvalidRequest.Wrap("credits cannot be empty")
}
Expand Down

0 comments on commit 3258537

Please sign in to comment.