Figment Datahub and Avalanche: Make an e-Voting dApp Using Figment Datahub, Avalanche, and Filebase
Learn make an e-Voting dApp Using Figment Datahub, Avalanche, and Filebase.
What is a dApp?
A dApp is a decentralized application that can operate autonomously, typically through the use of smart contracts, and runs on blockchain networks.
What is Figment DataHub?
Figment DataHub is a platform that enables developers to create decentralized applications (dApps) using the powerful and unique features of blockchain technology without having to be experts on the wide variety of blockchain protocols.
What is Avalanche?
Avalanche is a decentralized, open-source blockchain that has smart contract functionality.
In this guide, we’ll use Figment Datahub to create an API key that we’ll use to create a decentralized application on the Avalanche blockchain. We’ll also utilize S3FS-FUSE to mount a Filebase IPFS bucket locally to our system so that we can store our dApp source files.
This guide was written and tested using Ubuntu 20.04. Commands and workflow may vary depending on your operating system.
1. Login to your Figment Datahub console. Select ‘Create App’ to create a new Avalanche app.
2. Give your app a name, select ‘Staging’ for the environment, then select ‘Avalanche’ for the network.
Then use the ‘Create App’ button to create your new Avalanche app.
3. Once you’ve created your app, take note of the API key.
We will reference this API key later.
4. Next, we need a Filebase IPFS bucket.
To do this, navigate to console.filebase.com. If you don’t have an account already, sign up, then log in.
5. Select ‘Buckets’ from the left side bar menu, or navigate to console.filebase.com/buckets.
Select ‘Create Bucket’ in the top right corner to create a new bucket.
6. Enter a bucket name and choose the IPFS storage network to create the bucket.
Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.
7. Next, download and install S3FS-FUSE on a Linux or macOS system.
8. Set up an Access Key file for use with S3FS-FUSE.
Set up a credentials file for S3FS at ${HOME}/.passwd-s3fs.
You will need to save your Filebase Access and Secret keys to this file and give it owner permissions. You can do so with the following commands:
echo ACCESS_KEY_ID:SECRET_ACCESS_KEY > ${HOME}/.passwd-s3fs
chmod 600 ${HOME}/.passwd-s3fs
ACCESS_KEY_ID is your Filebase Access key, and SECRET_ACCESS_KEY is your Filebase Secret key. For more information on Filebase access keys, see here.
9. Mount your bucket.
You can mount a Filebase IPFS bucket with the command:
s3fs mybucket /path/to/mountpoint -o passwd_file=${HOME}/.passwd-s3fs -o url=https://s3.filebase.com
mybucket: name of your Filebase bucket
/path/to/mountpoint
10. Now, navigate into the mounted Filebase bucket.
cd /path/to/mounted/bucket
11. Next, create a new folder to house your e-Voting app scripts and navigate inside of it.
mkdir evoting
cd evoting
12. Initialize your npm workspace:
npm init
13. Then install the required npm dependencies:
npm install express dotenv @truffle/hdwallet-provider --save
14. Next, initialize a boilerplate project with truffle:
truffle init
This command sets up our initial project structure, which includes a contracts folder that will house our smart contracts, and a migrations folder for deployment functions.
15. Open the truffle-config.js
file that was created with the truffle init
command. Replace the contents of that file with the following:
truffle-config.js
file that was created with the truffle init
command. Replace the contents of that file with the following:16. Next, create a .env file. In this file, we’ll need a few values:
Create an account on the Avalanche network and enter your mnemonics in the .env file.
Input your Avalanche API key that we took note of earlier.
The format is as follows:
17. In the contracts directory, create a new file called Election.sol
. In the file, enter the following content:
Election.sol
. In the file, enter the following content:This is a solidity smart contract that will allow us to view the candidates and their standing in the election.
18. Create a new file in the migration directory named 2_deploy_contracts.js
. Enter the following code into this file:
2_deploy_contracts.js
. Enter the following code into this file:19. Then, compile the contracts with truffle:
truffle compile
Any time you make changes to the Election.sol
file, you will need to recompile the contract.
20. Before deploying the contracts further, make sure your Avalanche wallet has enough funds.
Use the Avalanche Faucet to get more test funds for use.
21. Run the migrations to deploy your Election contract:
truffle migrate --network fuji
22. Now let’s move on to creating a simple UI for interacting with our e-Voting dApp. Create a new src
directory and navigate inside of it.
src
directory and navigate inside of it.mkdir src
cd src
23. Make a new server.js
file and input the following code into the file:
server.js
file and input the following code into the file:24. Make another new file called index.html
with the following content:
index.html
with the following content:You can change the styling of this code to reflect your candidate's names, different colors and themes, and even photos of each candidate.
25. Then, make another new file called index.js
with the following content:
index.js
with the following content:26. Start the e-Voting dApp with the following command:
node server.js
27. You can view your dApp at http://localhost:3000/ and interact with the webpage we created with the files in the src
directory.
src
directory.Congratulations! You’ve created an e-Voting app that is powered by blockchain and stored on decentralized storage!
Last updated