Skip to content

Commit

Permalink
Template values wrapper (#114)
Browse files Browse the repository at this point in the history
* ЕTemplate values wrapper

* Fix

* Fix
  • Loading branch information
droserasprout authored Aug 5, 2021
1 parent b35eed3 commit 1113daa
Show file tree
Hide file tree
Showing 16 changed files with 17 additions and 50 deletions.
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_origination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_tez_to_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_token_to_tez.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions src/demo_quipuswap/handlers/on_fa2_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
4 changes: 0 additions & 4 deletions src/demo_quipuswap/handlers/on_fa2_invest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa2_origination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa2_tez_to_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa2_token_to_tez.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/demo_quipuswap/handlers/on_fa2_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
4 changes: 0 additions & 4 deletions src/demo_quipuswap/handlers/on_fa2_withdraw_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/dipdup/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/dipdup/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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


Expand Down Expand Up @@ -69,6 +69,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."""

Expand All @@ -77,13 +89,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

Expand Down

0 comments on commit 1113daa

Please sign in to comment.