Skip to content

Commit

Permalink
Merge pull request #1964 from OffchainLabs/add-pyth-oracles-1545
Browse files Browse the repository at this point in the history
docs: add-pyth-as-oracle
  • Loading branch information
anegg0 authored Jan 10, 2025
2 parents c3da7ed + 7473be5 commit 023fd4a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions arbitrum-docs/for-devs/oracles/oracles-content-map.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Learn how to run an Arbitrum node.
href="/for-devs/oracles/ora/"
target="_blank"
/>
<Card
title="Pyth"
description="Learn out to use Pyth"
href="/for-devs/oracles/pyth/"
target="_blank"
/>
<Card
title="Supra price feed"
description="Querying price feeds with Supra"
Expand Down
77 changes: 77 additions & 0 deletions arbitrum-docs/for-devs/oracles/pyth/pyth.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
id: 'pyth'
title: 'Pyth'
description: 'Learn how to use Pyth Oracle'
author: pete-vielhaber
sme: pete-vielhaber
sidebar_label: 'pyth'
content_type: how-to
---

The [Pyth network](https://pyth.network/) a first-party oracle network, securely and transparently delivering real-time market data to [multiple chains](https://docs.pyth.network/price-feeds/contract-addresses).

The network comprises some of the world’s [largest exchanges, market makers, and financial services providers](https://pyth.network/publishers). These publish proprietary data on-chain for aggregation and distribution to smart contract applications.

## Pyth price feeds

The Pyth network introduces an innovative low-latency [pull oracle design](https://docs.pyth.network/price-feeds/pull-updates), where users can pull price updates on-chain when needed, enabling everyone in the blockchain environment to access that data point. Pyth network updates the prices every 400ms, making Pyth the fastest on-chain oracle.

Here is a working example of a contract that fetches the latest price of ARB/USD on the Arbitrum network.

You have to pass Pyth's contract address for Arbitrum mainnet/testnet and the desired price feed ID to fetch the latest price.

Install the Pyth SDK Solidity package in your project:

```tsx
npm install @pythnetwork/pyth-sdk-solidity
```

And then, in a few lines of code you can fetch the latest price on the Arbitrum network.

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
contract MyFirstPythContract {
IPyth pyth;
// Pass the address of Pyth's contract for Arbitrum mainnet(0xff1a0f4744e8582DF1aE09D5611b887B6a12925C)
constructor(address _pyth) {
pyth = IPyth(_pyth);
}
function fetchPrice(
bytes[] calldata updateData,
bytes32 priceFeed
) public payable returns (int64) {
// Fetch the priceUpdate from hermes.
uint updateFee = pyth.getUpdateFee(updateData);
pyth.updatePriceFeeds{value: updateFee}(updateData);
// Fetch the latest price
PythStructs.Price memory price = pyth.getPrice(priceFeed);
return price.price;
}
}
```

Here you can fetch the `updateData` from Pyth's [Hermes](https://hermes.pyth.network/docs/) feed, which listens to Pythnet and Wormhole for price updates; or you can use the [pyth-evm-js](https://github.com/pyth-network/pyth-crosschain/blob/main/target_chains/ethereum/sdk/js/src/EvmPriceServiceConnection.ts#L15) SDK. Check [How to Fetch Price Updates](https://docs.pyth.network/price-feeds/fetch-price-updates) to pull the latest data.

## Pyth Entropy

Pyth Entropy allows developers to quickly and easily generate secure **random numbers** on the blockchain.

Check [how to generate random numbers in EVM contracts](https://docs.pyth.network/entropy/generate-random-numbers/evm) for a detailed walkthrough.

### Supported networks for Arbitrum(Pyth Entropy):

- Arbitrum: [`0x7698E925FfC29655576D0b361D75Af579e20AdAc`](https://arbiscan.io/address/0x7698E925FfC29655576D0b361D75Af579e20AdAc)
- Arbitrum Sepolia: [`0x549Ebba8036Ab746611B4fFA1423eb0A4Df61440`](https://sepolia.arbiscan.io/address/0x549Ebba8036Ab746611B4fFA1423eb0A4Df61440)

## Additional resources

Check out the following links to get started with Pyth:

- [Pyth EVM Integration Guide](https://docs.pyth.network/price-feeds/use-real-time-data/evm)
- [Pyth Docs](https://docs.pyth.network/home)
- [Pyth API Reference](https://api-reference.pyth.network/price-feeds/evm/getPrice)
- [Pyth Examples](https://github.com/pyth-network/pyth-examples)
- [Pyth Price Feed Ids](https://pyth.network/developers/price-feed-ids)

0 comments on commit 023fd4a

Please sign in to comment.