Site icon Tech Blog

A Step-by-Step Guide on Integrating Witnet PriceFeed on Conflux

This article will elaborate on a Step-by-Step Guide on Integrating Witnet PriceFeed on Conflux

What is Witnet?

Witnet PriceFeed on Conflux

Witnet is known as a decentralized oracle network that is born to simplify the integration of smart contracts on blockchain systems with outside data and information. It enables smart contracts to securely and autonomously access information from the internet and other off-chain sources, which is crucial for many blockchain-based use cases.

Reading data in real-time is of great necessity for decentralized apps (dApps) to function properly. This is particularly true for DeFi programs that depend on precise pricing data. A trustworthy and decentralized way to access external data on the Conflux blockchain is provided by Witnet, a decentralized oracle network. We will provide useful code examples to help you walk you through the process of integrating Witnet’s PriceFeed on Conflux on this article.

Understanding  what is Witnet PriceFeed

Before we begin the Step-by-Step Guide on Integrating Witnet PriceFeed on Conflux, let’s define Witnet PriceFeed and understand why decentralized apps (dApps) on Conflux find it useful.

Witnet PriceFeed is a dependable option for acquiring trustworthy and impervious pricing information since it is a decentralized oracle solution that collects price data from many sources. Conflux dApps may get precise pricing information for a variety of assets via Witnet PriceFeed, protecting the reliability of their DeFi protocols and apps.

Step-by-Step Guide on Integrating Witnet PriceFeed on Conflux

It is quite easy to get data in real-time and other data sources using Witnet data feeds.

Step 1

  1. Set Up a Development Environment: Before you start, ensure you have a development environment set up. You can use tools like Remix IDE, VS Code or Truffle to streamline your development process.
  2. MetaMask: Ensure you have metamask installed on your browser pluggin section, you can go here Metamask Install and install your metamask.
  3. Install the Conflux Portal Wallet: You’ll need a Conflux wallet to interact with the Conflux blockchain. Download and install the Conflux Portal Wallet extension for your preferred browser.( If you don’t want to use metamask for deployment)
  4. Install Node.js: If you haven’t already, download and install Node.js from the official website (https://nodejs.org/).(If you developing with your VScode)
  5. Install WitnetJS: Witnet provides a JavaScript library for interacting with the network. Install it using npm with the following command( During integrating and interacting with your (DAPP)

This are also prerequisites before you get started:

  1. A basic understanding of Solidity and Remix IDE.
  2. A Conflux wallet or metamask and some mainnet CFX tokens  or testnet tokens by going here Faucets for deploying and interacting with the smart contract.
  3. Node.js and npm installed on your development machine (If you want to use VScode and Truffle for development and deployment).

Step 2

 

You can find the support currency pairs for conflux here; Witnet Support Pricefeed for conflux

NB: Ensure to take note of the Witnet Price Router for each depending on the pair you want to deploy

Example: 0x806c8dFd322EE2d52b188CC472e0814F64304C32

Step 3: Create Your Remix Project

Now, let’s create a new project in Remix IDE:

  1. Open Remix IDE in your browser (https://remix.ethereum.org/).
  2. Click on the “+” button in the file explorer to create a new file. Name it “WitnetPriceFeed.sol.”
  3. Write your Solidity smart contract that will utilize the Witnet PriceFeed. Here’s a simple example:

 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

import “witnet-solidity-bridge/contracts/interfaces/IWitnetPriceRouter.sol”;

contract MyContract {
IWitnetPriceRouter immutable public router;

/**
* IMPORTANT: pass the WitnetPriceRouter address depending on
* the network you are using! Please find available addresses here:
* https://docs.witnet.io/smart-contracts/price-feeds/contract-addresses
*/
constructor(IWitnetPriceRouter _router) {
router = _router;
}

/// Returns the BTC / USD price (6 decimals), ultimately provided by the Witnet oracle.
function getBtcUsdPrice() public view returns (int256 _price) {
(_price,,) = router.valueFor(bytes4(0x24beead4));
}

/// Returns the ETH / USD price (6 decimals), ultimately provided by the Witnet oracle.
function getEthUsdPrice() public view returns (int256 _price) {
(_price,,) = router.valueFor(bytes4(0x3d15f701));
}

/// Returns the BTC / ETH price (6 decimals), derived from the ETH/USD and
/// the BTC/USD pairs that were ultimately provided by the Witnet oracle.
function getBtcEthPrice() public view returns (int256 _price) {
return (1000000 * getBtcUsdPrice()) / getEthUsdPrice();
}
}

  1. Save your contract.

Step 3: Compile and Deploy Your Contract

Now, let’s compile and deploy your contract to the Conflux blockchain:

  1. Select the “Solidity” compiler from the Remix sidebar.
  2. Compile your contract by clicking the “Compile WitnetPriceFeed.sol” button.
  3. In the “Deploy & Run Transactions” section, select “Injected Web3” as the Environment.
  4. Click the “Deploy” button to deploy your contract to the Conflux blockchain using your Conflux Portal Wallet.

Step 4: Interact with the Witnet PriceFeed

After deploying your contract, you can now interact with the Witnet PriceFeed:

  1. Call the fetchPrice function to fetch the current price from the Witnet network and store it in the currentPrice variable.
  2. Use the currentPrice variable in your application as needed.
Exit mobile version