Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add statechange for owned-fraction in rgb21 invoice data #227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cymqqqq
Copy link
Contributor

@cymqqqq cymqqqq commented Jun 25, 2024

Background:

Now there are Amount, and Ownedfraction in the RGB invoice module, that is
pub struct Amount( u64); and pub struct OwnedFraction(u64);. We can perform a state change operation for the amount type when we want to build a transfer transition, and there is also the check_add method for the amount type.
Both the StateChange and check_add for the amount type can perform algorithm operation(+, -).

Then for the rgb21 invoice, there is Ownedfraction which is u64 similar to Amount, and now if we want to build a transfer operation for the rgb21 contract, we just push all of them(owned fraction, token index) into the data state. So I think maybe there is also a state change and a check_add method requirement for the Ownedfraction type, and then the user can add an owned fraction when they want to transfer. Finally, they can push (changed owned fraction, token index) into a data state.

Description:

impl StateChange for Ownedfraction

Copy link

codecov bot commented Jun 25, 2024

Codecov Report

Attention: Patch coverage is 0% with 22 lines in your changes missing coverage. Please review.

Project coverage is 17.5%. Comparing base (7f637c6) to head (e480eaf).
Report is 1 commits behind head on master.

Files Patch % Lines
src/interface/contract.rs 0.0% 22 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master    #227     +/-   ##
========================================
- Coverage    17.6%   17.5%   -0.1%     
========================================
  Files          37      37             
  Lines        7535    7557     +22     
========================================
  Hits         1323    1323             
- Misses       6212    6234     +22     
Flag Coverage Δ
rust 17.5% <0.0%> (-0.1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dr-orlovsky dr-orlovsky added the enhancement New feature or request label Jun 25, 2024
@dr-orlovsky dr-orlovsky added this to the v0.11.0 milestone Jun 25, 2024
Copy link
Member

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is always nice to have compiler guarantees instead of unreachable - pls see my comments

Comment on lines 155 to 164
*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!(),
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with Ord you can get rid of unreachable and instead enjoy compiler-provided exhaustive security for matches:

Suggested change
*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!(),
};
*self = match self {
OwnedFractionChange::Dec(neg) => OwnedFractionChange::Dec(*neg + sub),
OwnedFractionChange::Zero => OwnedFractionChange::Dec(sub),
OwnedFractionChange::Inc(pos) => match sub.cmp(pos) {
Ordering::Less => OwnedFractionChange::Inc(*pos - sub),
Ordering::Equal => OwnedFractionChange::Zero,
Ordering::Greater => OwnedFractionChange::Dec(sub - *pos),
},
};

src/interface/contract.rs Show resolved Hide resolved
OwnedFractionChange::Dec(neg) if *neg < add => OwnedFractionChange::Inc(add - *neg),
OwnedFractionChange::Dec(_) => unreachable!(),
OwnedFractionChange::Dec(neg) => match add.cmp(neg) {
Ordering::Greater => OwnedFractionChange::Dec(*neg - add),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be less, not greater - the order of comparison is reversed in the match

Copy link
Member

@dr-orlovsky dr-orlovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. Will wait for review of those actually working with NFTs - @zoedberg and @crisdut

@dr-orlovsky
Copy link
Member

The PR makes sense for me. @zoedberg @crisdut can you confirm the same so we can merge it before the next release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

2 participants