Ankr: Create a Truffle Project with Ankr and Filebase

Learn how to create a Truffle Project with Ankr and Filebase.

What is Ankr?

Ankr is an API and blockchain node utility that can be used to create API projects and deploy your own blockchain nodes.

The Ankr API can be used in a variety of different applications, making it useful for project development and application creation. Ankr offers a selection of public API endpoints, so there is no need for an account to get started. To create custom, private apps, check out their guide on creating your own Ethereum API endpoint here.

In this guide, we’ll use Truffle, Ganache, and Ankr to create an example app located on a local development environment using the Ropsten blockchain network with files backed up to an IPFS bucket on Filebase.

Prerequisites:

This guide was written and tested using Ubuntu 20.04. Commands and workflow may vary depending on your operating system.

1. First, 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.

2. 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.

3. 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.

4. Next, download and install S3FS-FUSE on a Linux or macOS system.

5. 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.

6. 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

7. Now, navigate into the mounted Filebase bucket.

cd /path/to/mounted/bucket

8. Next, create a new folder to house your project scripts and navigate inside of it.

mkdir truffle-project

cd truffle-project

9. Initialize your npm workspace:

npm init

Provide any necessary configuration options for your workspace, or accept the default configuration settings for your npm workspace.

10. Install Truffle, Ganche and Webpack into your project:

npm install -g truffle

npm install ganache --global

npm install -g webpack

11. Unbox the truffle package:

truffle unbox webpack

Your project will have the following files and directories:

  • app/

  • contracts/

  • migrations/

  • test/

  • truffle-config.js

12. Next, let’s create a sample contract. In the contracts folder, create a new file called example.sol with the following content:

pragma solidity >=0.4.21 <0.7.0;

contract example{
    function multiply(uint a) returns(uint d){
        return a * 10;
    }
}

This is a very basic contract that does a multiplication problem. There are a variety of different types of contracts, the most common being contracts for minting NFTs. You can find a variety of guides that go in depth in the creation of NFT contracts [here].

13. Then in the migrations folder, create a new file called 2_deploy_contracts.js with the following content:

const example = artifacts.require("example");
module.exports = async function(deployer) {
    await deployer.deploy(newContract);
};

14. Now we need a wallet in order to sign our contract transactions. Install the truffle-hdwallet-provider and the babel-register packages with the following commands:

npm init

npm install babel-register truffle-hdwallet-provider --save

15. Open the existing truffle-config.js file and replace it’s contents with the following code:

const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim(); //.secret   Mnemonic Phrase
var HDWalletProvider = require("truffle-hdwallet-provider");
require('babel-register')

module.exports = {

    networks: {
        networkName: {
            provider: () => new HDWalletProvider(mnemonic, `https://rpc.ankr.com/eth`),
            network_id: 1, 
            gas: 4612388, 
            confirmations: 2, 
            timeoutBlocks: 200,
            skipDryRun: true, 
        },
        
    mocha: {
    },

    compilers: {
        solc: {
        }
    }
}

This contract uses the Ankr public Ethereum endpoint. Learn more about Ankr by checking out their website here.

16. Compile your contract with the following command:

truffle compile

17. Deploy your contract on the network with the following command:

truffle migrate --network networkName

From here, you can create new contracts and deploy them in the same way you deployed this example contract. You can create contracts for almost anything, including minting NFTs or transferring tokens.

If you have any questions, please join our Discord server, or send us an email at hello@filebase.com

Last updated