diff --git a/pkg/load_config.go b/pkg/load_config.go index 8b923c2..a34288f 100644 --- a/pkg/load_config.go +++ b/pkg/load_config.go @@ -1,7 +1,6 @@ package pkg import ( - "fmt" "main/pkg/fs" "main/pkg/types" @@ -25,12 +24,7 @@ func GetConfig(filesystem fs.FS, path string) (*types.Config, error) { defaults.MustSet(configStruct) for _, chain := range configStruct.Chains { - if chain.MintscanPrefix != "" { - chain.Explorer = &types.Explorer{ - ProposalLinkPattern: fmt.Sprintf("https://mintscan.io/%s/proposals/%%s", chain.MintscanPrefix), - WalletLinkPattern: fmt.Sprintf("https://mintscan.io/%s/account/%%s", chain.MintscanPrefix), - } - } + chain.Explorer = chain.GetExplorer() } return configStruct, nil diff --git a/pkg/types/chain.go b/pkg/types/chain.go index 2cfa322..c383751 100644 --- a/pkg/types/chain.go +++ b/pkg/types/chain.go @@ -5,11 +5,6 @@ import ( "main/pkg/utils" ) -type Explorer struct { - ProposalLinkPattern string `toml:"proposal-link-pattern"` - WalletLinkPattern string `toml:"wallet-link-pattern"` -} - type Wallet struct { Address string `toml:"address"` Alias string `toml:"alias"` @@ -28,9 +23,11 @@ type Chain struct { PrettyName string `toml:"pretty-name"` KeplrName string `toml:"keplr-name"` LCDEndpoints []string `toml:"lcd-endpoints"` - ProposalsType string `default:"v1beta1" toml:"proposals-type"` + ProposalsType string `default:"v1beta1" toml:"proposals-type"` Wallets []*Wallet `toml:"wallets"` MintscanPrefix string `toml:"mintscan-prefix"` + PingPrefix string `toml:"ping-prefix"` + PingHost string `default:"https://ping.pub" toml:"ping-host"` Explorer *Explorer `toml:"explorer"` Type string `default:"cosmos" toml:"type"` @@ -75,7 +72,7 @@ func (c Chain) GetName() string { return c.Name } -func (c Chain) GetExplorerProposalsLinks(proposalID string) []Link { +func (c *Chain) GetExplorerProposalsLinks(proposalID string) []Link { links := []Link{} if c.KeplrName != "" { @@ -119,14 +116,20 @@ func (c Chain) GetWalletLink(wallet *Wallet) Link { return link } -type Chains []*Chain +func (c *Chain) GetExplorer() *Explorer { + if c.MintscanPrefix != "" { + return &Explorer{ + ProposalLinkPattern: fmt.Sprintf("https://mintscan.io/%s/proposals/%%s", c.MintscanPrefix), + WalletLinkPattern: fmt.Sprintf("https://mintscan.io/%s/account/%%s", c.MintscanPrefix), + } + } -func (c Chains) FindByName(name string) *Chain { - for _, chain := range c { - if chain.Name == name { - return chain + if c.PingPrefix != "" { + return &Explorer{ + ProposalLinkPattern: fmt.Sprintf("%s/%s/gov/%%s", c.PingHost, c.PingPrefix), + WalletLinkPattern: fmt.Sprintf("%s/%s/account/%%s", c.PingHost, c.PingPrefix), } } - return nil + return c.Explorer } diff --git a/pkg/types/chain_test.go b/pkg/types/chain_test.go index 66e6e87..89e91b6 100644 --- a/pkg/types/chain_test.go +++ b/pkg/types/chain_test.go @@ -20,30 +20,6 @@ func TestWalletAddressOrAliasWithAlias(t *testing.T) { assert.Equal(t, "alias", wallet.AddressOrAlias(), "Wrong value!") } -func TestFindChainByNameIfPresent(t *testing.T) { - t.Parallel() - - chains := Chains{ - {Name: "chain1"}, - {Name: "chain2"}, - } - - chain := chains.FindByName("chain2") - assert.NotNil(t, chain, "Chain should be presented!") -} - -func TestFindChainByNameIfNotPresent(t *testing.T) { - t.Parallel() - - chains := Chains{ - {Name: "chain1"}, - {Name: "chain2"}, - } - - chain := chains.FindByName("chain3") - assert.Nil(t, chain, "Chain should not be presented!") -} - func TestGetLinksEmpty(t *testing.T) { t.Parallel() @@ -125,3 +101,33 @@ func TestGetWalletLinkWithAlias(t *testing.T) { assert.Equal(t, "alias", link.Name, "Wrong value!") assert.Equal(t, "example.com/wallet", link.Href, "Wrong value!") } + +func TestSetExplorerMintscan(t *testing.T) { + t.Parallel() + + chain := Chain{MintscanPrefix: "test"} + explorer := chain.GetExplorer() + + assert.NotNil(t, explorer) + assert.Equal(t, "https://mintscan.io/test/account/%s", explorer.WalletLinkPattern) + assert.Equal(t, "https://mintscan.io/test/proposals/%s", explorer.ProposalLinkPattern) +} + +func TestSetExplorerPing(t *testing.T) { + t.Parallel() + + chain := Chain{PingPrefix: "test", PingHost: "https://example.com"} + explorer := chain.GetExplorer() + + assert.NotNil(t, explorer) + assert.Equal(t, "https://example.com/test/account/%s", explorer.WalletLinkPattern) + assert.Equal(t, "https://example.com/test/gov/%s", explorer.ProposalLinkPattern) +} + +func TestSetExplorerEmpty(t *testing.T) { + t.Parallel() + + chain := Chain{} + explorer := chain.GetExplorer() + assert.Nil(t, explorer) +} diff --git a/pkg/types/chains.go b/pkg/types/chains.go new file mode 100644 index 0000000..a4896ad --- /dev/null +++ b/pkg/types/chains.go @@ -0,0 +1,13 @@ +package types + +type Chains []*Chain + +func (c Chains) FindByName(name string) *Chain { + for _, chain := range c { + if chain.Name == name { + return chain + } + } + + return nil +} diff --git a/pkg/types/chains_test.go b/pkg/types/chains_test.go new file mode 100644 index 0000000..a244b4c --- /dev/null +++ b/pkg/types/chains_test.go @@ -0,0 +1,31 @@ +package types + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFindChainByNameIfPresent(t *testing.T) { + t.Parallel() + + chains := Chains{ + {Name: "chain1"}, + {Name: "chain2"}, + } + + chain := chains.FindByName("chain2") + assert.NotNil(t, chain, "Chain should be presented!") +} + +func TestFindChainByNameIfNotPresent(t *testing.T) { + t.Parallel() + + chains := Chains{ + {Name: "chain1"}, + {Name: "chain2"}, + } + + chain := chains.FindByName("chain3") + assert.Nil(t, chain, "Chain should not be presented!") +} diff --git a/pkg/types/explorer.go b/pkg/types/explorer.go new file mode 100644 index 0000000..0e3f8e6 --- /dev/null +++ b/pkg/types/explorer.go @@ -0,0 +1,6 @@ +package types + +type Explorer struct { + ProposalLinkPattern string `toml:"proposal-link-pattern"` + WalletLinkPattern string `toml:"wallet-link-pattern"` +}