Skip to main content
Use this guide when you need real-time price data or oracle feeds on Berachain. Pyth uses on-demand updates: your contract pulls the latest price when needed instead of subscribing to a stream.

Repository

Official documentation

Requirements

Stack

Solidity, Foundry (Forge, Cast), Node.js, Pyth Hermes API (price payloads).

Quick start

  1. Clone and install
    git clone https://github.com/berachain/guides.git && cd guides/apps/pyth-oracle
    npm install
    
  2. Import wallet (Foundry keystore): cast wallet import deployer --interactive. Set BERACHAIN_ARTIO_RPC (e.g. https://rpc.ankr.com/berachain_testnet).
  3. Deploy consumer contract
    forge build
    forge create ./src/ConsumerContract.sol:ConsumerContract --rpc-url $BERACHAIN_ARTIO_RPC --account deployer
    
  4. Fetch price and update on-chain
    curl -s "https://hermes.pyth.network/v2/updates/price/latest?&ids[]=0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace" | jq -r ".binary.data[0]" > price_update.txt
    cast send <CONTRACT> --rpc-url $BERACHAIN_ARTIO_RPC "updatePrice(bytes[])" "[0x\`cat price_update.txt\`]" --account deployer --value 0.0001ether
    
  5. Read price: cast call <CONTRACT> --rpc-url $BERACHAIN_ARTIO_RPC "getPrice()"
Troubleshooting: StalePrice — re-run the update; InsufficientFee — increase --value (e.g. 0.0005ether).

Key files

PurposePath
Consumer contractsrc/ConsumerContract.sol