From 9fbcb806d5e93b330a5f4d02e64890b938144aed Mon Sep 17 00:00:00 2001 From: Lev Gorodetskiy Date: Wed, 4 Aug 2021 16:19:25 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=95Template=20values=20wrapper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/demo_quipuswap/dipdup.yml | 2 +- .../handlers/on_fa12_divest_liquidity.py | 3 --- .../handlers/on_fa12_invest_liquidity.py | 3 --- .../handlers/on_fa12_origination.py | 3 --- .../handlers/on_fa12_tez_to_token.py | 3 --- .../handlers/on_fa12_token_to_tez.py | 3 --- .../handlers/on_fa12_transfer.py | 3 --- .../handlers/on_fa12_withdraw_profit.py | 3 --- .../handlers/on_fa2_divest_liquidity.py | 4 ---- .../handlers/on_fa2_invest_liquidity.py | 4 ---- .../handlers/on_fa2_origination.py | 3 --- .../handlers/on_fa2_tez_to_token.py | 3 --- .../handlers/on_fa2_token_to_tez.py | 3 --- .../handlers/on_fa2_transfer.py | 3 --- .../handlers/on_fa2_withdraw_profit.py | 4 ---- src/dipdup/config.py | 4 ++-- src/dipdup/context.py | 20 ++++++++++++++++--- 17 files changed, 20 insertions(+), 51 deletions(-) diff --git a/src/demo_quipuswap/dipdup.yml b/src/demo_quipuswap/dipdup.yml index b573ae671..3407537cc 100644 --- a/src/demo_quipuswap/dipdup.yml +++ b/src/demo_quipuswap/dipdup.yml @@ -166,4 +166,4 @@ indexes: dex_contract: hdao_dex_mainnet token_contract: hdao_token_mainnet symbol: hDAO - decimals: 6 + # decimals: 6 diff --git a/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py b/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py index f80906dff..8cee139b3 100644 --- a/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py +++ b/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py @@ -15,9 +15,6 @@ async def on_fa12_divest_liquidity( transfer: Transaction[TransferParameter, Fa12TokenStorage], transaction_1: OperationData, ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - storage = divest_liquidity.storage decimals = int(ctx.template_values['decimals']) diff --git a/src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py b/src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py index d149d8d24..dd1a854d0 100644 --- a/src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py +++ b/src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py @@ -14,9 +14,6 @@ async def on_fa12_invest_liquidity( invest_liquidity: Transaction[InvestLiquidityParameter, QuipuFa12Storage], transfer: Transaction[TransferParameter, Fa12TokenStorage], ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - storage = invest_liquidity.storage decimals = int(ctx.template_values['decimals']) diff --git a/src/demo_quipuswap/handlers/on_fa12_origination.py b/src/demo_quipuswap/handlers/on_fa12_origination.py index 01e47089d..76e5be43f 100644 --- a/src/demo_quipuswap/handlers/on_fa12_origination.py +++ b/src/demo_quipuswap/handlers/on_fa12_origination.py @@ -8,9 +8,6 @@ async def on_fa12_origination( ctx: HandlerContext, quipu_fa12_origination: Origination[QuipuFa12Storage], ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - symbol = ctx.template_values['symbol'] for address, value in quipu_fa12_origination.storage.storage.ledger.items(): diff --git a/src/demo_quipuswap/handlers/on_fa12_tez_to_token.py b/src/demo_quipuswap/handlers/on_fa12_tez_to_token.py index a9b6c5a55..c87751364 100644 --- a/src/demo_quipuswap/handlers/on_fa12_tez_to_token.py +++ b/src/demo_quipuswap/handlers/on_fa12_tez_to_token.py @@ -14,9 +14,6 @@ async def on_fa12_tez_to_token( tez_to_token_payment: Transaction[TezToTokenPaymentParameter, QuipuFa12Storage], transfer: Transaction[TransferParameter, Fa12TokenStorage], ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - decimals = int(ctx.template_values['decimals']) symbol = ctx.template_values['symbol'] trader = tez_to_token_payment.data.sender_address diff --git a/src/demo_quipuswap/handlers/on_fa12_token_to_tez.py b/src/demo_quipuswap/handlers/on_fa12_token_to_tez.py index 52cf90210..8fedbc31b 100644 --- a/src/demo_quipuswap/handlers/on_fa12_token_to_tez.py +++ b/src/demo_quipuswap/handlers/on_fa12_token_to_tez.py @@ -15,9 +15,6 @@ async def on_fa12_token_to_tez( transfer: Transaction[TransferParameter, Fa12TokenStorage], transaction_0: OperationData, ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - decimals = int(ctx.template_values['decimals']) symbol = ctx.template_values['symbol'] trader = token_to_tez_payment.data.sender_address diff --git a/src/demo_quipuswap/handlers/on_fa12_transfer.py b/src/demo_quipuswap/handlers/on_fa12_transfer.py index 87cf1b979..c8ca68543 100644 --- a/src/demo_quipuswap/handlers/on_fa12_transfer.py +++ b/src/demo_quipuswap/handlers/on_fa12_transfer.py @@ -9,9 +9,6 @@ async def on_fa12_transfer( ctx: HandlerContext, transfer: Transaction[TransferParameter, QuipuFa12Storage], ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - symbol = ctx.template_values['symbol'] from_address = transfer.parameter.from_ to_address = transfer.parameter.to diff --git a/src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py b/src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py index 1844289e2..68c9bb486 100644 --- a/src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py +++ b/src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py @@ -13,9 +13,6 @@ async def on_fa12_withdraw_profit( withdraw_profit: Transaction[WithdrawProfitParameter, QuipuFa12Storage], transaction_0: Optional[OperationData] = None, ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - symbol = ctx.template_values['symbol'] trader = withdraw_profit.data.sender_address diff --git a/src/demo_quipuswap/handlers/on_fa2_divest_liquidity.py b/src/demo_quipuswap/handlers/on_fa2_divest_liquidity.py index 879d4e8d3..8e09fa4c8 100644 --- a/src/demo_quipuswap/handlers/on_fa2_divest_liquidity.py +++ b/src/demo_quipuswap/handlers/on_fa2_divest_liquidity.py @@ -15,10 +15,6 @@ async def on_fa2_divest_liquidity( transfer: Transaction[TransferParameter, Fa2TokenStorage], transaction_1: OperationData, ) -> None: - - if ctx.template_values is None: - raise Exception('This index must be templated') - storage = divest_liquidity.storage decimals = int(ctx.template_values['decimals']) diff --git a/src/demo_quipuswap/handlers/on_fa2_invest_liquidity.py b/src/demo_quipuswap/handlers/on_fa2_invest_liquidity.py index 25b3716c5..62efb8775 100644 --- a/src/demo_quipuswap/handlers/on_fa2_invest_liquidity.py +++ b/src/demo_quipuswap/handlers/on_fa2_invest_liquidity.py @@ -14,10 +14,6 @@ async def on_fa2_invest_liquidity( invest_liquidity: Transaction[InvestLiquidityParameter, QuipuFa2Storage], transfer: Transaction[TransferParameter, Fa2TokenStorage], ) -> None: - - if ctx.template_values is None: - raise Exception('This index must be templated') - storage = invest_liquidity.storage decimals = int(ctx.template_values['decimals']) diff --git a/src/demo_quipuswap/handlers/on_fa2_origination.py b/src/demo_quipuswap/handlers/on_fa2_origination.py index 96f6b03fd..4e832722b 100644 --- a/src/demo_quipuswap/handlers/on_fa2_origination.py +++ b/src/demo_quipuswap/handlers/on_fa2_origination.py @@ -8,9 +8,6 @@ async def on_fa2_origination( ctx: HandlerContext, quipu_fa2_origination: Origination[QuipuFa2Storage], ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - symbol = ctx.template_values['symbol'] for address, value in quipu_fa2_origination.storage.storage.ledger.items(): diff --git a/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py b/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py index 6ffa3ecf0..dc5ed7c76 100644 --- a/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py +++ b/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py @@ -14,9 +14,6 @@ async def on_fa2_tez_to_token( tez_to_token_payment: Transaction[TezToTokenPaymentParameter, QuipuFa2Storage], transfer: Transaction[TransferParameter, Fa2TokenStorage], ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - decimals = int(ctx.template_values['decimals']) symbol = ctx.template_values['symbol'] trader = tez_to_token_payment.data.sender_address diff --git a/src/demo_quipuswap/handlers/on_fa2_token_to_tez.py b/src/demo_quipuswap/handlers/on_fa2_token_to_tez.py index 23b3ca3a6..63c88f293 100644 --- a/src/demo_quipuswap/handlers/on_fa2_token_to_tez.py +++ b/src/demo_quipuswap/handlers/on_fa2_token_to_tez.py @@ -15,9 +15,6 @@ async def on_fa2_token_to_tez( transfer: Transaction[TransferParameter, Fa2TokenStorage], transaction_0: OperationData, ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - decimals = int(ctx.template_values['decimals']) symbol = ctx.template_values['symbol'] trader = token_to_tez_payment.data.sender_address diff --git a/src/demo_quipuswap/handlers/on_fa2_transfer.py b/src/demo_quipuswap/handlers/on_fa2_transfer.py index 29af9ca7e..acec6cb89 100644 --- a/src/demo_quipuswap/handlers/on_fa2_transfer.py +++ b/src/demo_quipuswap/handlers/on_fa2_transfer.py @@ -9,9 +9,6 @@ async def on_fa2_transfer( ctx: HandlerContext, transfer: Transaction[TransferParameter, QuipuFa2Storage], ) -> None: - if ctx.template_values is None: - raise Exception('This index must be templated') - transfer_parameter = transfer.parameter.__root__[0] symbol = ctx.template_values['symbol'] diff --git a/src/demo_quipuswap/handlers/on_fa2_withdraw_profit.py b/src/demo_quipuswap/handlers/on_fa2_withdraw_profit.py index 22259c474..11b362e74 100644 --- a/src/demo_quipuswap/handlers/on_fa2_withdraw_profit.py +++ b/src/demo_quipuswap/handlers/on_fa2_withdraw_profit.py @@ -13,10 +13,6 @@ async def on_fa2_withdraw_profit( withdraw_profit: Transaction[WithdrawProfitParameter, QuipuFa2Storage], transaction_0: Optional[OperationData] = None, ) -> None: - - if ctx.template_values is None: - raise Exception('This index must be templated') - symbol = ctx.template_values['symbol'] trader = withdraw_profit.data.sender_address diff --git a/src/dipdup/config.py b/src/dipdup/config.py index ecdf4e4b7..fbb4c7278 100644 --- a/src/dipdup/config.py +++ b/src/dipdup/config.py @@ -516,10 +516,10 @@ class OperationHandlerConfig(HandlerConfig): @dataclass class TemplateValuesMixin: def __post_init_post_parse__(self) -> None: - self._template_values: Optional[Dict[str, str]] = None + self._template_values: Dict[str, str] = {} @property - def template_values(self) -> Optional[Dict[str, str]]: + def template_values(self) -> Dict[str, str]: return self._template_values @template_values.setter diff --git a/src/dipdup/context.py b/src/dipdup/context.py index 70cc82f6d..db4f72021 100644 --- a/src/dipdup/context.py +++ b/src/dipdup/context.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os import sys from pprint import pformat @@ -8,7 +10,7 @@ from dipdup.config import ContractConfig, DipDupConfig, IndexConfig, IndexTemplateConfig, PostgresDatabaseConfig from dipdup.datasources.datasource import Datasource -from dipdup.exceptions import ContractAlreadyExistsError, IndexAlreadyExistsError +from dipdup.exceptions import ConfigurationError, ContractAlreadyExistsError, IndexAlreadyExistsError from dipdup.utils import FormattedLogger @@ -69,6 +71,18 @@ async def reindex(self) -> None: await self.restart() +class TemplateValuesDict(dict): + def __init__(self, ctx, **kwargs): + self.ctx = ctx + super().__init__(**kwargs) + + def __getitem__(self, key): + try: + return dict.__getitem__(self, key) + except KeyError as e: + raise ConfigurationError(f'Index `{self.ctx.index_config.name}` requires `{key}` template value to be set') from e + + class HandlerContext(DipDupContext): """Common handler context.""" @@ -77,13 +91,13 @@ def __init__( datasources: Dict[str, Datasource], config: DipDupConfig, logger: FormattedLogger, - template_values: Optional[Dict[str, str]], + template_values: Dict[str, str], datasource: Datasource, index_config: IndexConfig, ) -> None: super().__init__(datasources, config) self.logger = logger - self.template_values = template_values + self.template_values = TemplateValuesDict(self, **template_values) self.datasource = datasource self.index_config = index_config From 3ef4d11d4dc32034f55ee7558709979db89e6917 Mon Sep 17 00:00:00 2001 From: Lev Gorodetskiy Date: Wed, 4 Aug 2021 16:20:13 +0300 Subject: [PATCH 2/3] Fix --- src/demo_quipuswap/dipdup.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/demo_quipuswap/dipdup.yml b/src/demo_quipuswap/dipdup.yml index 3407537cc..b573ae671 100644 --- a/src/demo_quipuswap/dipdup.yml +++ b/src/demo_quipuswap/dipdup.yml @@ -166,4 +166,4 @@ indexes: dex_contract: hdao_dex_mainnet token_contract: hdao_token_mainnet symbol: hDAO - # decimals: 6 + decimals: 6 From d69e331b89d38dda859bc329cbd6111adf2726df Mon Sep 17 00:00:00 2001 From: Lev Gorodetskiy Date: Wed, 4 Aug 2021 16:23:01 +0300 Subject: [PATCH 3/3] Fix --- src/dipdup/context.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/dipdup/context.py b/src/dipdup/context.py index db4f72021..6b5e9cdea 100644 --- a/src/dipdup/context.py +++ b/src/dipdup/context.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import os import sys from pprint import pformat