Skip to content

fix(apps/transfer): print the transfer amount in the invalid amount error - #9177

Open
im-xiangda-li wants to merge 1 commit into
lfdt-ibc:mainfrom
im-xiangda-li:fix/apps/transfer-print-invalid-amount
Open

im-xiangda-li wants to merge 1 commit into
lfdt-ibc:mainfrom
im-xiangda-li:fix/apps/transfer-print-invalid-amount

Conversation

@im-xiangda-li

Copy link
Copy Markdown

Description

Token.Validate in modules/apps/transfer/types/token.go formatted the parsed transfer amount with %d:

if !amount.IsPositive() {
    return errorsmod.Wrapf(ErrInvalidAmount, "amount must be strictly positive: got %d", amount)
}

sdkmath.Int is a struct that wraps a *big.Int. It implements String(), but not fmt.Formatter, so %d prints the struct's pointer field as a decimal number instead of the amount:

amount=0  -> "amount must be strictly positive: got {102013986923936}"
amount=-1 -> "amount must be strictly positive: got {102013986924000}"

The error text therefore never showed the offending amount, and it changed between runs and between nodes. When a packet with amount "0" is received, this text ends up in the error acknowledgement that the transfer module emits, so different nodes emitted different event attributes for the same packet. The acknowledgement bytes themselves are built from the error code only, so state and the app hash are not affected.

This PR formats the amount with %s, which uses Int.String(), so the error message contains the actual amount:

amount=0  -> "amount must be strictly positive: got 0: invalid token amount"
amount=-1 -> "amount must be strictly positive: got -1: invalid token amount"

Fixes #9130

Checklist

  • One logical change, diff kept as small as practical
  • Commits signed off for the DCO (git commit -s), matching your commit author identity
  • AI assistance, if any, disclosed with a Co-Authored-By or Assisted-By trailer in the commit body
  • Commit messages follow Conventional Commits
  • Tests added or updated, covering expected and error paths
  • CI is green
  • SPDX license header on new files
  • Pull request title follows Conventional Commits (the changelog is generated from it)
  • ADR merged first if this changes architecture, a public interface, or a spec

Test

A regression test was added in modules/apps/transfer/types/token_test.go:

$ go test ./modules/apps/transfer/types/ -run TestValidateErrorMessageContainsAmount -v

=== RUN   TestValidateErrorMessageContainsAmount
=== RUN   TestValidateErrorMessageContainsAmount/failure:_amount_is_zero
=== RUN   TestValidateErrorMessageContainsAmount/failure:_amount_is_negative
=== RUN   TestValidateErrorMessageContainsAmount/failure:_amount_is_a_large_negative_number
--- PASS: TestValidateErrorMessageContainsAmount (0.00s)
    --- PASS: TestValidateErrorMessageContainsAmount/failure:_amount_is_zero (0.00s)
    --- PASS: TestValidateErrorMessageContainsAmount/failure:_amount_is_negative (0.00s)
    --- PASS: TestValidateErrorMessageContainsAmount/failure:_amount_is_a_large_negative_number (0.00s)
PASS
ok      github.com/lfdt-ibc/ibc-go/v11/modules/apps/transfer/types  0.059s

The test asserts that the error message contains the actual amount and that the internal math.Int struct is not printed.

Before this change the same test fails, showing the pointer address reported in the issue:

--- FAIL: TestValidateErrorMessageContainsAmount/failure:_amount_is_negative (0.00s)
    Error: Error "amount must be strictly positive: got {6683232201952}: invalid token amount"
           does not contain "amount must be strictly positive: got -1"

The full package test suite, gofmt and go vet pass as well:

$ go test ./modules/apps/transfer/types/
ok      github.com/lfdt-ibc/ibc-go/v11/modules/apps/transfer/types  0.059s
$ gofmt -l modules/apps/transfer/types/token.go modules/apps/transfer/types/token_test.go
$ go vet ./modules/apps/transfer/types/

…rror

`Token.Validate` formatted the parsed amount with `%d`. `sdkmath.Int`
wraps a `*big.Int` and implements `String()`, but not `fmt.Formatter`, so
`%d` printed the address of the internal pointer instead of the amount:

    amount must be strictly positive: got {102013986923936}

The message therefore never showed the offending amount, and it differed
between runs and between nodes. On a receive of a packet with amount "0"
the text ends up in the error acknowledgement emitted by the transfer
module, so different nodes emitted different event attributes for the same
packet.

Format the amount with `%s` so the error contains the actual value.

Fixes lfdt-ibc#9130

Co-Authored-By: MiniMax M3.1 Flash <noreply@minimax.io>
Signed-off-by: Xiangda Li <102507799+im-xiangda-li@users.noreply.github.com>
@im-xiangda-li
im-xiangda-li requested a review from a team as a code owner November 27, 2026 07:37
@greptile-apps

greptile-apps Bot commented Nov 27, 2026

Copy link
Copy Markdown

This PR does not match any of the 1 configured review trigger rule.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transfer: Token.Validate formats sdkmath.Int with %d, so the error text prints a pointer address

1 participant