How to Use the Push Oracle
This section explains the main methods and patterns for integrating the Chaos push oracle into your EVM smart contracts, including fetching prices, accessing historical data, and subscribing to updates.1. Set up your environment.
- Import the Chaos push oracle contract Application Binary Interface (ABI).
- Identify the deployed contract address for your desired network, such as the Ethereum mainnet and testnets.
2. Fetch the latest price
Use thelatestAnswer() function to obtain the most recent price. The result is an integer representing the price, formatted with the specified number of decimals.
3. Access historical data
Retrieve historical price data for specific rounds using thegetRoundData function. Each round provides a comprehensive dataset, including the price, timestamp, and block number.
4. Monitor updates in real-time
Subscribe to theNewPriceUpdate event to react to new price reports. This is particularly useful for applications requiring immediate responses to market changes.
Example:
Push Contract Integration
Here’s a basic example demonstrating how to integrate the Chaos push oracle into your smart contract:The following code is for illustrative purposes only, has not been audited, and should not be used in production without thorough testing.
Important Considerations
-
Decimals handling
The
latestAnswer()andgetRoundData()functions return prices as integers. Use thedecimalsproperty of the oracle to format the values for display or further calculations. -
Timestamp validation
If the precise timing is crucial, then use the
getRoundData()function to validate the timestamp of the data retrieved. -
Real-time updates
Utilize the
NewPriceUpdateevent to build reactive systems that trigger actions based on price changes.
How to Use the Pull Oracle
To integrate with the pull oracle, you will need to fetch signed price data from the Chaos API and then use it in your smart contract.Step 1: Obtain API Keys and Signer Address
Before you can fetch data or verify signatures, you must know the oracle’s trusted signer address. Contact the Chaos team to obtain your API keys and the signer address required for verification.Step 2: Fetch Signed Price Data
Using your API key, make a GET request to the/prices/evm/crypto endpoint to retrieve the latest EVM-signed price data. You can find more details about the endpoint in the API reference.
The API response will be a JSON object containing an array of prices. Here is an example of a single price object in the response:
Step 3: Verify and Consume the Price On-Chain
The data from the API response must be passed to your smart contract for verification. The contract should hash the price data and verify the signature against the trusted signer address you obtained in Step 1. You can use the following smart contract to verify the signature on-chain. This contract provides functions to hash the price data and verify the ECDSA signature.Solidity
SignatureVerifier, you need to:
- Deploy the contract: Deploy the
SignatureVerifiercontract to your target blockchain. - Hash the data: In your consumer contract, call the
hashPriceFeedDatafunction with the parameters from the API response (feedId,price,expo,roundId,timestamp,bid,ask). - Verify the signature: Call the
verifySignaturefunction with the hash from the previous step, thesignaturefrom the API response, and the oracle’s trusted signer address.