-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.rs
64 lines (56 loc) · 2.1 KB
/
utils.rs
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
use std::time::Duration;
use anyhow::Result;
use colored::Colorize;
use regex::Regex;
use starknet::{
core::types::{BlockId, BlockTag, FieldElement, StarknetError},
providers::{Provider, ProviderError},
};
pub async fn watch_tx<P>(provider: P, transaction_hash: FieldElement) -> Result<()>
where
P: Provider,
P::Error: 'static,
{
loop {
// TODO: check with sequencer gateway if it's not confirmed after an extended period of
// time, as full nodes don't have access to failed transactions and would report them
// as `NotReceived`.
match provider.get_transaction_receipt(transaction_hash).await {
Ok(_) => {
// With JSON-RPC, once we get a receipt, the transaction must have been confirmed.
// Rejected transactions simply aren't available. This needs to be changed once we
// implement the sequencer fallback.
eprintln!(
"Transaction {} confirmed",
format!("{:#064x}", transaction_hash).bright_yellow()
);
return Ok(());
}
Err(ProviderError::StarknetError(StarknetError::TransactionHashNotFound)) => {
eprintln!("Transaction not confirmed yet...");
}
Err(err) => return Err(err.into()),
}
tokio::time::sleep(Duration::from_secs(10)).await;
}
}
pub fn parse_block_id(id: &str) -> Result<BlockId> {
let regex_block_number = Regex::new("^[0-9]{1,}$").unwrap();
if id == "latest" {
Ok(BlockId::Tag(BlockTag::Latest))
} else if id == "pending" {
Ok(BlockId::Tag(BlockTag::Pending))
} else if regex_block_number.is_match(id) {
Ok(BlockId::Number(id.parse::<u64>()?))
} else {
Ok(BlockId::Hash(FieldElement::from_hex_be(id)?))
}
}
pub fn parse_felt_value(felt: &str) -> Result<FieldElement> {
let regex_dec_number = Regex::new("^[0-9]{1,}$").unwrap();
if regex_dec_number.is_match(felt) {
Ok(FieldElement::from_dec_str(felt)?)
} else {
Ok(FieldElement::from_hex_be(felt)?)
}
}