|
| 1 | +import { SubstrateExtrinsic } from "@subql/types" |
| 2 | +import { extractUserOpFromExtrinsic } from "@/utils/extrinsic.helpers" |
| 3 | + |
| 4 | +// The user operation is read out of the place_bid extrinsic rather than intents_getBidsForOrder, |
| 5 | +// because that RPC pins the chain head internally and the pallet's offchain copy expires — so on any |
| 6 | +// backfill the extrinsic is the only source that still has the bid. place_bid can arrive wrapped in |
| 7 | +// batch/proxy/sudo, so the walk has to find it at depth, and has to match on commitment: one batch |
| 8 | +// may carry bids for several orders, and picking the wrong one would attribute another order's quote |
| 9 | +// to this event. |
| 10 | + |
| 11 | +const COMMITMENT = `0x${"11".repeat(32)}` |
| 12 | +const OTHER_COMMITMENT = `0x${"22".repeat(32)}` |
| 13 | +const USER_OP = "0xdeadbeef" |
| 14 | +const OTHER_USER_OP = "0xfeedface" |
| 15 | + |
| 16 | +const hex = (value: string) => ({ toHex: () => value }) |
| 17 | + |
| 18 | +const placeBid = (commitment: string, userOp: string) => ({ |
| 19 | + section: "intentsCoprocessor", |
| 20 | + method: "placeBid", |
| 21 | + args: [hex(commitment), hex(userOp)], |
| 22 | +}) |
| 23 | + |
| 24 | +const batch = (calls: unknown[]) => ({ |
| 25 | + section: "utility", |
| 26 | + method: "batchAll", |
| 27 | + args: [calls], |
| 28 | +}) |
| 29 | + |
| 30 | +const proxy = (call: unknown) => ({ |
| 31 | + section: "proxy", |
| 32 | + method: "proxy", |
| 33 | + args: [hex("0xaaaa"), hex("0x00"), call], |
| 34 | +}) |
| 35 | + |
| 36 | +const asExtrinsic = (method: unknown) => ({ extrinsic: { method } }) as unknown as SubstrateExtrinsic |
| 37 | + |
| 38 | + |
| 39 | +describe("extractUserOpFromExtrinsic", () => { |
| 40 | + it("reads the user op from a direct place_bid call", () => { |
| 41 | + expect(extractUserOpFromExtrinsic(asExtrinsic(placeBid(COMMITMENT, USER_OP)), COMMITMENT)).toBe(USER_OP) |
| 42 | + }) |
| 43 | + |
| 44 | + it("finds a place_bid nested in a batch", () => { |
| 45 | + const call = batch([placeBid(OTHER_COMMITMENT, OTHER_USER_OP), placeBid(COMMITMENT, USER_OP)]) |
| 46 | + expect(extractUserOpFromExtrinsic(asExtrinsic(call), COMMITMENT)).toBe(USER_OP) |
| 47 | + }) |
| 48 | + |
| 49 | + it("finds a place_bid nested in a proxied batch", () => { |
| 50 | + const call = proxy(batch([placeBid(COMMITMENT, USER_OP)])) |
| 51 | + expect(extractUserOpFromExtrinsic(asExtrinsic(call), COMMITMENT)).toBe(USER_OP) |
| 52 | + }) |
| 53 | + |
| 54 | + it("returns the user op of the matching commitment, not the first bid in the batch", () => { |
| 55 | + const call = batch([placeBid(OTHER_COMMITMENT, OTHER_USER_OP), placeBid(COMMITMENT, USER_OP)]) |
| 56 | + expect(extractUserOpFromExtrinsic(asExtrinsic(call), OTHER_COMMITMENT)).toBe(OTHER_USER_OP) |
| 57 | + }) |
| 58 | + |
| 59 | + it("returns undefined when no place_bid matches the commitment", () => { |
| 60 | + const call = batch([placeBid(OTHER_COMMITMENT, OTHER_USER_OP)]) |
| 61 | + expect(extractUserOpFromExtrinsic(asExtrinsic(call), COMMITMENT)).toBeUndefined() |
| 62 | + }) |
| 63 | + |
| 64 | + it("returns undefined for an unrelated extrinsic", () => { |
| 65 | + const call = { section: "balances", method: "transfer", args: [hex("0xaaaa"), hex("0x01")] } |
| 66 | + expect(extractUserOpFromExtrinsic(asExtrinsic(call), COMMITMENT)).toBeUndefined() |
| 67 | + }) |
| 68 | + |
| 69 | + it("returns undefined when the event has no extrinsic", () => { |
| 70 | + expect(extractUserOpFromExtrinsic(undefined, COMMITMENT)).toBeUndefined() |
| 71 | + }) |
| 72 | + |
| 73 | + it("does not loop on a self-referential call tree", () => { |
| 74 | + const cyclic: any = { section: "utility", method: "batchAll", args: [] } |
| 75 | + cyclic.args = [[cyclic]] |
| 76 | + expect(extractUserOpFromExtrinsic(asExtrinsic(cyclic), COMMITMENT)).toBeUndefined() |
| 77 | + }) |
| 78 | +}) |
0 commit comments