This is a decentralized application built on the Ethereum/Polygon blockchain, it works like Dropbox or Google Drive by allowing users to securely store their files on the blockchain using IPFS Technology
Table of Contents
Please install or have installed the following:
- nodejs and npm
- python
- MetaMask Chrome extension installed in your browser
-
Installing Brownie: Brownie is a python framework for smart contracts development,testing and deployments. It's quit like HardHat but it uses python for writing test and deployements scripts instead of javascript. Here is a simple way to install brownie.
pip install --user pipx pipx ensurepath # restart your terminal pipx install eth-brownie
Or if you can't get pipx to work, via pip (it's recommended to use pipx)
pip install eth-brownie
Install ganache-cli:
npm install -g ganache-cli
-
Clone the repo:
git clone https://github.com/kaymen99/ipfs-storage-dapp.git cd ipfs-storage-dapp
-
Install Ganache: Ganache is a local blockchain that run on your machine, it's used during development stages because it allows quick smart contract testing and avoids all real Testnets problems. You can install ganache from this link : https://trufflesuite.com/ganache/
Next, you need to setup the ganache network with brownie :
cd ipfs-storage-dapp brownie networks add Ethereum ganache-local host=http://127.0.0.1:7545 chainid=5777
-
Set your environment variables To be able to deploy to real Polygon testnets you need to add your PRIVATE_KEY (You can find your PRIVATE_KEY from your ethereum wallet like metamask) to the .env file:
PRIVATE_KEY=<PRIVATE_KEY>
In this project i used the Polygon Testnet but you can choose to use ethereum testnets like rinkeby, Kovan.
To setup the Polygon Testnet with brownie you'll need an Alchemy account (it's free) and just create a new app on the polygon network
After creating the app copy the URL from -view key- and run this:
cd ipfs-storage-dapp brownie networks add Polygon polygon-mumbai host=<Copied URL> chainid=80001 name="Mumbai Testnet (Alchemy)"
You'll also need testnet MATIC. You can get MATIC into your wallet by using the Polygon testnet faucets located here.
-
As infura recently removed its free IPFS gateway i used
web3.storage
api for files into IPFS, this api is as simple as infura it requires the creation of a free account and a new api token which you can do here, when you finish add your api token into thesrc/utils/StoreContent.js
file:const web3storage_key = "YOUR-WEB3.STORAGE-API-TOKEN";
In the ipfs-storage-dapp folder you'll find a directory contracts, all the smart contracts build in brownie are stored there. The FileStorage contract is the core of this application, it plays the role of the backend and has the following features:
- SetUploadFee: for every file uploaded the user must pay a small fee set by the owner of the contract
- Upload: allows the user to upload his file
- getUserFiles: a function for getting all the files uploaded by a given user
- Chainlink Price Feed: the contract uses the price feed provided by chainlink oracle for converting the fee set by the owner from $ to MATIC
In the ipfs-storage-dapp folder you'll find a directory scripts, it contain all the python code for deploying your contracts and also some useful functions
The reset.py file is used to remove all previous contracts deployments from build directory:
brownie run scripts/reset.py
The deploy.py file allow the deployment to the blockchain, we'll use the local ganache for now:
brownie run scripts/deploy.py --network=ganache-local
The update_front_end.py is used to transfer all the smart contracts data (abi,...) and addresses to the front end in the artifacts directory:
brownie run scripts/update_front_end.py
After running this 3 cammands, the FileStorage contract is now deployed and is integrated with the front end
In your ipfs-storage-dapp folder you'll find a directory tests, it contain all the python code used for testing the smart contract functionalities
You can run all the tests by :
brownie test
Or you can test each function individualy:
brownie test -k <function name>
The user interface of this application is build using React JS, it can be started by running:
cd front-end
yarn
yarn start
It uses the following libraries:
- Ethers.js: for conecting to Metamask and interacting with smart contract
- ipfs-http-client: for connecting and uploading files to IPFS
- @reduxjs/toolkit: for managing the app states (account, balance, blockchain)
- Material UI: used for react components and styles
The files are structured as follows:
- Components: Contains all the app component(main, navbar, filestorage,...)
- features: contains the redux toolkit reducer and actions
- artifacts: contains all the smart contract data and addresses transfered earlier
- NetworksMap: a json file for some known blockchains names & chain id
To learn about Smart Contract and Brownie:
- Patrick Collins FreeCodeCamp Complete Course
- "Getting Started with Brownie" is a good tutorial to help you familiarize yourself with Brownie.
If you have any question or problem running this project just contact me: aymenMir1001@gmail.com
Distributed under the MIT License. See LICENSE.txt
for more information.