Skip to content

Commit

Permalink
web,cmd: Generalize chains API
Browse files Browse the repository at this point in the history
Make it easier to add new LOOPs by reducing the amount of boilerplate
  • Loading branch information
archseer committed Dec 12, 2024
1 parent 60f5569 commit 0a91bed
Show file tree
Hide file tree
Showing 27 changed files with 723 additions and 1,119 deletions.
12 changes: 3 additions & 9 deletions core/cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,9 @@ func NewApp(s *Shell) *cli.App {
},
},
{
Name: "chains",
Usage: "Commands for handling chain configuration",
Subcommands: cli.Commands{
chainCommand("EVM", EVMChainClient(s), cli.Int64Flag{Name: "id", Usage: "chain ID"}),
chainCommand("Cosmos", CosmosChainClient(s), cli.StringFlag{Name: "id", Usage: "chain ID"}),
chainCommand("Solana", SolanaChainClient(s),
cli.StringFlag{Name: "id", Usage: "chain ID, options: [mainnet, testnet, devnet, localnet]"}),
chainCommand("StarkNet", StarkNetChainClient(s), cli.StringFlag{Name: "id", Usage: "chain ID"}),
},
Name: "chains",
Usage: "Commands for handling chain configuration",
Subcommands: initChainSubCmds(s),
},
{
Name: "nodes",
Expand Down
60 changes: 56 additions & 4 deletions core/cmd/chains_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package cmd

import (
"fmt"
"strconv"
"strings"

"github.com/smartcontractkit/chainlink/v2/core/services/relay"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -39,12 +42,12 @@ type chainClient[P TableRenderer] struct {
path string
}

// newChainClient returns a new ChainClient for a particular type of chains.Config.
// NewChainClient returns a new ChainClient for a particular type of chains.Config.
// P is a TableRenderer corresponding to R, and P2 is the slice variant (type P2 []P).
func newChainClient[P TableRenderer](s *Shell, name string) ChainClient {
return &chainClient[P]{
func NewChainClient(s *Shell, network string) ChainClient {
return &chainClient[ChainPresenter]{
Shell: s,
path: "/v2/chains/" + name,
path: "/v2/chains/" + network,
}
}

Expand All @@ -53,3 +56,52 @@ func (cli *chainClient[P]) IndexChains(c *cli.Context) (err error) {
var p P
return cli.getPage(cli.path, c.Int("page"), &p)
}

// ChainPresenter implements TableRenderer for a ChainResource
type ChainPresenter struct {
presenters.ChainResource
}

// ToRow presents the ChainResource as a slice of strings.
func (p *ChainPresenter) ToRow() []string {
return []string{p.GetID(), strconv.FormatBool(p.Enabled), p.Config}
}

// RenderTable implements TableRenderer
// Just renders a single row
func (p ChainPresenter) RenderTable(rt RendererTable) error {
rows := [][]string{}
rows = append(rows, p.ToRow())

renderList(chainHeaders, rows, rt.Writer)

return nil
}

// ChainPresenters implements TableRenderer for a slice of ChainPresenters.
type ChainPresenters []ChainPresenter

// RenderTable implements TableRenderer
func (ps ChainPresenters) RenderTable(rt RendererTable) error {
rows := [][]string{}

for _, p := range ps {
rows = append(rows, p.ToRow())
}

renderList(chainHeaders, rows, rt.Writer)

return nil
}

func initChainSubCmds(s *Shell) []cli.Command {
var cmds []cli.Command
for network := range relay.SupportedNetworks {
if network == relay.NetworkDummy {
continue
}
cmds = append(cmds, chainCommand(network, NewChainClient(s, network), cli.StringFlag{Name: "id", Usage: "chain ID"}))
}

return cmds
}
48 changes: 0 additions & 48 deletions core/cmd/cosmos_chains_commands.go

This file was deleted.

33 changes: 0 additions & 33 deletions core/cmd/cosmos_chains_commands_test.go

This file was deleted.

48 changes: 0 additions & 48 deletions core/cmd/evm_chains_commands.go

This file was deleted.

38 changes: 0 additions & 38 deletions core/cmd/evm_chains_commands_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions core/cmd/nodes_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ type nodeClient[P TableRenderer] struct {

// newNodeClient returns a new NodeClient for a particular type of NodeStatus.
// P is a TableRenderer for []types.NodeStatus.
func newNodeClient[P TableRenderer](s *Shell, name string) NodeClient {
func newNodeClient[P TableRenderer](s *Shell, network string) NodeClient {
return &nodeClient[P]{
Shell: s,
path: "/v2/nodes/" + name,
path: "/v2/nodes/" + network,
}
}

Expand Down
48 changes: 0 additions & 48 deletions core/cmd/solana_chains_commands.go

This file was deleted.

32 changes: 0 additions & 32 deletions core/cmd/solana_chains_commands_test.go

This file was deleted.

Loading

0 comments on commit 0a91bed

Please sign in to comment.