Advanced smart contract programming is no walk in the park. However, if you focus on standardized smart contracts, you can deploy them in minutes. How is that possible? By importing OpenZeppelin contracts in Remix, which is what we’ll demonstrate in this article. Now, to use Remix to import OpenZeppelin contracts, a single line of code does the trick in most cases. For example, to create an ERC20 token, the following line of code introduces a verified smart contract template from OpenZeppelin in Remix:
import “https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol”;
If you wish to learn how to create a simple ERC-20 smart contract that utilizes the above line of code, make sure to use the video above or follow our lead in the “OpenZeppelin ERC-20 Example” section below. In addition, if you wish to start building dapps, such as Web3 wallets and portfolio trackers that can fetch on-chain data and listen to existing smart contracts, create your free Moralis account and start BUIDLing!
Overview
The core of today’s article will be our tutorial on creating an ERC-20 token. As we do so, we will utilize MetaMask, Remix, and OpenZeppelin. MetaMask will confirm the on-chain transaction required to deploy our ERC-20 contract on the Goerli testnet. This is where it pays to know how to get Goerli ETH from a reliable test ether faucet. With a sufficient amount of Goerli ETH, you will get to cover transaction gas and, in turn, complete the “create ERC-20 token” feat. However, you must first use Remix to import OpenZeppelin and add the required lines of code to create a simple smart contract. Essentially, this will be a beginner-friendly smart contract programming tutorial.
In the second part of today’s article, we will provide more context surrounding the topic of importing OpenZeppeling contracts in Remix. As such, we will explore Remix, OpenZeppelin, and smart contracts and answer the “what is OpenZeppelin?” and “what is the Remix IDE?” questions.
Finally, we’ll look beyond smart contracts and the tools required to write and deploy them. After all, you can create killer dapps based on existing smart contracts. This is where Moralis – the leading Web3 API provider – simplifies Web3 development. We’ll even show you one of Moralis’ ultimate Token API endpoints in action – we’ll fetch our example ERC-20 token’s metadata.
OpenZeppelin ERC-20 Example – How to Import OpenZeppelin Contracts in Remix
To complete this OpenZeppelin ERC-20 example, make sure to have the aforementioned tools ready. You should have a MetaMask web browser plugin installed and topped with some Goerli ETH. Now, if you need help with that, use the “how to get Goerli ETH” link in the overview section. With that said, it’s time to show you how to import OpenZeppelin contracts and deploy them with Remix.
Before you can start importing OpenZeppelin smart contracts in Remix, you need to know how to access Remix and create a new project. So, use your browser (the one with the MetaMask extension) and open Remix. You can simply query Google for “Remix IDE”:
Once on the Remix project page, click on the “IDE” option in the top menu. Then, select “Remix Online IDE”:
Once on the Remix IDE home screen, focus on the left sidebar. This is where you can select the default “contracts” folder and use the “new file” icon to create a new smart contract file:
When it comes to naming your smart contract script, you can use your own name or follow our lead and go with “MoralisSmartToken.sol”. With your new “.sol” file ready, it’s time to populate it with the required lines of code. So, let’s show you how simple our OpenZeppelin ERC-20 example is.
OpenZeppelin in Remix – Using a Verified ERC-20 Smart Contract
The first line in every Solidity smart contract defines the version of Solidity. So, this is the code snippet that focuses on the “0.8.0” version:
//SPDX-License-Identifier: MIT pragma solidity ^0.8.0;
The “^” symbol before the version number signals that all newer Solidity versions will also work with our smart contract. After the pragma line, we have a spot to import OpenZeppelin in Remix. There are several ways to do this, but we will use the GitHub link, as presented earlier. Hence, our example contract’s next line is the following:
import “https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol”;
By importing OpenZeppelin contracts in Remix, all functions and logic of imported contracts become available in your script. So, once you import the above ERC-20 contract, you have all you need to create your ERC-20 token.
Define Contract, Use Constructor, and Call the “Mint” Function
Below the import line, specify a new contract. Again, feel free to use the same name as we do (“MoralisSmartToken”) or choose your own name. This is the line of code that defines the new contract:
contract MoralisSmartToken is ERC20 {
Next, we need to add a constructor, which is a special kind of function that gets called the first time a smart contract is deployed to the network. When it comes to an ERC-20 constructor, it needs to take in two parameters: the token’s name and symbol. However, instead of hardcoding the actual name and symbol, you can use variables. That way, a user deploying the contract can choose the name and the symbol. This will become clearer once we show you how to deploy our example contract. Here are the lines of code you need to use to form a proper constructor:
constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) {
Inside any ERC-20 contract, there’s also the “mint” function. The latter takes in the address to which the minted tokens should be sent and the amount of an ERC-20 token to be minted. Since importing OpenZeppelin contracts in Remix also imports their functions, you can simply call the “mint” function inside your constructor:
_mint(msg.sender, 1000 * 10 **18);
In the line of code above, “msg.sender” is a global variable that refers to the address that deploys the smart contract. As far as the numbers inside the “mint” function go, they deal with the fact that Solidity doesn’t read decimals (18 decimal places) and will mint 1,000 tokens. This line is also the last line of our smart contract.
Our OpenZeppelin ERC-20 Example Script
Because you decided to use Remix to import an OpenZeppelin ERC-20 contract, the six lines of code covered above do the trick. Here’s the complete “MoralisSmartToken.sol” script:
//SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import “https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol”; contract MoralisSmartToken is ERC20 { constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) { _mint(msg.sender, 1000 * 10 **18); } }
Time to Deploy the Imported OpenZeppelin Contract in Remix
At this stage, you should have the above lines of code in place. As such, you can move beyond importing OpenZeppelin contracts in Remix. However, before you can deploy your smart contracts, you need to compile them, which is a simple two-click process. You just need to go to the “Solidity Compiler” tab and hit the “Compile [contract name]” button:
If there are no errors in your code, the following content appears in the side menu below the “Compile” button:
After successfully compiling your smart contract, it’s time to deploy it. This is where you’ll need your MetaMask wallet (connected to the Goerli testnet) and topped with some Goerli ETH. Once on the “Deploy and Run Transaction” tab, select “Injected Provider-MetaMask”:
On the same tab, there’s the “DEPLOY” section. The latter comes with a drop-down option where you get to enter your token’s name and symbol. With those variables assigned, you can click on the “transact” button and confirm the related transaction via the MetaMask module:
Add Your ERC-20 Token to MetaMask
As soon as the above transaction is confirmed, you can add your new ERC-20 token to your MetaMask. To do this, copy deployed contract’s address at the bottom of the “Deploy” tab in the Remix IDE:
Then, open your MetaMask, select the “Assets” tab and click on the “Import tokens” option:
On the “Import tokens” page, paste the above-copied contract address in the designated entry field. This will automatically populate the “symbol” and “decimals” fields. Finally, hit the “Add custom token” button:
That’s how easy it is to create and deploy an ERC-20 token using an OpenZeppelin contract in Remix! Now, if you’d like to dive into Remix, OpenZeppelin, and smart contracts further, read on!
Exploring Remix, OpenZeppelin, and Smart Contracts
To know how to import OpenZeppelin contracts, you don’t need to be an expert regarding the theory. However, understanding the basics usually makes things clearer. As such, let’s briefly cover Remix, OpenZeppeling, and smart contracts.
What are Remix and OpenZeppelin?
“Remix” is most commonly used to refer to the Remix IDE (integrated development environment). The latter is an open-source development environment that comes in the form of a web and desktop application. The Remix IDE enables devs to cover the entire process of smart contract development for Ethereum and other EVM-compatible chains. Furthermore, “Remix” may also refer to “the Remix project” – a platform for plugin architecture development tools. The leading sub-projects of the Remix project are the IDE, plugin engine, and Remix Libs. If you want to explore the Remix IDE in more detail, use the “what is the Remix IDE?” link in the overview of today’s article.
OpenZeppelin is an open-source platform for building secure dapps, with a framework providing the required tools to create and automate Web3 applications. OpenZeppelin also has strong audit services, which high-profile customers such as the Ethereum Foundation and Coinbase rely on. Aside from conducting security audits on demand and implementing security measures to ensure that your dapps are secure, OpenZeppelin has other tools/services. For instance, OpenZeppelin Defender is a web application that can, among many things, secure and automate smart contract operations. But what the majority of developers value the most is OpenZeppelin’s extensive library of modular and reusable contracts, one of which we used in today’s ERC-20 example. In case you want to dive deeper into the “what is OpenZeppelin?” topic, use the matching link in the overview.
What are Smart Contracts?
Smart contracts are on-chain programs – pieces of software stored on programmable blockchains such as Ethereum. Smart contracts are all about reinforcing predefined rules and automation. According to the code behind each smart contract, the latter executes predetermined actions when predefined conditions are met. As such, it’s obvious that standards must be set to ensure these on-chain programs’ validity, efficacy, and efficiency. One such standard is ERC-20, which focuses on fungible tokens. However, there are many other standards, and by importing OpenZeppeling contracts in Remix, you can comply with all ERC standards. If you’d like to explore two other token standards, make sure to read our “ERC721 and ERC1155” article.
Beyond Remix and OpenZeppelin
Whether you decide to use Remix to import OpenZeppelin and deploy your own smart contract or to focus on existing smart contracts, the key is to bring the power of automation to the public. This is where dapps play the most vital role. Moreover, now that you know how to import OpenZeppelin contracts, it’s time you start building dapps. Not long ago, you’d have to run your own RPC node and build your own infrastructure to create a decent dapp. But things have come a long way, and legacy devs can now easily join the Web3 revolution, and Moralis is arguably the best Web3 provider!
With a free account, you can access all of Moralis’ products:
Since all the powerful tools outlined above work on all leading blockchains, Moralis gives you a broad reach and future-proofs your work. Moralis is also all about cross-platform interoperability, enabling you to use your favorite Web2 programming languages, frameworks, and platforms to create killer dapps. To do so, these are the steps to take:
- Use Remix to import OpenZeppelin and deploy your smart contracts or focus on existing ones.
- Use Moralis to build dapps, such as Web3 wallets and portfolio trackers, enabling people to interact with smart contracts and Web3 in a user-friendly manner.
Token API Endpoint Example
In light of today’s “OpenZeppelin in Remix” tutorial, let’s look at the “getTokenMetadata” endpoint as an example. Just by accessing this endpoint’s reference page in Moralis’ docs, we can get any token’s metadata by choosing a chain and inserting a contract address:
Here are the results for the above-created “MST” token:
Now that you know how to import OpenZeppelin contracts, you can easily use the power of Moralis to target your contracts. Importing OpenZeppelin contracts in Remix or using any other alternative, start BUIDLing today with Moralis!
OpenZeppelin in Remix – Importing OpenZeppelin Contracts in Remix – Summary
In today’s article, we took you by the hand, showing you how to import OpenZeppelin contracts while focusing on an OpenZeppelin ERC-20 example. You now know that importing OpenZeppelin contracts in Remix is as simple as pasting a proper GitHub address right after the “import” line. Aside from learning how to use Remix to import OpenZeppelin, you also learned how to compile and deploy smart contracts using the Remix IDE. Moving beyond our “OpenZeppelin in Remix” tutorial, we also explained what OpenZeppelin, Remix, and smart contracts are. Last but not least, you also learned the gist of Moralis. As such, you are now ready to start building dapps around your or existing smart contracts.
Following today’s ERC-20 example, you could easily create an ERC-721 token or ERC-1155 NFTs. In both cases, you would import OpenZeppelin in Remix but instead focus on the ERC-721 or ERC-1155 contract templates. Also, during your development endeavors, you may need other useful tools, such as a reliable gwei to ETH calculator or a vetted crypto faucet.
Additionally, you may be interested in exploring other blockchain development topics. If so, make sure to check out the Moralis YouTube channel and the Moralis blog. Among many other topics, these are the places to learn more about the Solana Python API and the leading Solana testnet faucet. In case you’d like a more professional approach to your crypto education, consider enrolling in Moralis Academy. There you can start with Ethereum fundamentals or attend more advanced courses.
Read More: moralis.io