Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/caplight-eod-market-price/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Caplight End of Day Market Price

Deployments:
- [Testnet](https://testnet.explorer.seda.xyz/oracle-programs/58e9a7d1c7597e9580f4f44f4e64e3946bff70868f2a6e164da6cfe340a586ee)
- [Testnet](https://testnet.explorer.seda.xyz/oracle-programs/97ab05dee5f27f8ff1b7dafd4506a5de8924b17535989139c9e8b865ff062e0e)
<!-- - [Mainnet]() -->

## Overview
Expand All @@ -11,7 +11,7 @@ This Oracle Program fetches the latest market data returned from the [Caplight A
You can test this Oracle Program on testnet with the following command:

```sh
cargo post-dr caplight-eod-market-price 54782-29 -i 58e9a7d1c7597e9580f4f44f4e64e3946bff70868f2a6e164da6cfe340a586ee -r 3
cargo post-dr caplight-eod-market-price 54782-29 -i 97ab05dee5f27f8ff1b7dafd4506a5de8924b17535989139c9e8b865ff062e0e
```

## Execution Phase:
Expand Down
10 changes: 7 additions & 3 deletions examples/caplight-eod-market-price/src/execution_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use anyhow::Result;
use seda_sdk_rs::{Process, elog, log, proxy_http_fetch};

#[cfg(feature = "testnet")]
const API_URL: &str = "http://104.155.34.32:5384/proxy/";
const API_URL: &str = "http://18.130.226.194:5384/proxy/";
#[cfg(feature = "testnet")]
const PROXY_PUBLIC_KEY: &str = "0306346975352e34719df41928048482b285d24cd27f8e5fc2df7e4095f9cc14cf";
const PROXY_PUBLIC_KEY: &str = "03c4dcfca973ece4da6c53d0343132a9e8801e4062017d6ae6afd09d6576a6c942";

#[cfg(feature = "mainnet")]
const API_URL: &str = todo!("http://:5384/proxy/");
Expand Down Expand Up @@ -63,6 +63,10 @@ pub fn execution_phase() -> Result<()> {
timeout_ms: Some(20_000),
}),
);
log!(
"HTTP Response: {}",
String::from_utf8(response.bytes.clone())?
);

// Check if the HTTP request was successfully fulfilled or not.
if !response.is_ok() {
Expand All @@ -82,7 +86,7 @@ pub fn execution_phase() -> Result<()> {
.get("price")
.and_then(|price| price.as_f64())
.ok_or_else(|| anyhow::anyhow!("Price not found in response"))?;
let price_lossless = (price * 100.0) as u128;
let price_lossless = (price * 1_000_000.0) as u128;
log!("Fetched price: {price_lossless:?}");

// Report the successful result back to the SEDA network.
Expand Down
16 changes: 8 additions & 8 deletions examples/caplight-eod-market-price/src/tally_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ pub fn tally_phase() -> Result<()> {
}

// If there are valid prices revealed, calculate the median price from price reports.
let final_prices = median(&revealed_prices);
log!("Final median prices: {final_prices:?}");
let final_price = median(&revealed_prices);
log!("Final median prices: {final_price:?}");

let price_float = final_price as f64 / 1_000_000.0;
log!("Final median price float: {price_float:?}");

// Encode the final median price as a EVM `uint256`.
let result = ethabi::encode(&[final_prices]);
// Report the successful result in the tally phase.
Process::success(&result);
Process::success(price_float.to_string().as_bytes());

Ok(())
}

/// Finds the median of a list of prices per price report.
fn median(data: &[u128]) -> Token {
fn median(data: &[u128]) -> u128 {
let m = data.len();

// If there are no valid prices, report an error.
Expand All @@ -60,6 +61,5 @@ fn median(data: &[u128]) -> Token {
sorted_data[m / 2]
};

// convert to Token::Uint for encoding
Token::Uint(U256::from(median_price))
median_price
}
Loading