Build a dApp on xDai Chain With Secure Data Feeds

xDai chain is an Ethereum-based stable sidechain that uses USD-backed stablecoin xDai (a representation of DAI token) as the native coin of the network, offering developers fast, inexpensive, and stable transactions.

As the most widely adopted decentralized oracle network, Chainlink is the go-to oracle solution for xDai developers, integrating natively with xDai Chain to power tamper-proof external data feeds.

In this technical article, we’ll show you how to use Chainlink Price Feeds to give your xDai Chain smart contracts access to high-quality, tamper-resistant price data that offers broad market coverage and secures your dApp against potential oracle exploits and flash loan funded attacks.

The Need for Scalable and Stable Transactions

This year has seen explosive growth in DeFi protocols, with the total value locked (TVL) in DeFi skyrocketing from $16B in January, to now over $40B. However, along with this growth comes an increased volume of on-chain transactions, which has a flow-on effect for users who can experience unpredictably high gas fees and slow transaction times.

Layer-2 networks such as xDai Chain have emerged to support the growth of DeFi by providing increased scalability, higher throughput, and more stable transactions based on the xDai token. Within xDai’s token model, all gas fees for broadcasting transactions on the network are paid in xDai, creating a stable and economically predictable blockchain environment for developers to build on. Moreover, xDai Chain is a fully EVM-compatible sidechain to Ethereum, which means those familiar with using Solidity, MetaMask, and other Ethereum-specific tools and languages can easily work and interact with xDai Chain without having to learn new languages or set up new wallets to generate transactions.

However, like any other blockchain or layer-2 solution supporting smart contracts, there is a pressing need to provide smart contracts with access to high-quality and reliable external data and enable dApps that can react to real-world events, such as changes in the price of an asset, as well as output to off-chain systems.

Chainlink Price Feeds have established themselves as the standard in blockchain data feeds, becoming the most widely used feeds for securing real value for smart contracts that require external price data. Chainlink oracle networks’ decentralized architecture and wide array of premium data sources ensure the latest price in any feed is securely aggregated and reflects broad market coverage.

Now that we understand the advantages of building a decentralized application on xDai Chain, and the crucial role Chainlink Price Feeds play in providing high-quality and secure price data, we’ll go through an example of using a Chainlink Price Feed when building a dApp on xDai.

The first step in using Chainlink Price Feeds on xDai Chain is to set up your MetaMask wallet to connect to the xDai Chain mainnet. The main RPC is a load balancer with 4 nodes, health checks, and failover, however there are alternate RPCs to also connect to if required.

 

How to connect to xDai chain.
How to connect to xDai chain.

Once you have connected to xDai Chain, you can then use BlockScout faucet to obtain a small amount of free mainnet xDai. xDai is the native token of the xDai sidechain and is used as gas for transactions. If the faucet is empty, you can purchase xDai with fiat via Ramp or MtPelerin, via the USDT/xDAI pair on BitMax or you can use the Dai to xDai bridge.

How to obtain free mainnet xDai
Obtaining free mainnet xDai

Creating the Smart Contract

The easiest way to start building a smart contract that uses Chainlink Price Feeds on xDai is to begin with the standard Price Consumer contract. This is a basic standardized contract for initiating requests to Chainlink Price Feeds. We will open this contract in Remix and modify it to suit our needs. For the purposes of this demo, we will use the ETH/USD Price Feed. The first step is to initialize a xDai Price Feed reference contract in our constructor. The ETH/USD Price Feed reference contract is deployed at the address 0xa767f745331D267c7751297D982b050c93985627.

priceFeed = AggregatorV3Interface(0xa767f745331D267c7751297D982b050c93985627);

Next, we need to define a function to obtain the latest price from the Price Feed Aggregator contract that was instantiated in the constructor above. To do this, we define a new function that calls the latestRoundData function from the Aggregator contract. This is the function that returns the current state of the Aggregator contract, and in this case we are taking the current price and returning it in our consuming function.

function getLatestPrice() public view returns (int) {
        (
            uint80 roundID, 
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        return price;
    }

Deploying and Testing the Smart Contract

Now we are ready to deploy and test our contract. Compile the contract in Remix, then on the deployment tab, change the environment to “Injected Web3”, and ensure the wallet address below is the one in your MetaMask wallet that contains your xDai tokens, press the deploy button, and follow the steps. The end result is you have your smart contract deployed to the xDai Chain mainnet. You should take note of the deployed contract address via the transaction output in the Remix console.

Once deployed, we simply need to execute the getLatestPrice function. The result should be that the function returns the latest price from the ETH/USD Aggregator contract, which can then be used in our decentralized application on xDai Chain.

Viewing the ETH/USD Price Feed result
ETH/USD Price Feed result

Summary

xDai Chain offers a stable and economically predictable layer-2 option for decentralized applications to be built upon, with its fast, cheap, and stable transactions. This value proposition is further enhanced with Chainlink Price Feeds, which natively integrate with the sidechain to give xDai smart contracts access to high-quality aggregated price data that can be used in all kinds of applications.

If you’re a developer and want to quickly get your application connected to Chainlink Price Reference Data, visit the developer documentation and join the technical discussion in Discord. If you want to schedule a call to discuss the xDai/Chainlink integration more in-depth, reach out here.

More on This Topic

Need Integration Support?
Talk to an expert
Faucets
Get testnet tokens
Read the Docs
Technical documentation