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

feat(zoe): add E(userSeat).wasWantSatisfied() #5905

Merged
merged 4 commits into from
Aug 11, 2022
Merged

Conversation

Chris-Hibbert
Copy link
Contributor

closes: #5818
closes: #3056
refs: #4360

Description

Add a method on Zoe's userSeats that reveals whether the seat exited with its wants satisfied.

Security Considerations

This doesn't reveal interim state of a seat. In fact, it doesn't reveal anything the userSeat didn't already have access to, since it only needed the payouts and the proposal. But this is a more reliable way for dApps to find out than doing the calculation themselves.

Documentation Considerations

Needs to be added to the Zoe docs.

Testing Considerations

Tests simple cases of satisfying and not satisfying the wants. Verifies that the promise is available before the seat exits.

@Chris-Hibbert Chris-Hibbert added enhancement New feature or request Zoe package: Zoe labels Aug 6, 2022
@Chris-Hibbert Chris-Hibbert added this to the Mainnet 1 milestone Aug 6, 2022
@Chris-Hibbert Chris-Hibbert self-assigned this Aug 6, 2022
Copy link
Member

@erights erights left a comment

Choose a reason for hiding this comment

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

Needs to be added to the UserSeat type as well.

Comment on lines 100 to 104
wasWantSatisfied: async () => {
return E.when(payoutPromiseKit.promise, () =>
Object.keys(proposal.want).every(kwd =>
AmountMath.isGTE(currentAllocation[kwd], proposal.want[kwd]),
),
);
},
Copy link
Member

Choose a reason for hiding this comment

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

Id prefer that you use satisfiesWant, which you'll need to export from offerSafety.js. That way you'll already be prepared for multiples, which does export it defined as

export const satisfiesWant = (proposal, allocation) =>
  satisfiesInternal(proposal.want, allocation, 1n) >= 1n;
harden(satisfiesWant);

This also makes clear that this PR is necessary! If clients had tested this themselves, they also would have created an incompatibility with multiples!

Copy link
Member

Choose a reason for hiding this comment

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

I see that it was also exported on master, so no worries about that.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I remember we had a discussion about this, and agreed it was better for the public method to return 0 or 1 for now, rather than false and true, so that it could be more informative once we have multiples. How many times was the want satisfied?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated offerSafety to return 0 or 1, and added tests for satisfiesWant.

Copy link
Contributor Author

@Chris-Hibbert Chris-Hibbert left a comment

Choose a reason for hiding this comment

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

Needs to be added to the UserSeat type as well.

Done.

Comment on lines 100 to 104
wasWantSatisfied: async () => {
return E.when(payoutPromiseKit.promise, () =>
Object.keys(proposal.want).every(kwd =>
AmountMath.isGTE(currentAllocation[kwd], proposal.want[kwd]),
),
);
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated offerSafety to return 0 or 1, and added tests for satisfiesWant.

@Tartuffo Tartuffo removed this from the Mainnet 1 RC0 milestone Aug 10, 2022
Copy link
Member

@erights erights left a comment

Choose a reason for hiding this comment

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

LGTM

Need to rename wasWantSatisfied to something compat with returning a count. Best I came up with is "satisfiedWant", but is not great. Please come up with something better ;)

* @param {AmountKeywordRecord} giveOrWant
* @param {AmountKeywordRecord} allocation
* @returns {0|1}
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes Typescript amazes me ;)
No change suggested.

Comment on lines +85 to +86
satisfiesGive(proposal, allocation) > 0 ||
satisfiesWant(proposal, allocation) > 0
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for not relying on 0 being falsy ;)
No change suggested.

Comment on lines +90 to +91
harden(isOfferSafe);
harden(satisfiesWant);
Copy link
Member

Choose a reason for hiding this comment

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

Wow, we were missing hardens before. Good catch!

While not suggesting a change, I will note that my style is to export each in place, and follow each immediately by a harden. This is safe against the unhardened state being observable only if the exported declarations are not 'function' function declarations. Converting to arrow functions needs to happen sometime, but need not happen for old functions in the PR.

@@ -182,6 +182,7 @@
* @property {() => Promise<OR>} getOfferResult
* @property {() => void=} tryExit
* @property {() => Promise<boolean>} hasExited
* @property {() => Promise<0|1>} wasWantSatisfied
Copy link
Member

Choose a reason for hiding this comment

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

The name wasWantSatisfied definitely suggests a yes or no answer.
What name would work better with returning a count?

Copy link
Member

Choose a reason for hiding this comment

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

numWantsSatisfied or simply wantsSatisfied since singular is by far more common than multi

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like @turadg's suggestion of numWantsSatisfied. (It beats out 'wantsSatisfied' because the latter can be read as boolean.

Copy link
Member

Choose a reason for hiding this comment

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

because the latter can be read as boolean.

fwiw, that's what I proposed as its virtue. when the caller doesn't know or care about multiples they don't have to. If they try to do anything with it that works for a boolean and not 0 | 1 the type checker will inform them.

@Chris-Hibbert Chris-Hibbert changed the title add E(userSeat).wasWantSatisfied() feat(zoe): add E(userSeat).wasWantSatisfied() Aug 11, 2022
@Chris-Hibbert Chris-Hibbert added the automerge:squash Automatically squash merge label Aug 11, 2022
@mergify mergify bot merged commit 764c3cd into master Aug 11, 2022
@mergify mergify bot deleted the 5818-wasWantSatisfied branch August 11, 2022 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge:squash Automatically squash merge enhancement New feature or request Zoe package: Zoe
Projects
None yet
4 participants