All Articles

How to Integrate Truffle with Rails 6

Here is a short post on how to setup your Rails 6 app to work with Truffle.

Pre-requisites: Truffle project with Solidity smart contracts all ready to be deployed. Truffle already installed. Infura project ID. Metamask. Rinkeby testnet Ether or mainnet Ether. Rails 6 app already generated.

Run truffle init within the same root directory as the Rails app.

Run truffle compile.

Setup your truffle-config.js with the following:

const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();
const HDWalletProvider = require('truffle-hdwallet-provider');
const infuraKey = "118951e57cf64dc5a614119f6a85ae10";

const infuraRinkebyURL = `${infuraKey}`;
const infuraMainnetURL = `${infuraKey}`;

const provider = new HDWalletProvider(mnemonic,infuraRinkebyURL);
const mainNet = new HDWalletProvider(mnemonic,infuraMainnetURL);

module.exports = {
  networks: {
    development: {
     host: "",     // Localhost (default: none)
     port: 8545,            // Standard Ethereum port (default: none)
     network_id: "*",       // Any network (default: none)
      provider: () => provider, 
      network_id: 4,
      gas: 5500000
      provider: () => mainNet,
      network_id: 1, 
      gas: 5500000

Run truffle migrate --network rinkeby to deploy your contract to the testnet or truffle migrate --network mainnet to deploy your contract to the mainnet.

Use the gem install ethereum.rb to install the ethereum.rb gem. This gem provides an Ethereum ruby library to access the Ethereum blockchain asap.

Use these gems to make RPC calls and interact with contracts on Ethereum through Rails.


If all went well, you have Truffle configured to work with your Rails app.