๐Getting Started: Price Feed
This section explains the basics of building a SEDA Oracle Program and how to use it from any supported network using the PriceFeed example.
Quickstart
The easiest way to start building a SEDA Oracle Program is by using the seda-request-starter-kit. This starter kit offers a streamlined setup process and comes with the essential tools and structure to help you create and deploy your first Oracle Program on the SEDA network.
To get started, clone the repository and install the required dependencies:
Building the Oracle Program
The PriceFeed
Oracle Program is divided into 2 phases, as can be seen inassembly/index.ts
:
Execution: it fetches the price of an asset pair (e.g., BTC-USDT) from an external source and processes this data.
Tally: it calculates the median among all executions reports.
This will illustrate the basic mechanics of how data is processed and aggregated within the SEDA network.
Step 1: Entrypoint
To begin, the Oracle Program logic lives in theassembly
folder, where index.ts
serves as the entry point for your Oracle Program.
This setup initializes the PriceFeed
Oracle Program, invoking the execution and tally phases that make up the data request.
Step 2: Execution Phase
The execution phase is where the Oracle Program fetches data and performs computation on the fetched data. In this case, it fetches the price for a specified asset pair from a given data source.
Read Inputs
The user triggering the Data Request can specify a set of inputs that can be used in the data request to add more dynamic execution and templating. In the following example we start by reading the input parameters using Process.getInputs()
:
In this example, we assume the input is a UTF-8 encoded string. We log the asset pair being fetched to include this information in the data request execution. The input string is then split to extract the symbols needed for this specific API.
Fetch and Compute Data
Next, let's add the logic to fetch data from an external API and perform some calculations:
Here's a breakdown of the logic:
Fetch Data: Perform an HTTP request to fetch the price for the asset pair using the
httpFetch
function. The URL is constructed dynamically based on the asset symbols.Parse Response: The API response is parsed into a
PriceFeedResponse
object. This allows us to easily extract the price data from the response.Perform Calculations: The fetched price, which is a floating-point number, is multiplied by
1e6
to avoid precision loss and then converted into au128
integer.
For more information about fetching external data, please refer to the detailed guide Fetching Open Data.
Report Results & Error Handling
Finally, we need to report the result and indicate that the Process
has ended successfully using the Process.success()
call. Since Process
only works with Bytes
, we convert our result to bytes using Bytes.fromNumber
.
If there are any errors during the process, they can be handled using the Process.error()
call. Additionally, Console
can be used to log errors for debugging purposes.
Here's a summary of what's happening:
Error Handling: We check if the HTTP request was successful. If not, we log the error and use
Process.error
to report the issue to the SEDA network.Parse and Validate: We parse the API response into a
PriceFeedResponse
object and ensure that the price data can be correctly converted to a float.Report the Result: If everything is successful, we multiply the float by
1e6
to convert it into an integer and useProcess.success
to report the result back to the SEDA network.
This completes the execution phase, ensuring that errors are handled gracefully and results are accurately processed.
Step 3: Tally Phase
The tally phase is where the results collected during the execution phase are aggregated to reach a consensus. This phase processes the data gathered by multiple Overlay Nodes to produce a single result that can be returned to the blockchain.
In this example, we calculate the median price from the results obtained during the execution phase to provide a final output. Here's how we can implement this logic:
Explanation:
Retrieve Results: The
tallyPhase
gathers the execution results usingTally.getReveals()
. These results are parsed into unsigned integers (u128
) and stored for further analysis.Consensus Check: Before proceeding, it verifies that there are enough price data points from the execution phase to move forward with aggregation.
Aggregate Data: The parsed prices are used to calculate the median, a robust statistical measure that minimizes the impact of outliers and provides a reliable data representation.
Result Reporting: If the median calculation is successful, the final result is reported using
Process.success()
. If no valid results are available or consensus is not achieved, an error is logged usingProcess.error()
.
Now that the Oracle Program is implemented, we can generate the WebAssembly artifacts. This process compiles your code and places the resulting .wasm
files in the build
directory. After building, you'll have the necessary artifacts to deploy your Oracle Program on the SEDA network.
To build the Oracle Program, run the following command:
This process completes the PriceFeed Oracle Program. In the next chapters we will be showcasing how to deploy and trigger Data Requests.
Last updated