|
2 | 2 | from typing import cast |
3 | 3 | from uuid import uuid4 |
4 | 4 |
|
5 | | -from sqlalchemy.ext.asyncio import AsyncEngine |
| 5 | +from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker |
6 | 6 |
|
7 | | -from app.application.models import ( # noqa: F401 |
8 | | - Application, |
9 | | - ReviewImage, |
10 | | - ReviewSubmission, |
11 | | -) |
12 | | -from app.auth.models import EmailVerification, User # noqa: F401 |
13 | | -from app.blockchain.service_impl import BlockchainServiceImpl |
14 | | -from app.core.config import settings |
| 7 | +from app.auth.models import User |
| 8 | +from app.blockchain.service import BlockchainService |
15 | 9 | from app.core.security import get_password_hash |
16 | 10 | from app.db.base import Base |
17 | | -from app.db.session import AsyncSessionLocal |
18 | 11 | from app.event.models import Event |
19 | 12 | from app.store.models import Store |
20 | 13 |
|
21 | 14 | logger = logging.getLogger(__name__) |
22 | 15 |
|
23 | 16 |
|
24 | | -async def _deploy_or_none() -> str | None: |
25 | | - if not settings.blockchain_rpc_url or not settings.server_private_key: |
26 | | - return None |
27 | | - try: |
28 | | - return await BlockchainServiceImpl().deploy_contract() |
29 | | - except Exception: |
30 | | - logger.exception("[SEED] contract deploy failed, skipping") |
31 | | - return None |
| 17 | +class Seeder: |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + engine: AsyncEngine, |
| 21 | + session_factory: async_sessionmaker[AsyncSession], |
| 22 | + blockchain_service: BlockchainService | None, |
| 23 | + ) -> None: |
| 24 | + self._engine = engine |
| 25 | + self._sessions = session_factory |
| 26 | + self._blockchain = blockchain_service |
32 | 27 |
|
| 28 | + async def run(self) -> None: |
| 29 | + await self._reset() |
| 30 | + await self._seed() |
33 | 31 |
|
34 | | -async def _fund_contract(contract_address: str, amount_wei: int) -> None: |
35 | | - try: |
36 | | - await BlockchainServiceImpl().fund_contract(contract_address, amount_wei) |
37 | | - except Exception: |
38 | | - logger.exception("[SEED] contract fund failed contract=%s", contract_address) |
| 32 | + async def _reset(self) -> None: |
| 33 | + async with self._engine.begin() as conn: |
| 34 | + await conn.run_sync(Base.metadata.drop_all) |
| 35 | + await conn.run_sync(Base.metadata.create_all) |
39 | 36 |
|
| 37 | + async def _deploy_contract(self) -> str | None: |
| 38 | + if not self._blockchain: |
| 39 | + return None |
| 40 | + try: |
| 41 | + return await self._blockchain.deploy_contract() |
| 42 | + except Exception: |
| 43 | + logger.exception("[SEED] contract deploy failed, skipping") |
| 44 | + return None |
40 | 45 |
|
41 | | -async def reset_and_seed(engine: AsyncEngine) -> None: |
42 | | - async with engine.begin() as conn: |
43 | | - await conn.run_sync(Base.metadata.drop_all) |
44 | | - await conn.run_sync(Base.metadata.create_all) |
| 46 | + async def _fund_contract(self, contract_address: str, amount_wei: int) -> None: |
| 47 | + if not self._blockchain: |
| 48 | + return |
| 49 | + try: |
| 50 | + await self._blockchain.fund_contract(contract_address, amount_wei) |
| 51 | + except Exception: |
| 52 | + logger.exception( |
| 53 | + "[SEED] contract fund failed contract=%s", contract_address |
| 54 | + ) |
45 | 55 |
|
46 | | - async with AsyncSessionLocal() as session: |
47 | | - # --- ์ฌ์ฅ๋ ๊ณ์ --- |
48 | | - owner1 = User( |
49 | | - id=uuid4(), |
50 | | - username="๊น์ฒ ์", |
51 | | - email="owner1@test.com", |
52 | | - password_hash=get_password_hash("password123"), |
53 | | - role="OWNER", |
54 | | - ) |
55 | | - owner2 = User( |
56 | | - id=uuid4(), |
57 | | - username="์ด์ํฌ", |
58 | | - email="owner2@test.com", |
59 | | - password_hash=get_password_hash("password123"), |
60 | | - role="OWNER", |
61 | | - ) |
62 | | - session.add_all([owner1, owner2]) |
63 | | - await session.flush() |
| 56 | + async def _seed(self) -> None: |
| 57 | + async with self._sessions() as session: |
| 58 | + owner1 = User( |
| 59 | + id=uuid4(), |
| 60 | + username="๊น์ฒ ์", |
| 61 | + email="owner1@test.com", |
| 62 | + password_hash=get_password_hash("password123"), |
| 63 | + role="OWNER", |
| 64 | + ) |
| 65 | + owner2 = User( |
| 66 | + id=uuid4(), |
| 67 | + username="์ด์ํฌ", |
| 68 | + email="owner2@test.com", |
| 69 | + password_hash=get_password_hash("password123"), |
| 70 | + role="OWNER", |
| 71 | + ) |
| 72 | + session.add_all([owner1, owner2]) |
| 73 | + await session.flush() |
64 | 74 |
|
65 | | - # --- ๊ฐ๊ฒ --- |
66 | | - store1 = Store( |
67 | | - id=uuid4(), |
68 | | - name="๋ง์๋ ์ผ๊ฒน์ด", |
69 | | - address="์์ธ์ ๊ฐ๋จ๊ตฌ ํ
ํค๋๋ก 1๊ธธ 10", |
70 | | - category="RESTAURANT", |
71 | | - description="์ ์ ํ ๊ตญ๋ด์ฐ ๋ผ์ง๊ณ ๊ธฐ ์ ๋ฌธ์ ", |
72 | | - owner_id=owner1.id, |
73 | | - ) |
74 | | - store2 = Store( |
75 | | - id=uuid4(), |
76 | | - name="์นดํ ๋ธ๋ฃจ์", |
77 | | - address="์์ธ์ ๋งํฌ๊ตฌ ํ์ต๋ก 5๊ธธ 3", |
78 | | - category="CAFE", |
79 | | - description="์คํ์
ํฐ ์๋๋ฅผ ์ง์ ๋ก์คํ
ํ๋ ์นดํ", |
80 | | - owner_id=owner1.id, |
81 | | - ) |
82 | | - store3 = Store( |
83 | | - id=uuid4(), |
84 | | - name="์์ด๋ธ ํจ์
", |
85 | | - address="์์ธ์ ์ค๊ตฌ ๋ช
๋๊ธธ 20", |
86 | | - category="FASHION", |
87 | | - description="ํธ๋ ๋ํ ์ฌ์ฑ ์๋ฅ ํธ์ง์ต", |
88 | | - owner_id=owner2.id, |
89 | | - ) |
90 | | - store4 = Store( |
91 | | - id=uuid4(), |
92 | | - name="๊ธ๋ก์ฐ ๋ทฐํฐ์ด๋กฑ", |
93 | | - address="์์ธ์ ์์ด๊ตฌ ๋ฐํฌ๋๋ก 8๊ธธ 15", |
94 | | - category="BEAUTY", |
95 | | - description="ํผ๋ถ ๊ด๋ฆฌ ๋ฐ ๋ค์ผ ์ ๋ฌธ ๋ทฐํฐ์ต", |
96 | | - owner_id=owner2.id, |
97 | | - ) |
98 | | - session.add_all([store1, store2, store3, store4]) |
99 | | - await session.flush() |
| 75 | + store1 = Store( |
| 76 | + id=uuid4(), |
| 77 | + name="๋ง์๋ ์ผ๊ฒน์ด", |
| 78 | + address="์์ธ์ ๊ฐ๋จ๊ตฌ ํ
ํค๋๋ก 1๊ธธ 10", |
| 79 | + category="RESTAURANT", |
| 80 | + description="์ ์ ํ ๊ตญ๋ด์ฐ ๋ผ์ง๊ณ ๊ธฐ ์ ๋ฌธ์ ", |
| 81 | + owner_id=owner1.id, |
| 82 | + ) |
| 83 | + store2 = Store( |
| 84 | + id=uuid4(), |
| 85 | + name="์นดํ ๋ธ๋ฃจ์", |
| 86 | + address="์์ธ์ ๋งํฌ๊ตฌ ํ์ต๋ก 5๊ธธ 3", |
| 87 | + category="CAFE", |
| 88 | + description="์คํ์
ํฐ ์๋๋ฅผ ์ง์ ๋ก์คํ
ํ๋ ์นดํ", |
| 89 | + owner_id=owner1.id, |
| 90 | + ) |
| 91 | + store3 = Store( |
| 92 | + id=uuid4(), |
| 93 | + name="์์ด๋ธ ํจ์
", |
| 94 | + address="์์ธ์ ์ค๊ตฌ ๋ช
๋๊ธธ 20", |
| 95 | + category="FASHION", |
| 96 | + description="ํธ๋ ๋ํ ์ฌ์ฑ ์๋ฅ ํธ์ง์ต", |
| 97 | + owner_id=owner2.id, |
| 98 | + ) |
| 99 | + store4 = Store( |
| 100 | + id=uuid4(), |
| 101 | + name="๊ธ๋ก์ฐ ๋ทฐํฐ์ด๋กฑ", |
| 102 | + address="์์ธ์ ์์ด๊ตฌ ๋ฐํฌ๋๋ก 8๊ธธ 15", |
| 103 | + category="BEAUTY", |
| 104 | + description="ํผ๋ถ ๊ด๋ฆฌ ๋ฐ ๋ค์ผ ์ ๋ฌธ ๋ทฐํฐ์ต", |
| 105 | + owner_id=owner2.id, |
| 106 | + ) |
| 107 | + session.add_all([store1, store2, store3, store4]) |
| 108 | + await session.flush() |
100 | 109 |
|
101 | | - # --- ์ด๋ฒคํธ (๊ฐ๊ฐ ์ปจํธ๋ํธ ๋ฐฐํฌ) --- |
102 | | - event_specs = [ |
103 | | - dict( |
104 | | - title="์ผ๊ฒน์ด ๋ง์ง ๋ธ๋ก๊ทธ ๋ฆฌ๋ทฐ ๋ชจ์ง", |
105 | | - condition="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์ฌ์ง 5์ฅ ์ด์, 300์ ์ด์ ๋ฆฌ๋ทฐ ์์ฑ", |
106 | | - reward=10_000_000_000_000_000, # 0.01 ETH |
107 | | - is_active=True, |
108 | | - store_id=store1.id, |
109 | | - ), |
110 | | - dict( |
111 | | - title="์ผ๊ฒน์ด์ง ์ธ์คํ๊ทธ๋จ ๋ฆฌ๋ทฐ", |
112 | | - condition="์ธ์คํ๊ทธ๋จ์ ํด์ํ๊ทธ #๋ง์๋์ผ๊ฒน์ด ํฌํจ ๊ฒ์๋ฌผ ์
๋ก๋", |
113 | | - reward=5_000_000_000_000_000, # 0.005 ETH |
114 | | - is_active=True, |
115 | | - store_id=store1.id, |
116 | | - ), |
117 | | - dict( |
118 | | - title="์นดํ ๋ธ๋ฃจ์ ์๋ฃ ๋ฆฌ๋ทฐ", |
119 | | - condition="๋ค์ด๋ฒ ์ง๋ ๋ฆฌ๋ทฐ ์์ฑ", |
120 | | - reward=3_000_000_000_000_000, # 0.003 ETH |
121 | | - is_active=True, |
122 | | - store_id=store2.id, |
123 | | - ), |
124 | | - dict( |
125 | | - title="์์ด๋ธ ํจ์
์คํ์ผ๋ง ํ๊ธฐ", |
126 | | - condition="๊ตฌ๋งค ํ ์ฐฉ์ฉ์ท๊ณผ ํจ๊ป SNS ๊ฒ์", |
127 | | - reward=15_000_000_000_000_000, # 0.015 ETH |
128 | | - is_active=True, |
129 | | - store_id=store3.id, |
130 | | - ), |
131 | | - dict( |
132 | | - title="๊ธ๋ก์ฐ ๋ทฐํฐ์ด๋กฑ ์์ ํ๊ธฐ", |
133 | | - condition="์นด์นด์ค๋งต ๋ฆฌ๋ทฐ 200์ ์ด์ + ์ฌ์ง 3์ฅ ์ด์ ์ฒจ๋ถ", |
134 | | - reward=8_000_000_000_000_000, # 0.008 ETH |
135 | | - is_active=False, |
136 | | - store_id=store4.id, |
137 | | - ), |
138 | | - ] |
139 | | - for spec in event_specs: |
140 | | - contract_address = await _deploy_or_none() |
141 | | - if contract_address: |
142 | | - await _fund_contract(contract_address, cast(int, spec["reward"])) |
143 | | - session.add(Event(id=uuid4(), contract_address=contract_address, **spec)) |
144 | | - await session.commit() |
| 110 | + event_specs = [ |
| 111 | + dict( |
| 112 | + title="์ผ๊ฒน์ด ๋ง์ง ๋ธ๋ก๊ทธ ๋ฆฌ๋ทฐ ๋ชจ์ง", |
| 113 | + condition="๋ค์ด๋ฒ ๋ธ๋ก๊ทธ ์ฌ์ง 5์ฅ ์ด์, 300์ ์ด์ ๋ฆฌ๋ทฐ ์์ฑ", |
| 114 | + reward=10_000_000_000_000_000, # 0.01 ETH |
| 115 | + is_active=True, |
| 116 | + store_id=store1.id, |
| 117 | + ), |
| 118 | + dict( |
| 119 | + title="์ผ๊ฒน์ด์ง ์ธ์คํ๊ทธ๋จ ๋ฆฌ๋ทฐ", |
| 120 | + condition="์ธ์คํ๊ทธ๋จ์ ํด์ํ๊ทธ #๋ง์๋์ผ๊ฒน์ด ํฌํจ ๊ฒ์๋ฌผ ์
๋ก๋", |
| 121 | + reward=5_000_000_000_000_000, # 0.005 ETH |
| 122 | + is_active=True, |
| 123 | + store_id=store1.id, |
| 124 | + ), |
| 125 | + dict( |
| 126 | + title="์นดํ ๋ธ๋ฃจ์ ์๋ฃ ๋ฆฌ๋ทฐ", |
| 127 | + condition="๋ค์ด๋ฒ ์ง๋ ๋ฆฌ๋ทฐ ์์ฑ", |
| 128 | + reward=3_000_000_000_000_000, # 0.003 ETH |
| 129 | + is_active=True, |
| 130 | + store_id=store2.id, |
| 131 | + ), |
| 132 | + dict( |
| 133 | + title="์์ด๋ธ ํจ์
์คํ์ผ๋ง ํ๊ธฐ", |
| 134 | + condition="๊ตฌ๋งค ํ ์ฐฉ์ฉ์ท๊ณผ ํจ๊ป SNS ๊ฒ์", |
| 135 | + reward=15_000_000_000_000_000, # 0.015 ETH |
| 136 | + is_active=True, |
| 137 | + store_id=store3.id, |
| 138 | + ), |
| 139 | + dict( |
| 140 | + title="๊ธ๋ก์ฐ ๋ทฐํฐ์ด๋กฑ ์์ ํ๊ธฐ", |
| 141 | + condition="์นด์นด์ค๋งต ๋ฆฌ๋ทฐ 200์ ์ด์ + ์ฌ์ง 3์ฅ ์ด์ ์ฒจ๋ถ", |
| 142 | + reward=8_000_000_000_000_000, # 0.008 ETH |
| 143 | + is_active=False, |
| 144 | + store_id=store4.id, |
| 145 | + ), |
| 146 | + ] |
| 147 | + for spec in event_specs: |
| 148 | + contract_address = await self._deploy_contract() |
| 149 | + if contract_address: |
| 150 | + await self._fund_contract( |
| 151 | + contract_address, cast(int, spec["reward"]) |
| 152 | + ) |
| 153 | + session.add( |
| 154 | + Event(id=uuid4(), contract_address=contract_address, **spec) |
| 155 | + ) |
| 156 | + await session.commit() |
0 commit comments