Fetch Data via Push Model

Using the Push model means retrieving data from on-chain contract storage. This requires the data to exist in the on-chain contract storage, uploaded by a publisher or node operator beforehand. Entangle maintains a node that continuously publishes updates for supported assets, but anyone is free to run their own node instance to ensure redundancy or customize their setup.

To utilize an update in your contract, you can read the value of the asset of interest from the UDFOracle contract deployed on each supported chain. For demonstration purposes, let's create a consumer contract whose logic depends on the NGL/USD price.

Step #1: Initialize hardhat project

First of all, let's create new project and initialize hardhat in it.

$ mkdir push-consumer && cd push-consumer
$ yarn init -y
# Yarn initialization
$ yarn add --dev hardhat
$ yarn hardhat init
# Hardhat initialization

Step #2: Create PushConsumer contract and hardhat ignition script

Next, let's create our consumer contract. The consumer contract, referred to as the PushConsumer, will retrieve data stored in UDFOracle, which assumes the availability of verified updates pushed on-chain beforehand.

For this example, the contract will read the latest stored price of the NGL/USD asset and emit an event. To achieve this, we'll use the latestUpdate function, a free data retrieval method. For convenience, this function returns both the price and the timestamp in a single call. Providing both values ensures that users can verify the freshness of the data, which is critical for applications where price accuracy and timing are essential for decision-making.

./contracts/PushConsumer.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

interface IUDFOracle {
    /// @notice mapping of dataKey to the latest update
    function latestUpdate(bytes32 dataKey) external view returns (uint256,uint256);
}

contract PushConsumer {
    // @notice DataKey for NGL/USD asset
    bytes32 constant public NGL_DATAKEY = 0x4e474c2f55534400000000000000000000000000000000000000000000000000;

    // @notice PullOracle address on specific chain
    IUDFOracle public udfOracle;

    constructor(address _udfOracle) {
        udfOracle = IUDFOracle(_udfOracle);
    }

    event PriceConsumed(bytes32 feedKey, uint256 price, uint256 timestamp);

    // @notice function that reads and uses update from PullOracle
    function consumePrice() public {

        // Read latest update from PullOracle contract
        uint256 latestPrice; uint256 latestTimestamp;
        (latestPrice, latestTimestamp) = udfOracle.latestUpdate(NGL_DATAKEY);

        // Emit an event with the latest update
        emit PriceConsumed(NGL_DATAKEY, latestPrice, latestTimestamp);
    }
}

Next, create a ignition script to help with deployment of new PushConsumer contract.

./ignition/modules/PushConsumer.ts
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

// UDFOralce address on Eth Sepolia network
// TODO: Change address
const UDFOracleAddress = "0xa22Cb39480D660c1C68e3dEa2B9b4e3683773035"

const PushConsumerModule = buildModule("PushConsumerModule", (m) => {
  const consumer = m.contract("PushConsumer", [UDFOracleAddress]);

  return { consumer };
});

export default PushConsumerModule;

Optional: Spin up local development node forked from eth_sepolia.

To deploy your contract on testnet, you'll need ETH eth_sepolia testnet tokens. In case you don't have ETH on eth_sepolia, you can spin up a local development fork of eth_sepolia by using anvil node like this:

$ anvil --fork-url https://ethereum-sepolia-rpc.publicnode.com

Copy one of private keys from stdout to use it for deployment.


Step #3: Deploy PushConsumer

Now let's modify hardhat.config.ts to add eth_sepolia network.

./hardhat.config.ts
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
  solidity: "0.8.28",
  networks: {
    eth_sepolia: {
        chainId: 11155111,
        // or http://127.0.0.1:8545, if you forked it locally
        url: "https://ethereum-sepolia-rpc.publicnode.com",
        accounts: [ "0x" ], // TODO: Set deployer private key
    },
  },
};

export default config;

Now we're ready to deploy PushConsumer contract.

$ yarn hardhat ignition deploy ./ignition/modules/PushConsumer.ts --network eth_sepolia
 Confirm deploy to network eth_sepolia (11155111)? … yes
Compiled 1 Solidity file successfully (evm target: paris).
Hardhat Ignition 🚀

Deploying [ PushConsumerModule ]

Batch #1
  Executed PushConsumerModule#PushConsumer

[ PushConsumerModule ] successfully deployed 🚀

Deployed Addresses

PushConsumerModule#PushConsumer - 0x3f724844973A8e5F669499D366943A676F6EF7CE

Step #4: Execute PushConsumer.consumePrice transaction

To validate that we have our price, we will write an example backend script that send transaction to execute PushConsumer.consumePrice. Let's create a separate directory scripts and our new script consumePrice.ts to do just that.

$ mkdir scripts && touch ./scripts/consumePrice.ts
./scripts/consumePrice.ts
import {
  PushConsumer,
} from "../typechain-types";
import { ethers } from "hardhat";

// PushConsumer address we got from the deployment
const PushConsumerAddress = "0x3f724844973A8e5F669499D366943A676F6EF7CE";

async function main() {  
  const pushConsumer = await ethers.getContractAt(
    "PushConsumer",
    PushConsumerAddress
  ) as PushConsumer;

  let tx = await pushConsumer.consumePrice();
  await tx.wait();

  console.log("sent PushConsumer.consumePrice tx", tx.hash);
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

Execute this script to send transaction to the blockchain

$ yarn hardhat run ./scripts/consumePrice.ts --network eth_sepolia
sent PushConsumer.consumePrice tx 0xe8ffb6d67297241439333cfa4eb6d53aa814e491a9f5b52d3bb9a925153a316d

You can examine transaction closely using cast to verify that event was emitted with expected values.

$ cast run 0xe8ffb6d67297241439333cfa4eb6d53aa814e491a9f5b52d3bb9a925153a316d
Executing previous transactions from the block.
Traces:
  [18205] 0x06fAdf55c689Da5d17472FE604302e595Bd257c0::cd00fbb8()
    ├─ [10632] 0xa22Cb39480D660c1C68e3dEa2B9b4e3683773035::f0f0fce9(4e474c2f55534400000000000000000000000000000000000000000000000000) [staticcall]
       ├─ [5736] 0xE9698AE915c8D813929A89087607cb4771E15bB4::f0f0fce9(4e474c2f55534400000000000000000000000000000000000000000000000000) [delegatecall]
          └─  [Return] 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
       └─  [Return] 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    ├─  emit topic 0: 0xca844ada88fd18cb2bf8845797fa4b85183b1fbc60f9cd9b06db90df5b4e074b
               data: 0x4e474c2f5553440000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    └─  [Stop]


Transaction successfully executed.
Gas used: 39257

Last updated

Was this helpful?