|
1 |
| -import asyncio |
2 |
| -from typing import List, Optional, Union |
| 1 | +from typing import Optional |
3 | 2 |
|
4 | 3 | import pytest
|
5 | 4 |
|
6 | 5 | from starknet_py.devnet_utils.devnet_client import DevnetClient
|
7 |
| -from starknet_py.hash.selector import get_selector_from_name |
8 |
| -from starknet_py.net.account.base_account import BaseAccount |
9 |
| -from starknet_py.net.client_models import ( |
10 |
| - BlockHeader, |
11 |
| - Call, |
12 |
| - EmittedEvent, |
13 |
| - StarknetBlock, |
14 |
| - TransactionExecutionStatus, |
15 |
| - TransactionStatus, |
16 |
| -) |
| 6 | +from starknet_py.net.client_models import BlockHeader, StarknetBlock |
17 | 7 | from starknet_py.net.full_node_client import FullNodeClient
|
18 |
| -from starknet_py.net.models import StarknetChainId |
19 | 8 | from starknet_py.net.websockets.errors import WebsocketClientError
|
20 |
| -from starknet_py.net.websockets.models import ( |
21 |
| - NewEventsNotification, |
22 |
| - NewHeadsNotification, |
23 |
| - NewTransactionStatus, |
24 |
| - PendingTransactionsNotification, |
25 |
| - ReorgData, |
26 |
| - ReorgNotification, |
27 |
| - Transaction, |
28 |
| - TransactionStatusNotification, |
29 |
| -) |
| 9 | +from starknet_py.net.websockets.models import NewHeadsNotification |
30 | 10 | from starknet_py.net.websockets.websocket_client import WebsocketClient
|
31 |
| -from starknet_py.tests.e2e.fixtures.constants import MAX_RESOURCE_BOUNDS |
32 |
| - |
33 |
| - |
34 |
| -@pytest.mark.asyncio |
35 |
| -async def test_connect_and_disconnect(devnet_ws: str): |
36 |
| - websocket_client = WebsocketClient(devnet_ws) |
37 |
| - assert not await websocket_client.is_connected |
38 |
| - |
39 |
| - await websocket_client.connect() |
40 |
| - assert await websocket_client.is_connected |
41 |
| - |
42 |
| - await websocket_client.disconnect() |
43 |
| - assert not await websocket_client.is_connected |
44 |
| - |
45 |
| - |
46 |
| -@pytest.mark.asyncio |
47 |
| -async def test_subscribe_new_heads( |
48 |
| - websocket_client: WebsocketClient, |
49 |
| - devnet_client: DevnetClient, |
50 |
| -): |
51 |
| - received_block: Optional[BlockHeader] = None |
52 |
| - |
53 |
| - def handler(new_heads_notification: NewHeadsNotification): |
54 |
| - nonlocal received_block |
55 |
| - received_block = new_heads_notification.result |
56 |
| - |
57 |
| - subscription_id = await websocket_client.subscribe_new_heads(handler=handler) |
58 |
| - |
59 |
| - new_block_hash = await devnet_client.create_block() |
60 |
| - |
61 |
| - assert received_block is not None |
62 |
| - assert int(new_block_hash, 16) == received_block.block_hash |
63 |
| - |
64 |
| - unsubscribe_result = await websocket_client.unsubscribe(subscription_id) |
65 |
| - assert unsubscribe_result is True |
66 | 11 |
|
67 | 12 |
|
68 | 13 | @pytest.mark.asyncio
|
@@ -156,164 +101,6 @@ async def test_subscribe_new_heads_too_many_blocks_back(
|
156 | 101 | )
|
157 | 102 |
|
158 | 103 |
|
159 |
| -@pytest.mark.asyncio |
160 |
| -async def test_subscribe_events( |
161 |
| - websocket_client: WebsocketClient, |
162 |
| - deployed_balance_contract, |
163 |
| - argent_account_v040: BaseAccount, |
164 |
| -): |
165 |
| - emitted_events: List[EmittedEvent] = [] |
166 |
| - |
167 |
| - def handler(new_events_notification: NewEventsNotification): |
168 |
| - nonlocal emitted_events |
169 |
| - emitted_events.append(new_events_notification.result) |
170 |
| - |
171 |
| - subscription_id = await websocket_client.subscribe_events( |
172 |
| - handler=handler, from_address=argent_account_v040.address |
173 |
| - ) |
174 |
| - |
175 |
| - increase_balance_call = Call( |
176 |
| - to_addr=deployed_balance_contract.address, |
177 |
| - selector=get_selector_from_name("increase_balance"), |
178 |
| - calldata=[100], |
179 |
| - ) |
180 |
| - execute = await argent_account_v040.execute_v3( |
181 |
| - calls=increase_balance_call, resource_bounds=MAX_RESOURCE_BOUNDS |
182 |
| - ) |
183 |
| - await argent_account_v040.client.wait_for_tx(tx_hash=execute.transaction_hash) |
184 |
| - await argent_account_v040.client.get_transaction_receipt( |
185 |
| - tx_hash=execute.transaction_hash |
186 |
| - ) |
187 |
| - |
188 |
| - assert len(emitted_events) > 0 |
189 |
| - for emitted_event in emitted_events: |
190 |
| - assert emitted_event.from_address == argent_account_v040.address |
191 |
| - |
192 |
| - unsubscribe_result = await websocket_client.unsubscribe(subscription_id) |
193 |
| - assert unsubscribe_result is True |
194 |
| - |
195 |
| - |
196 |
| -@pytest.mark.asyncio |
197 |
| -async def test_subscribe_transaction_status( |
198 |
| - websocket_client: WebsocketClient, |
199 |
| - deployed_balance_contract, |
200 |
| - argent_account_v040: BaseAccount, |
201 |
| -): |
202 |
| - new_transaction_status: Optional[NewTransactionStatus] = None |
203 |
| - |
204 |
| - def handler(transaction_status_notification: TransactionStatusNotification): |
205 |
| - nonlocal new_transaction_status |
206 |
| - new_transaction_status = transaction_status_notification.result |
207 |
| - |
208 |
| - increase_balance_call = Call( |
209 |
| - to_addr=deployed_balance_contract.address, |
210 |
| - selector=get_selector_from_name("increase_balance"), |
211 |
| - calldata=[100], |
212 |
| - ) |
213 |
| - execute = await argent_account_v040.execute_v3( |
214 |
| - calls=increase_balance_call, resource_bounds=MAX_RESOURCE_BOUNDS |
215 |
| - ) |
216 |
| - |
217 |
| - subscription_id = await websocket_client.subscribe_transaction_status( |
218 |
| - handler=handler, transaction_hash=execute.transaction_hash |
219 |
| - ) |
220 |
| - |
221 |
| - await argent_account_v040.client.wait_for_tx(tx_hash=execute.transaction_hash) |
222 |
| - await argent_account_v040.client.get_transaction_receipt( |
223 |
| - tx_hash=execute.transaction_hash |
224 |
| - ) |
225 |
| - |
226 |
| - await asyncio.sleep(5) |
227 |
| - |
228 |
| - assert new_transaction_status is not None |
229 |
| - assert new_transaction_status.transaction_hash == execute.transaction_hash |
230 |
| - assert ( |
231 |
| - new_transaction_status.status.finality_status |
232 |
| - == TransactionStatus.ACCEPTED_ON_L2 |
233 |
| - ) |
234 |
| - assert ( |
235 |
| - new_transaction_status.status.execution_status |
236 |
| - == TransactionExecutionStatus.SUCCEEDED |
237 |
| - ) |
238 |
| - assert new_transaction_status.status.failure_reason is None |
239 |
| - |
240 |
| - unsubscribe_result = await websocket_client.unsubscribe(subscription_id) |
241 |
| - assert unsubscribe_result is True |
242 |
| - |
243 |
| - |
244 |
| -@pytest.mark.asyncio |
245 |
| -async def test_subscribe_pending_transactions( |
246 |
| - websocket_client: WebsocketClient, |
247 |
| - deployed_balance_contract, |
248 |
| - argent_account_v040: BaseAccount, |
249 |
| -): |
250 |
| - pending_transactions: List[Union[int, Transaction]] = [] |
251 |
| - |
252 |
| - def handler(pending_transaction_notification: PendingTransactionsNotification): |
253 |
| - nonlocal pending_transactions |
254 |
| - pending_transactions.append(pending_transaction_notification.result) |
255 |
| - |
256 |
| - subscription_id = await websocket_client.subscribe_pending_transactions( |
257 |
| - handler=handler, |
258 |
| - sender_address=[argent_account_v040.address], |
259 |
| - ) |
260 |
| - |
261 |
| - increase_balance_call = Call( |
262 |
| - to_addr=deployed_balance_contract.address, |
263 |
| - selector=get_selector_from_name("increase_balance"), |
264 |
| - calldata=[100], |
265 |
| - ) |
266 |
| - execute = await argent_account_v040.execute_v3( |
267 |
| - calls=increase_balance_call, resource_bounds=MAX_RESOURCE_BOUNDS |
268 |
| - ) |
269 |
| - |
270 |
| - await argent_account_v040.client.wait_for_tx(tx_hash=execute.transaction_hash) |
271 |
| - await argent_account_v040.client.get_transaction_receipt( |
272 |
| - tx_hash=execute.transaction_hash |
273 |
| - ) |
274 |
| - |
275 |
| - assert len(pending_transactions) == 1 |
276 |
| - pending_transaction = pending_transactions[0] |
277 |
| - |
278 |
| - transaction_hash = ( |
279 |
| - execute.transaction_hash |
280 |
| - if isinstance(pending_transaction, int) |
281 |
| - else pending_transaction.calculate_hash(StarknetChainId.SEPOLIA) |
282 |
| - ) |
283 |
| - assert execute.transaction_hash == transaction_hash |
284 |
| - |
285 |
| - unsubscribe_result = await websocket_client.unsubscribe(subscription_id) |
286 |
| - assert unsubscribe_result is True |
287 |
| - |
288 |
| - |
289 |
| -@pytest.mark.asyncio |
290 |
| -async def test_receive_reorg_notification( |
291 |
| - websocket_client: WebsocketClient, |
292 |
| - devnet_client: DevnetClient, |
293 |
| -): |
294 |
| - reorg_data: Optional[ReorgData] = None |
295 |
| - |
296 |
| - def handler_reorg(reorg_notification: ReorgNotification): |
297 |
| - nonlocal reorg_data |
298 |
| - reorg_data = reorg_notification.result |
299 |
| - |
300 |
| - subscription_id = await websocket_client.subscribe_new_heads(handler=lambda _: _) |
301 |
| - |
302 |
| - websocket_client.on_chain_reorg = handler_reorg |
303 |
| - new_block_hash = await devnet_client.create_block() |
304 |
| - |
305 |
| - await devnet_client.abort_block(block_hash=new_block_hash) |
306 |
| - |
307 |
| - await asyncio.sleep(5) |
308 |
| - |
309 |
| - assert reorg_data is not None |
310 |
| - assert reorg_data.starting_block_hash == int(new_block_hash, 16) |
311 |
| - assert reorg_data.ending_block_hash == int(new_block_hash, 16) |
312 |
| - |
313 |
| - unsubscribe_result = await websocket_client.unsubscribe(subscription_id) |
314 |
| - assert unsubscribe_result is True |
315 |
| - |
316 |
| - |
317 | 104 | @pytest.mark.asyncio
|
318 | 105 | async def test_unsubscribe_with_non_existing_id(
|
319 | 106 | websocket_client: WebsocketClient,
|
|
0 commit comments