-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbed06f
commit 13013a0
Showing
2 changed files
with
26 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
* xref:access-control.adoc[Access Control] | ||
* xref:crypto.adoc[Cryptography] | ||
* xref:utilities.adoc[Utilities] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,43 @@ | ||
= Utilities | ||
|
||
The OpenZeppelin Contracts provide a ton of useful utilities that you can use in your project. For a complete list, check out the xref:api:utils.adoc[API Reference]. | ||
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/utils/index.html[API Reference]. | ||
Here are some of the more popular ones. | ||
|
||
[[introspection]] | ||
== Introspection | ||
|
||
In Solidity, it's frequently helpful to know whether or not a contract supports an interface you'd like to use. ERC-165 is a standard that helps do runtime interface detection. Contracts provide helpers both for implementing ERC-165 in your contracts and querying other contracts: | ||
As well as in Solidity, at Stylus, 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 provide helpers both for implementing ERC-165 in your contracts and querying other contracts: | ||
|
||
* xref:api:utils.adoc#IERC165[`IERC165`] — this is the ERC-165 interface that defines xref:api:utils.adoc#IERC165-supportsInterface-bytes4-[`supportsInterface`]. When implementing ERC-165, you'll conform to this interface. | ||
* xref:api:utils.adoc#ERC165[`ERC165`] — inherit this contract if you'd like to support interface detection using a lookup table in contract storage. You can register interfaces using xref:api:utils.adoc#ERC165-_registerInterface-bytes4-[`_registerInterface(bytes4)`]: check out example usage as part of the ERC-721 implementation. | ||
* xref:api:utils.adoc#ERC165Checker[`ERC165Checker`] — ERC165Checker simplifies the process of checking whether or not a contract supports an interface you care about. | ||
* include with `using ERC165Checker for address;` | ||
* xref:api:utils.adoc#ERC165Checker-_supportsInterface-address-bytes4-[`myAddress._supportsInterface(bytes4)`] | ||
* xref:api:utils.adoc#ERC165Checker-_supportsAllInterfaces-address-bytes4---[`myAddress._supportsAllInterfaces(bytes4[\])`] | ||
* https://docs.rs/openzeppelin_stylus/utils/introspection/erc165/trait.IErc165.html[`IERC165`] — this is the ERC-165 trait that defines https://docs.rs/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/utils/introspection/erc165/trait.IErc165.html#tymethod.supports_interface[`supportsInterface`] function in your contract. | ||
|
||
[source,solidity] | ||
[source,rust] | ||
---- | ||
contract MyContract { | ||
using ERC165Checker for address; | ||
bytes4 private InterfaceId_ERC721 = 0x80ac58cd; | ||
/** | ||
* @dev transfer an ERC-721 token from this contract to someone else | ||
*/ | ||
function transferERC721( | ||
address token, | ||
address to, | ||
uint256 tokenId | ||
) | ||
public | ||
{ | ||
require(token.supportsInterface(InterfaceId_ERC721), "IS_NOT_721_TOKEN"); | ||
IERC721(token).transferFrom(address(this), to, tokenId); | ||
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 Solidity. Contracts provides these libraries for enhanced data structure management: | ||
Some use cases require more powerful data structures than arrays and mappings offered natively in Stylus sdk. | ||
Contracts provides these libraries for enhanced data structure management: | ||
|
||
- xref:api:utils.adoc#BitMaps[`BitMaps`]: Store packed booleans in storage. | ||
- xref:api:utils.adoc#Checkpoints[`Checkpoints`]: Checkpoint values with built-in lookups. | ||
- https://docs.rs/openzeppelin_stylus/utils/structs/bitmap/index.html[`BitMaps`]: Store packed booleans in storage. | ||
- https://docs.rs/openzeppelin_stylus/utils/structs/checkpoints/index.html[`Checkpoints`]: Checkpoint values with built-in lookups. |