Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow chains to be added to a router async #2

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}