Basic Witnet tutorial: how to create a decentralized Bitcoin price feed on Ethereum with Solidity and Witnet¶
What and why?¶
In this tutorial you will:
- Query two different APIs for the USD price of 1 bitcoin using 25 witnessing nodes from Witnet.
- Tell those nodes to aggregate the values from both APIs and report the result.
- Define how to tally the results reported by the different nodes into a single data point that can be trustlessly consumed by an Ethereum smart contract.
- Write a Solidity contract that keeps the bitcoin price in a public variable and handles updates using Witnet.
- Compile and deploy the entire flow into a local Ethereum network.
Remember: using Witnet from Ethereum and other EVM-compatible chains is particularly convenient because you do not need to own or pay any wit tokens: you pay the bridge nodes using ETH or the chain's native token and then they spend their own wit tokens when posting your requests into Witnet on your behalf.
How decentralized will this price feed be?¶
None of the parties involved in the process of deploying, updating and using the price feed will have any power to tamper with the integrity of the data points it provides:
- Once deployed, no one will be able to prevent the price feed from being updated or queried.
- Nobody can set the price directly. The only way to update it is through a Witnet request.
- The price is averaged from two different public APIs, thus mitigating their influence in the final price.
- The data is relayed by 25 different Witnet nodes, whose reported data points are aggregated and averaged, filtering out any outliers so as to cancel any malicious reporter who may try to leverage a slight drift of the data point.
What will the code look like?¶
The final result of this tutorial is available in this GitHub repository. You can also check it out with Truffle:
mkdir PriceFeed
cd PriceFeed
truffle unbox witnet/witnet-pricefeed-tutorial
Ready to start?¶
Let's begin by creating a new Witnet-enabled Solidity project.