EVM Smart Contracts
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. UDF 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 PushMarketplace
contract deployed on each supported chain. For demonstration purposes, in this guide we will create a consumer contract whose logic depends on the BTC/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 flow(select TypeScript project)
Step 2: Create PushReader contract and hardhat ignition script
Next, let's create our consumer contract. The consumer contract, referred to as the PushReader, will retrieve data stored in PushMarketplace
, which assumes the availability of verified updates pushed on-chain beforehand.
In this example, the contract will read the latest stored price of the BTC/USD asset and store it. To achieve this, we'll use the getFeedPrice
function, a free data retrieval method for previously subscribed readers.
This contract should already be in subscription so it can have access for verification. The subscription can be purchased from the marketplace and the consumer contract needs to be added as an authorized address for the subscription.
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.
// SPDX-License-Identifier: BSL1.1
pragma solidity ^0.8.20;
interface IPushMarketplace {
function getFeedPrice(
bytes32 feed
) external returns (uint256 price, uint256 timestamp);
}
contract PushReader {
IPushMarketplace public pushMarketplace;
uint public latestPrice;
uint public latestTimestamp;
constructor(address _pushMarketplace){
pushMarketplace = IPushMarketplace(_pushMarketplace);
}
function getData(bytes32 feedKey) external{
(latestPrice, latestTimestamp) = pushMarketplace.getFeedPrice(feedKey);
}
}
Next, create an ignition script to help with deployment of the new PushReader
contract.
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
// PushMarketplace address on Eth Sepolia network
const PushMarketplaceAddress = "0x48A10FF4ea77A099Cc966b537b5efC5b15c1060A"
const PushReaderModule = buildModule("PushReaderModule", (m) => {
const consumer = m.contract("PushReader", [PushMarketplaceAddress]);
return { consumer };
});
export default PushReaderModule;
Step 3: Deploy PushReader
Now let's modify the hardhat configuration file to add the eth_sepolia
network.
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 can deploy the PushReader
contract by running the command below.
yarn hardhat ignition deploy ./ignition/modules/PushReader.ts --network eth_sepolia
Step 4: Create Push Subscribtion
Now we should add deployed PushConsumer(PushReader) contract as allowed address to our subscribtion.
Subscribe to a push datafeed for desired feed and with desired parameters and put deployed PushConsumer contract as address that can read the contract.

Step 5: 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 called "scripts" and create a new script consumePrice.ts
in it.
mkdir scripts && touch ./scripts/consumePrice.ts
import {
PushReader,
} from "../typechain-types";
import { ethers } from "hardhat";
// PushConsumer address we got from the deployment
const PushReaderAddress = "0xe23650424625B602c0f996F25b287203632A3f16";
async function main() {
const pushConsumer = await ethers.getContractAt(
"PushReader",
PushReaderAddress
) as PushReader
;
const feedKey = ethers.encodeBytes32String("BTC/USD");
let tx = await pushReader.getFeedPrice(feedKey);
await tx.wait();
console.log("sent PushReader.getData tx", tx.hash);
let price = await pushReader.latestPrice();
let timestamp = await pushReader.latestTimestamp();
console.log("price:", price );
console.log("timestamp:", timestamp );
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
Change the PushReaderAddress
to the address you got from deployment and execute the script to send the transaction to the blockchain.
yarn hardhat run ./scripts/consumePrice.ts --network eth_sepolia
You can use cast to examine the transaction call trace and logs. The emitted event shows the latest update that we read. Note that gas usage may vary depending on the result and the version of the library in use.
cast run 0x8c9c7abdfc243500d8f20dafd69cb6b2f9b5c41a1bb5f8a374bb04a31e43dc94 -r https://eth-sepolia.public.blastapi.io
Last updated
Was this helpful?