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

fix: document contract; refactor for clarity; use defensive patterns #49

Merged
merged 2 commits into from
Jan 24, 2024

Conversation

dckc
Copy link
Member

@dckc dckc commented Jan 23, 2024

@kbennett2000 made a proposal a while back to add comments; he and I refined that into #38 . I have since split out the renaming part as #45 . A question for @samsiegart @0xpatrickdev @turadg @Chris-Hibbert etc. remains: how to handle the overlap between the comments here and the walkthru prose in the contract code walk-thru in the zoe docs? Copy the comments here into the walkthru code snippets? I suppose I'll have to try it out to see what looks most natural.

@kbennett2000 rather than changing the contract surface API again (and updating the UI again), this doesn't rename Price to Pay etc. as we did in #38

What it does:

  • add comments to clarify the control flow etc.,
    • note our no-use-before-define convention (in prose)
    • cite zoe overview walkthru of this code
    • cite Hardened JS docs
  • feat: hoist maxItems to default from terms instead of hard-coded
  • use atomicRearrange() instead of deprecated reallocate() rather than try to explain
  • validate contract terms with patterns
  • refactor bagValueSize() into sum() and bagCounts()
    • set off with "#region bag utilities"
  • punt trace() rather than explain

* The `Items` amount must use the `Item` brand and a bag value.
*/
const proposalShape = harden({
give: { Price: { brand: tradePrice.brand, value: M.any() } },

Choose a reason for hiding this comment

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

I think you can use M.gte() here and not need AmountMath.isGTE() below

Suggested change
give: { Price: { brand: tradePrice.brand, value: M.any() } },
give: { Price: { brand: tradePrice.brand, value: M.gte(price.amount) } },

or

Suggested change
give: { Price: { brand: tradePrice.brand, value: M.any() } },
give: { Price: M.gte(price) },

Copy link
Member Author

Choose a reason for hiding this comment

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

good point!

{ maxItems: M.bigint() },
),
};
// compatibility with an earlier contract metadata API
Copy link
Member

Choose a reason for hiding this comment

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

What does this mean? I have not seen meta.customTermsShape before.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's like a proposal shape but for terms.

Documentation is on the TODO list:

That issue has pointers to context.

exit: M.any(),
});

/** a seat for allocating proceeds of sales */
const proceeds = zcf.makeEmptySeatKit().zcfSeat;

/** @param {ZCFSeat} buyerSeat */
Copy link
Member

@0xpatrickdev 0xpatrickdev Jan 24, 2024

Choose a reason for hiding this comment

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

Suggested change
/** @param {ZCFSeat} buyerSeat */
/** @type {OfferHandler} */

This maybe gives more info to the user - they can learn about the second optional argument offerArgs

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems like an extra level of indirection to explain in the zoe walkthru.

If we had good auto-generated reference docs for OfferHandler, then yeah... but we don't, so...

* @param {ZCF<{tradePrice: Amount}>} zcf
* This contract is parameterized by terms for price and,
* optionally, a maximum number of items sold for that price (default: 3).
*
Copy link
Member

@0xpatrickdev 0xpatrickdev Jan 24, 2024

Choose a reason for hiding this comment

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

Suggested change
*
* These terms will extend `StandardTerms` present in all zoe contracts, `issuers` and `brands`.
*

Don't feel strongly about this suggestion but could be helpful.

@0xpatrickdev
Copy link
Member

how to handle the overlap between the comments here and the walkthru prose in the contract code walk-thru in the zoe docs? Copy the comments here into the walkthru code snippets? I suppose I'll have to try it out to see what looks most natural.

Hard to have an opinion without seeing what the revised docs look like. I would imagine 1-2 line comments are more tenable in a docs site versus multi-line.

* - handles offers to buy up to `maxItems` items at a time.
*
* As is typical in Zoe contracts, the flow is:
* 1. contract does internal setup
Copy link
Member

@0xpatrickdev 0xpatrickdev Jan 24, 2024

Choose a reason for hiding this comment

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

Suggested change
* 1. contract does internal setup
* 1. contract does internal setup (mint assets)

Can we expand on 'internal setup'?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, it doesn't mint any assets at startup. It creates a mint, but doesn't use it until an offer is made.

The point of this comment was to clarify the order that readers should use to look at the code. They can see the rest for themselves, I would think.

In fact, now that I think about it, this part of the comment doesn't belong on the doctring of start; it's not aimed at clients of start. It's an internal comment aimed at readers of the implementation. I'm moving it to the file comment.

dckc added 2 commits January 23, 2024 20:32
 - add comments to clarify the control flow etc.,
   - note our `no-use-before-define` convention (in prose)
   - cite zoe overview walkthru of this code
   - cite Hardened JS docs
 - feat: hoist maxItems to default from terms instead of hard-coded
 - use atomicRearrange() instead of deprecated reallocate()
   rather than try to explain
 - validate contract terms with patterns
 - refactor bagValueSize() into sum() and bagCounts()
   - set off with "#region bag utilities"
 - punt trace() rather than explain
@dckc dckc force-pushed the dc-contract-docs2 branch from ec8357f to d58a631 Compare January 24, 2024 02:32
@dckc dckc changed the base branch from dc-no-game to main January 24, 2024 02:33
@dckc dckc merged commit db4e168 into main Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants