Web3.py: Consider adding Chain Id to library

Created on 16 Feb 2018  路  8Comments  路  Source: ethereum/web3.py

  • Version: 4.0.0-b
  • Python: 3.6.3
  • OS: linux

What was wrong?

No clear way to access known chain ids.

How can it be fixed?

Proposed syntax

>>> from web3 import Chains
>>> Chains.Ropsten.id
3

I ran into issues here: https://web3py.readthedocs.io/en/latest/web3.eth.account.html#sign-a-contract-transaction as buildTransaction() requires a chainId. I didn't even know the chains had ids.

>>> unicorn_txn = unicorns.functions.transfer(
...     '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
...     1,
... ).buildTransaction({
...     'chainId': 1,
...     'gas': 70000,
...     'gasPrice': w3.toWei('1', 'gwei'),
...     'nonce': nonce,
... })

Maybe this will help others

According to this answer https://ethereum.stackexchange.com/a/17101/7187 the chain ids are as follows:

0: Olympic, Ethereum public pre-release testnet
1: Frontier, Homestead, Metropolis, the Ethereum public main network
1: Classic, the (un)forked public Ethereum Classic main network, chain ID 61
1: Expanse, an alternative Ethereum implementation, chain ID 2
2: Morden, the public Ethereum testnet, now Ethereum Classic testnet
3: Ropsten, the public cross-client Ethereum testnet
4: Rinkeby, the public Geth PoA testnet
42: Kovan, the public Parity PoA testnet
77: Sokol, the public POA Network testnet
99: Core, the public POA Network main network
7762959: Musicoin, the music blockchain

Most helpful comment

I like w3.net.chainId.

All 8 comments

Yeah, looks like this needs documentation (and maybe renaming, because it's not intuitive to me):
w3.version.network will give you the chain ID.

Just renaming network in web3.version to chainId will be enough? Would it make sense to return name based on the chain ID aswell?

The unintuitive part to me is the module name version. Unfortunately chain id and network id seem to be used interchangeably in a lot of places, so someone will be confused no matter which we pick. Maybe we should even just have both names.

can we rename version to something like info and have chainId return the chain id and it can have an alias of networkId for the reason you mentioned above?

The thing most consistent with https://github.com/ethereum/wiki/wiki/JSON-RPC would be to call it net.

The terms network and chain could be combined: networkchain or networkchainid

If net is used does that imply that chainId inside of the transaction dict will change to net for consistency?

transaction = {
        'chainId': 1     # chainId --> 'net'
        'to': address,
        'from': w3.eth.coinbase,
        'data': '',
        'value': 1000,
        'gas': gas_limit_hex,
        'gasPrice': gas_price_hex,
        'nonce': nonce_hex,        
    }

Ah, to clarify: because net is the prefix of the json-rpc call net_version, the most consistent with that spec would be: w3.net.version (and if we wanted to alias it, something like: w3.net.id or w3.net.chainId)

I like w3.net.chainId.

Was this page helpful?
0 / 5 - 0 ratings