ethereum / eth-utils

Utility functions for working with ethereum related codebases.
https://eth-utils.readthedocs.io/en/latest/
MIT License
312 stars 148 forks source link

Network name utility #253

Closed reedsa closed 10 months ago

reedsa commented 11 months ago

What feature should we add?

It would be convenient to retrieve the network name by using w3.network, which could just call the utility with w3.eth.chain_id.

A name_from_chain_id function is needed that will return the full name of the network. (example: "Ethereum Mainnet") A short_name_from_chain_id function would return the short name. (example: "eth")

List of networks in json: https://chainid.network/chains_mini.json

A script was written to parse the list json and create a list of enums: https://gist.github.com/reedsa/addafe9eb852db3267c0e90f1ce9867e An adaptation of the script could make it easy to update the list chain ids and names in our code.

DavidRomanovizc commented 11 months ago

Hello @reedsa! I'd like to try to contribute and solve this issue. Could you please provide me with further instructions and assign this task to me?

reedsa commented 11 months ago

@DavidRomanovizc sounds great! The network utility can be added to a new python file network.py under eth_utils.

There should be a new class for the Network that will store the chainId, symbol, name, shortName from the chain list.


network_names = {
    1: "Ethereum Mainnet",
    ...
}

network_short_names = {
    1: "eth",
}

class Network:
    chain_id: int
    name: str
    shortName: str

The method for this class could look like:

def network_from_chain_id(chain_id: int) -> Network:
    try:
        return network_names_by_id[chain_id]
    except KeyError:
        raise ValidationError(f"chain_id not recognized: {chain_id}")

The same pattern applies for the short names. Parameterized tests should be added as well to make sure the new functions are returning what is expected.

Again, appreciate your help on this. If you have any questions feel free to reach out!