-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13ff006
commit 4613a4e
Showing
11 changed files
with
198 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package base | ||
|
||
import "github.com/tessellated-io/router/types" | ||
|
||
// baseRouter encapsulates basic functionality for all routers. You probably shouldn't use this directly. | ||
type BaseRouter struct { | ||
// Map of chain-id to chain. | ||
chains map[string]types.Chain | ||
} | ||
|
||
// NewRouter returns a new BaseRouter | ||
|
||
func NewRouter() (BaseRouter, error) { | ||
chainMap := make(map[string]types.Chain) | ||
return BaseRouter{ | ||
chains: chainMap, | ||
}, nil | ||
} | ||
|
||
// Router Interace | ||
|
||
func (br *BaseRouter) GrpcEndpoint(chainID string) (string, error) { | ||
chain := br.chains[chainID] | ||
if chain == nil { | ||
return "", types.ErrNoChainWithID | ||
} | ||
|
||
return chain.GrpcEndpoint() | ||
} | ||
|
||
// Helper methods | ||
|
||
func (br *BaseRouter) AddChains(chains []types.Chain) { | ||
for _, chain := range chains { | ||
br.chains[chain.ChainID()] = chain | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package file | ||
|
||
import ( | ||
"os" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Config struct { | ||
Networks []NetworkConfig `yaml:"networks"` | ||
} | ||
|
||
type NetworkConfig struct { | ||
ChainID string `yaml:"chain-id"` | ||
GrpcEndpoint string `yaml:"grpc"` | ||
} | ||
|
||
// Parse a file into a UserConfig | ||
func parseConfig(filename string) (*Config, error) { | ||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config := &Config{} | ||
err = yaml.Unmarshal(data, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package file | ||
|
||
import ( | ||
"github.com/tessellated-io/pickaxe/arrays" | ||
"github.com/tessellated-io/router/base" | ||
"github.com/tessellated-io/router/types" | ||
) | ||
|
||
// fileRouter is a router that maps to networks in a config file. | ||
type fileRouter struct { | ||
base.BaseRouter | ||
|
||
configFile string | ||
} | ||
|
||
// Type assertion | ||
var _ types.Router = (*fileRouter)(nil) | ||
|
||
// NewRouter creates a new file router. | ||
func NewRouter(configFile string) (types.Router, error) { | ||
fileRouter := &fileRouter{ | ||
configFile: configFile, | ||
} | ||
|
||
err := fileRouter.loadConfigFile() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return fileRouter, nil | ||
} | ||
|
||
// Router interface | ||
|
||
func (fr *fileRouter) Refresh() error { | ||
return fr.loadConfigFile() | ||
} | ||
|
||
// Private methods | ||
|
||
func (fr *fileRouter) loadConfigFile() error { | ||
parsed, err := parseConfig(fr.configFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
chains := arrays.Map(parsed.Networks, func(networkConfig NetworkConfig) types.Chain { | ||
return types.NewChain(networkConfig.ChainID, &networkConfig.GrpcEndpoint) | ||
}) | ||
|
||
fr.BaseRouter.AddChains(chains) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module github.com/tessellated-io/router | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/tessellated-io/pickaxe v1.0.11 | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/tessellated-io/pickaxe v1.0.11 h1:gaQVdYh+DMSE1tXA44JCunwmQnXeplblp2HQFowiNaE= | ||
github.com/tessellated-io/pickaxe v1.0.11/go.mod h1:wO7zNwLt2PbwH5C9j+p/KM+4I6K4zXY7rrq1567CPJE= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package static | ||
|
||
import ( | ||
"github.com/tessellated-io/router/base" | ||
"github.com/tessellated-io/router/types" | ||
) | ||
|
||
// staticRouter uses a preconfigured set of routes. | ||
type staticRouter struct { | ||
base.BaseRouter | ||
} | ||
|
||
// Type assertion | ||
var _ types.Router = (*staticRouter)(nil) | ||
|
||
// NewRouter makes a new router with the given chains. | ||
func NewRouter(chains []types.Chain) (types.Router, error) { | ||
baseRouter, err := base.NewRouter() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
baseRouter.AddChains(chains) | ||
|
||
return &staticRouter{ | ||
BaseRouter: baseRouter, | ||
}, nil | ||
} | ||
|
||
// Router Interface | ||
|
||
func (sr *staticRouter) Refresh() error { | ||
// Intentional no-op | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package router | ||
package types | ||
|
||
import "errors" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package types | ||
|
||
// Router defines a set of rules that allow you to route to a set of blockchain nodes. | ||
type Router interface { | ||
Refresh() error | ||
|
||
GrpcEndpoint(chainName string) (string, error) | ||
} |