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
use crate::{Error, H256Ext};
/// Helpers that convert between RPC-compatible hash identifiers.
pub mod hash_string_number {
use super::*;
use crate::chain::Chain;
use avail_rust_core::{H256, HashNumber, types::HashStringNumber};
/// Resolves a [`HashStringNumber`] into the concrete block hash.
///
/// # Arguments
/// * `c` - Chain helper used to resolve block numbers to hashes when needed.
/// * `value` - Hash, string, or height describing the target block.
///
/// # Returns
/// Returns the resolved block hash (`H256`) or an error if the lookup fails.
///
/// # Errors
/// Propagates RPC errors returned by [`Chain::block_hash`], or decoding errors when parsing strings.
pub async fn to_hash(c: &Chain, value: impl Into<HashStringNumber>) -> Result<H256, Error> {
async fn inner(c: &Chain, value: HashStringNumber) -> Result<H256, Error> {
let hash = match value {
HashStringNumber::Hash(x) => x,
HashStringNumber::String(x) => H256::from_str(&x).map_err(Error::Other)?,
HashStringNumber::Number(x) => {
let hash = c.block_hash(Some(x)).await?;
let Some(hash) = hash else {
return Err(Error::Other(std::format!("No block hash was found for block height: {}", x)));
};
hash
},
};
Ok(hash)
}
inner(c, value.into()).await
}
/// Converts a [`HashStringNumber`] into a [`HashNumber`].
///
/// # Arguments
/// * `value` - Identifier representing either a block hash or height.
///
/// # Returns
/// Returns a `HashNumber` preserving the original semantics.
///
/// # Errors
/// Returns an [`Error`] when string decoding into a hash fails.
pub fn to_hash_number(value: impl Into<HashStringNumber>) -> Result<HashNumber, Error> {
fn inner(value: HashStringNumber) -> Result<HashNumber, Error> {
let hash_number = match value {
HashStringNumber::Hash(x) => HashNumber::Hash(x),
HashStringNumber::String(x) => HashNumber::Hash(H256::from_str(&x).map_err(Error::Other)?),
HashStringNumber::Number(x) => HashNumber::Number(x),
};
Ok(hash_number)
}
inner(value.into())
}
}
/// Helpers that convert flexible hash strings into concrete hashes.
pub mod hash_string {
use super::*;
use avail_rust_core::{H256, types::HashString};
/// Converts the provided hash representation into an [`H256`].
///
/// # Arguments
/// * `value` - Hash or string encoding of the target hash.
///
/// # Returns
/// Returns the concrete `H256` hash.
///
/// # Errors
/// Returns an [`Error`] if the string cannot be decoded into a valid hash.
pub fn to_hash(value: impl Into<HashString>) -> Result<H256, Error> {
fn inner(value: HashString) -> Result<H256, Error> {
let hash = match value {
HashString::Hash(x) => x,
HashString::String(x) => H256::from_str(&x).map_err(Error::Other)?,
};
Ok(hash)
}
inner(value.into())
}
}
/// Helpers for converting account identifiers into canonical [`crate::AccountId`] values.
pub mod account_id_like {
use super::*;
use crate::UserError;
use avail_rust_core::{AccountId, AccountIdLike};
/// Converts an account identifier into the canonical [`crate::AccountId`].
///
/// # Arguments
/// * `value` - Account representation accepted by RPC helpers.
///
/// # Returns
/// Returns the resolved `AccountId`.
///
/// # Errors
/// Returns an [`Error`] when the underlying conversion fails.
pub fn to_account_id(value: impl Into<AccountIdLike>) -> Result<AccountId, Error> {
fn inner(value: AccountIdLike) -> Result<AccountId, Error> {
let account_id: AccountId = value.try_into().map_err(UserError::Other)?;
Ok(account_id)
}
inner(value.into())
}
}