use std::str::FromStr;
use anyswap_core::{codec, transaction::LiquidChainCtx, types::SwapNetwork};
use elements::AssetId;
use thiserror::Error;
use crate::wallet::SwapWallet;
#[derive(Debug, Error)]
pub enum ContextError {
#[error("invalid Liquid genesis hash: {0}")]
GenesisHash(String),
#[error("invalid Liquid policy asset: {0}")]
PolicyAsset(String),
}
#[derive(Clone)]
pub struct SwapContext<W: SwapWallet> {
wallet: W,
liquid: Option<LiquidChainCtx>,
}
impl<W: SwapWallet> SwapContext<W> {
pub fn new(wallet: W) -> Self {
Self {
wallet,
liquid: None,
}
}
pub fn with_liquid(
mut self,
genesis_hash: &str,
policy_asset: &str,
) -> Result<Self, ContextError> {
self.liquid = Some(LiquidChainCtx {
genesis_hash: codec::parse_liquid_block_hash(genesis_hash)
.map_err(|error| ContextError::GenesisHash(error.to_string()))?,
policy_asset: AssetId::from_str(policy_asset)
.map_err(|error| ContextError::PolicyAsset(error.to_string()))?,
});
Ok(self)
}
pub fn wallet(&self) -> &W {
&self.wallet
}
pub fn network(&self) -> SwapNetwork {
self.wallet.network()
}
pub(crate) fn liquid(&self) -> Option<LiquidChainCtx> {
self.liquid
}
}