DEV Community

Neville Adam
Neville Adam

Posted on

How Can You Implement Off-Chain Data in a Blockchain Smart Contract?

Problem Faced:
Smart contracts cannot directly fetch real-world (off-chain) data, such as asset prices, weather conditions, or sports scores.

Solution:
Use oracles like Chainlink to bring external data onto the blockchain.

  • Smart contracts request data from an oracle.
  • The oracle fetches data from trusted APIs and pushes it back on-chain.

Using Chainlink Price Feed:
solididty

pragma solidity ^0.8.0;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract PriceConsumer {
    AggregatorV3Interface internal priceFeed;

    constructor() {
        priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); // ETH/USD on Rinkeby
    }

    function getLatestPrice() public view returns (int) {
        (,int price,,,) = priceFeed.latestRoundData();
        return price;
    }
}

Enter fullscreen mode Exit fullscreen mode

Build secure, scalable, and customized blockchain solutions tailored to your business needs. From smart contract development to decentralized applications, get end-to-end services for your blockchain projects. Our blockchain development ensures seamless integration, high security, and innovative solutions for Web3, DeFi, and enterprise blockchain applications. Let’s shape the future of decentralized technology together!

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay