This project sets up a comprehensive blockchain development environment using Docker, Solidity, Rust, and VSCode.
- Node.js & npm: JavaScript runtime and package manager.
- Hardhat: Modern Ethereum development environment.
- Ganache CLI: Local Ethereum blockchain emulator.
- Solidity Compiler: Latest Solidity compiler.
- Rust & cargo: For Rust-based blockchain development.
- OpenZeppelin: Secure smart contract library.
- VSCode: Code editor with extensions for blockchain development.
- Docker: Containerization platform.
- Foundry: Fast, portable, and modular toolkit for Ethereum application development.
Clone this repository to your local machine using the following command:
git clone https://github.com/yourusername/blockchain-dev-environment.git
cd blockchain-dev-environmentEnsure Docker is installed on your system. You can download Docker from the official Docker website and follow the installation instructions for your operating system.
Build the Docker image using the following command:
docker build -t blockchain-dev ./dockerRun the Docker container with port mapping and volume mounting for VSCode integration:
docker run -it -p 8545:8545 -v $(pwd):/usr/src/app --name BlockChainDevelopmentEnvironment blockchain-devInstall the Remote - Containers extension in VSCode. This allows you to use Docker containers as development environments.
-
Install the Remote - Containers Extension:
- Open VSCode, go to the Extensions view by clicking the Extensions icon or pressing
Ctrl+Shift+X, and search forRemote - Containers. Install it.
- Open VSCode, go to the Extensions view by clicking the Extensions icon or pressing
-
Open Folder in Container:
- Open your project folder in VSCode.
- Press
Ctrl+Shift+Pand typeRemote-Containers: Open Folder in Container. - Select your project folder. This will start a new VSCode instance inside the Docker container.
-
Create a New Hardhat Project:
- Inside the VSCode terminal, run:
npx hardhat
- Follow the prompts to create a new Hardhat project.
- Inside the VSCode terminal, run:
-
Set Up Rust Environment:
- Rust is already installed inside the Docker container. You can create and manage Rust projects as needed. For example, to create a new Rust project, run:
cargo new my_rust_project
- Rust is already installed inside the Docker container. You can create and manage Rust projects as needed. For example, to create a new Rust project, run:
-
Solidity Contracts:
- Inside the
contractsdirectory of your Hardhat project, create and edit your Solidity files. - Use the VSCode Solidity extension for syntax highlighting and other features.
- Inside the
-
Rust Contracts (for Substrate or other Rust-based blockchains):
- Inside your Rust project, create and edit Rust files.
- Use the Rust extension in VSCode for better development experience.
-
Compile Solidity Contracts:
- Inside the VSCode terminal, run:
npx hardhat compile
- Inside the VSCode terminal, run:
-
Deploy Contracts Using Hardhat:
- Inside the VSCode terminal, create a deployment script and run:
npx hardhat run scripts/deploy.js --network localhost
- Inside the VSCode terminal, create a deployment script and run:
-
Compile and Build Rust Contracts:
- Inside your Rust project, run:
cargo build
- Inside your Rust project, run:
-
Using Hardhat Console:
- Inside the VSCode terminal, run:
npx hardhat console --network localhost
- You can interact with your deployed contracts using JavaScript commands in the Hardhat console.
- Inside the VSCode terminal, run:
-
Using Web3.js:
- Install Web3.js if you haven’t already:
npm install web3
- Create a JavaScript file to interact with your contracts using Web3.js.
- Install Web3.js if you haven’t already:
-
Writing Tests for Solidity Contracts:
- Inside the
testdirectory of your Hardhat project, create test files using Mocha and Chai (Hardhat’s default testing framework). - Example test file (
test/MyContract.test.js):const { expect } = require("chai"); describe("MyContract", function() { it("should do something", async function() { const MyContract = await ethers.getContractFactory("MyContract"); const myContract = await MyContract.deploy(); await myContract.deployed(); expect(await myContract.myFunction()).to.equal(expectedValue); }); });
- Inside the
-
Running Tests:
- Inside the VSCode terminal, run:
npx hardhat test
- Inside the VSCode terminal, run:
-
Installing Node.js Packages:
- Use
npmto manage Node.js dependencies. Inside the VSCode terminal, run:npm install <package-name>
- Use
-
Managing Rust Crates:
- Use
cargoto manage Rust dependencies. Inside theCargo.tomlfile of your Rust project, add the desired dependencies.
- Use
-
VSCode Extensions:
- Make sure you have the following VSCode extensions installed:
- Solidity by Juan Blanco
- Rust Analyzer by rust-lang
- Prettier - Code formatter by Esben Petersen
- ESLint by Dirk Baeumer
- Make sure you have the following VSCode extensions installed:
-
Other Useful Tools:
- MetaMask: Use MetaMask for interacting with your dApps. Install it as a browser extension and connect it to your local Ganache network.
.devcontainer/: VSCode development container configuration.docker/: Dockerfile for setting up the environment.solidity/: Solidity project files.rust/: Rust project files.
By following this guide, you will have a modern and comprehensive blockchain development environment set up using Docker, integrated with VSCode, and equipped with all necessary tools for Solidity and Rust development. This setup ensures a consistent and isolated development environment, leveraging the latest technologies and best practices. You can modify the Dockerfile and devcontainer.json to suit your specific needs and add any other tools or dependencies required for your blockchain projects.