Skip to content

Commit

Permalink
wire api
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Jan 17, 2025
1 parent af3f633 commit 0b40d68
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 38 deletions.
76 changes: 76 additions & 0 deletions connect/chains/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package chains

import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"github.com/ignite/cli/v28/ignite/pkg/chainregistry"
)

const (
repoURL = "https://github.com/cosmos/chain-registry"
cosmosDirectoryAPIURL = "https://chains.cosmos.directory"
)

type ChainRegistry struct {
Chains map[string]chainregistry.Chain
Assets map[string]chainregistry.Asset
}

func NewChainRegistry() *ChainRegistry {
return &ChainRegistry{
Chains: make(map[string]chainregistry.Chain),
Assets: make(map[string]chainregistry.Asset),
}
}

func (r *ChainRegistry) FetchChains() error {
client := &http.Client{
Timeout: 5 * time.Second,
}

req, err := http.NewRequest(http.MethodGet, cosmosDirectoryAPIURL, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to fetch chains: %w", err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %w", err)
}

var cdOutput map[string]json.RawMessage
if err := json.Unmarshal(body, &cdOutput); err != nil {
return fmt.Errorf("failed to unmarshal cosmos.directory API response: %w", err)
}

rawChains, ok := cdOutput["chains"]
if !ok {
return fmt.Errorf("failed to get chains from response: cosmos.directory API may have changed")
}

var chains []chainregistry.Chain
if err := json.Unmarshal(rawChains, &chains); err != nil {
return fmt.Errorf("failed to unmarshal chains: %w", err)
}

for _, c := range chains {
r.Chains[c.ChainName] = c
}

fmt.Printf("Fetched %d chains from %s\n", len(r.Chains), repoURL)
for _, c := range r.Chains {
fmt.Printf("- %s\n", c.ChainName)
}

return nil
}
15 changes: 10 additions & 5 deletions connect/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ import (
func GetCommands() []*plugin.Command {
return []*plugin.Command{
{
Use: "connect [command]",
Aliases: []string{"c"},
Short: "Interact with any Cosmos SDK based blockchain using Ignite Connect",
Long: "Ignite Connect is an app that allows you to interact with any Cosmos SDK based blockchain.\n It leverages AutoCLI from client/v2",
Commands: []*plugin.Command{},
Use: "connect [command]",
Aliases: []string{"c"},
Short: "Interact with any Cosmos SDK based blockchain using Ignite Connect",
Long: "Ignite Connect is an app that allows you to interact with any Cosmos SDK based blockchain.\n It leverages AutoCLI from client/v2",
Commands: []*plugin.Command{
{
Use: "discover",
Short: "Discover chains to connect to",
},
},
},
}
}
26 changes: 26 additions & 0 deletions connect/cmd/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"context"

"github.com/ignite/cli/v28/ignite/pkg/cliui"
"github.com/ignite/cli/v28/ignite/services/plugin"

"github.com/ignite/apps/connect/chains"
)

const (
discoveringChains = "Discovering chains..."
)

func DiscoverHandler(ctx context.Context, cmd *plugin.ExecutedCommand) error {
session := cliui.New(cliui.StartSpinnerWithText(discoveringChains))
defer session.End()

chainRegistry := chains.NewChainRegistry()
if err := chainRegistry.FetchChains(); err != nil {
return err
}

return nil
}
28 changes: 17 additions & 11 deletions connect/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ go 1.23.0

require (
github.com/hashicorp/go-plugin v1.6.1
github.com/ignite/cli/v28 v28.7.0
github.com/ignite/cli/v28 v28.7.1-0.20250116085640-fcf01a4e4b39
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/AlecAivazis/survey/v2 v2.3.7 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/briandowns/spinner v1.23.0 // indirect
github.com/charmbracelet/lipgloss v0.6.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
Expand All @@ -23,7 +26,7 @@ require (
github.com/cosmos/cosmos-sdk v0.50.11 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
Expand Down Expand Up @@ -51,13 +54,16 @@ require (
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/microcosm-cc/bluemonday v1.0.23 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -76,16 +82,16 @@ require (
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.etcd.io/bbolt v1.3.10 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.29.0 // indirect
go.etcd.io/bbolt v1.4.0-alpha.0.0.20240404170359-43604f3112c5 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.22.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.24.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f // indirect
google.golang.org/grpc v1.67.1 // indirect
Expand Down
Loading

0 comments on commit 0b40d68

Please sign in to comment.