Skip to content

Commit

Permalink
add statechange for owned-fraction in rgb21
Browse files Browse the repository at this point in the history
  • Loading branch information
jack committed Jun 25, 2024
1 parent b2885ba commit 9609a20
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions invoice/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub struct TokenIndex(u32);
)]
pub struct OwnedFraction(u64);

impl KnownState for OwnedFraction {}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, Display)]
#[display("{1}@{0}")]
#[derive(StrictType, StrictEncode, StrictDecode)]
Expand Down
53 changes: 53 additions & 0 deletions src/interface/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use rgb::{
OutputAssignment, RevealedAttach, RevealedData, RevealedValue, VoidState, XOutpoint,
XOutputSeal, XWitnessId,
};
use rgbinvoice::OwnedFraction;
use strict_encoding::{FieldName, StrictDecode, StrictDumb, StrictEncode};
use strict_types::typify::TypedVal;
use strict_types::{decode, StrictVal, TypeSystem};
Expand Down Expand Up @@ -121,6 +122,58 @@ pub trait StateChange: Clone + Eq + StrictDumb + StrictEncode + StrictDecode {
fn merge_received(&mut self, state: Self::State);
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]

Check warning on line 126 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L126

Added line #L126 was not covered by tests
#[strict_type(lib = LIB_NAME_RGB_STD, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 130 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L130

Added line #L130 was not covered by tests
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum OwnedFractionChange {
#[display("-{0}")]
#[strict_type(tag = 0xFF)]
Dec(OwnedFraction),

#[display("0")]
#[strict_type(tag = 0, dumb)]
Zero,

#[display("+{0}")]
#[strict_type(tag = 0x01)]
Inc(OwnedFraction),
}

impl StateChange for OwnedFractionChange {
type State = OwnedFraction;

fn from_spent(state: Self::State) -> Self { OwnedFractionChange::Dec(state) }

Check warning on line 150 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L150

Added line #L150 was not covered by tests

fn from_received(state: Self::State) -> Self { OwnedFractionChange::Inc(state) }

Check warning on line 152 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L152

Added line #L152 was not covered by tests

fn merge_spent(&mut self, sub: Self::State) {
*self = match self {
OwnedFractionChange::Dec(neg) => OwnedFractionChange::Dec(*neg + sub),
OwnedFractionChange::Zero => OwnedFractionChange::Dec(sub),
OwnedFractionChange::Inc(pos) if *pos > sub => OwnedFractionChange::Inc(*pos - sub),
OwnedFractionChange::Inc(pos) if *pos == sub => OwnedFractionChange::Zero,
OwnedFractionChange::Inc(pos) if *pos < sub => OwnedFractionChange::Dec(sub - *pos),
OwnedFractionChange::Inc(_) => unreachable!(),

Check warning on line 161 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L154-L161

Added lines #L154 - L161 were not covered by tests
};
}

Check warning on line 163 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L163

Added line #L163 was not covered by tests

fn merge_received(&mut self, add: Self::State) {
*self = match self {
OwnedFractionChange::Inc(pos) => OwnedFractionChange::Inc(*pos + add),
OwnedFractionChange::Zero => OwnedFractionChange::Inc(add),
OwnedFractionChange::Dec(neg) if *neg > add => OwnedFractionChange::Dec(*neg - add),
OwnedFractionChange::Dec(neg) if *neg == add => OwnedFractionChange::Zero,
OwnedFractionChange::Dec(neg) if *neg < add => OwnedFractionChange::Inc(add - *neg),
OwnedFractionChange::Dec(_) => unreachable!(),

Check warning on line 172 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L165-L172

Added lines #L165 - L172 were not covered by tests
};
}

Check warning on line 174 in src/interface/contract.rs

View check run for this annotation

Codecov / codecov/patch

src/interface/contract.rs#L174

Added line #L174 was not covered by tests
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD, tags = custom)]
Expand Down

0 comments on commit 9609a20

Please sign in to comment.