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 then recompiles the Witnet requests:

npm run compile
yarn compile

You just got migration scripts for free!

Migration scripts are very useful in 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.

In addition to compiling your requests into Solidity, the Witnet request compiler that lives inside the Truffle box also writes autogenerated migration scripts for your contracts. If you look at the migrations folder, you should find these two files:

  • 1_witnet_core.js: deploys all the Witnet-related contracts if you are deploying on a local or private network; or dynamically links them if you are on a public network.
  • 2_user_contracts.js: contains autogenerated migration scripts for your consumer contracts.

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

const Witnet = artifacts.require("Witnet")
const WitnetProxy = artifacts.require("WitnetProxy")
const PriceFeed = artifacts.require("PriceFeed")

module.exports = async function (deployer) {
  await deployer.link(Witnet, [PriceFeed])
  await deployer.deploy(PriceFeed, WitnetProxy.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 _wrb, 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 you 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 2_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 simple as executing:

truffle migrate

Please take into account that Truffle's own local network lacks any bridging capability with Witnet. This means that it is good 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=ethereum.rinkeby deploys on the Ethereum Rinkeby testnet.
  • truffle migrate --network=ethereum.goerli deploys on the Ethereum Görli testnet.

You can find a complete list of supported networks in the addresses object in the migrations/2_user_migrations.js file, and also here.

Interact with your contract

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

Remember: You are not alone!

Join the Witnet Community Discord or Telegram. Members of the Witnet community will be happy to answer your questions and assist you through this tutorial.