-
Notifications
You must be signed in to change notification settings - Fork 6
feat: add signed context oracle support #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9fd313d
Add oracle support to rain.solver
8fabe2e
fix: address review feedback on oracle module
5513fce
fix: use viem instead of ethers for ABI encoding
e0cf6c5
feat: add retry with backoff and per-URL cooloff for oracle fetching
08a544f
refactor: remove retry delays, use fail-fast with cooloff only
4162cbb
refactor: move oracle health state to OracleManager class on OrderMan…
77d52ba
refactor: drop OracleManager class, use SharedState + standalone fns
82ae2c9
refactor: use Result type instead of throwing
1c39eea
refactor: use existing order types, drop redundant SignedContextV1 in…
b6be597
fix: make oracle signed context actually work
d983c49
update
rouzwelt d2b8bcd
Merge branch '2026-03-27-v6-calldata-fix' into feat/oracle-support
rouzwelt 497717c
update
rouzwelt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import { ethers } from "ethers"; | ||
| import { ABI } from "../common"; | ||
|
|
||
| /** | ||
| * Extract oracle URL from order meta bytes. | ||
| * | ||
| * TODO: Replace with SDK's RaindexOrder.extractOracleUrl() once the wasm | ||
| * package includes it. For now, returns null (stub). | ||
| * | ||
| * @param metaHex - Hex string of meta bytes (e.g. "0x1234...") | ||
| * @returns Oracle URL if found, null otherwise | ||
| */ | ||
| export function extractOracleUrl(metaHex: string): string | null { | ||
| // TODO: Implement CBOR decoding to find RaindexSignedContextOracleV1 | ||
| // magic number 0xff7a1507ba4419ca and extract URL. | ||
| // Pending SDK update — see rain.orderbook PR #2478. | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Signed context response from oracle endpoint. | ||
| * Maps directly to SignedContextV1 in the orderbook contract. | ||
| */ | ||
| export interface SignedContextV1 { | ||
| signer: string; | ||
| context: string[]; | ||
| signature: string; | ||
| } | ||
|
|
||
| /** | ||
| * Order details for an oracle request entry. | ||
| */ | ||
| export interface OracleOrderRequest { | ||
| order: { | ||
| owner: string; | ||
| evaluable: { interpreter: string; store: string; bytecode: string }; | ||
| validInputs: { token: string; vaultId: string }[]; | ||
| validOutputs: { token: string; vaultId: string }[]; | ||
| nonce: string; | ||
| }; | ||
| inputIOIndex: number; | ||
| outputIOIndex: number; | ||
| counterparty: string; | ||
| } | ||
|
|
||
| /** Oracle request timeout in ms */ | ||
| const ORACLE_TIMEOUT_MS = 5_000; | ||
|
|
||
| /** | ||
| * ABI type string for the batch oracle request body: | ||
| * abi.encode((OrderV4, uint256, uint256, address)[]) | ||
| */ | ||
| const OracleRequestTupleType = | ||
| `tuple(${ABI.Orderbook.V5.OrderV4} order, uint256 inputIOIndex, uint256 outputIOIndex, address counterparty)[]` as const; | ||
|
|
||
| /** | ||
| * Fetch signed contexts from an oracle endpoint (batch format). | ||
| * | ||
| * POSTs abi.encode((OrderV4, uint256, uint256, address)[]) and expects | ||
| * a JSON array of SignedContextV1 objects back, matching request length. | ||
| * | ||
| * @param url - Oracle endpoint URL | ||
| * @param orders - Array of order requests (usually 1 per IO pair) | ||
| * @returns Array of signed contexts in the same order as the request | ||
| */ | ||
| export async function fetchSignedContext( | ||
| url: string, | ||
| orders: OracleOrderRequest[], | ||
| ): Promise<SignedContextV1[]> { | ||
| const tuples = orders.map((req) => [ | ||
| req.order, | ||
| req.inputIOIndex, | ||
| req.outputIOIndex, | ||
| req.counterparty, | ||
| ]); | ||
|
|
||
| const body = ethers.utils.defaultAbiCoder.encode([OracleRequestTupleType], [tuples]); | ||
|
|
||
| const controller = new AbortController(); | ||
| const timeout = setTimeout(() => controller.abort(), ORACLE_TIMEOUT_MS); | ||
|
|
||
| let response: Response; | ||
| try { | ||
| response = await fetch(url, { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/octet-stream" }, | ||
| body: ethers.utils.arrayify(body), | ||
| signal: controller.signal, | ||
| }); | ||
| } finally { | ||
| clearTimeout(timeout); | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error(`Oracle request failed: ${response.status} ${response.statusText}`); | ||
| } | ||
|
|
||
| const json: unknown = await response.json(); | ||
|
|
||
| if (!Array.isArray(json)) { | ||
| throw new Error("Oracle response must be an array"); | ||
| } | ||
|
|
||
| if (json.length !== orders.length) { | ||
| throw new Error( | ||
| `Oracle response length (${json.length}) does not match request length (${orders.length})`, | ||
| ); | ||
| } | ||
|
|
||
| // Validate shape of each entry | ||
| const contexts: SignedContextV1[] = json.map((entry: unknown, i: number) => { | ||
| if ( | ||
| typeof entry !== "object" || | ||
| entry === null || | ||
| typeof (entry as any).signer !== "string" || | ||
| !Array.isArray((entry as any).context) || | ||
| typeof (entry as any).signature !== "string" | ||
| ) { | ||
| throw new Error(`Oracle response[${i}] is not a valid SignedContextV1`); | ||
| } | ||
| return entry as SignedContextV1; | ||
| }); | ||
|
rouzwelt marked this conversation as resolved.
Outdated
|
||
|
|
||
| return contexts; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.