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.
Prerequisites:
This guide was written and tested using Ubuntu 20.04. Commands and workflow may vary depending on your operating system.
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.
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.
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:
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:
require('dotenv').config();
const HDWalletProvider = require("@truffle/hdwallet-provider");
//Account credentials from which our contract will be deployed
const mnemonic = process.env.MNEMONIC;
//API key of your Datahub account for Avalanche Fuji test network
const APIKEY = process.env.APIKEY;
module.exports = {
networks: {
fuji: {
provider: function() {
return new HDWalletProvider({mnemonic, providerOrUrl: `https://avalanche--fuji--rpc.datahub.figment.io/apikey/${APIKEY}/ext/bc/C/rpc`, chainId: "0xa869"})
},
network_id: "*",
gas: 3000000,
gasPrice: 470000000000,
skipDryRun: true
}
},
solc: {
optimizer: {
enabled: true,
runs: 200
}
}
}
16. Next, create a .env file. In this file, we’ll need a few values:
Input your Avalanche API key that we took note of earlier.
17. In the contracts directory, create a new file called Election.sol. In the file, enter the following content:
pragma solidity >=0.4.21 <0.6.0;
contract Election {
//Structure of candidate standing in the election
struct Candidate {
uint id;
string name;
uint voteCount;
}
//Storing candidates in a map
mapping(uint => Candidate) public candidates;
//Storing address of those voters who already voted
mapping(address => bool) public voters;
//Number of candidates in standing in the election
uint public candidatesCount;
//Adding 2 candidates during the deployment of contract
constructor () public {
addCandidate("Filebase Robot");
addCandidate("Figment Robot");
}
//Private function to add a candidate
function addCandidate (string memory _name) private {
candidatesCount ++;
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
}
//Public vote function for voting a candidate
function vote (uint _candidate) public {
require(!voters[msg.sender], "Voter has already Voted!");
require(_candidate <= candidatesCount && _candidate >= 1, "Invalid candidate to Vote!");
voters[msg.sender] = true;
candidates[_candidate].voteCount++;
}
}
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: