Skip to content

Commit

Permalink
fix: can't parse OnAck and OnTimeout packetData Receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
fx0x55 committed Dec 16, 2022
1 parent ff2091f commit 3e6454d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
18 changes: 18 additions & 0 deletions x/ibc/applications/transfer/keeper/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ func parseReceiveAndAmountByPacket(data types.FungibleTokenPacketData) (sdk.AccA
return receiverAddr, transferAmount, sdk.ZeroInt(), nil
}

func parseAmountAndFeeByPacket(data types.FungibleTokenPacketData) (sdk.Int, sdk.Int, error) {
// parse the transfer amount
transferAmount, ok := sdk.NewIntFromString(data.Amount)
if !ok {
return sdk.Int{}, sdk.Int{}, sdkerrors.Wrapf(transfertypes.ErrInvalidAmount, "unable to parse transfer amount (%s) into sdk.Int", data.Amount)
}

feeAmount := sdk.ZeroInt()
if data.Router != "" {
fee, ok := sdk.NewIntFromString(data.Fee)
if !ok || fee.IsNegative() {
return sdk.Int{}, sdk.Int{}, sdkerrors.Wrapf(transfertypes.ErrInvalidAmount, "fee amount is invalid:%s", data.Fee)
}
feeAmount = fee
}
return transferAmount, feeAmount, nil
}

func parsePacketAddress(ibcSender string) (sdk.AccAddress, error) {
_, addBytes, err := bech32.DecodeAndConvert(ibcSender)
return addBytes, err
Expand Down
63 changes: 63 additions & 0 deletions x/ibc/applications/transfer/keeper/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,69 @@ func TestParseReceiveAndAmountByPacket(t *testing.T) {
}
}

func TestParseAmountAndFeeByPacket(t *testing.T) {
type expect struct {
amount sdk.Int
fee sdk.Int
}
testCases := []struct {
name string
packet types.FungibleTokenPacketData
expPass bool
errStr string
expect expect
}{
{
"pass - no router only amount ",
types.FungibleTokenPacketData{Amount: "1"},
true, "",
expect{amount: sdk.NewInt(1), fee: sdk.ZeroInt()},
},
{
"error - amount is empty",
types.FungibleTokenPacketData{Amount: ""},
false,
"unable to parse transfer amount () into sdk.Int: invalid token amount",
expect{amount: sdk.Int{}, fee: sdk.Int{}},
},
{
"error - fee is empty",
types.FungibleTokenPacketData{Amount: "1", Fee: "", Router: "aaa"},
false,
"fee amount is invalid:: invalid token amount",
expect{amount: sdk.Int{}, fee: sdk.Int{}},
},
{
"error - fee is negative",
types.FungibleTokenPacketData{Amount: "1", Fee: "-1", Router: "aaa"},
false,
"fee amount is invalid:-1: invalid token amount",
expect{amount: sdk.Int{}, fee: sdk.Int{}},
},
{
"pass - fee is zero",
types.FungibleTokenPacketData{Amount: "1", Fee: "0", Router: "aaa"},
true,
"",
expect{amount: sdk.NewInt(1), fee: sdk.ZeroInt()},
},
}

for i, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualAmount, actualFee, err := parseAmountAndFeeByPacket(tc.packet)
if tc.expPass {
require.NoError(t, err, "valid test case %d failed: %v", i, err)
} else {
require.Error(t, err)
require.EqualValues(t, tc.errStr, err.Error())
}
require.EqualValues(t, tc.expect.amount.String(), actualAmount.String())
require.EqualValues(t, tc.expect.fee.String(), actualFee.String())
})
}
}

func TestParsePacketAddress(t *testing.T) {
testCases := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions x/ibc/applications/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Packet, data types.FungibleTokenPacketData, ack channeltypes.Acknowledgement) error {
switch ack.Response.(type) {
case *channeltypes.Acknowledgement_Error:
_, amount, fee, err := parseReceiveAndAmountByPacket(data)
amount, fee, err := parseAmountAndFeeByPacket(data)
if err != nil {
return err
}
Expand All @@ -258,7 +258,7 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Pac
// OnTimeoutPacket refunds the sender since the original packet sent was
// never received and has been timed out.
func (k Keeper) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet, data types.FungibleTokenPacketData) error {
_, amount, fee, err := parseReceiveAndAmountByPacket(data)
amount, fee, err := parseAmountAndFeeByPacket(data)
if err != nil {
return err
}
Expand Down

0 comments on commit 3e6454d

Please sign in to comment.