Objectives
By the end of this lesson you should be able to:- Import and use code from another file
- Utilize OpenZeppelin contracts within Remix
OpenZeppelin
OpenZeppelin has a robust library of well-documented smart contracts. These include a number of standard-compliant token implementations and a suite of utilities. All the contracts are audited and are therefore safer to use than random code you might find on the internet (you should still do your own audits before releasing to production).Docs
The docs start with installation instructions, which we’ll return to when we switch over to local development. You do not need to install anything to use these contracts in Remix. Find the documentation for theEnumerableSet
under Utils. This library will allow you to create sets of bytes32
, address
, and uint256
. Since they’re enumerated, you can iterate through them. Neat!
Implementing the OpenZeppelin EnumerableSet
Create a new file to work in and add thepragma
and license identifier.
In Remix, you can import libraries directly from GitHub!
EnumerableSet.sol
pop into your workspace files, nested deeply in a bunch of folders.
Trying It Out
Add a contract calledSetExploration
. Review the extensive comments within the contract itself.
To use the EnumerableSet
, you need to use the using
keyword. This directive attaches all of the library methods to the type. Doing so allows you to call the method on the variable with dot notation, and the variable itself will be supplied as the first argument.
Follow the pattern in the example in the comments, but name the variable visitors
:
registerVisitor
that makes use of the library’s add
function to add the sender of the message to the visitors
set.
There’s also an
_add
function, which is private.Reveal code
Reveal code
numberOfVisitors
. Thanks to using
, this can cleanly call the length
function:
Reveal code
Reveal code
Conclusion
In this lesson, you imported a library from OpenZeppelin and implemented some of its functions. You also learned how to use theusing
keyword.