use std::collections::btree_set;
use bitcoin::OutPoint;
use rgb::{ConsignmentType, ContractState, InmemConsignment, NodeId, OwnedValue};
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(StrictEncode, StrictDecode)]
pub struct Asset(ContractState);
impl Asset {
pub fn known_coins(&self) -> btree_set::Iter<OwnedValue> { self.0.owned_values.iter() }
pub fn outpoint_coins(&self, outpoint: OutPoint) -> Vec<OwnedValue> {
self.known_coins()
.filter(|a| a.seal == outpoint)
.cloned()
.collect()
}
}
impl<T> TryFrom<&InmemConsignment<T>> for Asset
where T: ConsignmentType
{
type Error = Error;
fn try_from(consignment: &InmemConsignment<T>) -> Result<Self, Self::Error> {
let state = ContractState::from(consignment);
let asset = Asset(state);
asset.validate()?;
Ok(asset)
}
}
impl Asset {
fn validate(&self) -> Result<(), Error> {
if self.0.schema_id != crate::schema().schema_id() {
Err(Error::WrongSchemaId)?;
}
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Display, From, Error)]
#[display(doc_comments)]
pub enum Error {
WrongSchemaId,
GenesisSeal,
EpochSealConfidential(NodeId),
BurnSealConfidential(NodeId),
InflationAssignmentConfidential(NodeId),
NotAllEpochsExposed,
}