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 ERC721 hooks #978

Merged
merged 15 commits into from
May 14, 2024
Merged

Conversation

ericnordelo
Copy link
Member

Partially fixes #965

PR Checklist

  • Tests
  • Documentation
  • Added entry to CHANGELOG.md
  • Tried the feature on a public network

@ericnordelo ericnordelo changed the title Add erc721 hooks Add ERC721 hooks Apr 23, 2024
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

The changes look great, Eric! I left some comments, questions, and suggestions

src/token/erc721/erc721.cairo Outdated Show resolved Hide resolved
src/token/erc721/erc721.cairo Show resolved Hide resolved
src/token/erc721/erc721.cairo Show resolved Hide resolved
src/token/erc721/erc721.cairo Outdated Show resolved Hide resolved
src/token/erc721/erc721.cairo Show resolved Hide resolved
docs/modules/ROOT/pages/api/erc721.adoc Outdated Show resolved Hide resolved
Comment on lines 522 to 527
all of the `owner` assets.

Requirements:

- `operator` cannot be the caller.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm the requirement isn't correct, it should be "operator cannot be the owner".

Also, should this be a requirement? I don't see anything against self approvals in the EIP and the current Solidity implementation only has a check that the operator isn't the zero address (here). On top of that, the proposed _approve_with_optional_event already allows for self approvals (here). Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

After giving it some time, I think we can remove the requirement.

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 we don't need the check for the operator != zero either, since the zero address is always unauthorized in the check_authorized method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right, but this would allow is_approved_for_all to potentially return true when the operator is zero, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but I don't think there's a real issue with that. Even if people approve the zero address, if for some reason the zero address approval causes issues, there must be a big bug somewhere in the process, right?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Even if people approve the zero address, if for some reason the zero address approval causes issues, there must be a big bug somewhere in the process, right?

Totally agree. My uneasiness with removing the check is the fact that it allows the getter to return an incorrect value. To me, that's a bug (a very small one in this context but a bug nonetheless). Do you disagree?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair point. I think keeping it as consistent as possible with Solidity is good anyway. Let's keep the operator not zero check.

docs/modules/ROOT/pages/api/erc721.adoc Outdated Show resolved Hide resolved
docs/modules/ROOT/pages/api/erc721.adoc Outdated Show resolved Hide resolved
docs/modules/ROOT/pages/api/erc721.adoc Outdated Show resolved Hide resolved
ericnordelo and others added 5 commits April 30, 2024 12:59
Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
Co-authored-by: Andrew Fleming <fleming.andrew@protonmail.com>
…airo-contracts into feat/add-erc721-hooks-#965
src/token/erc721/erc721.cairo Outdated Show resolved Hide resolved
docs/modules/ROOT/pages/api/erc721.adoc Outdated Show resolved Hide resolved
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

Changes look good! I think we should also add tests for the new internals (_require_owned, _check_authorized, _update). And we should also test the behavior of hooks. Since ERC20 should have hooks tests as well, maybe we can create a separate issue/PR to handle hooks testing (and not further block this PR). WDYT?

@ericnordelo
Copy link
Member Author

I added the tests for the internal methods that were missing, but not sure what's the best approach to test hooks, since the only implementation we provide is empty, and how can we test an empty behavior? If we were to test that functions don't do things that's infinite possibilities right? Any ideas?

@andrew-fleming
Copy link
Collaborator

not sure what's the best approach to test hooks, since the only implementation we provide is empty, and how can we test an empty behavior? If we were to test that functions don't do things that's infinite possibilities right? Any ideas?

We must test ALL possibilities!!!

No, there's no point in testing empty behaviors. What we can test is the side effects of the before and after hook with a mock to confirm they're called in the correct order i.e. before_update is called before _update. A Cairo mocking library would be super useful :(

Unless you have another idea, I say let's get this merged and regroup on this. WDYT?

Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

I left a couple tiny suggestions on the new tests. We just need to conclude on the open discussion and this should be good to go!

src/tests/token/test_erc721.cairo Show resolved Hide resolved
src/tests/token/test_erc721.cairo Outdated Show resolved Hide resolved
Copy link
Collaborator

@andrew-fleming andrew-fleming left a comment

Choose a reason for hiding this comment

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

Last suggestion: let's add the non-zero operator requirement for _set_approval_for_all in the API doc and then this should be good to go!

@ericnordelo ericnordelo merged commit e190e55 into OpenZeppelin:main May 14, 2024
5 of 6 checks passed
@ericnordelo ericnordelo deleted the feat/add-erc721-hooks-#965 branch May 14, 2024 10:53
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.

Add Hooks to ERC721 and ERC1155
2 participants