Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp: adding MetadataFromVersion to 29-fee pkg types #4100

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions modules/apps/29-fee/types/metadata.go
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions modules/apps/29-fee/types/metadata_test.go
Original file line number Diff line number Diff line change
@@ -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)
}