API Reference

updatePriceFeedsIfNecessary

Update the on-chain price feeds using the provided updateData only if the on-chain prices are older than the valid time period.

Description

This method updates the on-chain price feeds using the provided updateData if the on-chain data is not sufficiently fresh.

The caller provides two matched arrays, priceIds and publishTimes. This function applies the update if there exists an index i such that priceIds[i]'s last publishTime is before than publishTimes[i]. Callers should typically pass publishTimes[i] to be equal to the publishTime of the corresponding price id in updateData.

This method is a variant of updatePriceFeeds that reduces gas usage when multiple callers are sending the same price updates.

This function requires the caller to pay a fee to perform the update. The required fee for a given set of updates can be computed by passing them to getUpdateFee.

This method returns the transaction hash of the update transaction.

Error Response

The above method can return the following error response:

  • NoFreshUpdate: The provided update is not fresh enough to apply. It means the provided publishTime is not equal to corresponding corresponding price id in updateData.
  • InvalidUpdateData: The provided update data is invalid or incorrectly signed.
  • InsufficientFee: The fee provided is less than the required fee. Try calling getUpdateFee to get the required fee.

Arguments

  • The price update data for the contract to verify. Fetch this data from Hermes API.

  • The price ids to update.

  • The timestamp for each price id that determines whether to apply the update.

  • The update fee in wei. This fee is sent as the value of the transaction.

Examples

import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
//
address contractAddress =
IPyth pyth = IPyth(contractAddress);
bytes[] memory updateData = new bytes[](1);
updateData[0] = /* <updateData> */;
bytes32[] memory priceIds = new bytes32[](1);
priceIds[0] = /* <priceId> */;
uint64[] memory publishTimes = new uint64[](1);
publishTimes[0] = /* <publishTime> */;
uint fee = /* <fee> */;
pyth.updatePriceFeedsIfNecessary{value: fee}(updateData, priceIds, publishTimes);