From da5d521ee9a983c0b50c731f15a3da711faf528c Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Tue, 24 Oct 2023 17:37:44 -0400 Subject: [PATCH 1/2] Add Chain feature --- router/router.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/router/router.go b/router/router.go index daecaba..3440e86 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,15 @@ 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() - - _, isSet := chainMap[chainName] - if isSet { - return nil, fmt.Errorf("duplicate chain name: %s", chainName) - } + router := &router{ + chains: chainMap, + } - chainMap[chainName] = chain + for _, chain := range chains { + router.AddChain(chain) } - return &router{ - chains: chainMap, - }, nil + return router, nil } // Router Interface @@ -56,3 +52,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 +} From b4db05c47dbc90e1cb98c32799122e1dcedd0c36 Mon Sep 17 00:00:00 2001 From: Keefer Taylor | Tessellated Date: Tue, 24 Oct 2023 18:24:01 -0400 Subject: [PATCH 2/2] fix linter and check value --- router/router.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/router/router.go b/router/router.go index 3440e86..3d8707a 100644 --- a/router/router.go +++ b/router/router.go @@ -27,7 +27,10 @@ func NewRouter(chains []Chain) (Router, error) { } for _, chain := range chains { - router.AddChain(chain) + err := router.AddChain(chain) + if err != nil { + return nil, err + } } return router, nil