Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template values wrapper #114

Merged
merged 3 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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