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

chore: update docs on comparators #4281

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add more examples
sklppy88 committed Jan 30, 2024
commit f547ea372ec793ab80c56c2364f76c6a50e694a7
13 changes: 13 additions & 0 deletions docs/docs/developers/contracts/syntax/storage/private_state.md
Original file line number Diff line number Diff line change
@@ -304,6 +304,8 @@ This method sets the offset value, which determines where to start retrieving no

#### Examples

##### Example 1

The following code snippet creates an instance of `NoteGetterOptions`, which has been configured to find the cards that belong to `account_address`. The returned cards are sorted by their points in descending order, and the first `offset` cards with the highest points are skipped.

#include_code state_vars-NoteGetterOptionsSelectSortOffset /yarn-project/noir-contracts/contracts/docs_example_contract/src/options.nr rust
@@ -335,3 +337,14 @@ One thing to remember is, `filter` will be applied on the notes after they are p
The limit is `MAX_READ_REQUESTS_PER_CALL` by default. But we can set it to any value **smaller** than that:

#include_code state_vars-NoteGetterOptionsPickOne /yarn-project/noir-contracts/contracts/docs_example_contract/src/options.nr rust

##### Example 2

An example of how we can use a Comparator to select notes when calling a Noir contract from aztec.js is below.

#include_code state_vars-NoteGetterOptionsComparatorExampleTs /yarn-project/end-to-end/src/e2e_note_getter.test.ts typescript

In this example, we use the above typescript code to invoke a call to our Noir contract below. This Noir contract function takes an input to match with, and a comparator to use when fetching and selecting notes from storage.

#include_code state_vars-NoteGetterOptionsComparatorExampleNoir /yarn-project/noir-contracts/contracts/docs_example_contract/src/main.nr rust

2 changes: 2 additions & 0 deletions yarn-project/end-to-end/src/e2e_note_getter.test.ts
Original file line number Diff line number Diff line change
@@ -49,7 +49,9 @@ describe('e2e_note_getter', () => {
contract.methods.read_note(5, Comparator.LT).view(),
contract.methods.read_note(5, Comparator.GT).view(),
contract.methods.read_note(5, Comparator.LTE).view(),
// docs:start:state_vars-NoteGetterOptionsComparatorExampleTs
contract.methods.read_note(5, Comparator.GTE).view(),
// docs:end:state_vars-NoteGetterOptionsComparatorExampleTs
]);

expect(
Original file line number Diff line number Diff line change
@@ -109,12 +109,14 @@ contract DocsExample {
storage.test.insert(&mut note, true);
}

// docs:start:state_vars-NoteGetterOptionsComparatorExampleNoir
unconstrained fn read_note(amount: Field, comparator: u3) -> pub [Option<CardNote>; 10] {
let options = NoteViewerOptions::new().select(0, amount, Option::some(comparator));
let notes = storage.test.view_notes(options);

notes
}
// docs:end:state_vars-NoteGetterOptionsComparatorExampleNoir

#[aztec(private)]
fn update_legendary_card(randomness: Field, points: u8) {