Skip to content
Closed
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
16 changes: 2 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions crates/common/src/add_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use rain_interpreter_eval::{
};
use rain_interpreter_parser::{Parser2, ParserError, ParserV2};
use rain_metadata::{
types::dotrain::gui_state_v1::DotrainGuiStateV1, ContentEncoding, ContentLanguage, ContentType,
Error as RainMetaError, KnownMagic, RainMetaDocumentV1Item,
types::dotrain::gui_state_v1::DotrainGuiStateV1,
types::raindex_signed_context_oracle::RaindexSignedContextOracleV1, ContentEncoding,
ContentLanguage, ContentType, Error as RainMetaError, KnownMagic, RainMetaDocumentV1Item,
};
use rain_metadata_bindings::MetaBoard::emitMetaCall;
use rain_orderbook_app_settings::deployment::DeploymentCfg;
Expand Down Expand Up @@ -130,6 +131,21 @@ impl AddOrderArgs {
});
}

// If the order has an oracle URL, add a RaindexSignedContextOracleV1 meta item
let additional_meta = {
let mut meta = additional_meta.unwrap_or_default();
if let Some(ref oracle_url) = deployment.order.oracle_url {
let oracle = RaindexSignedContextOracleV1::parse(oracle_url)
.map_err(AddOrderArgsError::RainMetaError)?;
meta.push(oracle.to_meta_item());
}
if meta.is_empty() {
None
} else {
Some(meta)
}
};

Ok(AddOrderArgs {
dotrain: dotrain.to_string(),
inputs,
Expand Down Expand Up @@ -686,6 +702,7 @@ price: 2e18;
network: network_arc.clone(),
deployer: None,
orderbook: None,
oracle_url: None,
};
let deployment = DeploymentCfg {
document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))),
Expand Down Expand Up @@ -809,6 +826,7 @@ _ _: 0 0;
network: network_arc.clone(),
deployer: None,
orderbook: None,
oracle_url: None,
};
let deployment = DeploymentCfg {
document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))),
Expand Down Expand Up @@ -975,6 +993,7 @@ _ _: 0 0;
network: network_arc.clone(),
deployer: None,
orderbook: None,
oracle_url: None,
};
let deployment = DeploymentCfg {
document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))),
Expand Down Expand Up @@ -1319,6 +1338,7 @@ _ _: 16 52;
network: network_arc.clone(),
deployer: None,
orderbook: None,
oracle_url: None,
};
DeploymentCfg {
document: default_document(),
Expand Down
1 change: 1 addition & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod erc20;
pub mod fuzz;
pub mod local_db;
pub mod meta;
pub mod oracle;
pub mod parsed_meta;
pub mod raindex_client;
pub mod rainlang;
Expand Down
3 changes: 3 additions & 0 deletions crates/common/src/oracle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Re-export oracle types and functions from the quote crate.
// This maintains backward compatibility for code in common that uses oracle functionality.
pub use rain_orderbook_quote::oracle::*;
24 changes: 24 additions & 0 deletions crates/common/src/raindex_client/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ impl RaindexOrder {
_ => None,
})
}

/// Extract oracle URL from raw meta bytes.
///
/// Takes raw meta bytes, CBOR decodes them as RainMetaDocumentV1Items,
/// and finds RaindexSignedContextOracleV1 by magic number.
/// Returns the oracle URL if found, None otherwise.
#[wasm_bindgen(js_name = extractOracleUrl)]
pub fn extract_oracle_url_wasm(meta_bytes: &[u8]) -> Option<String> {
RaindexOrder::extract_oracle_url(meta_bytes)
}
#[wasm_bindgen(getter)]
pub fn transaction(&self) -> Option<RaindexTransaction> {
self.transaction.clone()
Expand Down Expand Up @@ -400,6 +410,19 @@ impl RaindexOrder {
_ => None,
})
}

/// Extract oracle URL from raw meta bytes.
///
/// Takes raw meta bytes, CBOR decodes them as RainMetaDocumentV1Items,
/// and finds RaindexSignedContextOracleV1 by magic number.
/// Returns the oracle URL if found, None otherwise.
pub fn extract_oracle_url(meta_bytes: &[u8]) -> Option<String> {
use rain_metadata::{types::raindex_signed_context_oracle::RaindexSignedContextOracleV1, RainMetaDocumentV1Item};

let items = RainMetaDocumentV1Item::cbor_decode(meta_bytes).ok()?;
let oracle = RaindexSignedContextOracleV1::find_in_items(&items).ok()??;
Some(oracle.url().to_string())
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
pub fn transaction(&self) -> Option<RaindexTransaction> {
self.transaction.clone()
}
Expand Down Expand Up @@ -718,6 +741,7 @@ impl RaindexOrder {
&rpc_urls,
Some(block_number),
sell_token,
self.oracle_url(),
)
.await
}
Expand Down
20 changes: 20 additions & 0 deletions crates/common/src/raindex_client/take_orders/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn build_candidate_from_quote(
output_io_index,
max_output: data.max_output,
ratio: data.ratio,
signed_context: vec![],
}))
}

Expand Down Expand Up @@ -102,6 +103,7 @@ pub fn estimate_take_order(
))
}

#[allow(clippy::too_many_arguments)]
pub async fn execute_single_take(
candidate: TakeOrderCandidate,
mode: ParsedTakeOrdersMode,
Expand All @@ -110,7 +112,25 @@ pub async fn execute_single_take(
rpc_urls: &[Url],
block_number: Option<u64>,
sell_token: Address,
oracle_url: Option<String>,
) -> Result<TakeOrdersCalldataResult, RaindexError> {
// Fetch signed context from oracle if URL provided
let mut candidate = candidate;
if let Some(url) = oracle_url {
let body = crate::oracle::encode_oracle_body(
&candidate.order,
candidate.input_io_index,
candidate.output_io_index,
taker,
);
match crate::oracle::fetch_signed_context(&url, body).await {
Ok(ctx) => candidate.signed_context = vec![ctx],
Err(e) => {
tracing::warn!("Failed to fetch oracle data from {}: {}", url, e);
}
}
}
Comment on lines +117 to +132
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Recompute the quote-derived limits after fetching the taker-bound oracle context.

Line 124 sends the real taker to the oracle, but Lines 136-179 still use candidate.ratio and candidate.max_output from the pre-fetch quote. If the oracle changes price or capacity per counterparty, those branches now run on stale data and can incorrectly return NoLiquidity / InsufficientLiquidity or size the take against the wrong ratio.

Also applies to: 136-179

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/common/src/raindex_client/take_orders/single.rs` around lines 117 -
132, After successfully fetching and assigning candidate.signed_context (via
fetch_signed_context), recompute any quote-derived limits and ratios (e.g.,
candidate.ratio and candidate.max_output) from the updated signed context/quote
before running the subsequent liquidity checks and sizing logic; specifically,
recalculate whatever function or logic produces candidate.ratio and
candidate.max_output using the newly-set candidate.signed_context (and
candidate.order/taker as needed) so that the branches that follow (the logic
currently using candidate.ratio and candidate.max_output) operate on fresh
values rather than the pre-fetch quote.


let zero = Float::zero()?;

if candidate.ratio.gt(price_cap)? {
Expand Down
Loading
Loading