Skip to main content
Blockchain Oracles serve as intermediaries that connect smart contracts with external (off-chain) data. Examples:
  • Price Feeds: Real-time pricing for cryptocurrencies, stocks, or commodities.
  • Event Information: Updates on real-world events like sports results or weather conditions.
  • API Access: Connections to external web services and systems.
Oracles, being external third-party services, require careful consideration of their reliability, security, and decentralization to avoid risks such as incorrect data, manipulation, or single points of failure.

Deployed Oracles on NEAR

Here is a directory of third-party oracle services deployed on the NEAR blockchain:
NameCreatorDescription
Price OracleNearDefiOpen source oracle for real-time asset pricing
Pyth Network OraclePyth NetworkHigh-frequency, low-latency oracle for price feeds

Price Oracle by NearDefi

Query Assets

near view priceoracle.near get_assets
Example response:
[
  [
    'wrap.near',
    {
      reports: [
        {
          oracle_id: 'thorinoracle.near',
          timestamp: '1669795900809627816',
          price: { multiplier: '17030', decimals: 28 }
        },
        ...
      ]
    }
  ]
]

Get Assets Price

near view priceoracle.near get_price_data
Example response:
{
  timestamp: '1706631861981947371',
  recency_duration_sec: 90,
  prices: [
    {
      asset_id: 'wrap.near',
      price: { multiplier: '30702', decimals: 28 }
    },
    {
      asset_id: 'aurora',
      price: { multiplier: '235662', decimals: 20 }
    },
    {
      asset_id: 'meta-pool.near',
      price: { multiplier: '38770', decimals: 28 }
    },
    ...
  ]
}
For USD values, divide the multiplier by 10^4.

Pyth Network Oracle

Getting Started

To use Pyth oracle, you need:
  • Price ID(s)
  • Hermes API Endpoint
  • Smart contract address
NetworkPrice Feed IDsHermes API AddressContract Address
testnetNEAR testnet Price Feed IDshermes-beta.pyth.networkpyth-oracle.testnet
mainnetNEAR mainnet Price Feed IDshermes.pyth.networkpyth-oracle.near
When using Price Feed IDs, you must remove the 0x prefix.Example (testnet):
const PRICE_FEED_ID = 'c415de8d2eba7db216527dff4b60e8f3a5311c740dadb233e13e12547e226750';

Update Price Feeds

Pyth Oracle has two core methods:
  1. update_price_feeds - Updates the Pyth smart contract with the price feed you provide
    • args: data (hex-encoded price feed)
    • type: object
    • example: { "data": "504e41..." }
  2. get_price - Fetches the most recent price stored in the contract
    • args: price_identifier
    • type: object
    • example: { price_identifier: 'f9c0172ba10dfa8...' }

1) Fetch Off-Chain Price Feed

You can obtain an off-chain price feed using Pyth’s Hermes API. Example (Node.js):
async function getHermesPriceData(priceId) {
  const hermesEndpoint = 'https://hermes-beta.pyth.network';
  const url = `${hermesEndpoint}/v2/updates/price/latest?ids[]=${priceId}`;
  
  const response = await fetch(url);
  const data = await response.json();
  
  return data.binary.data[0];
}

const priceId = 'c415de8d2eba7db216527dff4b60e8f3a5311c740dadb233e13e12547e226750';
const priceData = await getHermesPriceData(priceId);

2) Update Pyth Oracle Contract

After fetching an off-chain price feed, call update_price_feeds on the Pyth Oracle:
const pythOracleContract = 'pyth-oracle.testnet';

await wallet.signAndSendTransaction({
  receiverId: pythOracleContract,
  actions: [{
    type: 'FunctionCall',
    params: {
      methodName: 'update_price_feeds',
      args: {
        data: priceData,
      },
      gas: '300000000000000',
      deposit: '1000000000000000000000',
    }
  }]
});
Although unused deposit will be refunded, you can calculate an estimate by calling the get_update_fee_estimate method on the Pyth contract.

Get Price

After updating the price feed, you can view it on-chain by calling get_price:
const pythOracleContract = 'pyth-oracle.testnet';
const priceIdentifier = 'c415de8d2eba7db216527dff4b60e8f3a5311c740dadb233e13e12547e226750';

const price = await wallet.viewMethod({
  contractId: pythOracleContract,
  method: 'get_price',
  args: {
    price_identifier: priceIdentifier,
  },
});

console.log(price);

Additional Resources

Build docs developers (and LLMs) love