Skip to content

Commit

Permalink
Pass template_values to handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout committed Apr 7, 2021
1 parent 86e9dd2 commit 85e7101
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 30 deletions.
19 changes: 15 additions & 4 deletions src/dipdup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ class SqliteDatabaseConfig:
:param path: Path to .sqlite3 file, leave default for in-memory database
"""

kind: Literal['sqlite']
path: str = ':memory:'

@property
def connection_string(self):
return f'sqlite://{self.path}'
return f'{self.kind}://{self.path}'


@dataclass
Expand All @@ -48,7 +49,7 @@ class DatabaseConfig:
:param database: Schema name
"""

driver: str
kind: Union[Literal['postgres'], Literal['mysql']]
host: str
port: int
user: str
Expand All @@ -57,7 +58,7 @@ class DatabaseConfig:

@property
def connection_string(self):
return f'{self.driver}://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}'
return f'{self.kind}://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}'


@dataclass
Expand Down Expand Up @@ -144,6 +145,7 @@ class OperationIndexConfig:
def __post_init_post_parse__(self):
self._state: Optional[State] = None
self._rollback_fn: Optional[Callable] = None
self._template_values: Dict[str, str] = None

def hash(self) -> str:
return hashlib.sha256(
Expand Down Expand Up @@ -173,6 +175,14 @@ def rollback_fn(self) -> Callable:
def rollback_fn(self, value: Callable) -> None:
self._rollback_fn = value

@property
def template_values(self) -> Optional[Dict[str, str]]:
return self._template_values

@template_values.setter
def template_values(self, value: Dict[str, str]) -> None:
self._template_values = value


@dataclass
class BigmapdiffHandlerPatternConfig:
Expand Down Expand Up @@ -235,7 +245,7 @@ class DipDupConfig:
datasources: Dict[str, Union[TzktDatasourceConfig]]
indexes: Dict[str, IndexConfigT]
templates: Optional[Dict[str, IndexConfigTemplateT]] = None
database: Union[SqliteDatabaseConfig, DatabaseConfig] = SqliteDatabaseConfig()
database: Union[SqliteDatabaseConfig, DatabaseConfig] = SqliteDatabaseConfig(kind='sqlite')

def __post_init_post_parse__(self):
self._logger = logging.getLogger(__name__)
Expand All @@ -248,6 +258,7 @@ def __post_init_post_parse__(self):
raw_template = re.sub(value_regex, value, raw_template)
json_template = json.loads(raw_template)
self.indexes[index_name] = template.__class__(**json_template)
self.indexes[index_name].template_values = index_config.values

for index_config in self.indexes.values():
if isinstance(index_config, OperationIndexConfig):
Expand Down
4 changes: 2 additions & 2 deletions src/dipdup/datasources/tzkt/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def match_operation(self, pattern_config: OperationHandlerPatternConfig, operati

async def process(
self,
callback: Callable[[OperationHandlerConfig, List[OperationData], List[OperationData]], Awaitable[None]],
callback: Callable[[OperationIndexConfig, OperationHandlerConfig, List[OperationData], List[OperationData]], Awaitable[None]],
) -> int:
keys = list(self._operations.keys())
self._logger.info('Matching %s operation groups', len(keys))
Expand All @@ -55,7 +55,7 @@ async def process(

if len(matched_operations) == len(handler_config.pattern):
self._logger.info('Handler `%s` matched! %s', handler_config.callback, key)
await callback(handler_config, matched_operations, operations)
await callback(self._index_config, handler_config, matched_operations, operations)
if key in self._operations:
del self._operations[key]

Expand Down
3 changes: 2 additions & 1 deletion src/dipdup/datasources/tzkt/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ async def add_subscription(self, address: str, types: Optional[List[str]] = None

async def on_operation_match(
self,
index_config: OperationIndexConfig,
handler_config: OperationHandlerConfig,
matched_operations: List[OperationData],
operations: List[OperationData],
Expand All @@ -247,7 +248,7 @@ async def on_operation_match(
)
args.append(context)

await handler_config.callback_fn(*args, operations)
await handler_config.callback_fn(*args, operations, index_config.template_values)

@classmethod
def convert_operation(cls, operation_json: Dict[str, Any]) -> OperationData:
Expand Down
3 changes: 2 additions & 1 deletion src/dipdup/templates/handler.py.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Dict, List, Optional
from dipdup.models import HandlerContext, OperationData

from {{ package }}.models import *
Expand All @@ -11,5 +11,6 @@ async def {{ handler }}(
{{ pattern.entrypoint }}: HandlerContext[{{ pattern.entrypoint.title().replace('_', '') }}],
{%- endfor %}
operations: List[OperationData],
template_values: Optional[Dict[str, str]] = None,
) -> None:
...
8 changes: 3 additions & 5 deletions src/dipdup_hic_et_nunc/dipdup-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ spec_version: 0.0.1
package: dipdup_hic_et_nunc

database:
driver: postgres
kind: postgres
host: db
port: 5432
user: dipdup
database: dipdup
password: ${POSTGRES_PASSWORD:-changeme}

contracts:
HEN_objkts:
address: ${HEN_OBJKTS:-KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9}
HEN_minter:
address: ${HEN_MINTER:-KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton}
HEN_objkts: ${HEN_OBJKTS:-KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9}
HEN_minter: ${HEN_MINTER:-KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton}

datasources:
tzkt_mainnet:
Expand Down
1 change: 1 addition & 0 deletions src/dipdup_hic_et_nunc/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ spec_version: 0.0.1
package: dipdup_hic_et_nunc

database:
kind: sqlite
path: db.sqlite3

contracts:
Expand Down
9 changes: 5 additions & 4 deletions src/dipdup_hic_et_nunc/handlers/on_mint.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from typing import List

from typing import Dict, List, Optional
from dipdup.models import HandlerContext, OperationData

from dipdup_hic_et_nunc.models import *

from dipdup_hic_et_nunc.types.KT1Hkg5qeNhfwpKW4fXvq7HGZB9z2EnmCCA9.parameter.mint_OBJKT import MintObjkt
from dipdup_hic_et_nunc.types.KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton.parameter.mint import Mint


async def on_mint(
mint_OBJKT: HandlerContext[MintObjkt],
mint: HandlerContext[Mint],
operations: List[OperationData],
template_values: Optional[Dict[str, str]] = None,
) -> None:
...
...
2 changes: 1 addition & 1 deletion src/dipdup_hic_et_nunc/handlers/on_rollback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ async def on_rollback(
) -> None:
_logger.warning('Rollback event received, reindexing')
await Tortoise._drop_databases()
os.execl(sys.executable, sys.executable, *sys.argv)
os.execl(sys.executable, sys.executable, *sys.argv)
9 changes: 5 additions & 4 deletions src/dipdup_hic_et_nunc/handlers/on_transfer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import List

from typing import Dict, List, Optional
from dipdup.models import HandlerContext, OperationData

from dipdup_hic_et_nunc.models import *
from dipdup_hic_et_nunc.types.KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton.parameter.transfer import Transfer

from dipdup_hic_et_nunc.types.KT1RJ6PbjHpwc3M5rw5s2Nbmefwbuwbdxton.parameter.transfer import Transfer

async def on_transfer(
transfer: HandlerContext[Transfer],
operations: List[OperationData],
template_values: Optional[Dict[str, str]] = None,
) -> None:
...
...
4 changes: 2 additions & 2 deletions src/dipdup_hic_et_nunc/hasura_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"role": "user",
"permission": {
"columns": [
"token_info",
"id",
"token_id",
"id"
"token_info"
],
"filter": {},
"allow_aggregations": true
Expand Down
1 change: 1 addition & 0 deletions src/dipdup_quipuswap/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ spec_version: 0.0.1
package: dipdup_quipuswap

database:
kind: sqlite
path: db.sqlite3

contracts:
Expand Down
3 changes: 2 additions & 1 deletion src/dipdup_quipuswap/handlers/on_fa12_token_to_tez.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Dict, List, Optional

from dipdup.models import HandlerContext, OperationData
from dipdup_quipuswap.models import *
Expand All @@ -10,5 +10,6 @@ async def on_fa12_token_to_tez(
tokenToTezPayment: HandlerContext[Tokentotezpayment],
transfer: HandlerContext[Transfer],
operations: List[OperationData],
template_values: Optional[Dict[str, str]] = None,
) -> None:
...
3 changes: 2 additions & 1 deletion src/dipdup_quipuswap/handlers/on_fa20_tez_to_token.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Dict, List, Optional

from dipdup.models import HandlerContext, OperationData
from dipdup_quipuswap.models import *
Expand All @@ -10,5 +10,6 @@ async def on_fa20_tez_to_token(
tezToTokenPayment: HandlerContext[Teztotokenpayment],
transfer: HandlerContext[Transfer],
operations: List[OperationData],
template_values: Optional[Dict[str, str]] = None,
) -> None:
...
1 change: 1 addition & 0 deletions tests/test_dipdup/dipdup-templated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ spec_version: 0.0.1
package: dipdup_quipuswap

database:
kind: sqlite
path: db.sqlite3

contracts:
Expand Down
1 change: 1 addition & 0 deletions tests/test_dipdup/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ spec_version: 0.0.1
package: dipdup_hic_et_nunc

database:
kind: sqlite
path: db.sqlite3

contracts:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_dipdup/test_datasources/test_tzkt/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ async def test_process(self):

await self.cache.process(callback_mock)

self.assertIsInstance(callback_mock.await_args[0][0], OperationIndexConfig)
self.assertEqual(
callback_mock.await_args[0][0],
callback_mock.await_args[0][1],
OperationHandlerConfig(
callback='',
pattern=[OperationHandlerPatternConfig(destination='KT1AFA2mwNUMNd4SsujE1YYp29vd8BZejyKW', entrypoint='hDAO_batch')],
),
)
self.assertIsInstance(callback_mock.await_args[0][1], list)
self.assertIsInstance(callback_mock.await_args[0][1][0], OperationData)
self.assertIsInstance(callback_mock.await_args[0][2], list)
self.assertIsInstance(callback_mock.await_args[0][2][0], OperationData)
self.assertIsInstance(callback_mock.await_args[0][3], list)
self.assertIsInstance(callback_mock.await_args[0][3][0], OperationData)
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async def test_on_operation_message_data(self):
await self.datasource.on_operation_message([operations_message], self.index_config.contract, sync=True)

on_operation_match_mock.assert_awaited_with(
self.index_config,
self.index_config.handlers[0],
[operation],
ANY,
Expand Down Expand Up @@ -155,7 +156,7 @@ async def test_on_operation_match(self):
self.index_config.handlers[0].callback_fn = callback_mock

self.datasource._synchronized.set()
await self.datasource.on_operation_match(self.index_config.handlers[0], [matched_operation], operations)
await self.datasource.on_operation_match(self.index_config, self.index_config.handlers[0], [matched_operation], operations)

call_arg = callback_mock.await_args[0][0]
self.assertIsInstance(call_arg, HandlerContext)
Expand Down

0 comments on commit 85e7101

Please sign in to comment.