Skip to content
Open
34 changes: 0 additions & 34 deletions src/Artifacts/src/tests/test_artifacts.py

This file was deleted.

24 changes: 0 additions & 24 deletions src/BaseContract/base_contract.py

This file was deleted.

9 changes: 0 additions & 9 deletions src/BaseContract/test/test_base_contract.py

This file was deleted.

29 changes: 0 additions & 29 deletions src/BaseContract/utils.py

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/base_contract/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from web3 import Web3
from src.artifacts.src.index import Artifacts
102 changes: 102 additions & 0 deletions src/base_contract/base_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from web3 import Web3
import asyncio

import src.artifacts.src.index as index
import src.base_contract.utils as utils


class BaseContract:
provider: any
web3: any
contract: any
network_id: int
coordinator: any
artifact: any
name: str
address: str or None

def __init__(self,
artifact_name: str,
web3: any = None,
network_id: int = 1,
network_provider: any = None,
artifacts_dir: str = None,
coordinator: str = None,
address: str = None
):
try:
if artifacts_dir is None:
self.artifact = index.Artifacts[artifact_name]
self.coor_artifact = index.Artifacts['ZAPCOORDINATOR']
else:
artifacts: any = utils.Utils.get_artifacts(artifacts_dir)
self.artifact = utils.Utils.open_artifact_in_dir(artifacts[artifact_name])
self.coor_artifact = utils.Utils.open_artifact_in_dir(artifacts['ZAPCOORDINATOR'])

self.name = artifact_name
self.provider = web3 or Web3(network_provider or Web3.HTTPProvider("https://cloudflare-eth.com"))
self.w3 = web3 or Web3(network_provider or Web3.HTTPProvider("https://cloudflare-eth.com"))
self.network_id = network_id or 1
self.address = address or self.artifact['networks'][str(self.network_id)]['address']

if coordinator is not None:
self.coordinator = self.w3.eth.contract(address=self.w3.toChecksumAddress(coordinator),
abi=self.coor_artifact['abi'])

contract_address = self.get_contract()
self.contract = self.w3.eth.contract(address=self.w3.toChecksumAddress(contract_address),
abi=self.artifact['abi'])

else:
self.coor_address = self.coor_artifact['networks'][str(self.network_id)]['address']
self.coordinator = self.w3.eth.contract(address=self.w3.toChecksumAddress(self.coor_address),
abi=self.coor_artifact['abi'])

self.contract = self.w3.eth.contract(address=self.w3.toChecksumAddress(self.address),
abi=self.artifact['abi'])

except Exception as e:
raise e

async def _get_contract(self) -> str:
"""
This async function fetches the contract address from the coordinator abi. Thereafter, the function returns the
address where it is assigned to the address kwarg in the contract instance.

:return: the contract address of the coordinator.
"""
await asyncio.sleep(.5)
contract_address = self.coordinator.functions.getContract(self.name).call()
return contract_address

async def _get_contract_owner(self) -> str:
"""
This async function fetches the owner of the contract instance.

:return: the contract owner's address.
"""
await asyncio.sleep(.5)
contract_owner = self.contract.functions.owner().call()
return contract_owner

def get_contract(self) -> str:
"""
A synchronous function that wraps the asynchronous _get_contract method. This function returns the contract
address fetched from the asynchronous _get_contract function.

:return: the contract address.
"""
task = self._get_contract()
contract_address = asyncio.run(task)
return contract_address

def get_contract_owner(self) -> str:
"""
A synchronous function that wraps the asynchronous _get_contract_owner method. This function returns the
contract owner's address.

:return: the contract owner's address.
"""
task = self._get_contract_owner()
owner = asyncio.run(task)
return owner
41 changes: 41 additions & 0 deletions src/base_contract/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import json


class Utils:

"""
# @ignore
# @params {string} buildDir
# @returns {any}
"""

@staticmethod
def get_artifacts(build_dir: str) -> dict:
artifacts = {
'ARBITER': os.path.join(build_dir, 'Arbiter.json'),
'BONDAGE': os.path.join(build_dir, 'Bondage.json'),
'DISPATCH': os.path.join(build_dir, 'Dispatch.json'),
'REGISTRY': os.path.join(build_dir, 'Registry.json'),
'CurrentCost': os.path.join(build_dir, 'CurrentCost.json'),
'PiecewiseLogic': os.path.join(build_dir, 'PiecewiseLogic.json'),
'ZAP_TOKEN': os.path.join(build_dir, 'ZapToken.json'),
'Client1': os.path.join(build_dir, 'Client1.json'),
'Client2': os.path.join(build_dir, 'Client2.json'),
'Client3': os.path.join(build_dir, 'Client3.json'),
'Client4': os.path.join(build_dir, 'Client4.json'),
'ZAPCOORDINATOR': os.path.join(build_dir, 'ZapCoordinator.json'),
'TOKENDOTFACTORY': os.path.join(build_dir, 'TokenDotFactory.json'),
}
return artifacts

"""
# @ignore
# @params {string} artifact
# @returns {dict}
"""
@staticmethod
def open_artifact_in_dir(artifact: str) -> dict:
with open(artifact) as f:
abi = json.load(f)
return abi
9 changes: 0 additions & 9 deletions src/portedFiles/default_tx_test.py

This file was deleted.

13 changes: 0 additions & 13 deletions src/portedFiles/filter_test.py

This file was deleted.

File renamed without changes.
37 changes: 37 additions & 0 deletions tests/base_contract/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from unittest.mock import MagicMock, patch
import pytest
import re
import src.base_contract.base_contract as base_contract

"""Default setup"""

MOCK_ABI = {
'TEST_ARTIFACT': {'abi': [], 'networks':
{'1': {'address': '0xmainnet'},
'42': {'address': '0xkovan'},
'31337': {'address': '0xdevnet'}}},
'ZAPCOORDINATOR': {'abi': [], 'networks':
{'1': {'address': '0xcoormainnet'},
'42': {'address': '0xcoorkovan'},
'31337': {'address': '0xcoordevnet'}}}
}


@pytest.fixture
def anyio_backend():
"""
Ensures anyio uses the default, pytest-asyncio plugin
for running async tests.
"""
return 'asyncio'


@pytest.fixture
@patch.dict(base_contract.index.Artifacts, MOCK_ABI, clear=True)
@patch('src.base_contract.Web3', autospec=True)
def instance(mock_Web3):
with patch('src.base_contract.Web3') as mock_Web3:
mock_Web3.return_value = MagicMock()
w3 = mock_Web3()
w3.eth.contract.side_effect = MagicMock()
return base_contract.BaseContract(artifact_name='TEST_ARTIFACT', web3=w3)
Loading