-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c056812
commit 1fae624
Showing
23 changed files
with
210 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 30 additions & 111 deletions
141
dex_screener/handlers/hydradx/asset/asset_type/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,30 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC | ||
from abc import abstractmethod | ||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
|
||
from dex_screener.models import Asset | ||
|
||
if TYPE_CHECKING: | ||
from dipdup.models.substrate import SubstrateEvent | ||
|
||
|
||
class AbstractHydrationAsset(ABC): | ||
asset_type: str = ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def handle_register_asset(cls, event: SubstrateEvent) -> Asset: ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def handle_update_asset(cls, event: SubstrateEvent) -> Asset: ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def create_asset(cls, asset_id: int, event: SubstrateEvent, fields: dict[str, Any] | None) -> Asset: ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def update_asset(cls, asset_id: int, updated_fields: dict[str, Any], event: SubstrateEvent) -> Asset: ... | ||
|
||
|
||
class InvalidEventDataError(ValueError): | ||
pass | ||
|
||
|
||
class BaseHydrationAsset(AbstractHydrationAsset): | ||
@classmethod | ||
async def handle_register_asset(cls, event: SubstrateEvent) -> Asset: | ||
match event.payload: | ||
case { | ||
'asset_id': int(asset_id), | ||
'name': str(asset_name), | ||
}: | ||
pass | ||
case { | ||
'asset_id': int(asset_id), | ||
'asset_name': str(asset_name), | ||
}: | ||
pass | ||
case _: | ||
raise InvalidEventDataError('Unhandled Event Payload.') | ||
|
||
return await cls.create_asset(asset_id, event, {'name': asset_name}) | ||
|
||
@classmethod | ||
async def handle_update_asset(cls, event: SubstrateEvent) -> Asset: | ||
match event.payload: | ||
case { | ||
'asset_id': int(asset_id), | ||
'asset_name': str(asset_name), | ||
}: | ||
updated_fields = { | ||
'name': asset_name, | ||
} | ||
case _: | ||
raise InvalidEventDataError('Unhandled Event Data.') | ||
|
||
return await cls.update_asset( | ||
asset_id=asset_id, | ||
updated_fields=updated_fields, | ||
event=event, | ||
) | ||
|
||
@classmethod | ||
async def create_asset(cls, asset_id: int, event: SubstrateEvent, fields: dict[str, Any] | None = None) -> Asset: | ||
if fields is None: | ||
fields = {} | ||
|
||
return await Asset.create( | ||
id=asset_id, | ||
asset_type=cls.asset_type, | ||
updated_at_block_id=event.level, | ||
**fields, | ||
) | ||
|
||
@classmethod | ||
async def update_asset(cls, asset_id: int, updated_fields: dict[str, Any], event: SubstrateEvent) -> Asset: | ||
updated_fields.setdefault('asset_type', cls.asset_type) | ||
updated_fields.setdefault('updated_at_block_id', event.level) | ||
asset, _ = await Asset.update_or_create( | ||
id=asset_id, | ||
defaults=updated_fields, | ||
) | ||
return asset | ||
|
||
|
||
class HexNamedHydrationAsset(BaseHydrationAsset): | ||
@classmethod | ||
async def handle_register_asset(cls, event: SubstrateEvent) -> Asset: | ||
match event.data.args: | ||
case { | ||
'assetId': int(asset_id), | ||
'assetName': str(asset_name), | ||
}: | ||
pass | ||
case _: | ||
raise InvalidEventDataError('Unhandled Event Data.') | ||
|
||
return await cls.create_asset(asset_id, event, {'name': asset_name}) | ||
from scalecodec.exceptions import RemainingScaleBytesNotEmptyException | ||
|
||
DipDupEventDataCollectPayloadUnhandledError = (RemainingScaleBytesNotEmptyException, NotImplementedError, ValueError) | ||
|
||
def validate_framework_exception(exception): | ||
match exception: | ||
case ValueError( | ||
args=( | ||
'zip() argument 2 is longer than argument 1' | ||
| | ||
'zip() argument 2 is shorter than argument 1', | ||
) | ||
): | ||
pass | ||
case NotImplementedError( | ||
args=( | ||
'Decoder class for "bounded_collections:bounded_vec:BoundedVec@65" not found' | ||
| | ||
'Decoder class for "bounded_collections:bounded_vec:BoundedVec@68" not found' | ||
| | ||
'Decoder class for "bounded_collections:bounded_vec:BoundedVec@70" not found' | ||
| | ||
'Decoder class for "bounded_collections:bounded_vec:BoundedVec@193" not found', | ||
) | ||
): | ||
pass | ||
case RemainingScaleBytesNotEmptyException(): | ||
pass | ||
case _: | ||
raise |
106 changes: 106 additions & 0 deletions
106
dex_screener/handlers/hydradx/asset/asset_type/abstract_hydration_asset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC | ||
from abc import abstractmethod | ||
from typing import Any | ||
|
||
from dipdup.models.substrate import SubstrateEvent | ||
|
||
from dex_screener.handlers.hydradx.asset.asset_type.exception import InvalidEventDataError | ||
from dex_screener.models import Asset | ||
|
||
|
||
class AbstractHydrationAsset(ABC): | ||
asset_type: str = ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def handle_register_asset(cls, event: SubstrateEvent) -> Asset: ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def handle_update_asset(cls, event: SubstrateEvent) -> Asset: ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def create_asset(cls, asset_id: int, event: SubstrateEvent, fields: dict[str, Any] | None) -> Asset: ... | ||
|
||
@classmethod | ||
@abstractmethod | ||
async def update_asset(cls, asset_id: int, updated_fields: dict[str, Any], event: SubstrateEvent) -> Asset: ... | ||
|
||
|
||
class BaseHydrationAsset(AbstractHydrationAsset): | ||
@classmethod | ||
async def handle_register_asset(cls, event: SubstrateEvent) -> Asset: | ||
match event.payload: | ||
case { | ||
'asset_id': int(asset_id), | ||
'name': str(asset_name), | ||
}: | ||
pass | ||
case { | ||
'asset_id': int(asset_id), | ||
'asset_name': str(asset_name), | ||
}: | ||
pass | ||
case _: | ||
raise InvalidEventDataError('Unhandled Event Payload.') | ||
|
||
return await cls.create_asset(asset_id, event, {'name': asset_name}) | ||
|
||
@classmethod | ||
async def handle_update_asset(cls, event: SubstrateEvent) -> Asset: | ||
match event.payload: | ||
case { | ||
'asset_id': int(asset_id), | ||
'asset_name': str(asset_name), | ||
}: | ||
updated_fields = { | ||
'name': asset_name, | ||
} | ||
case _: | ||
raise InvalidEventDataError('Unhandled Event Data.') | ||
|
||
return await cls.update_asset( | ||
asset_id=asset_id, | ||
updated_fields=updated_fields, | ||
event=event, | ||
) | ||
|
||
@classmethod | ||
async def create_asset(cls, asset_id: int, event: SubstrateEvent, fields: dict[str, Any] | None = None) -> Asset: | ||
if fields is None: | ||
fields = {} | ||
|
||
return await Asset.create( | ||
id=asset_id, | ||
asset_type=cls.asset_type, | ||
updated_at_block_id=event.level, | ||
**fields, | ||
) | ||
|
||
@classmethod | ||
async def update_asset(cls, asset_id: int, updated_fields: dict[str, Any], event: SubstrateEvent) -> Asset: | ||
updated_fields.setdefault('asset_type', cls.asset_type) | ||
updated_fields.setdefault('updated_at_block_id', event.level) | ||
asset, _ = await Asset.update_or_create( | ||
id=asset_id, | ||
defaults=updated_fields, | ||
) | ||
return asset | ||
|
||
|
||
class HexNamedHydrationAsset(BaseHydrationAsset): | ||
@classmethod | ||
async def handle_register_asset(cls, event: SubstrateEvent) -> Asset: | ||
match event.data.args: | ||
case { | ||
'assetId': int(asset_id), | ||
'assetName': str(asset_name), | ||
}: | ||
pass | ||
case _: | ||
raise InvalidEventDataError('Unhandled Event Data.') | ||
|
||
return await cls.create_asset(asset_id, event, {'name': asset_name}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class InvalidEventDataError(ValueError): | ||
pass |
2 changes: 1 addition & 1 deletion
2
dex_screener/handlers/hydradx/asset/asset_type/hydration_bond_asset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
dex_screener/handlers/hydradx/asset/asset_type/hydration_erc20_asset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
dex_screener/handlers/hydradx/asset/asset_type/hydration_external_asset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
dex_screener/handlers/hydradx/asset/asset_type/hydration_pool_share_asset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
dex_screener/handlers/hydradx/asset/asset_type/hydration_stable_swap_asset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.