Skip to content

Commit

Permalink
Normalize methods, chainIDs (vs chainName) (#3)
Browse files Browse the repository at this point in the history
* normalize to chainids

* add bech32 prefixes
  • Loading branch information
keefertaylor authored Oct 30, 2023
1 parent d196e1a commit 13ff006
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
22 changes: 13 additions & 9 deletions router/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package router

// Chain defines an abstraction around a Chain that Validator tooling or other blockchain applications may use.
type Chain interface {
GetHumanReadableName() string
GetChainName() string
HumanReadableName() string
ChainID() string
Bech32Prefix() string

GetGrpcEndpoint() (string, error)
GrpcEndpoint() (string, error)
}

// private implementation
type chain struct {
chainName string
chainID string
humanReadableName string
bech32Prefix string

grpcEndpoint *string
}
Expand All @@ -20,21 +22,23 @@ type chain struct {
var _ Chain = (*chain)(nil)

// Create a new Chain
func NewChain(chainName, humanReadableName string, grpcEndpoint *string) (Chain, error) {
func NewChain(chainID, humanReadableName, bech32Prefix string, grpcEndpoint *string) (Chain, error) {
return &chain{
chainName: chainName,
chainID: chainID,
humanReadableName: humanReadableName,
bech32Prefix: bech32Prefix,

grpcEndpoint: grpcEndpoint,
}, nil
}

// Chain Interface

func (c *chain) GetHumanReadableName() string { return c.chainName }
func (c *chain) GetChainName() string { return c.humanReadableName }
func (c *chain) HumanReadableName() string { return c.humanReadableName }
func (c *chain) ChainID() string { return c.chainID }
func (c *chain) Bech32Prefix() string { return c.bech32Prefix }

func (c *chain) GetGrpcEndpoint() (string, error) {
func (c *chain) GrpcEndpoint() (string, error) {
if c.grpcEndpoint == nil {
return "", ErrNoEndpointValueProvided
} else {
Expand Down
2 changes: 1 addition & 1 deletion router/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "errors"

var (
ErrNoEndpointValueProvided = errors.New("no value provided for requested endpoint")
ErrNoChainWithName = errors.New("no known chain with provided name")
ErrNoChainWithID = errors.New("no known chain with provided ID")
)
31 changes: 20 additions & 11 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "fmt"

// Router defines a way to get addresses and API endpoints for blockchain nodes
type Router interface {
GetHumanReadableName(chainName string) (string, error)
GetGrpcEndpoint(chainName string) (string, error)
HumanReadableName(chainName string) (string, error)
GrpcEndpoint(chainName string) (string, error)

AddChain(chain Chain) error
}
Expand Down Expand Up @@ -38,26 +38,35 @@ func NewRouter(chains []Chain) (Router, error) {

// Router Interface

func (r *router) GetHumanReadableName(chainName string) (string, error) {
chain := r.chains[chainName]
func (r *router) HumanReadableName(chainID string) (string, error) {
chain := r.chains[chainID]
if chain == nil {
return "", ErrNoChainWithName
return "", ErrNoChainWithID
}

return chain.GetHumanReadableName(), nil
return chain.HumanReadableName(), nil
}

func (r *router) GetGrpcEndpoint(chainName string) (string, error) {
chain := r.chains[chainName]
func (r *router) Bech32Prefix(chainID string) (string, error) {
chain := r.chains[chainID]
if chain == nil {
return "", ErrNoChainWithName
return "", ErrNoChainWithID
}

return chain.GetGrpcEndpoint()
return chain.Bech32Prefix(), nil
}

func (r *router) GrpcEndpoint(chainID string) (string, error) {
chain := r.chains[chainID]
if chain == nil {
return "", ErrNoChainWithID
}

return chain.GrpcEndpoint()
}

func (r *router) AddChain(chain Chain) error {
chainName := chain.GetChainName()
chainName := chain.ChainID()

_, isSet := r.chains[chainName]
if isSet {
Expand Down

0 comments on commit 13ff006

Please sign in to comment.