From d196e1afe8802f51696222ccf616371556d181c1 Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Tue, 24 Oct 2023 18:25:37 -0400 Subject: [PATCH] Allow chains to be added to a router async (#2) * Add Chain feature * fix linter and check value --- router/router.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/router/router.go b/router/router.go index daecaba..3d8707a 100644 --- a/router/router.go +++ b/router/router.go @@ -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 @@ -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 @@ -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 +}