This is part of the FreeCodeCamp Solidity & Javascript Blockchain Course.
Video Link : ⌨️ (05:30:42) Lesson 5: Ethers.js Simple Storage
git --version and you see a response like git version x.x.xnode --version and get an ouput like: vx.x.xnpm
yarn --version and get an output like: x.x.x
If you can’t or don’t want to run and install locally, you can work with this repo in Gitpod. If you do this, you can skip the clone this repo part.
Clone this repo
git clone https://github.com/PatrickAlphaC/ethers-simple-storage
cd ethers-simple-storage
Then install dependencies
yarn
Note: You’ll notice in our
package.jsonwe are using"solc": "0.8.7-fixed". Usually, you’ll just be able to do"solc": "0.8.7"to get a specific version, but there was a bit of an issue with that one… You’ll find out why we use 0.8.7
If you like typescript, run git checkout typescript then run yarn
quickstart on your ganache applicationSave the workspace. This way, next time you open ganache you can start the workspace you’ve created, otherwise you’ll have to redo all the steps below.
RPC SERVER sting in your ganache CLI, and place it into your .env file similar to what’s in .env.example.
.env Example:
RPC_URL=http://0.0.0.0:8545
.env file, similar to what you see in .env.example.
.env Example:
PRIVATE_KEY=11ee3108a03081fe260ecdc106554d09d9d1209bcafd46942b10e02943effc4a
Run
yarn compile
You’ll see files SimpleStorage_sol_SimpleStorage.abi and SimpleStorage_sol_SimpleStorage.bin be created.
node deploy.js
yarn add ganache
Settings > Server > Host Name
Change Host Name to vEthernet (WSL)
node deploy.js
Make sure you have a metamask or other wallet, and export the private key.
undefinedIMPORTANTundefined
USE A METAMASK THAT DOESNT HAVE ANY REAL FUNDS IN IT. Just in case you accidentally push your private key to a public place. I highly recommend you use a different metamask or wallet when developing.
Export your private key and place it in your .env file, as done above.
Go to Alchemy and create a new project on the testnet of choice (ie, Sepolia)
Grab your URL associated with the testnet, and place it into your .env file.
Make sure you have testnet ETH in your account. You can get some here. You should get testnet ETH for the same testnet that you made a project in Alchemy (ie, Sepolia)
Run
node deploy.js
Troubleshooting: Ethers 6.X.X instability
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL)
^
TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
If you are experiencing issues with the latest version of Ethers, ethers ^6.0.2, which appears to be unstable, you can easily solve the problem by downgrading to a previous version of Ethers, such as ethers 5.7.2.
To downgrade to ethers 5.7.2, run either of the following commands on your command line interface, depending on your package manager:
For npm:
npm install ethers@5.7.2
For yarn:
yarn add ethers@5.7.2
If you appreciated this, feel free to follow me or donate!
ETH/Polygon/Avalanche/etc Address: 0x9680201d9c93d65a3603d2088d125e955c73BD65