Materials prepared for ETHSydney August - Get started with developing in Ethereum, Tuesday Aug 13 2024.
If you like this, please share it.
-
Have the capabilities to launch your own sh*tcoin within minutes!
You can do this on Ethereum mainnet for the cost of ~600k gas or ~USD 3, at time of writing. As well as most "EVM" (Ethereum Virtual Machine) compatible chains like Arbitrum, Base or Optimism.
-
Demystify ERC-20s
An ERC-20 token contract is a little bit like a database table with two columns - owner and balance. And with stored procedures to implement transfer rules.
balances: section in the ERC-20 browser.
With events logs recording the transfers
events: section in the ERC-20 browser.
Token amounts are stored as raw numbers with a specified number of
decimals
,18
in this case.
- Deploy your own ERC-20 token contract - contracts/ERC20Token.sol (Sepolia 0x1e5D...4D59) or contracts/FixedSupplyToken.sol (Sepolia 0x8fAF...d093).
- Interact with your own ERC-20 token contract
- Deploy a simple vault that operates with your ERC-20 token contract - contracts/SimpleVault.sol
- Interact with your simple vault
On completion of Exercises 1 & 2, you will have deployed and interacted with your first ERC-20 token contract on the Ethereum Sepolia testnet
I built a simple tool to inspect and interact with ERC-20 token contracts. The main source code file is docs/index.html. View the developer pane when running this tool to see the simple interactions required between your web browser and the blockchain.
This tool can also be run locally by executing e.g. anywhere in the docs/ folder of this GitHub repository downloaded onto your local machine
- Web browser, with the MetaMask https://metamask.io/, or Rabby https://rabby.io/ browser plug-in
- Sepolia testnet ethers in your MetaMask account. Try https://www.alchemy.com/faucets, https://www.infura.io/faucet/sepolia, https://sepolia-faucet.pk910.de/ or https://cloud.google.com/application/web3/faucet/ethereum/sepolia
- For developing and testing on your local computer, familiarity with the Linux, MacOS or Windows command line. And npx to run commands from Node packages.
- Contents and UI for ERC-721 and ERC-1155
- Overview
- Exercise 1 - Deploy Your Own ERC-20 Token Contract
- Exercise 2 - Interact With Your ERC-20 Token Contract
Go to https://remix.ethereum.org/. In the FILE EXPLORER
tab, create a new ERC20Token.sol
document under default_workspace
-> contracts
. Copy the content from contracts/ERC20Token.sol or , or contracts/FixedSupplyToken.sol and paste into your newly created ERC20Token.sol
document.
Change the _name
and _symbol
on lines 80 and 81. Save and you should have a green tick below the third icon on the sidebar.
Switch to the SOLIDITY COMPILER
tab. Note the COMPILER
version, 0.8.26+commit.8a87fa7a
in this case. Check that Enable optimisation
is ticked and the number beside it set to 200
.
Switch to the DEPLOY & RUN TRANSACTIONS
tab. Set ENVIRONMENT
to Injected Provider - MetaMask
and your MetaMask account should appear under the ACCOUNT
label.
Select CONTRACT
to be ERC20Token - contracts/ERC20Token.sol
.
Click [Deploy], and [Confirm] your transaction in MetaMask.
In the bottom pane, you will see your completed transaction and newly created contract address
. 0xac09587d186d70d93dd9b16328c2e4fa845cc9cf
in my case.
The contract can be viewed at https://sepolia.etherscan.io/address/0xac09587d186d70d93dd9b16328c2e4fa845cc9cf, in my case.
Switch to the Contract
tab.
Click on [Verify and Publish] to upload the source code for your ERC-20 token contract. Select the Compiler Type
of Solidity(Single file)
. Select the Compiler Version
to compiler version you compiled your contract with, 0.8.26+commit.8a87fa7a
currently. Select the Open Source License Type
of 3) MIT License (MIT)
. Click [Continue].
Paste the contents of ERC20Token.sol from Remix into the text box under Enter the Solidity Contract Code below
.
Set Optimization
to Yes
and Runs
to 200
, matching the same parameters in Remix above.
Click [Verify and Publish].
CONGRATULATIONS You have published your ?first contract!
The newly deployed contract code can be viewed at https://sepolia.etherscan.io/address/0xac09587d186d70d93dd9b16328c2e4fa845cc9cf#code, in my case.
The ERC-20 token transactions can be viewed at https://sepolia.etherscan.io/token/0xac09587d186d70d93dd9b16328c2e4fa845cc9cf, in my case. You can see 1,000,000
tokens being minted from address 0x0000...0000
to deploying address.
Click on the [Holders] tab to view the account holdings.
View your ERC-20 token contract on https://bokkypoobah.github.io/GetStartedWithDevelopingInEthereum/.
Using my newly deployed ERC-20 token contract 0xAC09587d186D70d93dd9B16328C2E4fA845cC9CF
, you can see the initially minted 1,000,000
TOOSEXY
tokens transferred to my account when my ERC-20 token contract was deployed.
If required, create your second MetaMask account and transfers some Sepolia testnet ethers to it.
In the transfer: block, set to:
to your second MetaMask account, and tokens:
to 10
. Click [Transfer], then confirm the transaction in MetaMask.
After MetaMask sends your transaction, you should see your txHash:
. Click on the link to View in explorer
if you want to view your transaction.
Click [Retrieve], and you can see the new transaction of 10
tokens, and 3 rows in the balances: table.
Execute a second transfer of 1
token to your second account.
Click [Retrieve] at the top of the page, and you can see the new transaction of 1
token, and balances: table updated appropriately.
In the approve: block, set spender:
to your second MetaMask account, and tokens:
to 100
. Click [Approve], then confirm the transaction in MetaMask.
After MetaMask sends your transaction, you should see your txHash:
. Click on the link to View in explorer
if you want to view your transaction.
Click [Retrieve] at the top of the page, and you can see the new Approval
transaction of 100
token, and allowances: table updated appropriately.
In the allowance: block, you can check the allowance just set.
Switch to your second account in MetaMask.
In the transferFrom: block, set from:
to be your first account, and tokens:
to be 0.12345
. Click [Transfer From], then confirm the transaction in MetaMask.
Click [Retrieve] at the top of the page, and you can see the new Transfer
transaction of 0.12345
token, and balances: table updated appropriately.
In the allowance: block, you can see that the allowance of 100
that you set has now been reduced to 99.87655
, as account2 has used 0.12345
of their allowance.
TODO
- Deploy and interact with a simple gadget that uses the ERC-20 basic building block
- Set up the Hardhat testing environment - https://hardhat.org/docs
- Modify contracts/SimpleVault.sol to break the functionality and confirm with the tests
- Deploy your own SimpleVault to the Sepolia testnet
- Send and withdraw some of your very own ERC-20 tokens created in Exercise 1
npm install --save-dev hardhat
# Run test
npx hardhat test
# Or run test and save output in ./testIt.out
./10_testIt.sh
# 00_test_0
# Deployment
# Signers
# * owner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
# * otherAccount: 0x70997970C51812dc3A010C7d01b50e0d17dc79C8
# Deploying ERC20
# * symbol: MYSYMBOL
# * name: My Name
# * decimals: 18
# * totalSupply: 1000000000000000000000000
# Deploying SimpleVault
# * owner: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
# ✔ ERC20 token should have the correct symbol, name, decimals and totalSupply (548ms)
# ✔ ERC20 token should emit an event on transfers and balanceOf adds up
# ✔ SimpleVault should process token deposits and withdrawals correctly
#
#
# 3 passing (581ms)
e.g., Bank balance, Opal card balance, transfers
e.g., Bank account number, Opal card number, drivers license, registry item
e.g., Game items
- Solidity compiler and deployer - https://remix.ethereum.org/
- Solidity Documentation - https://docs.soliditylang.org/
- OpenZeppelin Contracts - https://github.com/OpenZeppelin/openzeppelin-contracts
- Hardhat development environment - https://hardhat.org/
- mindmap/GetStartedWithDevelopingInEthereum-202408.mm that can be viewed in FreePlane
Enjoy!
(c) BokkyPooBah / Bok Consulting Pty Ltd 2024. The MIT Licence.