diff --git a/modules/apps/29-fee/types/metadata.go b/modules/apps/29-fee/types/metadata.go new file mode 100644 index 00000000000..6771595c5dc --- /dev/null +++ b/modules/apps/29-fee/types/metadata.go @@ -0,0 +1,15 @@ +package types + +import errorsmod "cosmossdk.io/errors" + +// MetadataFromVersion attempts to parse the given string into a fee version Metadata, +// an error is returned if it fails to do so. +func MetadataFromVersion(version string) (Metadata, error) { + var metadata Metadata + err := ModuleCdc.UnmarshalJSON([]byte(version), &metadata) + if err != nil { + return Metadata{}, errorsmod.Wrapf(ErrInvalidVersion, "failed to unmarshal metadata from version: %s", version) + } + + return metadata, nil +} diff --git a/modules/apps/29-fee/types/metadata_test.go b/modules/apps/29-fee/types/metadata_test.go new file mode 100644 index 00000000000..2261cc3efbd --- /dev/null +++ b/modules/apps/29-fee/types/metadata_test.go @@ -0,0 +1,30 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types" + ibcmock "github.com/cosmos/ibc-go/v7/testing/mock" +) + +func TestMetadataFromVersion(t *testing.T) { + testMetadata := types.Metadata{ + AppVersion: ibcmock.Version, + FeeVersion: types.Version, + } + + versionBz, err := types.ModuleCdc.MarshalJSON(&testMetadata) + require.NoError(t, err) + + metadata, err := types.MetadataFromVersion(string(versionBz)) + require.NoError(t, err) + require.Equal(t, ibcmock.Version, metadata.AppVersion) + require.Equal(t, types.Version, metadata.FeeVersion) + + metadata, err = types.MetadataFromVersion("") + require.Error(t, err) + require.ErrorIs(t, err, types.ErrInvalidVersion) + require.Empty(t, metadata) +}