paint-brush
Deploying Hybrid Smart Contracts on Rootstockby@edwinliavaa
New Story

Deploying Hybrid Smart Contracts on Rootstock

by Edwin Liava'aNovember 18th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Building a smart contract designed to tap into Chainlink's Price Feeds on Rootstock to get real-world asset prices.
featured image - Deploying Hybrid Smart Contracts on Rootstock
Edwin Liava'a HackerNoon profile picture


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

The battle-tested Chainlink Price Feed  aggregator smart contract

State Variables

This is the main state variable in the contract. It's an internal reference to the Chainlink Price Feed aggregator smart contract.

Constructor

The constructor initializes the priceFeed state variable with the address passed as an argument.

Functions

This is a view function that retrieves the latest price from the Chainlink Price Feed.It uses the latestRoundData() function from the AggregatorV3Interface to get the current price.

Key Points

  • The contract relies on an external Chainlink oracle for price data, making it a hybrid smart contract.
  • The getLatestPrice() function is marked as view, 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”


// scripts/deploy.ts

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.


Slither - run command

Slither - Yellow - Potential issue detected, we should probably take a closer look

Slither - Green - Areas that are probably ok, may be informational findings, we may want to have a look


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:

image

Now, let's install all the required dependencies:

image

Let's rename our project directory to better reflect its purpose:

image

We'll need to modify our package.json and package-lock.json to reflect our project's new name and add Chainlink specific dependencies:

image

After installation, add the plugin to your Hardhat config:

image

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:

image

Don't forget to create a .env file to store your sensitive information:

image

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.

image

Output:

image

Next, let's test our Chainlink confirguration to get a list of all available getter-method for a specific registry image

Output: image

image

image

imageNow, 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:


image

To bring our contract to life, we need a deployment script. Create scripts/deploy.ts:

image

Finally, let's deploy our hybrid smart contract to the Rootstock RSK Testnet by running the deploy script:

image

Output:

image

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:


  1. Use of internal variables for interacting with other contracts.
  2. Proper error handling could be added to deal with potential issues when calling latestRoundData().
  3. 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: