Skip to content

Commit

Permalink
docs: erc165 + structs (#304)
Browse files Browse the repository at this point in the history
Follows up #62

#### PR Checklist

- [ ] Tests
- [x] Documentation

---------

Co-authored-by: Gustavo Gonzalez <gustavo.gonzalez@openzeppelin.com>
(cherry picked from commit c40dc8e)
  • Loading branch information
qalisander committed Oct 4, 2024
1 parent 762a0e5 commit 89ee202
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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 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.

Check failure on line 14 in docs/modules/ROOT/pages/utilities.adoc

View workflow job for this annotation

GitHub Actions / Linkspector

[linkspector] docs/modules/ROOT/pages/utilities.adoc#L14

Cannot reach https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html. Status: 404
Raw output
message:"Cannot reach https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html. Status: 404"  location:{path:"docs/modules/ROOT/pages/utilities.adoc"  range:{start:{line:14  column:2}  end:{line:14  column:112}}}  severity:ERROR  source:{name:"linkspector"  url:"https://github.com/UmbrellaDocs/linkspector"}

Check failure on line 14 in docs/modules/ROOT/pages/utilities.adoc

View workflow job for this annotation

GitHub Actions / Linkspector

[linkspector] docs/modules/ROOT/pages/utilities.adoc#L14

Cannot reach https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html#tymethod.supports_interface. Status: 404
Raw output
message:"Cannot reach https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html#tymethod.supports_interface. Status: 404"  location:{path:"docs/modules/ROOT/pages/utilities.adoc"  range:{start:{line:14  column:165}  end:{line:14  column:303}}}  severity:ERROR  source:{name:"linkspector"  url:"https://github.com/UmbrellaDocs/linkspector"}

Check failure on line 14 in docs/modules/ROOT/pages/utilities.adoc

View workflow job for this annotation

GitHub Actions / Linkspector

[linkspector] docs/modules/ROOT/pages/utilities.adoc#L14

Cannot reach https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html#tymethod.supports_interface. Status: 404
Raw output
message:"Cannot reach https://docs.rs/openzeppelin-stylus/0.1.0-rc/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html#tymethod.supports_interface. Status: 404"  location:{path:"docs/modules/ROOT/pages/utilities.adoc"  range:{start:{line:14  column:404}  end:{line:14  column:542}}}  severity:ERROR  source:{name:"linkspector"  url:"https://github.com/UmbrellaDocs/linkspector"}

[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.

0 comments on commit 89ee202

Please sign in to comment.