/

Introduction

This in depth tutorial is designed to walk you through the process of building your first dapp using a Rhombus oracle. It will guide you step by step on how to incorporate real world data into your smart contracts with the Rhombus Lighthouse Delivery method. You will learn how to interact with existing Lighthouses on Rinkeby in your smart contracts, and how to perform local testing of Lighthouses using Truffle and a local Ganache chain.

This tutorial is meant for those with a good understanding of Ethereum and smart contracts, dapp development using solidity, and the Truffle framework, but are new to using Rhombus oracles.

Note : For those new to Truffle or Ethereum, please read the Truffle Tutorial before proceeding.

In this tutorial, we will be covering:

  1. Project setup
  2. Using a lighthouse oracle in the project

The Gambling Game Scenario

Alice wants to start a casino where players can bet Ether on the outcome of a dice roll. Unfortunately, it is hard to generate truly random numbers on the Ethereum blockchain. She has asked you to help integrate an oracle into her project which will periodically obtain a random number generated off-chain. You know that Rhombus provides a random number oracle service, and must figure out how to incorporate it into the project.

Alice already has the casino functionality completed, and is waiting on you to provide the random number

Project Setup

Create a directory where you would like to keep this project, and move inside it with your terminal. Run the truffle command truffle unbox RhombusNetwork/tutorial. Truffle should automatically unbox and the project and download it into your folder.

Alternatively, visit the project’s github link here and download the project files.

Using a lighthouse oracle in the project

What you have just downloaded is a skeleton project with pieces missing. This tutorial will give you code and explanations on how you can fill out the missing sections to complete the project

Looking inside the contracts folder, you will see four files.

  • Gamble.sol is the main file for our Betting Game project. This is where we want to read lighthouse data into
  • Ilighthouse is a lighthouse interface file. This is must be included in Gamble.sol to interact with currently deployed lighthouses. You do not need to modify this file, simply include it in your project
  • Lighthouse.sol is the actual lighthouse code. Do not modify this file. It only needs to be included if you are doing local testing of projects using lighthouse oracles with Truffle/Ganache
  • Migrations.sol comes with every Truffle project and can be ignored. You do not need to modify this file

Gamble.sol already contains the gambling logic Alice has coded. Fill in the missing sections to provide a random number.

Rhombus provides oracle data through a mechanism called a lighthouse. In simple terms, a lighthouse is an address where a single integer value that contains off-chain data is stored. Rhombus ensures the value is updated periodically, and can provide guarantees that the value is correct.

In this case, there is a random number generator lighthouse already deployed on Rinkeby, and you must access this lighthouse from your casino project smart contract (which will also be deployed on Rinkeby)

To obtain a value from an existing Rhombus lighthouse oracle, see the Gamble.sol file in the contracts folder.

  1. First, you must include the Lighthouse Interface file ILighthouse.sol in the smart contract file where you want to read data into, Gamble.sol. Add the following code snippet into section 1.
import "../contracts/Ilighthouse.sol";
  1. The Ilighthouse file contains the interface for a Lighthouse Object. Now you can declare a global ILighthouse object in your Gamble.sol file. Add this to section 2.
ILighthouse  public myLighthouse;
  1. Create a constructor for Gamble.sol. Pass in an Ilighthouse object as a constructor parameter. Add the following code to section 3.
constructor(ILighthouse _myLighthouse) public {
    myLighthouse = _myLighthouse;
}

What you did here is define the lighthouse that the Gamble.sol smart contract will read data from. When you deploy your project to Rinkeby, you will give Gamble.sol the address of the random number lighthouse that Rhombus has deployed. This address will be interpreted by the constructor as an Ilighthouse object, and set as the global Ilighthouse variable myLighthouse. Now you can use myLighthouse to call lighthouse functions such as reading data. Lets do this now.

  1. Scroll down in Gamble.sol and insert the following code in section 4
uint winningNumber;
bool ok;
(winningNumber,ok) = myLighthouse.peekData(); // obtain random number from Rhombus Lighthouse

Here, your smart contract will go to the address represented by myLighthouse, which is the address you passed into the constructor during deployment (and also the address of the random number lighthouse) and call the peekData() function at that address. The random number lighthouse will then return a pair of values to you, a bool and an int. The bool ok contains whether or not the data is the latest updated value, and the int winningNumber contains what you have been after this whole time – the random number (from 1 to 6) you need in integer format.

Deploying your project

Now that you’ve helped Alice complete her gambling project, its time to deploy it and showcase it to the world. Go into the migrations folder. You should see two files

  • 1_initial_migrations.js comes with every truffle project. You can ignore it
  • 2_deploy_project.js is where you will write the code to deploy the smart contracts you have added to your project, Gamble.sol.

Go into 2_deploy_project.js and add the following code snipped into section 5.

if(network == "rinkeby") {

  var address = '0x613D2159db9ca2fBB15670286900aD6c1C79cC9a';   //address of RNG lighthouse on rinkeby
  deployer.deploy(Gamble, address);

} else {    // For local testing

  // First deploy the lighthouse, then use the lighthouse's address to deploy gamble. This allows
  // gamble to know which lighthouse to obtain data from.
  deployer.deploy(Lighthouse).then(function() {
    return deployer.deploy(Gamble, Lighthouse.address);
  });

}

Lets focus on the first if statement, which contains code for Rinkeby deployment. Remember how in Gamble.sol you created a constructor which had to take an address as an argument (and interpret it as an Ilighthouse object)? This is where you pass that address in. We have provided you with the address of the random number lighthouse on Rinkeby, and here you are deploying the Gamble.sol file with the address as a parameter.

Next, exit out of the migrations folder and go back to the main project directory. Find the truffle-config.js file and open it. This file contains information for the blockchain you will be connected to and deploying to. Paste the following code into section 6.

var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = ""; // Enter the mnemonic for your rinkeby account (testnet deployment only)
module.exports = {

  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,     // port 7545 for Ganache GUI version and port 8545 for Ganache-cli
      network_id: "*" // Match any network id

    },

    rinkeby: {
      provider: function() {
        return new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/v3/[your infura link here]");
      },
      network_id: 4
    }

  }
};

These are the deployment settings you need to get your project onto Rinkeby. The deployment settings for testing with a local Ganache chain is included as well. Note that you will need to have an Infura link and a wallet with Rinkeby ether to deploy the project.

With this all set up, go to your terminal and run truffle migrate --network rinkeby to complete deployment. Congratulations, your project is now available for anyone to use!