Skip to Content

Creating a DApp Using Oracle Data

Developer Tutorial

We have created a full step-by-step developer tutorial with a working NFT DApp using the Morpher Oracle. If you are looking for a thorough introduction into the Oracle and see it in action, then this is for you!

Open the tutorial

To integrate the Morpher ERC-4337 Data Oracle into your decentralized application, follow these steps to create a DApp that utilizes oracle data through a data-dependent smart contract.

Prerequisites

  • Knowledge of Solidity: Basic experience of smart contract development.
  • Knowledge of Account Abstraction: Familiarity with the ERC-4337 fundamentals.

Step 1: Implement the DataDependent Interface

Your smart contract must implement the DataDependent interface to specify the data it requires from the oracle. For each function of the contract that needs oracle data, you have to return an array of DataRequirements for that specific function selector. In each data requirement, you have to specify the provider address, the data key that the provider sells data for and the address of the consumer of that data (usually your smart contract itself).

pragma solidity ^0.8.26; interface DataDependent { struct DataRequirement { address provider; address requester; bytes32 dataKey; } function requirements( bytes4 _selector ) external view returns (DataRequirement[] memory); } contract YourContract is DataDependent { address dataProvider; //get this from the Feed page address oracle; bytes32 BTC_USD = keccak256("BINANCE:BTC_USDT"); // ... function requirements(bytes4 _selector) external view override returns (DataRequirement[] memory) { if (_selector == 0x6a627842) { DataRequirement[] memory requirement = new DataRequirement[](1); requirement[0] = DataRequirement(dataProvider, address(this), BTC_USD); return requirement; } return new DataRequirement[](0); } // ... }

Step 2: Call consumeData

When you need to consume the data, you need to call the OracleEntrypoint consumeData function. The following example show how you can fetch the price of an asset encoded with the 6-1-25 standard: 6 bytes for the timestamp, 1 for the number of decimals and 25 for the price value. Each provider can encode that as preferred, you need to know that when you’re writing your consume function.

pragma solidity ^0.8.26; interface OracleEntrypoint { function consumeData( address _provider, bytes32 _dataKey ) public payable returns (bytes32); function prices(address _provider, bytes32 _dataKey) public view returns(uint256); } contract YourContract is DataDependent { struct ResponseWithExpenses { uint value; uint expenses; } // ... function _invokeOracle(address _provider, bytes32 _key) private returns (ResponseWithExpenses memory) { uint expenses = OracleEntrypoint(oracle).prices(_provider, _key); // pay the oracle now, then get the funds later from sender as you wish (eg. deduct from msg.value) bytes32 response = OracleEntrypoint(oracle).consumeData{value: expenses}(_provider, _key); uint256 asUint = uint256(response); uint256 timestamp = asUint >> (26 * 8); // in this example we want the price to be fresh require(timestamp > 1000 * (block.timestamp - 30), "Data too old!"); uint8 decimals = uint8((asUint >> (25 * 8)) - timestamp * (2 ** 8)); // in this example we expect a response with 18 decimals require(decimals == 18, "Oracle response with wrong decimals!"); uint256 price = uint256( asUint - timestamp * (2 ** (26 * 8)) - decimals * (2 ** (25 * 8)) ); return ResponseWithExpenses(price, expenses); } // ... }

Step 3: Create an ERC-4337 Client

For the final part, you can refer to the documentation of Candide Labs’ AbstractionKit. Just make sure to install the DataDependent fork (npm i @morpher-io/dd-abstractionkit) instead.

From the developer perspective, there is nothing different when using the DataDependent version. The kit will take care of fetching the data requirements from your smart contract, estimate the gas using the modified rpc endpoint and submitting the data-dependent user operation as if it is a regular one. Remember to use your chosen provider’s Bundler RPC and not a random one!

Step 4: Enjoy Realtime Cheap Data!

Now you can provide your users DApps with real time oracle data for a (usually, depending on provider) very convenient price! If you want to start building right away, check our Morpher Data Feeds.