starktop 0.8.1

A TUI block explorer for the Starknet blockchain
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
use async_trait::async_trait;
use starknet::core::types::requests::{CallRequest, GetStorageAtRequest};
use starknet::core::types::{
    AddressFilter, BlockId, BlockStatus, BlockTag, BlockWithTxs, ContractClass, DeclareTransaction,
    DeployAccountTransaction, EventFilter, ExecutionResult, Felt, FunctionCall, InvokeTransaction,
    MaybePreConfirmedBlockWithTxHashes, MaybePreConfirmedBlockWithTxs, StorageKey, Transaction,
    TransactionReceipt, TransactionTrace,
};
use starknet::core::utils::get_contract_address;
use starknet::providers::{
    JsonRpcClient, Provider, ProviderRequestData, ProviderResponseData, jsonrpc::HttpTransport,
};
use tracing::{debug, warn};
use url::Url;

use super::DataSource;
use super::types::*;
use crate::error::{Result, StarktopError};
use crate::utils::felt_to_u128;

/// RPC-based data source using starknet-rust Provider.
pub struct RpcDataSource {
    provider: JsonRpcClient<HttpTransport>,
    rpc_url: String,
}

impl RpcDataSource {
    pub fn new(rpc_url: &str) -> Self {
        let provider = JsonRpcClient::new(HttpTransport::new(
            Url::parse(rpc_url).expect("invalid RPC URL"),
        ));
        Self {
            provider,
            rpc_url: rpc_url.to_string(),
        }
    }

    /// Shared helper: find the starting block with an expanding-window search, then
    /// paginate through matching events newest-first.
    ///
    /// Unlike `starknet_getEvents` (which returns oldest-first and has no
    /// reverse flag), this walks the range in fixed windows from `latest`
    /// downward. Each window is fully paginated oldest-first, then reversed
    /// locally, so the caller sees the *globally newest* events first and we
    /// stop as soon as `limit` is satisfied.
    async fn fetch_events_paginated(
        &self,
        address: Felt,
        from_block: Option<u64>,
        to_block: Option<u64>,
        limit: usize,
        keys: Option<Vec<Vec<Felt>>>,
        debug_label: &str,
    ) -> Result<Vec<SnEvent>> {
        let latest = self
            .provider
            .block_number()
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;
        // Upper bound for the reverse walk. Caller-specified `to_block` is
        // clamped to `latest` so requests for future blocks don't error out.
        let upper = to_block.map(|b| b.min(latest)).unwrap_or(latest);

        // Resolve the lower bound of the search range.
        // - Caller-provided: use as-is (cache-driven incremental).
        // - Otherwise: expanding-window probe from the tip to find the oldest
        //   window that still has events, so we don't walk the whole chain.
        let lower = if let Some(fb) = from_block {
            fb
        } else {
            let windows = [50_000u64, 200_000, 1_000_000, latest];
            let mut found_from = 0;
            for window in windows {
                let test_from = upper.saturating_sub(window);
                let test_filter = EventFilter {
                    from_block: Some(BlockId::Number(test_from)),
                    to_block: Some(BlockId::Number(upper)),
                    address: Some(AddressFilter::Single(address)),
                    keys: keys.clone(),
                };
                match self.provider.get_events(test_filter, None, 1).await {
                    Ok(page) if !page.events.is_empty() => {
                        found_from = test_from;
                        debug!(window, found_from, "Found events in block window");
                        break;
                    }
                    _ => continue,
                }
            }
            found_from
        };

        // Walk backwards in fixed windows, newest-first.
        // Window size is a trade-off: larger = fewer RPC calls on dense
        // addresses; smaller = less overfetch on sparse ones. 10k matches the
        // app's cold-start window for contracts in network/address.rs.
        const WINDOW: u64 = 10_000;
        const CHUNK_SIZE: u64 = 1000;

        let mut collected: Vec<SnEvent> = Vec::new();
        let mut to = upper;

        'outer: while to >= lower && collected.len() < limit {
            let win_from = lower.max(to.saturating_sub(WINDOW));

            // Fully paginate this window (oldest-first per RPC contract).
            let mut window_events: Vec<SnEvent> = Vec::new();
            let mut continuation_token: Option<String> = None;
            let filter = EventFilter {
                from_block: Some(BlockId::Number(win_from)),
                to_block: Some(BlockId::Number(to)),
                address: Some(AddressFilter::Single(address)),
                keys: keys.clone(),
            };

            // Per-block event index — reset every time the block changes so
            // (tx_hash, block_number, event_index) is a stable dedup key
            // matching what pathfinder emits.
            let mut prev_block: u64 = u64::MAX;
            let mut idx_in_block: u64 = 0;

            loop {
                let page = self
                    .provider
                    .get_events(filter.clone(), continuation_token.clone(), CHUNK_SIZE)
                    .await
                    .map_err(|e| StarktopError::Provider(e.to_string()))?;

                for e in &page.events {
                    let bn = e.block_number.unwrap_or(0);
                    if bn != prev_block {
                        idx_in_block = 0;
                        prev_block = bn;
                    }
                    window_events.push(SnEvent {
                        from_address: e.from_address,
                        keys: e.keys.clone(),
                        data: e.data.clone(),
                        transaction_hash: e.transaction_hash,
                        block_number: bn,
                        event_index: idx_in_block,
                    });
                    idx_in_block += 1;
                }

                match page.continuation_token {
                    Some(token) => continuation_token = Some(token),
                    None => break,
                }
            }

            // Reverse within-window events so this window's newest come first,
            // then append to the (globally newest-first) accumulator.
            window_events.reverse();
            collected.extend(window_events);

            if collected.len() >= limit {
                break 'outer;
            }
            if win_from == lower {
                break;
            }
            to = win_from.saturating_sub(1);
        }

        debug!(total = collected.len(), lower, "{debug_label}");
        // Trim to limit (windows may overshoot if the final window is dense).
        collected.truncate(limit);
        Ok(collected)
    }
}

#[async_trait]
impl DataSource for RpcDataSource {
    async fn get_latest_block_number(&self) -> Result<u64> {
        self.provider
            .block_number()
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))
    }

    async fn get_block(&self, number: u64) -> Result<SnBlock> {
        let block = self
            .provider
            .get_block_with_txs(BlockId::Number(number), None)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;

        match block {
            MaybePreConfirmedBlockWithTxs::Block(b) => Ok(convert_block(&b)),
            MaybePreConfirmedBlockWithTxs::PreConfirmedBlock(b) => {
                Ok(convert_block_from_pre_confirmed(&b))
            }
        }
    }

    async fn get_block_by_hash(&self, hash: Felt) -> Result<u64> {
        let block = self
            .provider
            .get_block_with_txs(BlockId::Hash(hash), None)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;

        match block {
            MaybePreConfirmedBlockWithTxs::Block(b) => Ok(b.block_number),
            MaybePreConfirmedBlockWithTxs::PreConfirmedBlock(_) => Err(StarktopError::Provider(
                "Block is pre-confirmed".to_string(),
            )),
        }
    }

    async fn get_block_with_txs(&self, number: u64) -> Result<(SnBlock, Vec<SnTransaction>)> {
        let block = self
            .provider
            .get_block_with_txs(BlockId::Number(number), None)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;

        match block {
            MaybePreConfirmedBlockWithTxs::Block(b) => {
                let sn_block = convert_block(&b);
                let txs = b
                    .transactions
                    .iter()
                    .enumerate()
                    .map(|(i, tx)| convert_transaction(tx, b.block_number, i as u64))
                    .collect();
                Ok((sn_block, txs))
            }
            MaybePreConfirmedBlockWithTxs::PreConfirmedBlock(b) => {
                let sn_block = convert_block_from_pre_confirmed(&b);
                let txs = b
                    .transactions
                    .iter()
                    .enumerate()
                    .map(|(i, tx)| convert_transaction(tx, 0, i as u64))
                    .collect();
                Ok((sn_block, txs))
            }
        }
    }

    async fn get_block_status(&self, number: u64) -> Result<String> {
        // Lightweight finality probe: fetch the block header + tx hashes only
        // (no full tx bodies) just to read the status field.
        let block = self
            .provider
            .get_block_with_tx_hashes(BlockId::Number(number))
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;

        match block {
            MaybePreConfirmedBlockWithTxHashes::Block(b) => {
                Ok(block_status_str(&b.status).to_string())
            }
            MaybePreConfirmedBlockWithTxHashes::PreConfirmedBlock(_) => {
                Ok("PRE_CONFIRMED".to_string())
            }
        }
    }

    async fn get_transaction(&self, hash: Felt) -> Result<SnTransaction> {
        let tx = self
            .provider
            .get_transaction_by_hash(hash, None)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;

        Ok(convert_transaction(&tx, 0, 0))
    }

    async fn get_receipt(&self, hash: Felt) -> Result<SnReceipt> {
        let receipt_with_block = self
            .provider
            .get_transaction_receipt(hash)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;

        Ok(convert_receipt(
            &receipt_with_block.receipt,
            &receipt_with_block.block,
        ))
    }

    async fn get_nonce(&self, address: Felt) -> Result<Felt> {
        self.provider
            .get_nonce(BlockId::Tag(BlockTag::Latest), address)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))
    }

    async fn get_class_hash(&self, address: Felt) -> Result<Felt> {
        self.provider
            .get_class_hash_at(BlockId::Tag(BlockTag::Latest), address)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))
    }

    async fn get_class_hash_at(&self, address: Felt, block: u64) -> Result<Felt> {
        self.provider
            .get_class_hash_at(BlockId::Number(block), address)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))
    }

    async fn get_class(&self, class_hash: Felt) -> Result<ContractClass> {
        self.provider
            .get_class(BlockId::Tag(BlockTag::Latest), class_hash)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))
    }

    async fn get_storage_at(&self, contract: Felt, key: Felt, block: Option<u64>) -> Result<Felt> {
        let block_id = match block {
            Some(n) => BlockId::Number(n),
            None => BlockId::Tag(BlockTag::Latest),
        };
        // starknet-rust 0.19 added a 4th param for response flags (RPC v0.10
        // optional `INCLUDE_LAST_UPDATE_BLOCK` metadata). We don't need
        // metadata for the privacy-pool sync, so pass `None` and unwrap the
        // bare value via `GetStorageAtResult::value()`.
        let result = self
            .provider
            .get_storage_at(contract, key, block_id, None)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))?;
        Ok(result.value())
    }

    async fn get_trace(&self, hash: Felt) -> Result<TransactionTrace> {
        self.provider
            .trace_transaction(hash)
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))
    }

    async fn get_recent_blocks(&self, count: usize) -> Result<Vec<SnBlock>> {
        if count == 0 {
            return Ok(Vec::new());
        }
        let latest = self.get_latest_block_number().await?;
        debug!(latest, count, "RPC: fetching recent blocks");
        // count >= 1 here, so the subtraction can't underflow. The
        // previous `count as u64 - 1` wrapped when count==0 (silently
        // producing u64::MAX and then a saturating_sub to 0, which
        // tried to fetch every block from 0..=latest).
        let start = latest.saturating_sub((count as u64).saturating_sub(1));
        let mut blocks = Vec::with_capacity(count);

        // Fan out via `join_all` over `&self.provider` instead of building a
        // fresh `JsonRpcClient<HttpTransport>` (and therefore a fresh reqwest
        // connection pool) per block. With 30 blocks on a cold start the old
        // form did 30 separate TCP+TLS handshakes; this reuses one keep-alive
        // pool for all of them.
        let futs = (start..=latest).map(|num| async move {
            (
                num,
                self.provider
                    .get_block_with_txs(BlockId::Number(num), None)
                    .await,
            )
        });
        let mut results: Vec<_> = futures::future::join_all(futs).await;

        // Sort by block number descending (newest first)
        results.sort_by(|a, b| b.0.cmp(&a.0));

        for (_num, result) in results {
            match result {
                Ok(block) => {
                    let sn_block = match block {
                        MaybePreConfirmedBlockWithTxs::Block(b) => convert_block(&b),
                        MaybePreConfirmedBlockWithTxs::PreConfirmedBlock(b) => {
                            convert_block_from_pre_confirmed(&b)
                        }
                    };
                    blocks.push(sn_block);
                }
                Err(e) => {
                    tracing::warn!("Failed to fetch block: {e}");
                }
            }
        }

        Ok(blocks)
    }

    async fn get_events_for_address(
        &self,
        address: Felt,
        from_block: Option<u64>,
        to_block: Option<u64>,
        limit: usize,
    ) -> Result<Vec<SnEvent>> {
        // Use transaction_executed selector to get exactly 1 event per account tx.
        // This is emitted by every account contract on every invoke.
        let tx_executed_selector =
            Felt::from_hex(crate::data::pathfinder::TRANSACTION_EXECUTED_SELECTOR).unwrap();
        let keys = Some(vec![vec![tx_executed_selector]]);
        self.fetch_events_paginated(
            address,
            from_block,
            to_block,
            limit,
            keys,
            "Fetched events for address",
        )
        .await
    }

    async fn get_contract_events(
        &self,
        address: Felt,
        from_block: Option<u64>,
        to_block: Option<u64>,
        limit: usize,
    ) -> Result<Vec<SnEvent>> {
        // No key filter — get ALL events from this contract
        self.fetch_events_paginated(
            address,
            from_block,
            to_block,
            limit,
            None,
            "Fetched contract events",
        )
        .await
    }

    async fn get_transfer_events(
        &self,
        address: Felt,
        from_block: u64,
        to_block: u64,
    ) -> Result<Vec<TokenTransfer>> {
        const CHUNK_SIZE: u64 = 1000;
        let start = std::time::Instant::now();
        let mut out: Vec<TokenTransfer> = Vec::new();
        // Two keyed scans (address as `to`, then as `from`); no emitter filter
        // so every token contract is covered. Pathfinder serves these from its
        // per-key bloom index, so the cost is the block span, not the keys.
        for keys in crate::network::ws::transfer_key_filters(address) {
            let filter = EventFilter {
                from_block: Some(BlockId::Number(from_block)),
                to_block: Some(BlockId::Number(to_block)),
                address: None,
                keys: Some(keys),
            };
            let mut continuation_token: Option<String> = None;
            loop {
                let page = self
                    .provider
                    .get_events(filter.clone(), continuation_token.clone(), CHUNK_SIZE)
                    .await
                    .map_err(|e| StarktopError::Provider(e.to_string()))?;
                for e in &page.events {
                    let raw = SnEvent {
                        from_address: e.from_address,
                        keys: e.keys.clone(),
                        data: e.data.clone(),
                        transaction_hash: e.transaction_hash,
                        block_number: e.block_number.unwrap_or(0),
                        event_index: e.event_index,
                    };
                    if let Some(t) = TokenTransfer::from_event(&raw, e.transaction_index) {
                        out.push(t);
                    }
                }
                match page.continuation_token {
                    Some(token) => continuation_token = Some(token),
                    None => break,
                }
            }
        }
        // Self-transfers match both scans.
        out.sort_by(TokenTransfer::cmp_newest_first);
        out.dedup_by_key(|t| t.key());
        debug!(
            address = %format!("{:#x}", address),
            from_block,
            to_block,
            count = out.len(),
            elapsed_ms = start.elapsed().as_millis(),
            "Fetched transfer events"
        );
        Ok(out)
    }

    async fn call_contract(
        &self,
        contract_address: Felt,
        selector: Felt,
        calldata: Vec<Felt>,
    ) -> Result<Vec<Felt>> {
        let call = FunctionCall {
            contract_address,
            entry_point_selector: selector,
            calldata,
        };
        self.provider
            .call(call, BlockId::Tag(BlockTag::Latest))
            .await
            .map_err(|e| StarktopError::Provider(e.to_string()))
    }

    async fn batch_call_contracts(
        &self,
        calls: Vec<(Felt, Felt, Vec<Felt>)>,
    ) -> Vec<Result<Vec<Felt>>> {
        if calls.is_empty() {
            return Vec::new();
        }
        let requests: Vec<ProviderRequestData> = calls
            .iter()
            .map(|(contract_address, selector, calldata)| {
                ProviderRequestData::Call(CallRequest {
                    request: FunctionCall {
                        contract_address: *contract_address,
                        entry_point_selector: *selector,
                        calldata: calldata.clone(),
                    },
                    block_id: BlockId::Tag(BlockTag::Latest),
                })
            })
            .collect();

        match self.provider.batch_requests(&requests).await {
            Ok(responses) => responses
                .into_iter()
                .map(|resp| match resp {
                    ProviderResponseData::Call(v) => Ok(v),
                    _ => Err(StarktopError::Provider(
                        "unexpected response type in batch".into(),
                    )),
                })
                .collect(),
            Err(e) => {
                // `batch_requests` is all-or-nothing — if any single call in the
                // batch fails, the whole thing errors. Fall back to per-call
                // requests so one bad call (e.g., a contract without the
                // expected selector) doesn't sink the rest.
                warn!(error = %e, "batch_requests failed, falling back to sequential");
                let mut out = Vec::with_capacity(calls.len());
                for (contract, selector, calldata) in calls {
                    out.push(self.call_contract(contract, selector, calldata).await);
                }
                out
            }
        }
    }

    async fn batch_get_storage_at(
        &self,
        contract: Felt,
        keys: &[Felt],
        block: Option<u64>,
    ) -> Vec<Result<Felt>> {
        if keys.is_empty() {
            return Vec::new();
        }
        let block_id = match block {
            Some(n) => BlockId::Number(n),
            None => BlockId::Tag(BlockTag::Latest),
        };
        let requests: Vec<ProviderRequestData> = keys
            .iter()
            .map(|k| {
                ProviderRequestData::GetStorageAt(GetStorageAtRequest {
                    contract_address: contract,
                    key: StorageKey(format!("{:#x}", k)),
                    block_id,
                    response_flags: None,
                })
            })
            .collect();
        match self.provider.batch_requests(&requests).await {
            Ok(responses) => responses
                .into_iter()
                .map(|resp| match resp {
                    ProviderResponseData::GetStorageAt(v) => Ok(v.value()),
                    _ => Err(StarktopError::Provider(
                        "unexpected response type in storage batch".into(),
                    )),
                })
                .collect(),
            Err(e) => {
                // Batch is all-or-nothing — fall back to sequential per-key
                // reads so one bad slot (e.g., contract not deployed yet at
                // `block`) doesn't sink the whole batch.
                warn!(error = %e, "batch_requests (storage) failed, falling back to sequential");
                let mut out = Vec::with_capacity(keys.len());
                for k in keys {
                    out.push(self.get_storage_at(contract, *k, block).await);
                }
                out
            }
        }
    }
}

// --- Type conversion helpers ---

/// Map a starknet-rs `BlockStatus` to the canonical string representation
/// used throughout starktop (matches the `TransactionFinalityStatus` strings in
/// `convert_receipt`).
fn block_status_str(status: &BlockStatus) -> &'static str {
    match status {
        BlockStatus::AcceptedOnL1 => "ACCEPTED_ON_L1",
        BlockStatus::AcceptedOnL2 => "ACCEPTED_ON_L2",
        BlockStatus::PreConfirmed => "PRE_CONFIRMED",
    }
}

fn convert_block(b: &BlockWithTxs) -> SnBlock {
    SnBlock {
        number: b.block_number,
        hash: b.block_hash,
        parent_hash: b.parent_hash,
        timestamp: b.timestamp,
        sequencer_address: b.sequencer_address,
        transaction_count: b.transactions.len(),
        l1_gas_price_fri: felt_to_u128(&b.l1_gas_price.price_in_fri),
        l1_gas_price_wei: felt_to_u128(&b.l1_gas_price.price_in_wei),
        l2_gas_price_fri: felt_to_u128(&b.l2_gas_price.price_in_fri),
        l1_data_gas_price_fri: felt_to_u128(&b.l1_data_gas_price.price_in_fri),
        starknet_version: b.starknet_version.clone(),
        status: block_status_str(&b.status).to_string(),
    }
}

fn convert_block_from_pre_confirmed(
    b: &starknet::core::types::PreConfirmedBlockWithTxs,
) -> SnBlock {
    SnBlock {
        number: b.block_number,
        hash: Felt::ZERO,
        parent_hash: Felt::ZERO,
        timestamp: b.timestamp,
        sequencer_address: b.sequencer_address,
        transaction_count: b.transactions.len(),
        l1_gas_price_fri: felt_to_u128(&b.l1_gas_price.price_in_fri),
        l1_gas_price_wei: felt_to_u128(&b.l1_gas_price.price_in_wei),
        l2_gas_price_fri: felt_to_u128(&b.l2_gas_price.price_in_fri),
        l1_data_gas_price_fri: felt_to_u128(&b.l1_data_gas_price.price_in_fri),
        starknet_version: b.starknet_version.clone(),
        status: "PRE_CONFIRMED".to_string(),
    }
}

fn convert_resource_bounds(rb: &starknet::core::types::ResourceBoundsMapping) -> SnResourceBounds {
    SnResourceBounds {
        l1_gas_max_amount: rb.l1_gas.max_amount,
        l1_gas_max_price: rb.l1_gas.max_price_per_unit,
        l2_gas_max_amount: rb.l2_gas.max_amount,
        l2_gas_max_price: rb.l2_gas.max_price_per_unit,
        l1_data_gas_max_amount: rb.l1_data_gas.max_amount,
        l1_data_gas_max_price: rb.l1_data_gas.max_price_per_unit,
    }
}

fn convert_transaction(tx: &Transaction, block_number: u64, index: u64) -> SnTransaction {
    match tx {
        Transaction::Invoke(invoke) => {
            let (hash, sender, calldata, nonce, version, tip, rb) = match invoke {
                InvokeTransaction::V0(v) => (
                    v.transaction_hash,
                    v.contract_address,
                    v.calldata.clone(),
                    None,
                    Felt::ZERO,
                    0u64,
                    None,
                ),
                InvokeTransaction::V1(v) => (
                    v.transaction_hash,
                    v.sender_address,
                    v.calldata.clone(),
                    Some(v.nonce),
                    Felt::ONE,
                    0u64,
                    None,
                ),
                InvokeTransaction::V3(v) => (
                    v.transaction_hash,
                    v.sender_address,
                    v.calldata.clone(),
                    Some(v.nonce),
                    Felt::THREE,
                    v.tip,
                    Some(convert_resource_bounds(&v.resource_bounds)),
                ),
            };
            SnTransaction::Invoke(InvokeTx {
                hash,
                sender_address: sender,
                calldata,
                nonce,
                version,
                actual_fee: None,
                execution_status: ExecutionStatus::Unknown,
                block_number,
                index,
                tip,
                resource_bounds: rb,
            })
        }
        Transaction::Declare(declare) => {
            let (hash, sender, class_hash, version, tip, rb) = match declare {
                DeclareTransaction::V0(v) => (
                    v.transaction_hash,
                    v.sender_address,
                    v.class_hash,
                    Felt::ZERO,
                    0u64,
                    None,
                ),
                DeclareTransaction::V1(v) => (
                    v.transaction_hash,
                    v.sender_address,
                    v.class_hash,
                    Felt::ONE,
                    0u64,
                    None,
                ),
                DeclareTransaction::V2(v) => (
                    v.transaction_hash,
                    v.sender_address,
                    v.class_hash,
                    Felt::TWO,
                    0u64,
                    None,
                ),
                DeclareTransaction::V3(v) => (
                    v.transaction_hash,
                    v.sender_address,
                    v.class_hash,
                    Felt::THREE,
                    v.tip,
                    Some(convert_resource_bounds(&v.resource_bounds)),
                ),
            };
            SnTransaction::Declare(DeclareTx {
                hash,
                sender_address: sender,
                class_hash,
                version,
                actual_fee: None,
                execution_status: ExecutionStatus::Unknown,
                block_number,
                index,
                tip,
                resource_bounds: rb,
            })
        }
        Transaction::DeployAccount(deploy_account) => {
            let (hash, class_hash, calldata, salt, version, nonce, tip, rb) = match deploy_account {
                DeployAccountTransaction::V1(v) => (
                    v.transaction_hash,
                    v.class_hash,
                    v.constructor_calldata.clone(),
                    v.contract_address_salt,
                    Felt::ONE,
                    v.nonce,
                    0u64,
                    None,
                ),
                DeployAccountTransaction::V3(v) => (
                    v.transaction_hash,
                    v.class_hash,
                    v.constructor_calldata.clone(),
                    v.contract_address_salt,
                    Felt::THREE,
                    v.nonce,
                    v.tip,
                    Some(convert_resource_bounds(&v.resource_bounds)),
                ),
            };
            let contract_address = get_contract_address(salt, class_hash, &calldata, Felt::ZERO);
            SnTransaction::DeployAccount(DeployAccountTx {
                hash,
                contract_address,
                class_hash,
                constructor_calldata: calldata,
                contract_address_salt: salt,
                nonce: Some(nonce),
                version,
                actual_fee: None,
                execution_status: ExecutionStatus::Unknown,
                block_number,
                index,
                tip,
                resource_bounds: rb,
            })
        }
        Transaction::L1Handler(l1) => SnTransaction::L1Handler(L1HandlerTx {
            hash: l1.transaction_hash,
            contract_address: l1.contract_address,
            entry_point_selector: l1.entry_point_selector,
            calldata: l1.calldata.clone(),
            nonce: Some(Felt::from(l1.nonce)),
            actual_fee: None,
            execution_status: ExecutionStatus::Unknown,
            block_number,
            index,
        }),
        Transaction::Deploy(deploy) => SnTransaction::Deploy(DeployTx {
            hash: deploy.transaction_hash,
            contract_address: Felt::ZERO,
            class_hash: deploy.class_hash,
            constructor_calldata: deploy.constructor_calldata.clone(),
            version: deploy.version,
            actual_fee: None,
            execution_status: ExecutionStatus::Unknown,
            block_number,
            index,
        }),
    }
}

fn convert_receipt(
    receipt: &TransactionReceipt,
    block: &starknet::core::types::ReceiptBlock,
) -> SnReceipt {
    use starknet::core::types::PriceUnit;

    // Helper macro to extract common fields from receipt variants
    macro_rules! extract_receipt {
        ($r:expr) => {
            (
                $r.transaction_hash,
                $r.actual_fee.amount,
                match $r.actual_fee.unit {
                    PriceUnit::Fri => "STRK",
                    PriceUnit::Wei => "ETH",
                },
                &$r.events,
                &$r.execution_result,
                $r.execution_resources.clone(),
                match &$r.finality_status {
                    starknet::core::types::TransactionFinalityStatus::AcceptedOnL2 => {
                        "ACCEPTED_ON_L2"
                    }
                    starknet::core::types::TransactionFinalityStatus::AcceptedOnL1 => {
                        "ACCEPTED_ON_L1"
                    }
                    starknet::core::types::TransactionFinalityStatus::PreConfirmed => {
                        "PRE_CONFIRMED"
                    }
                },
            )
        };
    }

    let (tx_hash, actual_fee, fee_unit, events, exec_result, exec_resources, finality) =
        match receipt {
            TransactionReceipt::Invoke(r) => extract_receipt!(r),
            TransactionReceipt::Declare(r) => extract_receipt!(r),
            TransactionReceipt::DeployAccount(r) => extract_receipt!(r),
            TransactionReceipt::L1Handler(r) => extract_receipt!(r),
            TransactionReceipt::Deploy(r) => extract_receipt!(r),
        };

    let block_number = block.block_number();
    let block_hash = block.block_hash();

    let (status, revert_reason) = match exec_result {
        ExecutionResult::Succeeded => (ExecutionStatus::Succeeded, None),
        ExecutionResult::Reverted { reason } => (
            ExecutionStatus::Reverted(reason.clone()),
            Some(reason.clone()),
        ),
    };

    let sn_events = events
        .iter()
        .enumerate()
        .map(|(i, e)| SnEvent {
            from_address: e.from_address,
            keys: e.keys.clone(),
            data: e.data.clone(),
            transaction_hash: tx_hash,
            block_number: 0,
            event_index: i as u64,
        })
        .collect();

    SnReceipt {
        transaction_hash: tx_hash,
        block_number,
        block_hash,
        actual_fee,
        fee_unit: fee_unit.to_string(),
        execution_status: status,
        execution_resources: SnExecutionResources {
            l1_gas: exec_resources.l1_gas,
            l2_gas: exec_resources.l2_gas,
            l1_data_gas: exec_resources.l1_data_gas,
        },
        events: sn_events,
        revert_reason,
        finality: finality.to_string(),
    }
}