Ethereum Request for Comments 20 was proposed by Fabian Vogelsteller in November of 2015. It is the most used and developed smart contract standard on the Ethereum blockchain. It defines a set of rules that developers must adhere to for it to be an ERC20 token. This standard is only for fungible tokens, not NFTs, and can only be exchanged for other ERC20 tokens.
MetaMask serves as a crypto wallet for Ethereum based tokens such as ERC20 and ERC721. It connects with other developer tools and DAPPs like Remix IDE, making it ideal for our workflow.
Remix IDE is an in-browser IDE that's focused on smart contract development. It offers a rich selection of plugins for debugging and testing. And educational plugins, like LearnEth, for new software developers in the blockchain space.
OpenZeppelin supplies an open-source library of high-quality, audited, Ethereum smart contracts. You can import all the base contracts from their GitHub. You could write the contract by hand, but this is prone to errors and not recommended. Let's take advantage of the tools available to us!
Let's look at the most basic implementation of an ERC20 contract:
I know what you're thinking. “That's it?” Yep, that’s all we need to deploy our Kickstand contract, let's go through line by line:
line 1: This is the SPDX License ID, every contract should have this at the top of the file. Here, we are saying this file is under the MIT license.
line 2: The version pragma enables certain compilers and checks. Our contract, will not compile with versions earlier than 0.8.4 or any version starting with 0.9.0 (denoted by the ^).
line 4: We are importing the base ERC20 contract from Open Zeppelin. This gives us all the required functions and events that define an ERC20 contract.
line 7: Much like classes, our Kickstand contract needs a constructor. Following this, we supply arguments to our base contract constructor, name, and symbol. The constructors execute once when the contract is deployed.
line 8: This function is minting our tokens. It takes two arguments. The first, msg.sender, is the address that deploys the contract and the second, is our contract's total amount of tokens. The equation says we want an initial supply of 10,000 tokens with a decimal value of decimals(). The function decimals() returns 18, which Ethereum and most ERC20 tokens use as a decimal value. So our contract will have an initial supply of 10,000.000000000000000000 tokens and technically be exchangeable with Ethereum. Pretty cool!
Next, you’ll need to select Kickstand.sol from the Contract dropdown and hit the deploy button.
Remix provides a sandbox for testing your contracts. The buttons are the functions that we are inheriting from the base ERC20 contract. Blue buttoned functions are either pure or view functions, meaning they are just reading properties or returning a value, so they don’t cost any gas (FREE!). Orange buttoned functions cost gas because they are writing to the blockchain.
These are the getters, functions, and events our contract is inheriting.
totalSupply() Returns the amount of the contract tokens that are in existence.
balanceOf () Returns the number of tokens of the given account.
allowance() Returns the remaining number of tokens that the spender is allowed to spend on behalf of the owner.
transfer() The amount of tokens specified by the function caller is sent to the recipient address. Returns a Boolean dependent on the success of the transfer. Emits the Transfer event.
approve() The function caller sets an amount the spender is allowed to transfer. Returns a Boolean dependent on the operation success. Emits the Approval event.
transferFrom() Moves the amount of tokens from the sender to the recipient. Returns a Boolean dependent on the operation success.
Transfer This event is emitted when the amount, value, of tokens are sent from the from address to the to address.
Approval This event is emitted when the amount, value, of tokens is approved by the owner to be used by the spender.
Below is a working contract that extends the ERC20 base contract, but rolls all our functions. We are grabbing the Transfer and Approval events from the base ERC20 from Open Zeppelin.
Now that we better understand our Kickstand contract, let's deploy it to a public test net.
Download the MetaMask browser extension. Then, sign in or sign up and choose the appropriate test network.
Get the test ether for that network. If you selected Ropsten Network, you’ll need to search for Ropsten faucets. It can take some time for the test ether to show up in your wallet, so you should extract ether from multiple faucets.
Back in the Remix IDE. In Deploy & Run, select Injected Web3 from the Environment pulldown.
Now you should see Kickstand.sol as an option in the Contract selection box, select it and hit the Deploy button.
A MetaMask popup will ask you to confirm the transaction.
And that’s it. You’ve written and deployed an ERC20 contract!
The crypto space is growing fast and ERC20 is just one of many different contract standards available to blockchain developers. Here are some awesome resources and tutorial that helped me along the way:
CryptoZombies: A gamified tutorial that teaches you blockchain and solidity development through ZOMBIES!!!.
Ethernaut: A Web3/Solidity-based wargame. Each level is a smart contract that needs to be 'hacked'. Created by the folks at OpenZeppelin so you know it's gonna be good!
OpenZeppelin Docs: The docs have comprehensive guides for all things contract and dapp development.
Ethereum Foundation: This is probably the best resource available to solidity developers. You'll find guides for all things Ethereum and Web3 as well as forums where you can interact with others in the community.
And if you are interested in blockchain development or want to have a DAPP built, feel free to drop us a line and we can chat.
Kick your software into gear