Skip to content

Commit

Permalink
imp: adding MetadataFromVersion to 29-fee pkg types (#4100)
Browse files Browse the repository at this point in the history
* adding MetadataFromVersion func to pkg types

* adding additional error context to MetadataFromVersion

* order imports
  • Loading branch information
damiannolan authored Jul 20, 2023
1 parent 5c17048 commit 31b3c60
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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)
}

0 comments on commit 31b3c60

Please sign in to comment.