Skip to content

Commit

Permalink
feat: add ping.pub explorer support (#87)
Browse files Browse the repository at this point in the history
* feat: add ping.pub explorer support

* chore: fixed links
  • Loading branch information
freak12techno committed May 12, 2024
1 parent 3338726 commit 696c541
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 44 deletions.
8 changes: 1 addition & 7 deletions pkg/load_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"fmt"
"main/pkg/fs"
"main/pkg/types"

Expand All @@ -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
Expand Down
29 changes: 16 additions & 13 deletions pkg/types/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
}
54 changes: 30 additions & 24 deletions pkg/types/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
13 changes: 13 additions & 0 deletions pkg/types/chains.go
Original file line number Diff line number Diff line change
@@ -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
}
31 changes: 31 additions & 0 deletions pkg/types/chains_test.go
Original file line number Diff line number Diff line change
@@ -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!")
}
6 changes: 6 additions & 0 deletions pkg/types/explorer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

type Explorer struct {
ProposalLinkPattern string `toml:"proposal-link-pattern"`
WalletLinkPattern string `toml:"wallet-link-pattern"`
}

0 comments on commit 696c541

Please sign in to comment.