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:
1. What is your smart contract designed to do?
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.
2. Why deploy on Roostock, what benefits does the platform offer compared to alternatives?
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.
3. Break down the key components of your smart contract i.e state variables, functions, events, etc
Smart Contract
RootStockPriceFeed.sol
SPDX-License-Identifier: MIT
Imports
State Variables
Constructor
Functions
Key Points
- The contract relies on an external Chainlink oracle for price data, making it a hybrid smart contract.
- The
getLatestPrice()
function is marked asview
, meaning it doesn't modify state but only reads from the oracle. - The contract assumes the Chainlink oracle is already deployed and has the correct interface implemented.
4. How did you test your smart contract? Did you notice any vulnerabilities while testing? If so, what were they and how did you correct for them
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ā
5. What steps have you taken to keep your smart contract safe?
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:
- Green - Areas that are probably ok, may be
informational
findings, we may want to have a look - Yellow - Potential issue detected, we should probably take a closer look
- Red - Signifant issues detected that should absolutely be addressed.
6. Share, step-by-step, how you deployed your smart contract on Rootstock.
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:
7. What did you learn from your deployment experience? What worked well, and what would you change the next time around?
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:
- Use of internal variables for interacting with other contracts.
- Proper error handling could be added to deal with potential issues when calling
latestRoundData()
. - Consider adding events for important state changes or actions taken by the contract.
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: