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 PushMarketplacecontract 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.
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.
./contracts/PushReader.sol
// 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.
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
./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
Expected Result
sent PushReader.getData tx 0x8c9c7abdfc243500d8f20dafd69cb6b2f9b5c41a1bb5f8a374bb04a31e43dc94
price: 76660817492475224330633n
timestamp: 1744027000n
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