Skip to main content

Connect to a network using Truffle

You can connect to a Quorum Blockchain Service (QBS) network using Truffle, which allows you to deploy smart contracts to QBS.

This guide uses the Truffle command line interface for interacting with QBS.

You can also connect to a network using Visual Studio Code with Truffle.

Prerequisites

Steps

  1. Initialize Truffle on your project folder:

    truffle init
  2. Install Truffle's HDWalletProvider:

    npm install @truffle/hdwallet-provider
  3. Set environment variables with dotenv.

    Install dotenv:

    npm install --save dotenv

    Create a .env file in your project's root folder, and set environment variables in the file as follows:

    MNEMONIC = // (string) your MetaMask 12-word seed phrase
    RPC_URL = // (string) your QBS connection string, e.g. "https://<Node-Name>.<Member-Name>.<Consortium-Name>.onquorum.net:3200/<API-Key>"

    !!! critical "Security warning"

    **Keep your mnemonic secret.**

    Mnemonics must be kept secret and not committed to any code respositories.
    Improper handling of mnemonics can lead to loss of funds and identity fraud.
  4. Edit truffle-config.js with the following text:

    const HDWalletProvider = require("@truffle/hdwallet-provider");
    const fs = require("fs");
    require("dotenv").config(); // store environment variables from '.env' to process.env

    module.exports = {
    // See <http://truffleframework.com/docs/advanced/configuration>
    // to customize your Truffle configuration
    networks: {
    default: {
    host: "127.0.0.1", // Localhost (default: none)
    port: 8545, // Standard Ethereum port (default: none)
    network_id: "*", // Any network (default: none)
    },

    qbs: {
    // Provider has to be wrapped into a function
    provider: () =>
    new HDWalletProvider(process.env.MNEMONIC, process.env.RPC_URL),
    network_id: "*", // Any network (default: none)
    gasPrice: 0,
    gas: 4500000,
    type: "quorum",
    },
    },
    compilers: {
    solc: {
    version: "0.7.0",
    },
    },
    };
  5. Deploy to QBS:

    truffle migrate --network qbs