This project is for practice solidity with hardhat and testing contract with chai. The main contract is 'Courses' contract which only owner can create course and update.The other people will interact with 'Cart' contract which need to choose index of course to cart array and then make the payment of total price of courses in the cart.The 'Courses' contract will act as parent of 'Cart' contract. You will learn about struct, array, map, inherit, custom error and so on from this project.
/contracts/MyPractice
course.sol
cart.sol
exam.sol
I currently update this project everyday on branch dev and remain branch main as stable version.
This project requires NodeJS (version 8 or later) and NPM. Node and NPM are really easy to install. To make sure you have them available on your machine,try running the following command.
$ npm -v && node -v
8.3.1
v16.14.0
Check solidity compiler version:
solcjs --version
0.8.9 commit.e5eed63a.Emscripten.clang
Install dependencies listed in package.json:
npm install
- Solidity Learning
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
BEFORE YOU INSTALL: please read the prerequisites
Start with cloning this repo on your local machine:
$ git clone https://github.com/Thanasornsawan/solidity-learning.git
$ cd solidity-learning
First terminal try this command:
npx hardhat node
Second terminal try this command:
npx hardhat run --network localhost scripts/testCart.js
But if you want to run on local without hardhat network, just remove network option:
npx hardhat run scripts/testCart.js
Back to the first terminal that have hardhat node, it will show gas execution all of contract.
If the function fall to the custom error in solidity,it will shows like this picture.
Check code coverage in solidity contract for make testing script cover all functions.
npx hardhat coverage
You can test the smart contract and view gas usage compare with currency on each testing function by hardhat gas reporter plugin. About setting currency for gas reporter is optional, you can get API KEY from (coinmarketcap pro)[https://pro.coinmarketcap.com/signup/] and then set credential COINMARKETCAP_API_KEY in .env file and then config the currency name in the hardhat.config.js
.
npx hardhat test
Turn on plugin hardhar gas reporter in the hardhat.config.js
gasReporter: {
enabled: (process.env.REPORT_GAS) ? true : false,
}
Turn off hardhat gas reporter in the hardhat.config.js.
gasReporter: {
enabled: (process.env.REPORT_GAS) ? false : true,
}
You can view another solution of test.js by openzeppelin-testhelper from here
solhint 'contracts/**/*.sol'
- Edit rule in file
.solhint.json
see detail
Deploy contract and proxy on rinkeby
PS C:\Users\Ploy\Documents\moralis\solidity\solidity-learning> npx hardhat .\scripts\deployProxy.js --network rinkeby
Compiled 2 Solidity files successfully
Deploying Proxy...
proxy address: 0x61509bda0A25247a3AFa430DC8d25601038fDecb
** check contract address on etherscan found address 0x086da8f8f4fdbbebe41e08793a47335b300ee21b
Verify the implementation contract on rinkeby
PS C:\Users\Ploy\Documents\moralis\solidity\solidity-learning>npx hardhat verify --network rinkeby 0x086da8f8f4fdbbebe41e08793a47335b300ee21b
Nothing to compile
Successfully submitted source code for contract
contracts/upgrade/MyTokenUpgradeable.sol:MyTokenProxy at 0x086da8f8f4fdbbebe41e08793a47335b300ee21b
for verification on the block explorer. Waiting for verification result...
Successfully verified contract MyTokenProxy on Etherscan.
https://rinkeby.etherscan.io/address/0x086da8f8f4fdbbebe41e08793a47335b300ee21b#code
Verify the proxy contract that it is proxy on etherscan
Click tab 'Contract' > 'More Options' > 'Is this a proxy?" > click 'verify' button.
Make new version of contract and re-deploy to the same proxy address
PS C:\Users\Ploy\Documents\moralis\solidity\solidity-learning> npx hardhat run .\scripts\upgradeProxy.js --network rinkeby
Upgrading Proxy...
proxy upgrade successfully
Verify the implementation contract on rinkeby
PS C:\Users\Ploy\Documents\moralis\solidity\solidity-learning> npx hardhat verify --network rinkeby 0x83797E6727A9C470aA46e72Ed07cf466d58EFdfA
Nothing to compile
Successfully submitted source code for contract
contracts/upgrade/MyTokenImplementation.sol:MyTokenImplementation at 0x83797E6727A9C470aA46e72Ed07cf466d58EFdfA
for verification on the block explorer. Waiting for verification result...
Successfully verified contract MyTokenImplementation on Etherscan.
https://rinkeby.etherscan.io/address/0x83797E6727A9C470aA46e72Ed07cf466d58EFdfA#code
The new implementation contract show method "Upgrade To" the same proxy address 0x61509bda0A25247a3AFa430DC8d25601038fDecb
Get the Gnosis contract via openzeppelin defender
Fill contact detail and owner address and then click "Create Gnosis Safe"
Put the Gnosis contract address in the script to change owner
PS C:\Users\Ploy\Documents\moralis\solidity\solidity-learning> npx hardhat run .\scripts\transferOwnerGnosis.js --network rinkeby
Previous owner: 0x176366cFD97885245fAEA72f8cB6951e52655Adf
Transferring ownership of ProxyAdmin...
New owner: 0x07F6310dD5bA6f545b1517F3fFf93B0E4C904401
Transferred ownership of ProxyAdmin to: 0x07F6310dD5bA6f545b1517F3fFf93B0E4C904401
Upgrade proxy contract to openzeppelin defender
PS C:\Users\Ploy\Documents\moralis\solidity\solidity-learning> npx hardhat run .\scripts\upgradeDefender.js --network rinkeby
Preparing proposal...
Upgrade proposal created at: https://defender.openzeppelin.com/#/admin/contracts/rinkeby-0x61509bda0A25247a3AFa430DC8d25601038fDecb/proposals/859f1761-3f31-450b-bfba-911501f9a438
You can view your proposal at admin panel here and click 'View propasal'
Sign transaction with metamask from one of owner in Gnosis account
Click 'Execute' to process the upgrade contract
You can check all transaction that you do with your proxy contract here
After click icon 'view in block explorer' nearby address of proxy contract, you will see your proxy changed
You can change policy of maximum people to approve proposal on Gnosis website
I use wsl linux ubuntu on windows because some tools not works on windows rigth now.The path to windows is /mnt
slither contracts/cart.sol --solc-remap "@openzeppelin=./node_modules/@openzeppelin"
You can check new version from https://github.com/crytic/echidna/releases
wget https://github.com/crytic/echidna/releases/download/v2.0.0/echidna-test-2.0.0-Ubuntu-18.04.tar.gz
sudo tar xzvf echidna-test-2.0.0-Ubuntu-18.04.tar.gz
sudo ln -s /mnt/c/Users/Ploy/Documents/echidna-test /usr/local/bin/echidna-test
chmod x /mnt/c/Users/Ploy/Documents/echidna-test
Echidna properties are Solidity functions.A property must: Have no argument Return true if it is successful Have its name starting with echidna refer from building-secure-contracts
echidna-test contracts/course.sol --crytic-args "--solc-remap @openzeppelin=./node_modules/@openzeppelin"
echidna-test ./ --contract Courses --config echidna-config.yaml
Result may return
Analyzing contract: /mnt/c/Users/Ploy/Documents/moralis/solidity/solidity-learning/contracts/course.sol:Courses
echidna-test: No tests found in ABI
Because currently,I still didn't make any function for echidna to test
- Thanasornsawan Varathanamongkolchai - Thanasornsawan