Skip to content

7. Migrate the contracts into an Ethereum network

This article is part of the beginner tutorial on creating a totally decentralized Bitcoin price feed on Ethereum with Solidity and Witnet.

Compile your Solidity contract

First off, run the compile command, which compiles your Solidity contracts and recompiles the Witnet requests too:

npm run compile
yarn compile

You got migration scripts, for free!

Migration scripts are one of the nicest features of Truffle. They allow you to configure how your contracts will be deployed on different Ethereum networks, set your contract's constructor arguments and link dynamic dependencies.

The Witnet requests compiler that lives inside the Truffle box quietly did something else than compiling your requests into Solidity contracts—it also wrote autogenerated migration scripts for your own contracts. If you look at the migrations folder, you should find these three files:

  • 1_initial_migration.js: basic Truffle infrastructure migrations.
  • 2_witnet_core.js: deploys all the Witnet-related contracts in case you are deploying on a local or private network; or dynamically link them if you are on a public network (Ethereum Mainnet, Rinkeby or Görli).
  • 3_user_contracts.js: contains autogenerated migration scripts for your consumer contracts.

Let's take a look at migrations/3_user_contracts.js:

// This file was auto-generated by the Witnet compiler, any manual changes will be overwritten except
// each contracts' constructor arguments (you can freely edit those and the compiler will respect them).
const Witnet = artifacts.require("Witnet")
const WitnetBridgeInterface = artifacts.require("WitnetBridgeInterface")
const PriceFeed = artifacts.require("PriceFeed")

module.exports = function (deployer) {, [PriceFeed])
  deployer.deploy(PriceFeed, WitnetBridgeInterface.address)

As you can see, the autogenerated migration script is:

  1. Dynamically linking the Witnet library to your PriceFeed contract.
  2. Deploying your PriceFeed contract.
  3. Passing the address of the Witnet Bridge Interface to the PriceFeed constructor.

For the compiler to pass the address of the Witnet Bridge Interface to all your consumer contracts, it is important that they have a constructor argument called _wbi, just like PriceFeed has.

Customize the constructor arguments

If your consumer contract has additional constructor arguments, the compiler will create default values for them.

Before running any migration, please make sure to double-check the default arguments that the compiler inserts for you, as they may not make any sense for your specific use case.

Once you modify any constructor arguments, the compiler will not rewrite those lines. If you mess them up or you want the compiler to generate default values for new constructor arguments, you can just delete those lines or remove the 3_user_contracts.js file altogether, then run the compiler once again.

Run the deployment

Deploying your contract into Truffle's own local Ethereum network is as easy as it gets:

truffle migrate

Please take into account that Truffle's own local network lacks any bridging capability with Witnet. This means that it is a good option for testing the migrations, but not for testing the entire workflow of your contracts. However, the Witnet community is working hard to overcome this limitation so that you can test your Witnet-connected contracts locally.

If you want to test your contracts in a more realistic environment, you can deploy them into a public network using the --network flag:

  • truffle migrate --network=rinkeby deploys on the Ethereum Rinkeby testnet.
  • truffle migrate --network=goerli deploys on the Ethereum Görli testnet.

Deploying on mainnet is not supported yet. Take a look at the community roadmap for more information on mainnet support.

Interact with your contract

The Truffle documentation has a comprehensive guide on interacting with your contracts.

Remember: You are not alone!

You are invited to join the Witnet Community Discord. Members of the Witnet community will be happy to answer your questions and doubts, as well as assisting you through this tutorial.