Final project for "Alchemy University, The Ethereum Developer Bootcamp". Took also inspiration from the "ERC20 Tokens by Block Explorer" series.
This project aims to create a token called "SolidityToken $STK". The tokens are then distributed to users who request them from the faucet. Contracts built with Hardhat. UI built with Ethers and Browserify.
Index |
---|
Project Layout |
Gas optimizations |
Tests |
Deploy the contracts |
SolidityToken Faucet UI |
There are three main folders:
contracts/contract
- contains the solidity contractscontracts/test
- contains test for the solidity contract of the ERC20 tokenstkfaucet-ui/
- contains the ui
The following gas optimizations were implemented in the contracts.
- x += y costs more gas than x = x + y for state variables
- Description: Gas can be saved by substituting the addition operator with plus-equals, same for minus
- References:
- Use of assembly to check for address(0)
- Description: By checking for
address(0)
using assembly language, you can avoid the use of more gas-expensive operations such as calling a smart contract or reading from storage. This can save 6 gas per instance. - Reference:
- Description: By checking for
- Mark payable functions guaranteed to revert when called by normal users
- Description: If a function modifier, like onlyOwner, is applied, the function will revert if a regular user attempts to pay it. Making the function payable will save valid callers money on gas since the compiler won't do checks to see if a payment was made.
The extra opcodes avoided are
CALLVALUE(2)
,DUP1(3)
,ISZERO(3)
,PUSH2(3)
,JUMPI(10)
,PUSH1(3)
,DUP1(3)
,REVERT(0)
,JUMPDEST(1)
,POP(2)
which costs an average of about 21 gas per call to the function, in addition to the extra deployment cost. - Reference:
- Description: If a function modifier, like onlyOwner, is applied, the function will revert if a regular user attempts to pay it. Making the function payable will save valid callers money on gas since the compiler won't do checks to see if a payment was made.
The extra opcodes avoided are
- Install dependencies using the following command
npm install
- Add a
.env
file using either the dummy keys provided in.env.example
or your own - To run all the tests for the contracts, use the following command
npx hardhat test --network hardhat
- From the contracts folder, follow the following steps
- Use the following command to deploy
SolidityToken.sol
on the Sepolia testnetnpx hardhat run --network sepolia scripts/deploySolidityToken.js
- Copy the address displayed in the terminal as a result. Paste it in
deployFaucet.js
in5: const contract = await Contract.deploy("ADDR-SOLIDITYTOKEN");
- Use the following command to deploy
Faucet.sol
on the Sepolia testnetnpx hardhat run --network sepolia scripts/deployFaucet.js
A version of Faucet is already deployed at 0x8e830f030a1a9094bbb6ae779879177f65a47f81 and a version of SolidityToken is deployed at 0x16e41846e3be94bd529afa9b475899bd15d88bc1 on the Sepolia testnet.
Host the project in the stkfaucet-ui directory on a server. If you're using Visual Studio, you can use the Live Server extension.
dist/bundle.js
is generated using Browserify with the following command
browserify src/index.js --standalone bundle -o dist/bundle.js