Skip to content

Commit

Permalink
Update project factory
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Apr 2, 2024
1 parent 09cb38c commit c639761
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
18 changes: 14 additions & 4 deletions python/src/skale_contracts/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,32 @@
if TYPE_CHECKING:
from eth_typing import Address
from .network import Network
from .project_metadata import ProjectMetadata


class Project(ABC):
"""Represents set of smart contracts known as project"""

def __init__(self, network: Network, metadata: ProjectMetadata) -> None:
def __init__(self, network: Network) -> None:
super().__init__()
self.network = network
self._metadata = metadata

@staticmethod
@abstractmethod
def name() -> str:
"""Name of the project"""

@property
@abstractmethod
def github_repo(self) -> str:
"""URL of github repo with the project"""


@property
def folder(self) -> str:
"""Folder name with instances json files"""
return self.name()


def get_instance(self, alias_or_address: str) -> Instance:
"""Create instance object based on alias or address"""
if self.network.web3.is_address(alias_or_address):
Expand Down Expand Up @@ -63,7 +73,7 @@ def get_instance_data_url(self, alias: str) -> str:
"""Get URL of a file containing address for provided alias"""
if self.network.is_listed():
return f'{REPOSITORY_URL}{self.network.as_listed().path}/' + \
f'{self._metadata.path}/{alias}.json'
f'{self.folder}/{alias}.json'
raise ValueError('Network is unknown')

@abstractmethod
Expand Down
28 changes: 10 additions & 18 deletions python/src/skale_contracts/project_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,25 @@

from __future__ import annotations
from typing import TYPE_CHECKING
from attr import dataclass
import inspect

from .project import Project
from . import projects
from .project_metadata import ProjectMetadata

if TYPE_CHECKING:
from .project import Project
from .network import Network


@dataclass
class Projects:
"""Contains all known projects"""
skale_manager = ProjectMetadata(name='skale-manager', path='skale-manager')
mainnet_ima = ProjectMetadata(name='mainnet-ima', path='mainnet-ima')
schain_ima = ProjectMetadata(name='schain-ima', path='schain-ima')
skale_allocator = ProjectMetadata(name='skale-allocator', path='skale-allocator')
projects_dict = {
class_type.name(): class_type
for _, class_type
in inspect.getmembers(projects, inspect.isclass)
if issubclass(class_type, Project)
}


def create_project(network: Network, name: str) -> Project:
"""Create Project object based on it's name"""
if name == Projects.skale_manager.name:
return projects.SkaleManager(network, Projects.skale_manager)
if name == Projects.mainnet_ima.name:
return projects.MainnetIma(network, Projects.mainnet_ima)
if name == Projects.schain_ima.name:
return projects.SchainIma(network, Projects.schain_ima)
if name == Projects.skale_allocator.name:
return projects.SkaleAllocator(network, Projects.skale_allocator)
if name in projects_dict:
return projects_dict[name](network)
raise ValueError(f'Project with name {name} is unknown')
4 changes: 0 additions & 4 deletions python/src/skale_contracts/project_metadata.py

This file was deleted.

12 changes: 9 additions & 3 deletions python/src/skale_contracts/projects/ima.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def __init__(self, project: Project, address: Address) -> None:
class ImaProject(Project):
"""Represents IMA project"""

@property
def github_repo(self) -> str:
return 'https://github.com/skalenetwork/ima/'
github_repo = 'https://github.com/skalenetwork/ima/'


class MainnetImaInstance(ImaInstance):
Expand Down Expand Up @@ -74,6 +72,10 @@ def contract_manager(self) -> Contract:
class MainnetImaProject(ImaProject):
"""Represents mainnet part of IMA project"""

@staticmethod
def name() -> str:
return 'mainnet-ima'

def create_instance(self, address: Address) -> Instance:
return MainnetImaInstance(self, address)

Expand Down Expand Up @@ -107,6 +109,10 @@ def get_contract_address(self, name: str, *args: str|Address|ChecksumAddress) ->
class SchainImaProject(ImaProject):
"""Represents schain part of IMA project"""

@staticmethod
def name() -> str:
return 'schain-ima'

def get_instance(self, alias_or_address: str) -> Instance:
if alias_or_address == PREDEPLOYED_ALIAS:
return self.create_instance(SchainImaInstance.PREDEPLOYED['MessageProxyForSchain'])
Expand Down
7 changes: 4 additions & 3 deletions python/src/skale_contracts/projects/skale_allocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def _get_escrow (self, beneficiary: Address) -> Address:
class SkaleAllocatorProject(Project):
"""Represents skale-allocator project"""

@property
def github_repo(self) -> str:
return 'https://github.com/skalenetwork/skale-allocator/'
@staticmethod
def name() -> str:
return 'skale-allocator'
github_repo = 'https://github.com/skalenetwork/skale-allocator/'

def create_instance(self, address: Address) -> Instance:
return SkaleAllocatorInstance(self, address)
Expand Down
7 changes: 4 additions & 3 deletions python/src/skale_contracts/projects/skale_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ def _actual_name(self, name: str) -> str:
class SkaleManagerProject(Project):
"""Represents skale-manager project"""

@property
def github_repo(self) -> str:
return 'https://github.com/skalenetwork/skale-manager/'
@staticmethod
def name() -> str:
return 'skale-manager'
github_repo = 'https://github.com/skalenetwork/skale-manager/'

def create_instance(self, address: Address) -> Instance:
return SkaleManagerInstance(self, address)
Expand Down

0 comments on commit c639761

Please sign in to comment.