Skip to content
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

Chain enum and UnsupportedChainException (python) #20

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
26 changes: 16 additions & 10 deletions python/docs/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ source : [in3-c/python/examples/connect_to_ethereum.py](https://github.com/slock
Connects to Ethereum and fetches attested information from each chain.
"""
import in3
from in3.eth.enums import Chain


print('\nEthereum Main Network')
Expand All @@ -86,13 +87,13 @@ gas_price = client.eth.gas_price()
print('Latest BN: {}\nGas Price: {} Wei'.format(latest_block, gas_price))

print('\nEthereum Kovan Test Network')
client = in3.Client('kovan')
client = in3.Client(Chain.KOVAN)
latest_block = client.eth.block_number()
gas_price = client.eth.gas_price()
print('Latest BN: {}\nGas Price: {} Wei'.format(latest_block, gas_price))

print('\nEthereum Goerli Test Network')
client = in3.Client('goerli')
client = in3.Client(Chain.GOERLI)
latest_block = client.eth.block_number()
gas_price = client.eth.gas_price()
print('Latest BN: {}\nGas Price: {} Wei'.format(latest_block, gas_price))
Expand Down Expand Up @@ -125,9 +126,10 @@ source : [in3-c/python/examples/incubed_network.py](https://github.com/slockit/i
Shows Incubed Network Nodes Stats
"""
import in3
from in3.eth.enums import Chain

print('\nEthereum Goerli Test Network')
client = in3.Client('goerli')
client = in3.Client(Chain.GOERLI)
node_list = client.refresh_node_list()
print('\nIncubed Registry:')
print('\ttotal servers:', node_list.totalServers)
Expand Down Expand Up @@ -210,6 +212,7 @@ Resolves ENS domains to Ethereum addresses
ENS is a smart-contract system that registers and resolves `.eth` domains.
"""
import in3
from in3.eth.enums import Chain


def _print():
Expand All @@ -223,21 +226,21 @@ domain = 'depraz.eth'
print('\nEthereum Name Service')

# Instantiate In3 Client for Goerli
chain = 'goerli'
chain = Chain.GOERLI
client = in3.Client(chain, cache_enabled=False)
address = client.ens_address(domain)
# owner = client.ens_owner(domain)
# _print()

# Instantiate In3 Client for Mainnet
chain = 'mainnet'
chain = Chain.MAINNET
client = in3.Client(chain, cache_enabled=False)
address = client.ens_address(domain)
owner = client.ens_owner(domain)
_print()

# Instantiate In3 Client for Kovan
chain = 'kovan'
chain = Chain.KOVAN
client = in3.Client(chain, cache_enabled=True)
try:
address = client.ens_address(domain)
Expand Down Expand Up @@ -273,6 +276,7 @@ Works with included `data` field for smart-contract calls.
"""
import json
import in3
from in3.eth.enums import Chain
import time


Expand All @@ -285,8 +289,8 @@ receiver = input("Receiver address: ")
# 1000000000 == 1 Gwei Check https://etherscan.io/gasTracker.
value_in_wei = 1463926659
# None for Eth mainnet
chain = 'goerli'
client = in3.Client(chain if chain else 'mainnet')
chain = Chain.GOERLI
client = in3.Client(chain if chain else Chain.MAINNET)
# A transaction is only final if a certain number of blocks are mined on top of it.
# This number varies with the chain's consensus algorithm. Time can be calculated over using:
# wait_time = blocks_for_consensus * avg_block_time_in_secs
Expand Down Expand Up @@ -363,9 +367,10 @@ Manually calling ENS smart-contract
![UML Sequence Diagram of how Ethereum Name Service ENS resolves a name.](https://lh5.googleusercontent.com/_OPPzaxTxKggx9HuxloeWtK8ggEfIIBKRCEA6BKMwZdzAfUpIY6cz7NK5CFmiuw7TwknbhFNVRCJsswHLqkxUEJ5KdRzpeNbyg8_H9d2RZdG28kgipT64JyPZUP--bAizozaDcxCq34)
"""
import in3
from in3.eth.enums import Chain


client = in3.Client('goerli')
client = in3.Client(Chain.GOERLI)
domain_name = client.ens_namehash('depraz.eth')
ens_registry_addr = '0x00000000000c2e074ec69a0dfb2997ba6c7d2e1e'
ens_resolver_abi = 'resolver(bytes32):address'
Expand Down Expand Up @@ -418,13 +423,14 @@ import base64
import json

import in3
from in3.eth.enums import Chain
import hashlib
import random
import time

if __name__ == '__main__':

c = in3.Client(chain='ewc', in3_config=in3.ClientConfig(transport_binary_format=True))
c = in3.Client(chain=Chain.EWC, in3_config=in3.ClientConfig(transport_binary_format=True))

smart_meter_registry_addr = '0xf23FF7472FC62C6bEe2F960f5b4170Ab3C1C26d2'
# meter, bucket, operator, timestamp, data
Expand Down
5 changes: 3 additions & 2 deletions python/examples/connect_to_ethereum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Connects to Ethereum and fetches attested information from each chain.
"""
import in3
from in3.eth.enums import Chain


print('\nEthereum Main Network')
Expand All @@ -11,13 +12,13 @@
print('Latest BN: {}\nGas Price: {} Wei'.format(latest_block, gas_price))

print('\nEthereum Kovan Test Network')
client = in3.Client('kovan')
client = in3.Client(Chain.KOVAN)
latest_block = client.eth.block_number()
gas_price = client.eth.gas_price()
print('Latest BN: {}\nGas Price: {} Wei'.format(latest_block, gas_price))

print('\nEthereum Goerli Test Network')
client = in3.Client('goerli')
client = in3.Client(Chain.GOERLI)
latest_block = client.eth.block_number()
gas_price = client.eth.gas_price()
print('Latest BN: {}\nGas Price: {} Wei'.format(latest_block, gas_price))
Expand Down
3 changes: 2 additions & 1 deletion python/examples/incubed_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
Shows Incubed Network Nodes Stats
"""
import in3
from in3.eth.enums import Chain

print('\nEthereum Goerli Test Network')
client = in3.Client('goerli')
client = in3.Client(Chain.GOERLI)
node_list = client.refresh_node_list()
print('\nIncubed Registry:')
print('\ttotal servers:', node_list.totalServers)
Expand Down
7 changes: 4 additions & 3 deletions python/examples/resolve_eth_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
ENS is a smart-contract system that registers and resolves `.eth` domains.
"""
import in3
from in3.eth.enums import Chain


def _print():
Expand All @@ -16,21 +17,21 @@ def _print():
print('\nEthereum Name Service')

# Instantiate In3 Client for Goerli
chain = 'goerli'
chain = Chain.GOERLI
client = in3.Client(chain, cache_enabled=False)
address = client.ens_address(domain)
# owner = client.ens_owner(domain)
# _print()

# Instantiate In3 Client for Mainnet
chain = 'mainnet'
chain = Chain.MAINNET
client = in3.Client(chain, cache_enabled=False)
address = client.ens_address(domain)
owner = client.ens_owner(domain)
_print()

# Instantiate In3 Client for Kovan
chain = 'kovan'
chain = Chain.KOVAN
client = in3.Client(chain, cache_enabled=True)
try:
address = client.ens_address(domain)
Expand Down
5 changes: 3 additions & 2 deletions python/examples/send_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import json
import in3
from in3.eth.enums import Chain
import time


Expand All @@ -17,8 +18,8 @@
# 1000000000 == 1 Gwei Check https://etherscan.io/gasTracker.
value_in_wei = 1463926659
# None for Eth mainnet
chain = 'goerli'
client = in3.Client(chain if chain else 'mainnet')
chain = Chain.GOERLI
client = in3.Client(chain if chain else Chain.MAINNET)
# A transaction is only final if a certain number of blocks are mined on top of it.
# This number varies with the chain's consensus algorithm. Time can be calculated over using:
# wait_time = blocks_for_consensus * avg_block_time_in_secs
Expand Down
3 changes: 2 additions & 1 deletion python/examples/smart_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
![UML Sequence Diagram of how Ethereum Name Service ENS resolves a name.](https://lh5.googleusercontent.com/_OPPzaxTxKggx9HuxloeWtK8ggEfIIBKRCEA6BKMwZdzAfUpIY6cz7NK5CFmiuw7TwknbhFNVRCJsswHLqkxUEJ5KdRzpeNbyg8_H9d2RZdG28kgipT64JyPZUP--bAizozaDcxCq34)
"""
import in3
from in3.eth.enums import Chain


client = in3.Client('goerli')
client = in3.Client(Chain.GOERLI)
domain_name = client.ens_namehash('depraz.eth')
ens_registry_addr = '0x00000000000c2e074ec69a0dfb2997ba6c7d2e1e'
ens_resolver_abi = 'resolver(bytes32):address'
Expand Down
12 changes: 7 additions & 5 deletions python/in3/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from in3.eth.factory import EthObjectFactory
from in3.libin3.enum import In3Methods
from in3.libin3.runtime import In3Runtime
from in3.exception import EnsDomainFormatException
from in3.model import In3Node, NodeList, ClientConfig, chain_configs
from in3.exception import EnsDomainFormatException, ChainNotFoundException
from in3.model import In3Node, NodeList, ClientConfig, chain_configs, Chain
from in3.transport import https_transport


Expand All @@ -16,16 +16,18 @@ class Client:
Once with the latest list at hand, the client can request any other on-chain information using the same scheme.
Args:
chain (str): Ethereum chain to connect to. Defaults to mainnet. Options: 'mainnet', 'kovan', 'goerli', 'ewc'.
Constants available in in3.model.Chain.
in3_config (ClientConfig or str): (optional) Configuration for the client. If not provided, default is loaded.
cache_enabled (bool): False will disable local storage caching.
transport (function): Transport function for custom request routing. Defaults to https.
"""

def __init__(self, chain: str = 'mainnet', in3_config: ClientConfig = None, cache_enabled: bool = True,
def __init__(self, chain: str = Chain.MAINNET, in3_config: ClientConfig = None, cache_enabled: bool = True,
transport=https_transport):

if not isinstance(chain, str) or chain.lower() not in ['mainnet', 'kovan', 'goerli', 'ewc']:
raise AssertionError('Client: Chain name not supported. Try mainnet, kovan, goerli, ewc.')
supported_chains = [Chain.MAINNET, Chain.EWC, Chain.GOERLI, Chain.KOVAN]
if not chain or not isinstance(chain, str) or chain.lower() not in supported_chains:
raise ChainNotFoundException(chain, supported_chains)
# TODO: Clear Chain-configs
if in3_config and not isinstance(in3_config, ClientConfig):
raise AssertionError('Client: Use in3.ClientConfig to create a new client configuration instance.')
Expand Down
19 changes: 19 additions & 0 deletions python/in3/eth/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def _get_enum_options(cls):
return [
cls().__getattribute__(attr)
for attr in dir(cls)
if not callable(cls().__getattribute__(attr)) and not attr.startswith(u"_")
]


class Chain:
MAINNET = "mainnet"
KOVAN = "kovan"
GOERLI = "goerli"
EWC = "ewc"
IPFS = "ipfs"
EVAN = "evan"

@staticmethod
def options():
return _get_enum_options(Chain)
9 changes: 9 additions & 0 deletions python/in3/exception.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from in3.eth.enums import Chain


class IN3BaseException(Exception):
""" In3 Base Exception """
pass
Expand Down Expand Up @@ -36,3 +39,9 @@ class TransportException(IN3BaseException):
class EnsDomainFormatException(IN3BaseException):
def __init__(self):
super().__init__('Client: ENS domain name must end with .eth')


class ChainNotFoundException(IN3BaseException):
def __init__(self, chain, supported_chains=None):
supported_chains = supported_chains or Chain.options()
super().__init__("Client: '{}' is not a supported chain. Try one of {}.".format(chain, supported_chains))
25 changes: 13 additions & 12 deletions python/in3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings

from in3.eth.model import DataTransferObject, Account
from in3.eth.enums import Chain


class In3Node(DataTransferObject):
Expand Down Expand Up @@ -139,53 +140,53 @@ def __init__(self, chain_id: int, chain_id_alias: str, client_config: ClientConf


chain_configs = {
"mainnet": ChainConfig(
Chain.MAINNET: ChainConfig(
chain_id=int(0x1),
chain_id_alias="mainnet",
chain_id_alias=Chain.MAINNET,
client_config=ClientConfig(
chain_finality_threshold=10,
latest_block_stall=10,
node_signatures=2)
),
"kovan": ChainConfig(
Chain.KOVAN: ChainConfig(
chain_id=int(0x2a),
chain_id_alias="kovan",
chain_id_alias=Chain.KOVAN,
client_config=ClientConfig(
chain_finality_threshold=1,
latest_block_stall=6,
node_signatures=1,
node_signature_consensus=3)
),
"evan": ChainConfig(
Chain.EVAN: ChainConfig(
chain_id=int(0x4b1),
chain_id_alias="evan",
chain_id_alias=Chain.EVAN,
client_config=ClientConfig(
chain_finality_threshold=1,
latest_block_stall=6,
node_signatures=0,
node_signature_consensus=5)
),
"goerli": ChainConfig(
Chain.GOERLI: ChainConfig(
chain_id=int(0x5),
chain_id_alias="goerli",
chain_id_alias=Chain.GOERLI,
client_config=ClientConfig(
chain_finality_threshold=1,
latest_block_stall=6,
node_signatures=2)
),
"ipfs": ChainConfig(
Chain.IPFS: ChainConfig(
chain_id=int(0x7d0),
chain_id_alias="ipfs",
chain_id_alias=Chain.IPFS,
client_config=ClientConfig(
chain_finality_threshold=1,
latest_block_stall=5,
node_signatures=1,
node_signature_consensus=1
)
),
"ewc": ChainConfig(
Chain.EWC: ChainConfig(
chain_id=int(0xf6),
chain_id_alias="ewc",
chain_id_alias=Chain.EWC,
client_config=ClientConfig(
chain_finality_threshold=1,
latest_block_stall=6,
Expand Down
Loading