Smart Contract Backpack
Learn about Smart Contract Backpack - a CLI tool that allows developers to create production-ready and fully audited smart contracts
When you decide to include a Blockchain development environment in your project, other than setting up all the environment variables, dependencies, and package.json scripts, create-web3-dapp
will also allow you to start the Smart contracts backpack.
What is Smart Contract Backpack?
The Smart Contract Backpack is a CLI tool built on top of the OpenZeppelin smart contract libraries. It allows developers to create production-ready and fully audited smart contracts from the command line interface in a matter of seconds, ready to be deployed on the blockchain. It takes care of:
- Creating a smart contract: choosing from different standards and libraries to include
- Deployment scripts: will create the deployment scripts to deploy your contracts on-chain right away
- Blockchain development environment configuration: will set up the hardhat.config.js file to connect with the chain and, in case, testnet, selected during the setup process.
All of this, directly through the CLI.
The Smart Contract Backpack is built on top of the OpenZeppelin library and supports the almost same standards:
- ERC721
- ERC721A *(coming soon)
- ERC20 (coming soon)
- ERC1155 (coming soon)
How to Create Smart Contracts using Smart Contract Backpack?
The contract creation process is pretty straightforward:
- Select a standard
- Select a name - this will be the name of your smart contract and token
- Select a symbol - this will be the symbol of your smart contract and token
- Select the smart contract features you want to include - see the OpenZeppelin documentation to learn more about the available libraries for every standard
Follow the steps mentioned below to create smart contracts using the smart contract backpack:
1. Install the CW3D NPX package
In your terminal, navigate to the folder you want to create your project in and run:
npx create-web3-dapp@latest
If you've previously installed
create-web3-dapp
globally vianpx create-web3-dapp
:The CLI builder will notify you if a new version has been released. In any case, we always suggest you to run using the latest available version by running
npx create-web3-dapp@latest
.
2. Insert a project name and template
You'll now get asked to insert a project name and a template to start from:
- Create empty full-stack dapp: With is option you will be given an empty full-stack dapp with wallet connection built-in using Rainbow Kit, optionally, you can also choose to add a blockchain development environment (Hardhat or Foundry) in the project and adding a pre-defined smart contract like ERC721, ERC721A etc.
- Create pre-built template: You can select a pre-built template for a dapp with this option. Right now we have the Block Explorer dapp as a template and we are actively working on adding more templates soon!
Choose the "Create empty full-stack dapp" option.
3. Choose your chain
Once you have selected your starting template, you'll need to choose the chain you want to configure your project for. Note that you can always modify or add more chains in the future. Current choices include:
- Ethereum
- Polygon
- Arbitrum
- Optimism
4. Select testnet or mainnet
Select if you want to configure with mainnet or testnet. We generally recommend starting with a testnet if you're not ready to deploy to the live chain yet. Here are the corresponding test networks for each chain:
- Ethereum -> Goerli
- Polygon -> Mumbai
- Arbitrum -> Arb-Goerli
- Optimism -> Opt-Goerli
5. Select your blockchain development environment
If you haven't yet created, built, or deployed your smart contracts, you'll be able to install a blockchain development environment directly through CW3D.
Blockchain development environments like Hardhat or Foundry provide a streamlined and efficient way for developers to build, test and deploy smart contracts.
Adding a Blockchain development environment will also allow you to get access to the Smart Contracts Backpack.
You will see these options for selecting the blockchain development environment:
- Hardhat: Hardhat is a reliable, flexible, and extensible development environment for building EVM-based smart contracts.
- Foundry(coming soon): Foundry is a user-friendly, collaborative, and secure blockchain development platform that accelerates the process of building and deploying smart contracts.
Select Hardhat as that's the only option available right now. Then it will ask you if you want an empty Hardhat project or if you want to create a smart contract right from the CLI. Select "Yes" and it will start the Smart Contract Backpack.
Next, you will be shown some pre-made smart contracts (ERC721, ERC1155 etc.) and you can select one of them to include in your project, select ERC721 as that's the only option right now (we're working on adding more):
Next, you will be asked to give a name to your smart contract:
After that, you will need to define a symbol for your ERC721 tokens:
Next, you will be asked to select the libraries you want to include - see the OpenZeppelin documentation to learn more about the available libraries for every standard:
Select yes after you are done selecting the features:
6. Create an Alchemy Account or Enter your API key
Then you will be asked if you already have an Alchemy account, selecting yes or no based on if you have the Alchemy account or not.
If you already have an Alchemy account you will be redirected to your Alchemy dashboard from where you can copy and paste your API key into the terminal.
If not, you will be redirected to create a new Alchemy account and after creating the Alchemy account you can copy and paste your API key in the terminal.
We'll use an Alchemy API key to connect to the blockchain, read data, and deploy smart contracts.
Congratulations, you've just created your first smart contract using create-web3-dapp
🎉 let's now take a look at the generated files:
backend/hardhat.config.js
backend/contracts/MyContract.sol
backend/scripts/MyContract_deploy.js
Take a look at the Create Web3 Dapp Folder structure doc for a broader look at the project files' structure.
-
The hardhat.config.js file: contains the settings to connect to the blockchain and deploy your contracts. It comes pre-filled by
create-web3-dapp
with the info provided during the setup process. To learn more about thehardhat.config.js
file, and how it works, refer to the official Hardhat documentation. -
The .sol file: is the actual contract, ready to be deployed, with the standard and libraries you've selected during the setup process.
-
The deploy script: contains the ready-to-use code to actually initialize and deploy your smart contract on the selected chain.
Build and Deploy the Contract
Now we have a broad understanding of what the smart contract backpack is generating for us. Let's build and deploy the MyContract.sol
ERC721 contract that's generated for us.
Navigate to the .env
file and add your private key in there. Make sure you have some testnet ETH in the wallet whose private key you are adding there, this is important for deployment of the smart contract:
ALCHEMY_API_KEY=YOUR-API-KEY-HERE
ETHERSCAN_API_KEY=
PRIVATE_KEY=YOUR-PRIVATE-KEY-HERE
Compile the contract by running the following command in the backend
directory:
npm run build
Then deploy the contract on testnet by running the following command:
npm run deploy-testnet
This will deploy the contact and log the contract's address on the console:
MyContract deployed to: 0x57C07cBb850D5FFa04afdfff9816a5aC75f112Ab
Congratulations 🎉
You just deployed an ERC721 contract using create-web3-dapp
!
Updated almost 2 years ago