Skip to content

Commit 497717c

Browse files
committed
update
1 parent d2b8bcd commit 497717c

7 files changed

Lines changed: 125 additions & 124 deletions

File tree

src/oracle/fetch.test.ts

Lines changed: 2 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
import { Order } from "../order";
12
import { OracleErrorType } from "./error";
2-
import { describe, it, expect, vi, beforeEach, afterEach, assert } from "vitest";
33
import { OracleConstants, OracleHealthMap, OracleOrderRequest } from "./types";
4+
import { describe, it, expect, vi, beforeEach, afterEach, assert } from "vitest";
45
import {
56
isInCooloff,
67
extractOracleUrl,
78
fetchSignedContext,
89
recordOracleSuccess,
910
recordOracleFailure,
10-
isValidSignedContextV2,
1111
} from "./fetch";
12-
import { Order } from "../order";
1312

1413
// Mock fetch globally
1514
const mockFetch = vi.fn();
@@ -725,106 +724,3 @@ describe("recordOracleFailure", () => {
725724
expect(state?.cooloffUntil).toBe(existingCooloff);
726725
});
727726
});
728-
729-
describe("isValidSignedContextV2", () => {
730-
it("returns true for valid SignedContextV2 object", () => {
731-
const valid = {
732-
signer: "0x1234567890abcdef",
733-
context: ["0x01", "0x02"],
734-
signature: "0xabcdef",
735-
};
736-
expect(isValidSignedContextV2(valid)).toBe(true);
737-
});
738-
739-
it("returns true for valid object with empty context array", () => {
740-
const valid = {
741-
signer: "0x1234",
742-
context: [],
743-
signature: "0xsig",
744-
};
745-
expect(isValidSignedContextV2(valid)).toBe(true);
746-
});
747-
748-
it("returns false for null", () => {
749-
expect(isValidSignedContextV2(null)).toBe(false);
750-
});
751-
752-
it("returns false for undefined", () => {
753-
expect(isValidSignedContextV2(undefined)).toBe(false);
754-
});
755-
756-
it("returns false for non-object types", () => {
757-
expect(isValidSignedContextV2("string")).toBe(false);
758-
expect(isValidSignedContextV2(123)).toBe(false);
759-
expect(isValidSignedContextV2(true)).toBe(false);
760-
});
761-
762-
it("returns false when signer is missing", () => {
763-
const invalid = {
764-
context: ["0x01"],
765-
signature: "0xsig",
766-
};
767-
expect(isValidSignedContextV2(invalid)).toBe(false);
768-
});
769-
770-
it("returns false when signer is not a string", () => {
771-
const invalid = {
772-
signer: 12345,
773-
context: ["0x01"],
774-
signature: "0xsig",
775-
};
776-
expect(isValidSignedContextV2(invalid)).toBe(false);
777-
});
778-
779-
it("returns false when context is missing", () => {
780-
const invalid = {
781-
signer: "0x1234",
782-
signature: "0xsig",
783-
};
784-
expect(isValidSignedContextV2(invalid)).toBe(false);
785-
});
786-
787-
it("returns false when context is not an array", () => {
788-
const invalid = {
789-
signer: "0x1234",
790-
context: "not-an-array",
791-
signature: "0xsig",
792-
};
793-
expect(isValidSignedContextV2(invalid)).toBe(false);
794-
});
795-
796-
it("returns false when signature is missing", () => {
797-
const invalid = {
798-
signer: "0x1234",
799-
context: ["0x01"],
800-
};
801-
expect(isValidSignedContextV2(invalid)).toBe(false);
802-
});
803-
804-
it("returns false when signature is not a string", () => {
805-
const invalid = {
806-
signer: "0x1234",
807-
context: ["0x01"],
808-
signature: 12345,
809-
};
810-
expect(isValidSignedContextV2(invalid)).toBe(false);
811-
});
812-
813-
it("returns true when extra properties are present", () => {
814-
const valid = {
815-
signer: "0x1234",
816-
context: ["0x01"],
817-
signature: "0xsig",
818-
extraField: "extra",
819-
};
820-
expect(isValidSignedContextV2(valid)).toBe(true);
821-
});
822-
823-
it("returns false for empty object", () => {
824-
expect(isValidSignedContextV2({})).toBe(false);
825-
});
826-
827-
it("returns false for array", () => {
828-
expect(isValidSignedContextV2([])).toBe(false);
829-
});
830-
});

src/oracle/fetch.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
* Fetch signed context from an oracle endpoint (single request format).
1414
*
1515
* POSTs abi.encode(OrderV4, uint256, uint256, address) and expects
16-
* a JSON SignedContextV1 object back.
16+
* a JSON SignedContextV2 object back.
1717
*
1818
* Single attempt with a hard timeout — no retries, no in-loop delays.
1919
* Uses the provided health map for cooloff tracking.
@@ -86,7 +86,7 @@ export async function fetchSignedContext(
8686
}
8787

8888
// Validate shape of response
89-
if (isValidSignedContextV2(json)) {
89+
if (SignedContextV2.isValid(json)) {
9090
recordOracleSuccess(healthMap, url);
9191
return Result.ok(json);
9292
}
@@ -170,14 +170,3 @@ export function recordOracleFailure(healthMap: OracleHealthMap, url: string) {
170170
}
171171
healthMap.set(url, state);
172172
}
173-
174-
/** Validates if the given value is of SignedContextV2 type */
175-
export function isValidSignedContextV2(value: any): value is SignedContextV2 {
176-
return !(
177-
typeof value !== "object" ||
178-
value === null ||
179-
typeof (value as any).signer !== "string" ||
180-
!Array.isArray((value as any).context) ||
181-
typeof (value as any).signature !== "string"
182-
);
183-
}

src/oracle/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe("fetchOracleContext", () => {
5353
expect(fetchSignedContext as Mock).not.toHaveBeenCalled();
5454
});
5555

56-
it("returns currectly call fetchSignedContext when Order V4 when it returns error", async () => {
56+
it("returns correctly call fetchSignedContext when Order V4 when it returns error", async () => {
5757
const error = new OracleError("some error", OracleErrorType.FetchError);
5858
(fetchSignedContext as Mock).mockResolvedValueOnce(Result.err(error));
5959
const result = await fetchOracleContext.call(mockState, mockOrderDetails);
@@ -73,7 +73,7 @@ describe("fetchOracleContext", () => {
7373
);
7474
});
7575

76-
it("returns currectly call fetchSignedContext when Order V4 when it returns ok", async () => {
76+
it("returns correctly call fetchSignedContext when Order V4 when it returns ok", async () => {
7777
const validSignedContext = {
7878
signer: "0x000000000000000000000000abcdef1234567890",
7979
context: [

src/oracle/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export namespace OracleConstants {
1616
export const KnownUrls = ["https://st0x-oracle-server.fly.dev/context"] as const;
1717

1818
export function isKnown(url: string): boolean {
19-
return KnownUrls.some(url as any);
19+
return KnownUrls.includes(url as any);
2020
}
2121
}
2222

src/order/types/v4.test.ts

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from "assert";
22
import { Order, OrderbookVersions } from "./index";
3-
import { V4, PairV4 } from "./v4";
3+
import { V4, PairV4, SignedContextV2 } from "./v4";
44
import { SgOrder } from "../../subgraph";
55
import { decodeAbiParameters } from "viem";
66
import { normalizeFloat, Result } from "../../common";
@@ -187,3 +187,106 @@ describe("PairV4.fromArgs", () => {
187187
expect(result.error.readableMsg).toContain("failed to normalize");
188188
});
189189
});
190+
191+
describe("SignedContextV2.isValid", () => {
192+
it("returns true for valid SignedContextV2 object", () => {
193+
const valid = {
194+
signer: "0x1234567890abcdef",
195+
context: ["0x01", "0x02"],
196+
signature: "0xabcdef",
197+
};
198+
expect(SignedContextV2.isValid(valid)).toBe(true);
199+
});
200+
201+
it("returns true for valid object with empty context array", () => {
202+
const valid = {
203+
signer: "0x1234",
204+
context: [],
205+
signature: "0xsig",
206+
};
207+
expect(SignedContextV2.isValid(valid)).toBe(true);
208+
});
209+
210+
it("returns false for null", () => {
211+
expect(SignedContextV2.isValid(null)).toBe(false);
212+
});
213+
214+
it("returns false for undefined", () => {
215+
expect(SignedContextV2.isValid(undefined)).toBe(false);
216+
});
217+
218+
it("returns false for non-object types", () => {
219+
expect(SignedContextV2.isValid("string")).toBe(false);
220+
expect(SignedContextV2.isValid(123)).toBe(false);
221+
expect(SignedContextV2.isValid(true)).toBe(false);
222+
});
223+
224+
it("returns false when signer is missing", () => {
225+
const invalid = {
226+
context: ["0x01"],
227+
signature: "0xsig",
228+
};
229+
expect(SignedContextV2.isValid(invalid)).toBe(false);
230+
});
231+
232+
it("returns false when signer is not a string", () => {
233+
const invalid = {
234+
signer: 12345,
235+
context: ["0x01"],
236+
signature: "0xsig",
237+
};
238+
expect(SignedContextV2.isValid(invalid)).toBe(false);
239+
});
240+
241+
it("returns false when context is missing", () => {
242+
const invalid = {
243+
signer: "0x1234",
244+
signature: "0xsig",
245+
};
246+
expect(SignedContextV2.isValid(invalid)).toBe(false);
247+
});
248+
249+
it("returns false when context is not an array", () => {
250+
const invalid = {
251+
signer: "0x1234",
252+
context: "not-an-array",
253+
signature: "0xsig",
254+
};
255+
expect(SignedContextV2.isValid(invalid)).toBe(false);
256+
});
257+
258+
it("returns false when signature is missing", () => {
259+
const invalid = {
260+
signer: "0x1234",
261+
context: ["0x01"],
262+
};
263+
expect(SignedContextV2.isValid(invalid)).toBe(false);
264+
});
265+
266+
it("returns false when signature is not a string", () => {
267+
const invalid = {
268+
signer: "0x1234",
269+
context: ["0x01"],
270+
signature: 12345,
271+
};
272+
expect(SignedContextV2.isValid(invalid)).toBe(false);
273+
});
274+
275+
it("returns true when extra properties are present", () => {
276+
const valid = {
277+
signer: "0x1234",
278+
context: ["0x01"],
279+
signature: "0xsig",
280+
extraField: "extra",
281+
};
282+
expect(SignedContextV2.isValid(valid)).toBe(true);
283+
});
284+
285+
it("returns false for empty object", () => {
286+
expect(SignedContextV2.isValid({})).toBe(false);
287+
});
288+
289+
it("returns false for array", () => {
290+
expect(SignedContextV2.isValid([])).toBe(false);
291+
});
292+
});

src/order/types/v4.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ export type SignedContextV2 = {
6262
context: `0x${string}`[];
6363
signature: `0x${string}`;
6464
};
65+
export namespace SignedContextV2 {
66+
/** Validates if the given value is of SignedContextV2 type */
67+
export function isValid(value: any): value is SignedContextV2 {
68+
return !(
69+
typeof value !== "object" ||
70+
value === null ||
71+
typeof (value as any).signer !== "string" ||
72+
!Array.isArray((value as any).context) ||
73+
(value as any).context.some((v: any) => typeof v !== "string") ||
74+
typeof (value as any).signature !== "string"
75+
);
76+
}
77+
}
6578

6679
export type TakeOrderV4 = {
6780
order: V4;

0 commit comments

Comments
 (0)