Home

Awesome

Foundry Simple Storage

This is part of the Cyfrin Updraft Solidity Blockchain Course.

⭐️ Updraft | Foundry Simple Storage

🎥 YouTube (6:23:59) | Section 6 | Foundry Simple Storage

Getting Started

Requirements

<!-- If you're installing foundry for the first time, you can put this in your `.bash_profile` or `.zshrc` depending on if you're using bash or zsh shell. You can check which shell you are currently using by looking at the value of the SHELL environment variable or examining the current process name. Run the following command in your terminal: ```bash echo $SHELL ``` And you'll see if you're using bash or zsh. ```bash if [ -f ~/.bashrc ]; then source ~/.bashrc fi ``` --> <!-- If you are using zsh, make sure to place your configurations, aliases, and functions in the .zshrc file located in your home directory. If the file doesn't exist, you can create it with touch ~/.zshrc. -->

Optional Gitpod

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.

Open in Gitpod

Setup

Clone this repo

git clone https://github.com/Cyfrin/foundry-simple-storage-cu
cd foundry-simple-storage-cu

Usage

  1. Setup your anvil chain by running the following in your terminal:
anvil

You'll get a large output where the ending looks something like:

==================

31337

Base Fee
==================

1000000000

Gas Limit
==================

30000000

Genesis Timestamp
==================

1717267365

Listening on 127.0.0.1:8545
  1. Copy the number at the end of Listening on XXX and place it into your .env file similar to what's in .env.example, and add an http:// to it.

.env Example:

RPC_URL=http://127.0.0.1:8545
  1. Scroll up in the anvil output, and you'll see a section that looks like this:
Private Keys
==================

(0) 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
(1) 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d
(2) 0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a
(3) 0x7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6
(4) 0x47e179ec197488593b187f80a00eb0da91f1b9d0b13f8733639f19c30a34926a
(5) 0x8b3a350cf5c34c9194ca85829a2df0ec3153be0318b5e2d3348e872092edffba
(6) 0x92db14e403b83dfe3df233f83dfa3a0d7096f21ca9b0d6d6b8d88b2b4ec1564e
(7) 0x4bbbf85ce3377467afe5d46f804f221813b2bb87f24d81f60f1fcdbf7cbf4356
(8) 0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97
(9) 0x2a871d0798f97d79848a013d4936a73bf4cc922c825d33c1cf7073dff6d409c6

Wallet
==================
Mnemonic:          test test test test test test test test test test test junk
Derivation path:   m/44'/60'/0'/0/

Grab the top key and place it into your .env.

.env Example:

PRIVATE_KEY=11ee3108a03081fe260ecdc106554d09d9d1209bcafd46942b10e02943effc4a

  1. Compile your code

Run

forge compile
  1. Deploy your contract
forge create SimpleStorage --private-key <PRIVATE_KEY>

Deploying to a testnet

Make sure you have a metamask or other wallet, and export the private key.

IMPORTANT

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.

  1. Export your private key, and place it in your .env file. DO NOT USE A KEY ASSOCIATED WITH REAL FUNDS. If you plan to, follow the instructions laid out in this section:
    1. Never use a .env again
    2. Or this one
  2. Go to Alchemy and create a new project on the testnet of choice (ie, Sepolia)
  3. Grab your URL associated with the testnet, and place it into your .env file.
  4. 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)
  5. Run
forge create SimpleStorage --private-key <PRIVATE_KEY> --rpc-url <ALCHEMY_URL>

Or, you can use a deploy script!

forge script script/DeploySimpleStorage.s.sol --private-key <PRIVATE_KEY> --rpc-url <ALCHEMY_URL>

zkSync instructions

Prerequisites

Run a local zkSync test node

  1. Setup the config
npx zksync-cli dev config 

Select in-memory node and no additional plugins.

  1. Run the node
npx zksync-cli dev start

If you get an error like: Command exited with code 1: Error response from daemon: dial unix docker.raw.sock: connect: connection refused, this means docker is not running.

Deploy to zkSync

forge create src/SimpleStorage.sol:SimpleStorage --rpc-url  http://127.0.0.1:8011 --private-key xxx --legacy --zksync

or

forge script script/DeploySimpleStorage.s.sol --private-key xxx --rpc-url http://127.0.0.1:8011 --legacy --zksync --broadcast

Thank you!

If you appreciated this, feel free to follow me or donate!

ETH/zkSync/Arbitrum/Optimism/etc Address: cyfrin1.eth: 0x3846c3A30E62075Fa916216b35EF04B8F53931f6 (Confirm the ENS matches the address!)

Patrick Collins Twitter Patrick Collins YouTube Patrick Collins Linkedin Patrick Collins Medium