Skip to content

Commit

Permalink
Etheroll.get_or_create() helper
Browse files Browse the repository at this point in the history
Factory that caches instances until parameters change.
  • Loading branch information
AndreMiras committed Nov 7, 2019
1 parent 1a7da2e commit 5af5385
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
26 changes: 24 additions & 2 deletions pyetheroll/etheroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def merge_logs(bet_logs, bet_results_logs):

class Etheroll:

_etheroll = None
CONTRACT_ADDRESSES = {
ChainID.MAINNET: "0xA52e014B3f5Cc48287c2D483A3E026C32cc76E6d",
ChainID.ROPSTEN: "0xe12c6dEb59f37011d2D9FdeC77A6f1A8f3B8B1e8",
Expand All @@ -73,8 +74,9 @@ def __init__(
chain_id: ChainID = ChainID.MAINNET,
contract_address: str = None,
):
if contract_address is None:
contract_address = self.CONTRACT_ADDRESSES[chain_id]
contract_address = (
contract_address or self.CONTRACT_ADDRESSES[chain_id]
)
self.contract_address = contract_address
self.chain_id = chain_id
# ethereum_tester = EthereumTester()
Expand Down Expand Up @@ -111,6 +113,26 @@ def __init__(
self.contract_abi
)

@classmethod
def get_or_create(
cls,
api_key: str = DEFAULT_API_KEY_TOKEN,
chain_id: ChainID = ChainID.MAINNET,
contract_address: str = None,
):
"""
Gets or creates the Etheroll object.
Also recreates the object if one init property changed.
"""
contract_address = contract_address or cls.CONTRACT_ADDRESSES[chain_id]
if cls._etheroll is None or (
cls._etheroll.etherscan_api_key,
cls._etheroll.chain_id,
cls._etheroll.contract_address,
) != (api_key, chain_id, contract_address):
cls._etheroll = cls(api_key, chain_id, contract_address)
return cls._etheroll

def definitions(self, contract_abi, typ):
"""
Returns all events definitions (built from ABI definition).
Expand Down
19 changes: 19 additions & 0 deletions tests/test_etheroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ def test_init(self):
etheroll = Etheroll()
assert etheroll.contract is not None

def test_get_or_create(self):
"""
Checks that etheroll is cached.
The cached value should be updated on (testnet/mainnet) network change.
"""
abi_str = "[]"
assert Etheroll._etheroll is None
with patch_get_abi(abi_str):
etheroll = Etheroll.get_or_create()
assert etheroll._etheroll is not None
# it's obviously pointing to the same object for now,
# but shouldn't not be later after we update some settings
assert etheroll == Etheroll.get_or_create() == Etheroll._etheroll
assert etheroll.chain_id == ChainID.MAINNET
# the cached object is invalidated if the network changes
with patch_get_abi(abi_str):
assert etheroll != Etheroll.get_or_create(chain_id=ChainID.ROPSTEN)
assert Etheroll._etheroll.chain_id == ChainID.ROPSTEN

def create_account_helper(self, password):
"""
Reduces the PBKDF2 iterations/work-factor to speed up account creation.
Expand Down

0 comments on commit 5af5385

Please sign in to comment.