Skip to content

Conversation

@klaidliadon
Copy link
Contributor

No description provided.

@david-littlefarmer
Copy link
Contributor

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 Networks map.

type Networks map[string]*Network

And then define the methods for Networks

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.
Introducing the helper functions which is used for consts is starting to be confusing.

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":                   &ETHERLINK,
	"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": &ETHERLINK_SHADOWNET_TESTNET,
	"etherlink-testnet":           &ETHERLINK_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 networksByChainID and networksByName can be removed, because it's not needed at all.

Comment on lines 157 to 166
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) {
Copy link
Contributor

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 {
Copy link
Contributor

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
}
Copy link
Contributor

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

Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants