Skip to content

Commit de8886d

Browse files
committed
feat: rgb21 interface improviments
1 parent 0a9b5ee commit de8886d

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/interface/contract.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222
use std::collections::{BTreeSet, HashSet};
2323
use std::ops::Deref;
2424

25-
use amplify::confinement::{LargeOrdMap, LargeVec, SmallVec};
25+
use amplify::confinement::{LargeOrdMap, LargeVec, SmallVec, U16};
2626
use bp::Outpoint;
2727
use rgb::{
28-
AssetTag, AssignmentType, AttachId, BlindingFactor, ContractId, ContractState, FungibleOutput,
29-
MediaType, RevealedAttach, RevealedData, WitnessId, XOutpoint, XOutputSeal,
28+
AssetTag, AssignmentType, AttachId, BlindingFactor, ContractId, ContractState, DataOutput,
29+
FungibleOutput, MediaType, RevealedAttach, RevealedData, WitnessId, XOutpoint, XOutputSeal,
3030
};
31-
use strict_encoding::FieldName;
31+
use strict_encoding::{FieldName, StrictDeserialize};
3232
use strict_types::typify::TypedVal;
3333
use strict_types::{decode, StrictVal};
3434

35+
use super::rgb21::Allocation;
3536
use crate::interface::{IfaceId, IfaceImpl};
3637

3738
#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
@@ -123,6 +124,28 @@ impl From<&FungibleOutput> for FungibleAllocation {
123124
}
124125
}
125126

127+
#[derive(Clone, Eq, PartialEq, Debug)]
128+
pub struct DataAllocation {
129+
pub owner: XOutputSeal,
130+
pub witness: AllocationWitness,
131+
pub value: Allocation,
132+
}
133+
134+
impl From<DataOutput> for DataAllocation {
135+
fn from(out: DataOutput) -> Self { Self::from(&out) }
136+
}
137+
138+
impl From<&DataOutput> for DataAllocation {
139+
fn from(out: &DataOutput) -> Self {
140+
DataAllocation {
141+
owner: out.seal,
142+
witness: out.witness.into(),
143+
value: Allocation::from_strict_serialized::<U16>(out.state.as_ref().to_owned())
144+
.expect("invalid allocation data"),
145+
}
146+
}
147+
}
148+
126149
pub trait OutpointFilter {
127150
fn include_output(&self, output: impl Into<XOutpoint>) -> bool;
128151
}
@@ -237,6 +260,26 @@ impl ContractIface {
237260
Ok(LargeVec::try_from_iter(state).expect("same or smaller collection size"))
238261
}
239262

263+
pub fn data(
264+
&self,
265+
name: impl Into<FieldName>,
266+
filter: &impl OutpointFilter,
267+
) -> Result<LargeVec<DataAllocation>, ContractError> {
268+
let name = name.into();
269+
let type_id = self
270+
.iface
271+
.assignments_type(&name)
272+
.ok_or(ContractError::FieldNameUnknown(name))?;
273+
let state = self
274+
.state
275+
.data()
276+
.iter()
277+
.filter(|outp| outp.opout.ty == type_id)
278+
.filter(|outp| filter.include_output(outp.seal))
279+
.map(DataAllocation::from);
280+
Ok(LargeVec::try_from_iter(state).expect("same or smaller collection size"))
281+
}
282+
240283
// TODO: Add rights, attachments and structured data APIs
241284
pub fn outpoint(
242285
&self,

src/interface/rgb21.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ impl Allocation {
101101
pub fn with(index: TokenIndex, fraction: OwnedFraction) -> Allocation {
102102
Allocation(index, fraction)
103103
}
104+
105+
pub fn token_index(self) -> TokenIndex { self.0 }
106+
107+
pub fn fraction(self) -> OwnedFraction { self.1 }
104108
}
105109

106110
impl StrictSerialize for Allocation {}

0 commit comments

Comments
 (0)