Skip to content

Commit

Permalink
Allow chains to be added to a router async (#2)
Browse files Browse the repository at this point in the history
* Add Chain feature

* fix linter and check value
  • Loading branch information
keefertaylor authored Oct 24, 2023
1 parent 202129e commit d196e1a
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ 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)

AddChain(chain Chain) error
}

// Private implementing type
Expand All @@ -21,20 +22,18 @@ var _ Router = (*router)(nil)
// NewRouter makes a new router with the given chains.
func NewRouter(chains []Chain) (Router, error) {
chainMap := make(map[string]Chain)
for _, chain := range chains {
chainName := chain.GetChainName()
router := &router{
chains: chainMap,
}

_, isSet := chainMap[chainName]
if isSet {
return nil, fmt.Errorf("duplicate chain name: %s", chainName)
for _, chain := range chains {
err := router.AddChain(chain)
if err != nil {
return nil, err
}

chainMap[chainName] = chain
}

return &router{
chains: chainMap,
}, nil
return router, nil
}

// Router Interface
Expand All @@ -56,3 +55,16 @@ func (r *router) GetGrpcEndpoint(chainName string) (string, error) {

return chain.GetGrpcEndpoint()
}

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

_, isSet := r.chains[chainName]
if isSet {
return fmt.Errorf("duplicate chain name: %s", chainName)
}

r.chains[chainName] = chain

return nil
}

0 comments on commit d196e1a

Please sign in to comment.