This story was written using HackerNoon’s writing prompt for the Bitcoin Writing Contest. If you’d like to participate in the #bitcoin writing contest but feel this template isn’t right for you, feel free to explore any of the other two options:
My hybrid smart contract is designed to tap into Chainlink's Price Feeds on Rootstock to get real-world asset prices. This is particularly useful if you're building DeFi applications that need reliable price data.
Because Rootstock, being merge-mined with Bitcoin, inherits its unparalleled security model while maintaining full EVM compatibility. This means I can leverage the entire Ethereum development ecosystem while building on Bitcoin's foundation. Moreover, if also add Chainlink's oracle network to the mix, I suddenly have the ability to create a smart contract that can interact with real-world data and systems.
RootStockPriceFeed.sol
SPDX-License-Identifier: MIT
getLatestPrice()
function is marked as view
, meaning it doesn't modify state but only reads from the oracle.This is a proof-of-concept(POC) and since I am using the Chainlink battle-tested AggregatorV3Interface.sol contract but with no offical price feed contract address for RootStock on Chainlink https://docs.chain.link/data-feeds/price-feeds/addresses, I tested my RootStockPriceFeed.sol using a depolyment script to do a staging test on the RSK Testnet using my own public address “0xEd752dCE9f6c1Db35FeDABca445617A0d2B0b674”
Eventhough there was no need to do an official audit of my POC, I used one of the most popular static analysis tools to do an audit to ensure safety of my hybrid smart contract using: Slither.
The output color codes potential issues:
informational
findings, we may want to have a look
Before we dive into the technical details, make sure you have Node.js and nvm version manager for node.js installed on your system since we'll need it to install and use Node version v18.0.0 that is compatible with Rootstock starter kit. You'll also need Hardhat and a basic grasp of smart contracts with Solidity and JavaScript/TypeScript. You'll also need a Metamask wallet configured for the Rootstock network and some test RBTC if you're planning to follow along.
Let's start by setting up our development environment. First, we'll clone the Rootstock Hardhat Starter Kit, which provides a solid foundation for our project:
Now, let's install all the required dependencies:
Let's rename our project directory to better reflect its purpose:
We'll need to modify our package.json and package-lock.json to reflect our project's new name and add Chainlink specific dependencies:
After installation, add the plugin to your Hardhat config:
The magic happens in our configuration setup. We need to tell Hardhat how to work with both Rootstock and Chainlink. I've found that keeping this configuration clean and well-organized saves countless hours down the line. Update your hardhat.config.ts
file:
Don't forget to create a .env
file to store your sensitive information:
Next, let's test our RootStock configuration, but first make sure you have sufficient Testnet RBTC funds available on your Metamask wallet. You can get some from a Rootstock Testnet Faucet.
Then, let's deploy the contract. We'll use the --tags
flag to only deploy the MultiToken 1155
contract.
Output:
Next, let's test our Chainlink confirguration to get a list of all available getter-method for a specific registry
Output:
Now, let's build something practical - a smart contract that taps into Chainlink's Price Feeds on Rootstock to get real-world asset prices. This is particularly useful if you're building DeFi applications that need reliable price data. Here's what that looks like:
To bring our contract to life, we need a deployment script. Create scripts/deploy.ts
:
Finally, let's deploy our hybrid smart contract to the Rootstock RSK Testnet by running the deploy script:
Output:
The magnificence of combining Rootstock with Chainlink through Hardhat is that it opens up a world of possibilities. You're not just building smart contracts; you're given the opportunity to creating secure, Bitcoin-based applications that can interact with real-world data. The security of Bitcoin's network, the flexibility of EVM compatibility, and reliable external data through Chainlink oracles - it's a powerful combination that's greater than the sum of its parts.
This simple contract demonstrates how to create a hybrid smart contract that leverages off-chain data (via Chainlink) to provide on-chain functionality (retrieving asset prices) on RootStock.
All worked well, with some warnings after a quick audit using Slither static analysis tool and next time around I will do a few changes:
latestRoundData()
.
You can visit my GitHub Repo and check out the POC here: https://github.com/EdwinLiavaa/Rootstock-Chainlink-Hardhat-Starterkit
That’s all!
If you’d like to participate in the #bitcoin writing contest but feel this template isn’t right for you, feel free to explore any of the other two options: