Skip to content

Commit 1d31bec

Browse files
Dynamic templates (#44)
1 parent 0566e61 commit 1d31bec

File tree

13 files changed

+543
-219
lines changed

13 files changed

+543
-219
lines changed

src/demo_registrydao/dipdup.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ database:
77

88
contracts:
99
registry:
10-
address: KT1QMdCTqzmY4QKHntV1nZEinLPU1GbxUFQu
10+
address: KT1R8AZn5KG7mkbnJ5bzMuUw2isL8tMYkDVD
1111
typename: registry
1212

1313
datasources:
@@ -39,5 +39,4 @@ templates:
3939
indexes:
4040
registry:
4141
template: registry_dao
42-
values:
43-
contract: registry
42+
similar_to: registry

src/demo_registrydao/types/registry/parameter/propose.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,11 @@
33

44
from __future__ import annotations
55

6-
from typing import List, Optional, Union
6+
from typing import Dict
77

88
from pydantic import BaseModel
99

1010

11-
class DiffItem(BaseModel):
12-
key: str
13-
new_value: Optional[str]
14-
15-
16-
class ProposalType0(BaseModel):
17-
agora_post_id: str
18-
diff: List[DiffItem]
19-
20-
21-
class ProposalMetadatum(BaseModel):
22-
proposal_type_0: ProposalType0
23-
24-
25-
class ProposalType1(BaseModel):
26-
frozen_scale_value: Optional[str]
27-
frozen_extra_value: Optional[str]
28-
slash_scale_value: Optional[str]
29-
slash_division_value: Optional[str]
30-
max_proposal_size: Optional[str]
31-
32-
33-
class ProposalMetadatum1(BaseModel):
34-
proposal_type_1: ProposalType1
35-
36-
37-
class ProposalMetadatum2(BaseModel):
38-
receivers_0: List[str]
39-
40-
41-
class ProposalMetadatum3(BaseModel):
42-
receivers_1: List[str]
43-
44-
4511
class ProposeParameter(BaseModel):
4612
frozen_token: str
47-
proposal_metadata: Union[ProposalMetadatum, ProposalMetadatum1, ProposalMetadatum2, ProposalMetadatum3]
13+
proposal_metadata: Dict[str, str]

src/demo_registrydao/types/registry/storage.py

Lines changed: 43 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33

44
from __future__ import annotations
55

6-
from typing import Any, Dict, List, Optional, Union
6+
from typing import Any, Dict, List
77

88
from pydantic import BaseModel
99

1010

11-
class Registry(BaseModel):
12-
affected_proposal_key: str
13-
last_updated: str
14-
value: Optional[str]
11+
class FreezeHistory(BaseModel):
12+
current_period_num: str
13+
current_unstaked: str
14+
past_unstaked: str
15+
staked: str
1516

1617

17-
class Extra(BaseModel):
18-
frozen_extra_value: str
19-
frozen_scale_value: str
20-
max_proposal_size: str
21-
proposal_receivers: List[str]
22-
registry: Dict[str, Registry]
23-
slash_division_value: str
24-
slash_scale_value: str
18+
class LastPeriodChange(BaseModel):
19+
changed_on: str
20+
period_num: str
2521

2622

2723
class Key(BaseModel):
@@ -34,16 +30,14 @@ class LedgerItem(BaseModel):
3430
value: str
3531

3632

37-
class MigrationStatu(BaseModel):
38-
notInMigration: Dict[str, Any]
33+
class MaxQuorumThreshold(BaseModel):
34+
denominator: str
35+
numerator: str
3936

4037

41-
class MigrationStatu1(BaseModel):
42-
migratingTo: str
43-
44-
45-
class MigrationStatu2(BaseModel):
46-
migratedTo: str
38+
class MinQuorumThreshold(BaseModel):
39+
denominator: str
40+
numerator: str
4741

4842

4943
class Key1(BaseModel):
@@ -61,67 +55,55 @@ class ProposalKeyListSortByDateItem(BaseModel):
6155
timestamp: str
6256

6357

64-
class DiffItem(BaseModel):
65-
key: str
66-
new_value: Optional[str]
67-
68-
69-
class ProposalType0(BaseModel):
70-
agora_post_id: str
71-
diff: List[DiffItem]
72-
73-
74-
class Metadatum(BaseModel):
75-
proposal_type_0: ProposalType0
76-
77-
78-
class ProposalType1(BaseModel):
79-
frozen_extra_value: Optional[str]
80-
frozen_scale_value: Optional[str]
81-
max_proposal_size: Optional[str]
82-
slash_division_value: Optional[str]
83-
slash_scale_value: Optional[str]
84-
85-
86-
class Metadatum1(BaseModel):
87-
proposal_type_1: ProposalType1
88-
89-
90-
class Metadatum2(BaseModel):
91-
receivers_0: List[str]
92-
93-
94-
class Metadatum3(BaseModel):
95-
receivers_1: List[str]
96-
97-
9858
class Voter(BaseModel):
99-
address: str
100-
nat: str
59+
vote_amount: str
60+
vote_type: bool
61+
voter_address: str
10162

10263

10364
class Proposals(BaseModel):
10465
downvotes: str
105-
metadata: Union[Metadatum, Metadatum1, Metadatum2, Metadatum3]
66+
metadata: Dict[str, str]
67+
period_num: str
10668
proposer: str
69+
proposer_fixed_fee_in_token: str
10770
proposer_frozen_token: str
10871
start_date: str
10972
upvotes: str
11073
voters: List[Voter]
11174

11275

76+
class QuorumThreshold(BaseModel):
77+
denominator: str
78+
numerator: str
79+
80+
11381
class RegistryStorage(BaseModel):
11482
admin: str
115-
extra: Extra
83+
custom_entrypoints: Dict[str, str]
84+
decision_lambda: str
85+
extra: Dict[str, str]
86+
fixed_proposal_fee_in_token: str
87+
freeze_history: Dict[str, FreezeHistory]
88+
frozen_token_id: str
89+
last_period_change: LastPeriodChange
11690
ledger: List[LedgerItem]
91+
max_proposals: str
92+
max_quorum_threshold: MaxQuorumThreshold
93+
max_votes: str
94+
max_voting_period: str
11795
metadata: Dict[str, str]
118-
migration_status: Union[MigrationStatu, MigrationStatu1, MigrationStatu2]
96+
min_quorum_threshold: MinQuorumThreshold
97+
min_voting_period: str
11998
operators: List[Operator]
12099
pending_owner: str
121100
permits_counter: str
101+
proposal_check: str
122102
proposal_key_list_sort_by_date: List[ProposalKeyListSortByDateItem]
123103
proposals: Dict[str, Proposals]
124-
quorum_threshold: str
104+
quorum_threshold: QuorumThreshold
105+
rejected_proposal_return_value: str
125106
token_address: str
126107
total_supply: Dict[str, str]
108+
unfrozen_token_id: str
127109
voting_period: str

src/dipdup/codegen.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from os import mkdir
77
from os.path import basename, dirname, exists, join, splitext
88
from shutil import rmtree
9-
from typing import Any, Dict
9+
from typing import Any, Dict, cast
1010

1111
from jinja2 import Template
1212

@@ -15,11 +15,12 @@
1515
BigMapIndexConfig,
1616
ContractConfig,
1717
DipDupConfig,
18-
IndexTemplateConfig,
18+
DynamicTemplateConfig,
1919
OperationHandlerConfig,
2020
OperationHandlerOriginationPatternConfig,
2121
OperationHandlerTransactionPatternConfig,
2222
OperationIndexConfig,
23+
StaticTemplateConfig,
2324
TzktDatasourceConfig,
2425
)
2526
from dipdup.datasources.tzkt.datasource import TzktDatasource
@@ -80,15 +81,27 @@ async def create_package(config: DipDupConfig):
8081
file.write(models_code)
8182

8283

83-
async def fetch_schemas(config: DipDupConfig):
84+
async def resolve_dynamic_templates(config: DipDupConfig) -> None:
85+
for index_name, index_config in config.indexes.items():
86+
if isinstance(index_config, DynamicTemplateConfig):
87+
config.indexes[index_name] = StaticTemplateConfig(
88+
template=index_config.template,
89+
values=dict(contract=cast(str, index_config.similar_to)),
90+
)
91+
config.pre_initialize()
92+
index_config = config.indexes[index_name]
93+
94+
95+
async def fetch_schemas(config: DipDupConfig) -> None:
8496
_logger.info('Creating `schemas` package')
8597
schemas_path = join(config.package_path, 'schemas')
8698
with suppress(FileExistsError):
8799
mkdir(schemas_path)
88100

89101
schemas_cache = SchemasCache()
90102

91-
for index_name, index_config in config.indexes.items():
103+
for index_config in config.indexes.values():
104+
92105
if isinstance(index_config, OperationIndexConfig):
93106
for operation_handler_config in index_config.handlers:
94107
for operation_pattern_config in operation_handler_config.pattern:
@@ -160,7 +173,12 @@ async def fetch_schemas(config: DipDupConfig):
160173
with suppress(FileExistsError):
161174
mkdir(big_map_schemas_path)
162175

163-
big_map_schema = next(ep for ep in contract_schemas['bigMaps'] if ep['path'] == big_map_pattern_config.path)
176+
try:
177+
big_map_schema = next(ep for ep in contract_schemas['bigMaps'] if ep['path'] == big_map_pattern_config.path)
178+
except StopIteration as e:
179+
raise ConfigurationError(
180+
f'Contract `{contract_config.address}` has no big map path `{big_map_pattern_config.path}`'
181+
) from e
164182
big_map_key_schema = big_map_schema['keySchema']
165183
big_map_key_schema_path = join(big_map_schemas_path, f'{big_map_pattern_config.path}.key.json')
166184

@@ -175,7 +193,7 @@ async def fetch_schemas(config: DipDupConfig):
175193
with open(big_map_value_schema_path, 'w') as file:
176194
file.write(json.dumps(big_map_value_schema, indent=4))
177195

178-
elif isinstance(index_config, IndexTemplateConfig):
196+
elif isinstance(index_config, StaticTemplateConfig):
179197
raise RuntimeError('Config is not initialized')
180198

181199
else:

0 commit comments

Comments
 (0)