-
Notifications
You must be signed in to change notification settings - Fork 13
Add functions to get networks by ChainID and name #320
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
base: master
Are you sure you want to change the base?
Conversation
|
As i am looking at it, maybe it would be better, if the https://github.com/0xsequence/go-sequence/blob/master/lib/networks/networks_list.gen.go would generate variable right in type Networks map[string]*NetworkAnd then define the methods for func (n Networks) GetByChainID(chainID uint64) (*Network, bool) {
for _, network := range n {
if network.ChainID == chainID {
return network, true
}
}
return nil, false
}
func (n Networks) GetByName(name string) (*Network, bool) {
network, ok := n[name]
if !ok {
return nil, false
}
return network, true
}The networks package is mainly used for defining the structs for config. IMHO the initialization to networks map would be best and cleanest solution. And define the list of static networks like this. var StaticNetworks = Networks{
"mainnet": &MAINNET,
"ropsten": &ROPSTEN,
"rinkeby": &RINKEBY,
"goerli": &GOERLI,
"optimism": &OPTIMISM,
"telos": &TELOS,
"telos-testnet": &TELOS_TESTNET,
"kovan": &KOVAN,
"bsc": &BSC,
"optimism-kovan": &OPTIMISM_KOVAN,
"bsc-testnet": &BSC_TESTNET,
"gnosis": &GNOSIS,
"polygon": &POLYGON,
"monad": &MONAD,
"optimism-goerli": &OPTIMISM_GOERLI,
"polygon-zkevm": &POLYGON_ZKEVM,
"moonbeam": &MOONBEAM,
"moonbase-alpha": &MOONBASE_ALPHA,
"sei-testnet": &SEI_TESTNET,
"sei": &SEI,
"soneium": &SONEIUM,
"soneium-minato": &SONEIUM_MINATO,
"b3-sepolia": &B3_SEPOLIA,
"somnia": &SOMNIA,
"sandbox-testnet": &SANDBOX_TESTNET,
"b3": &B3,
"base": &BASE,
"monad-testnet": &MONAD_TESTNET,
"incentiv-testnet": &INCENTIV_TESTNET,
"immutable-zkevm": &IMMUTABLE_ZKEVM,
"immutable-zkevm-testnet": &IMMUTABLE_ZKEVM_TESTNET,
"homeverse": &HOMEVERSE,
"incentiv": &INCENTIV,
"incentiv-testnet-v2": &INCENTIV_TESTNET_V2,
"hardhat": &HARDHAT,
"hardhat2": &HARDHAT2,
"apechain-testnet": &APECHAIN_TESTNET,
"apechain": &APECHAIN,
"homeverse-testnet": &HOMEVERSE_TESTNET,
"arbitrum": &ARBITRUM,
"arbitrum-nova": &ARBITRUM_NOVA,
"etherlink": ÐERLINK,
"avalanche-testnet": &AVALANCHE_TESTNET,
"avalanche": &AVALANCHE,
"somnia-testnet": &SOMNIA_TESTNET,
"mumbai": &MUMBAI,
"amoy": &AMOY,
"blast": &BLAST,
"base-goerli": &BASE_GOERLI,
"base-sepolia": &BASE_SEPOLIA,
"borne-testnet": &BORNE_TESTNET,
"etherlink-shadownet-testnet": ÐERLINK_SHADOWNET_TESTNET,
"etherlink-testnet": ÐERLINK_TESTNET,
"arbitrum-goerli": &ARBITRUM_GOERLI,
"arbitrum-sepolia": &ARBITRUM_SEPOLIA,
"xai": &XAI,
"katana": &KATANA,
"arc-testnet": &ARC_TESTNET,
"sepolia": &SEPOLIA,
"optimism-sepolia": &OPTIMISM_SEPOLIA,
"toy-testnet": &TOY_TESTNET,
"skale-nebula-testnet": &SKALE_NEBULA_TESTNET,
"blast-sepolia": &BLAST_SEPOLIA,
"skale-nebula": &SKALE_NEBULA,
"xai-sepolia": &XAI_SEPOLIA,
}And then the |
lib/networks/networks.go
Outdated
| func GetByChainID(chainID uint64) (Network, bool) { | ||
| for _, network := range networksByChainID { | ||
| if network.ChainID == chainID { | ||
| return network, true | ||
| } | ||
| } | ||
| return Network{}, false | ||
| } | ||
|
|
||
| func GetByName(name string) (Network, bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we return *Network pointer? So we don't copy the data?
Refactor GetByChainID and GetByName methods to be methods of Networks type, returning pointers to Network.
| return nil | ||
| } | ||
|
|
||
| func (n Networks) GetByName(name string) *Network { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please return boolean too
| for _, network := range n { | ||
| if network.ChainID == chainID { | ||
| return network | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't Networks a map? I think we can look up items directly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
item, ok := n[chainID]
return item, ok
No description provided.