Skip to content

Commit

Permalink
bank: name and symbol metadata fields (#8677)
Browse files Browse the repository at this point in the history
* bank: name and symbol metadata fields

* cli test

* changelog
  • Loading branch information
fedekunze authored and dudong2 committed Mar 11, 2022
1 parent bcf23fc commit d65f6ff
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 1 deletion.
5 changes: 5 additions & 0 deletions proto/lbm/bank/v1/bank.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ message Metadata {
// display indicates the suggested denom that should be
// displayed in clients.
string display = 4;
// name defines the name of the token (eg: Cosmos Atom)
string name = 5;
// symbol is the token symbol usually shown on exchanges (eg: ATOM). This can
// be the same as the display.
string symbol = 6;
}
10 changes: 10 additions & 0 deletions x/bank/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func (s *IntegrationTestSuite) SetupSuite() {

bankGenesis.DenomMetadata = []types.Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Expand All @@ -57,6 +59,8 @@ func (s *IntegrationTestSuite) SetupSuite() {
Display: "atom",
},
{
Name: "Ethereum",
Symbol: "ETH",
Description: "Ethereum mainnet token",
DenomUnits: []*types.DenomUnit{
{
Expand Down Expand Up @@ -246,6 +250,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDenomsMetadata() {
expected: &types.QueryDenomsMetadataResponse{
Metadatas: []types.Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Expand All @@ -263,6 +269,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDenomsMetadata() {
Display: "atom",
},
{
Name: "Ethereum",
Symbol: "ETH",
Description: "Ethereum mainnet token",
DenomUnits: []*types.DenomUnit{
{
Expand Down Expand Up @@ -293,6 +301,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDenomsMetadata() {
respType: &types.QueryDenomMetadataResponse{},
expected: &types.QueryDenomMetadataResponse{
Metadata: types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Expand Down
4 changes: 4 additions & 0 deletions x/bank/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func (s *IntegrationTestSuite) TestDenomMetadataGRPCHandler() {
&types.QueryDenomsMetadataResponse{
Metadatas: []types.Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Expand Down Expand Up @@ -152,6 +154,8 @@ func (s *IntegrationTestSuite) TestDenomMetadataGRPCHandler() {
&types.QueryDenomMetadataResponse{},
&types.QueryDenomMetadataResponse{
Metadata: types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Expand Down
2 changes: 2 additions & 0 deletions x/bank/client/rest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (s *IntegrationTestSuite) SetupSuite() {

bankGenesis.DenomMetadata = []types.Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{
Expand Down
4 changes: 4 additions & 0 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,8 @@ func (suite *IntegrationTestSuite) TestIterateAllDenomMetaData() {

func (suite *IntegrationTestSuite) getTestMetadata() []types.Metadata {
return []types.Metadata{{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand All @@ -1047,6 +1049,8 @@ func (suite *IntegrationTestSuite) getTestMetadata() []types.Metadata {
Display: "atom",
},
{
Name: "Token",
Symbol: "TOKEN",
Description: "The native staking token of the Token Hub.",
DenomUnits: []*types.DenomUnit{
{"1token", uint32(5), []string{"decitoken"}},
Expand Down
8 changes: 8 additions & 0 deletions x/bank/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestGenesisStateValidate(t *testing.T) {
Supply: sdk.Coins{sdk.NewInt64Coin("uatom", 1)},
DenomMetadata: []Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand Down Expand Up @@ -85,6 +87,8 @@ func TestGenesisStateValidate(t *testing.T) {
GenesisState{
DenomMetadata: []Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand All @@ -95,6 +99,8 @@ func TestGenesisStateValidate(t *testing.T) {
Display: "atom",
},
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand All @@ -113,6 +119,8 @@ func TestGenesisStateValidate(t *testing.T) {
GenesisState{
DenomMetadata: []Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand Down
9 changes: 9 additions & 0 deletions x/bank/types/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@ import (
)

// Validate performs a basic validation of the coin metadata fields. It checks:
// - Name and Symbol are not blank
// - Base and Display denominations are valid coin denominations
// - Base and Display denominations are present in the DenomUnit slice
// - Base denomination has exponent 0
// - Denomination units are sorted in ascending order
// - Denomination units not duplicated
func (m Metadata) Validate() error {
if strings.TrimSpace(m.Name) == "" {
return errors.New("name field cannot be blank")
}

if strings.TrimSpace(m.Symbol) == "" {
return errors.New("symbol field cannot be blank")
}

if err := sdk.ValidateDenom(m.Base); err != nil {
return fmt.Errorf("invalid metadata base denom: %w", err)
}
Expand Down
56 changes: 55 additions & 1 deletion x/bank/types/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestMetadataValidate(t *testing.T) {
{
"non-empty coins",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand All @@ -32,6 +34,8 @@ func TestMetadataValidate(t *testing.T) {
{
"base coin is display coin",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"atom", uint32(0), []string{"ATOM"}},
Expand All @@ -42,16 +46,35 @@ func TestMetadataValidate(t *testing.T) {
false,
},
{"empty metadata", types.Metadata{}, true},
{
"blank name",
types.Metadata{
Name: "",
},
true,
},
{
"blank symbol",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "",
},
true,
},
{
"invalid base denom",
types.Metadata{
Base: "",
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Base: "",
},
true,
},
{
"invalid display denom",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Base: "uatom",
Display: "",
},
Expand All @@ -60,6 +83,8 @@ func TestMetadataValidate(t *testing.T) {
{
"duplicate denom unit",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand All @@ -73,6 +98,8 @@ func TestMetadataValidate(t *testing.T) {
{
"invalid denom unit",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"", uint32(0), []string{"microatom"}},
Expand All @@ -85,6 +112,8 @@ func TestMetadataValidate(t *testing.T) {
{
"invalid denom unit alias",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{""}},
Expand All @@ -97,6 +126,8 @@ func TestMetadataValidate(t *testing.T) {
{
"duplicate denom unit alias",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom", "microatom"}},
Expand All @@ -109,6 +140,8 @@ func TestMetadataValidate(t *testing.T) {
{
"no base denom unit",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"matom", uint32(3), []string{"milliatom"}},
Expand All @@ -122,6 +155,8 @@ func TestMetadataValidate(t *testing.T) {
{
"base denom exponent not zero",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(1), []string{"microatom"}},
Expand All @@ -133,9 +168,26 @@ func TestMetadataValidate(t *testing.T) {
},
true,
},
{
"invalid denom unit",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
{"", uint32(3), []string{"milliatom"}},
},
Base: "uatom",
Display: "uatom",
},
true,
},
{
"no display denom unit",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand All @@ -148,6 +200,8 @@ func TestMetadataValidate(t *testing.T) {
{
"denom units not sorted",
types.Metadata{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
Expand Down

0 comments on commit d65f6ff

Please sign in to comment.