tx_indexer/
aux.rs

1use anyhow::anyhow;
2use data_encoding::HEXLOWER;
3use plutus_ledger_api::{
4    csl::{csl_to_pla::TryToPLA, lib as csl},
5    v3::{
6        address::Address,
7        crypto::LedgerBytes,
8        script::{MintingPolicyHash, ScriptHash},
9        value::CurrencySymbol,
10    },
11};
12use std::str::FromStr;
13
14#[derive(Debug, Clone)]
15pub struct ParseCurrencySymbol(pub CurrencySymbol);
16
17impl FromStr for ParseCurrencySymbol {
18    type Err = &'static str;
19
20    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
21        Ok(ParseCurrencySymbol(CurrencySymbol::NativeToken(
22            MintingPolicyHash(ScriptHash(LedgerBytes(
23                HEXLOWER.decode(&s.to_owned().into_bytes()).unwrap(),
24            ))),
25        )))
26    }
27}
28
29#[derive(Debug, Clone)]
30pub struct ParseAddress(pub Address);
31
32impl FromStr for ParseAddress {
33    type Err = anyhow::Error;
34
35    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
36        Ok(ParseAddress(
37            csl::Address::from_bech32(s)
38                .map_err(|err| anyhow!("Couldn't parse bech32 address: {}", err))?
39                .try_to_pla()
40                .map_err(|err| anyhow!("Couldn't convert address: {}", err))?,
41        ))
42    }
43}