Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Query Berachain Data with Pyth Oracles

Pyth Network is a decentralized oracle network that provides high-quality, real-time data for Berachain. This repo provides an example of how to query Pyth Network data on Berachain's Bepolia Testnet.

Pyth differs from the existing oracle paradigm by using on-demand price updates, where users pull on-chain prices only when needed - learn more here.

Requirements

Quick Setup

Step 1 - Setup Project & Install Dependencies

# FROM: ./pyth-oracle

npm install;

Step 2 - Set up for Deployment

Run the following to import your wallet's private key into Foundry's keystore (with the deployer alias):

cast wallet import deployer --interactive;

Confirm that your wallet was imported by running:

cast wallet list;

# [Example output]
# deployer (Local)

Load the Berachain RPC into your terminal session:

export BERACHAIN_BEPOLIA_RPC="https://bepolia.rpc.berachain.com"

Step 3 - Deploying to Berachain

Compile the smart contract:

# FROM: ./pyth-oracle

forge build;

Deploy the smart contract (you will be prompted for the keystore password you set earlier):

# FROM: ./pyth-oracle

forge create ./src/ConsumerContract.sol:ConsumerContract --rpc-url $BERACHAIN_BEPOLIA_RPC --account deployer

# [Expected Similar Output]:
# Enter keystore password:
# Deployer: 0x529CA3A690E1bB4e9F04d132bd99D4398f626A44
# Deployed to: 0x9106b2041C896224Af2142ea9C7349aa283Df7C6
# Transaction hash: 0xc8efdd3132080491b42c469fb2219bc6f0432981a46cdd3f6ae73b9e834ff4e4

Step 4 - Interacting with your Contract

Fetch the payload for priceUpdateData from the Pyth API & write to file:

# FROM: ./pyth-oracle

curl -s "https://hermes.pyth.network/v2/updates/price/latest?&ids[]=0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace" | jq -r ".binary.data[0]" > price_update.txt

Call updatePrice with the payload:

# FROM: ./pyth-oracle

cast send <YOUR_DEPLOYED_CONTRACT> --rpc-url $BERACHAIN_BEPOLIA_RPC "updatePrice(bytes[])"  "[0x`cat price_update.txt`]" --account deployer --value 0.0001ether

# [Expected Similar Output]:
# blockHash               0xf00e38ea8197d088973dc51502b9fb62d089ac31b6fe01002e83a969e9c05f93
# blockNumber             1037572
# contractAddress
# cumulativeGasUsed       208351
# effectiveGasPrice       3000000017
# from                    0x529CA3A690E1bB4e9F04d132bd99D4398f626A44
# ...

Next, query the price with getPrice():

cast call <YOUR_DEPLOYED_CONTRACT> --rpc-url $BERACHAIN_BEPOLIA_RPC "getPrice()"

# [Expected Similar Output]:
# 0x0000000000000000000000000000000000000000000000000000005c7eedf820000000000000000000000000000000000000000000000000000000000eb29f7bfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000000000000000000000000000000000000000000000000000065f256b1

If you encounter errors, see Troubleshooting.

Optionally, decode the hexadecimal output with abi-decode:

cast abi-decode "getPrice()(int64,uint64,int32,uint)"  <YOUR_GETPRICE_OUTPUT>

Troubleshooting

  • If you don't act quickly enough, you may encounter the error 0x19abf40e representing a StalePrice error. This means that the price_update.txt was too old to be used by the contract. Re-run the sequence of commands in Step 4 to retry.
  • The error code 0x025dbdd4 represents an InsufficientFee error. Try raising the value of $BERA in the updatePrice call e.g. 0.0005ether.