use std::sync::Arc;
use serde::Serialize;
use subxt::blocks::ExtrinsicEvents;
use subxt::ext::sp_core::H256;
use crate::types::MandalaConfig;
#[derive(Debug, Clone)]
pub enum ExtrinsicStatus {
Pending,
Failed(Reason),
Success(ExtrinsicResult),
}
impl ExtrinsicStatus {
#[must_use]
pub fn is_pending(&self) -> bool {
matches!(self, Self::Pending)
}
#[must_use]
pub fn is_failed(&self) -> bool {
matches!(self, Self::Failed(..))
}
#[must_use]
pub fn is_success(&self) -> bool {
matches!(self, Self::Success(..))
}
}
impl Default for ExtrinsicStatus {
fn default() -> Self {
Self::Pending
}
}
#[cfg(feature = "serde")]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub struct Reason(String);
impl Reason {
pub fn inner(&self) -> &str {
self.0.as_str()
}
}
impl ToString for Reason {
fn to_string(&self) -> String {
self.0.clone()
}
}
impl From<String> for Reason {
fn from(value: String) -> Self {
Self(value)
}
}
#[derive(Debug)]
pub struct ExtrinsicResult(Arc<ExtrinsicEvents<MandalaConfig>>);
impl Clone for ExtrinsicResult {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl ExtrinsicResult {
#[cfg(feature = "unstable_sp_core")]
pub fn into_inner(self) -> Arc<ExtrinsicEvents<MandalaConfig>> {
self.0
}
#[cfg(feature = "unstable_sp_core")]
pub fn inner(&self) -> &ExtrinsicEvents<MandalaConfig> {
&self.0
}
}
impl ExtrinsicResult {
pub fn hash(&self) -> Hash {
self.0.extrinsic_hash().into()
}
}
impl From<ExtrinsicEvents<MandalaConfig>> for ExtrinsicResult {
fn from(value: ExtrinsicEvents<MandalaConfig>) -> Self {
Self(Arc::new(value))
}
}
#[cfg(feature = "serde")]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub struct Hash(String);
impl From<ExtrinsicResult> for Hash {
fn from(value: ExtrinsicResult) -> Self {
Self(value.hash().to_string())
}
}
impl From<H256> for Hash {
fn from(value: H256) -> Self {
let hex_str = format!("0x{}", hex::encode(value.as_bytes()));
Self(hex_str)
}
}
impl ToString for Hash {
fn to_string(&self) -> String {
self.0.clone()
}
}
impl Hash {
pub fn inner_as_str(&self) -> &str {
self.0.as_str()
}
#[cfg(feature = "unstable_sp_core")]
pub fn into_inner(&self) -> H256 {
use std::str::FromStr;
H256::from_str(self.inner_as_str()).expect("internal conversion shouldn't fail!")
}
}
#[cfg(test)]
mod hash_tests {
use super::*;
use subxt::ext::sp_core::hexdisplay::HexDisplay;
#[test]
fn test_from_h256() {
let h256 = H256::random();
let hex_display_h256 = HexDisplay::from(&h256.0);
let h256_str = format!("0x{}", hex_display_h256.to_string());
let hash_str = Hash::from(h256).to_string();
assert_eq!(hash_str, h256_str)
}
}