Skip to content

Commit

Permalink
iface: add ContractIface::outputs_typed method
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 17, 2024
1 parent 9240917 commit c2cc811
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/interface/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
use std::borrow::Borrow;
use std::collections::{BTreeSet, HashMap, HashSet};

use amplify::Wrapper;
use rgb::validation::Scripts;
use rgb::{
AssignmentType, AttachId, ContractId, OpId, Opout, Schema, State, StateData, XOutputSeal,
XWitnessId, STATE_DATA_MAX_LEN,
};
use strict_encoding::{FieldName, SerializeError};
use strict_encoding::{FieldName, SerializeError, StrictDeserialize};
use strict_types::{typify, SemId, StrictVal, TypeSystem};

use crate::contract::{Allocation, WitnessInfo};
Expand Down Expand Up @@ -58,14 +59,27 @@ pub enum ContractError {
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct Output {
pub struct Output<T = StrictVal> {
pub opout: Opout,
pub seal: XOutputSeal,
pub state: StrictVal,
pub state: T,
pub attach_id: Option<AttachId>,
pub witness: Option<XWitnessId>,
}

impl<T: StrictDeserialize> From<Allocation> for Output<T> {
fn from(a: Allocation) -> Self {
Output {
opout: a.opout,
seal: a.seal,
state: T::from_strict_serialized(a.state.data.to_inner())
.expect("data in stash are not valid"),
attach_id: a.state.attach,
witness: a.witness,
}
}
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display)]
#[cfg_attr(
feature = "serde",
Expand Down Expand Up @@ -286,6 +300,19 @@ impl<S: ContractStateRead> ContractIface<S> {
.map(|a| self.allocation_to_output(a)))
}

pub fn outputs_typed<'c, T: StrictDeserialize + 'c>(
&'c self,
name: impl Into<FieldName>,
filter: impl AssignmentsFilter + 'c,
) -> Result<impl Iterator<Item = Output<T>> + 'c, ContractError> {
let type_id = self.assignment_type(name)?;
Ok(self
.allocations(filter)
.filter(move |a| a.opout.ty == type_id)
.cloned()
.map(Output::from))
}

pub fn history(
&self,
filter_outpoints: impl AssignmentsFilter,
Expand Down

0 comments on commit c2cc811

Please sign in to comment.