How to build a Decentralized Oracle on Ethereum — A Step-by-Step Guide
Download Docker Desktop on your Machine and install it.
Great! Now that you have a wallet, let’s get some ETH to interact with the Ethereum Blockchain, and some RLC to interact with the iExec’s platform.
Reminder: All commands in this tutorial that interact with the Blockchain will use the Kovan testnet blockchain. It works identically to Ethereum Mainnet, but doesn’t require real money to get started. Perfect for testing!
Let’s ask iExec for some free Kovan RLC:
iexec wallet getRLC --chain kovan
At this stage, you should see your Wallet displayed in your terminal with a credit of 200 nRLC.
Now to get some free Kovan ETH, you will need to post your Ethereum wallet address in a Gitter group chat. Let’s show our wallet address:
iexec wallet show --chain kovan
Let’s go over the Kovan Faucet Gitter, sign in to Gitter and copy-paste your wallet address in the discussion.
Before going further, we need to ensure that our wallet received the ETH and the RLC we asked for. Run this command to check your wallet balance:
iexec wallet show --chain kovan
Now let’s top up our iExec account and move on to the next section:
iexec account deposit 200 --chain kovan
In order to run the price-feed application on the iExec’s decentralized cloud, we need to go through the following steps:
- Dockerize the “price-feed” application,
- Push it on the DockerHub public registry
- Deploy it on the iExec’s Platform
Ensure that your terminal is in the price-feed folder, and let’s create a new folder named app, containing the off-chain logic:
mkdir app
cd app # enter the folder
Then download the price-feed JS script that implements the price fetching logic (we encourage you to have a look at the JS code):
curl -o ./price-feed.js https://raw.githubusercontent.com/iExecBlockchainComputing/iexec-apps/master/PriceFeed/src/oracle.js
Now, let’s dockerize our JS application. The first step is to open your IDE (Developer environment), create a file named Dockerfile in the app folder, and paste below content in it:
FROM node:11-alpine
COPY price-feed.js /src/price-feed.js
RUN npm i https ethers fs
ENTRYPOINT [“node”, “src/price-feed.js”]
At this stage, we are ready to build the Docker image and publish it to dockerhub. In your terminal, run these commands:
docker build . #write down your image ID (it appears after the text "successfully built 19acce70289d" <-- that is the image ID)
You’ve built a docker image. Now we want to tag this image with our dockerhub username:
docker tag <IMAGE_ID> <YOUR_DOCKERHUB_NAME>/price-feed #ex: docker tag 19acce70289d iexechub/price-feed
Finally, we can push our local image to the dockerhub public repository:
docker push <YOUR_DOCKERHUB_NAME>/price-feed #write down your image digest (it appears after sha256:959eb75b13efb41a8f37495784150574d66175adebd0c6c18216b482c574d109 <-- this is your image digest)
Now we are just a few steps from having our application being “iExec’s ready”:
cd .. #move to upper folder
iexec app init #tell the SDK that you want to create an app
Then, open the newly created “iexec.json” file, and edit these three fields:
- owner: “Your_key_wallet”
- name: We name it “PriceFeed”.
- multiaddr: replace by dockerhub repository name. “registry.hub.docker.com/<YOUR_DOCKERHUB_NAME>/price-feed:latest”.
- checksum: given by the command docker push. (add 0x before the sha256:)
Now let’s deploy your price-feed application on iExec, in the terminal:
iexec app deploy --chain kovan #that is a blockchain transaction
Finally, let’s publish sell orders for your application, so iExec platform users are able to use your application at the price you write on the orders:
iexec order init --app
iexec order sign --app
iexec order publish --app # publish your apporder on the marketplace and get an orderHash
iexec order show --app [orderHash]
Let’s clone the repo on your terminal, enter the smart-contract folder, and install JS dependencies:
git clone https://github.com/iExecBlockchainComputing/price-feed-doracle.git smart-contract
cd smart-contract
npm install --unsafe-perm=true --allow-root
In order to deploy the smart contract on the Blockchain, you need to tell Truffle your wallet private key. Here is how you can show your wallet’s private key:
cd .. # move to upper folder
iexec wallet show --show-private-key # copy your private key for next step
Now you are ready to deploy. Thanks to truffle, that’s as simple as this:
cd smart-contract # move back to the smart-contract folder
MNEMONIC=
<YOUR_PRIVATE_KEY> ./node_modules/.bin/truffle migrate --network kovan # Deploy your smart contract to the Blockchain
Great, your DOracle smart contract is now deployed on Kovan testnet blockchain. How about feeding it with the ETH/USD price?
Now let’s buy a “price-feed” run on iExec by running these commands on your terminal:
cd .. # move back to parent folder price-feed
Let’s show the unique ID (order hash) of the orders we want to buy. One is the workerpool order, and the other one is our price feed app order:
iexec orderbook workerpool --category 2 # copy the workerpool order hash
iexec orderbook app <address> # copy the app order hash
Now we want to fill the orders (workerpool + app), that’s as simple as running:
iexec order fill --app <APP_ORDER_HASH> --workerpool <WORKERPOOL_ORDER_HASH> --chain kovan # start the DOracle computation!
That’s it! we sent to the iExec platform the computation request. To ensure we all talk the same language, you now have a deal, and in that deal there is one task (that’s a way to enable the coming bag of task feature). Let’s track the progress of the computation by using these commands:
iexec deal show <DEAL_ID> --tasks 0 --chain kovan
iexec task show <TASK_ID> --watch --chain kovan
You can also head over the iExec Explorer (https://explorer.iex.ec) to track your tasks. After a few minutes, the status should turn to “completed”.
So what happened so far? We ask a workerpool to run our price-feed application, the workers fetched the ETH/USD price, the result is validated by the PoCo, and as soon as there is consensus, it is stored back into iExec’s smart contracts. Last step consists of calling the function named “_iexecDoracleGetVerifiedResult” on your doracle smart contract, so it will check the result stored on the iExec’s smart contract, run some filtering logic (ex: is the new price associated with a UTC date more recent than the previous one I pulled), and if all your custom conditions are fulfilled, then it will update itself with the new ETH/USD price! Makes sense?
We will soon update the article with a truffle script to show this last step. If you can’t wait, just try by yourself! Also, we encourage you to try out our price-feed frontend to see what the end-user experience could look like: https://price-feed-doracle.iex.ec
That’s it! We hope you realized how easy it is to build a decentralized oracle on iExec.
If you liked it, then you definitely want to learn more about our dev training reward program that makes building DOracle on iExec fun & financially rewarding:
Published at Wed, 10 Jul 2019 09:05:04 +0000
