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: this === undefined in Struct static methods #689

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Type inference for Structs with instance methods https://github.com/o1-labs/snarkyjs/pull/567
- also fixes `Struct.fromJSON`
- `SmartContract.fetchEvents` fixed when multiple event types existed https://github.com/o1-labs/snarkyjs/issues/627
- Error when using reduce with a `Struct` as state type https://github.com/o1-labs/snarkyjs/pull/689

## [0.7.3](https://github.com/o1-labs/snarkyjs/compare/5f20f496...d880bd6e)

Expand Down
12 changes: 2 additions & 10 deletions src/lib/zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1268,16 +1268,8 @@ Use the optional \`maxTransactionsWithActions\` argument to increase this number
// also, for each action length, compute the new state and then pick the actual one
let newStates = actionss.map((actions) => {
// we generate a new witness for the state so that this doesn't break if `apply` modifies the state
let newState = Circuit.witness(stateType, () => {
// TODO: why doesn't this work without the toConstant mapping?
let { toFields, fromFields, toAuxiliary } = stateType;
Copy link
Member Author

Choose a reason for hiding this comment

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

the problem here was that pulling those static members out of the class is not compatible with using this in those static methods. js rookie mistake

Copy link
Member Author

Choose a reason for hiding this comment

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

and it turns out wee don't need that toConstant logic anymore, at all, because Circuit.witness does that under the hood anyway

return fromFields(
toFields(state).map((x) => x.toConstant()),
toAuxiliary(state)
);
// return state;
});
Circuit.assertEqual(newState, state);
let newState = Circuit.witness(stateType, () => state);
Circuit.assertEqual(stateType, newState, state);
actions.forEach((action) => {
newState = reduce(newState, action);
});
Expand Down