From 89ee2024ad1ff9a3acda08f08a1ad10a36061a6d Mon Sep 17 00:00:00 2001 From: Alisander Qoshqosh <37006439+qalisander@users.noreply.github.com> Date: Wed, 2 Oct 2024 21:09:46 +0400 Subject: [PATCH] docs: erc165 + structs (#304) Follows up #62 #### PR Checklist - [ ] Tests - [x] Documentation --------- Co-authored-by: Gustavo Gonzalez (cherry picked from commit c40dc8e4ca836f28233d771f217d3354d5c03437) --- docs/modules/ROOT/nav.adoc | 1 + docs/modules/ROOT/pages/utilities.adoc | 43 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 docs/modules/ROOT/pages/utilities.adoc diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc index 05060ec6..1bcc4ea4 100644 --- a/docs/modules/ROOT/nav.adoc +++ b/docs/modules/ROOT/nav.adoc @@ -9,3 +9,4 @@ * xref:access-control.adoc[Access Control] * xref:crypto.adoc[Cryptography] +* xref:utilities.adoc[Utilities] diff --git a/docs/modules/ROOT/pages/utilities.adoc b/docs/modules/ROOT/pages/utilities.adoc new file mode 100644 index 00000000..249752b7 --- /dev/null +++ b/docs/modules/ROOT/pages/utilities.adoc @@ -0,0 +1,43 @@ += Utilities + +The OpenZeppelin Stylus Contracts provides a ton of useful utilities that you can use in your project. +For a complete list, check out the https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/index.html[API Reference]. +Here are some of the more popular ones. + +[[introspection]] +== Introspection + +It's frequently helpful to know whether a contract supports an interface you'd like to use. +https://eips.ethereum.org/EIPS/eip-165[`ERC-165`] is a standard that helps do runtime interface detection. +Contracts for Stylus provides helpers for implementing ERC-165 in your contracts: + +* https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html[`IERC165`] — this is the ERC-165 trait that defines https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html#tymethod.supports_interface[`supportsInterface`]. In order to implement ERC-165 interface detection, you should manually expose https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html#tymethod.supports_interface[`supportsInterface`] function in your contract. + +[source,rust] +---- +sol_storage! { + #[entrypoint] + struct Erc721Example { + #[borrow] + Erc721 erc721; + } +} + +#[public] +#[inherit(Erc721)] +impl Erc721Example { + pub fn supports_interface(interface_id: FixedBytes<4>) -> bool { + Erc721::supports_interface(interface_id) + } +} + +---- + +[[structures]] +== Structures + +Some use cases require more powerful data structures than arrays and mappings offered natively in alloy and the Stylus sdk. +Contracts for Stylus provides these libraries for enhanced data structure management: + +- https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/structs/bitmap/index.html[`BitMaps`]: Store packed booleans in storage. +- https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/structs/checkpoints/index.html[`Checkpoints`]: Checkpoint values with built-in lookups. \ No newline at end of file