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

docs: erc165 + structs #304

Merged
merged 8 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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 docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@

* xref:access-control.adoc[Access Control]
* xref:crypto.adoc[Cryptography]
* xref:utilities.adoc[Utilities]
43 changes: 43 additions & 0 deletions docs/modules/ROOT/pages/utilities.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
= Utilities

The OpenZeppelin Stylus Contracts provide 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 provide 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 provide 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.
Loading