Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/api/exchange/_methods/twapOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ export const TwapOrderRequest = /* @__PURE__ */ (() => {
/** Enable random order timing. */
t: v.boolean(),
}),
/** Trigger and stop prices. */
details: v.optional(v.object({
/** Condition that activates the order. */
t: v.nullable(v.object({
/** Trigger price. */
p: UnsignedDecimal,
/** Activate when the mark price is above (`true`) or below (`false`) the trigger price. */
a: v.boolean(),
})),
/** Price at which the order is terminated. */
s: v.nullable(UnsignedDecimal),
})),
}),
/** Nonce (timestamp in ms) used to prevent replay attacks. */
nonce: UnsignedInteger,
Expand Down
10 changes: 9 additions & 1 deletion tests/api/exchange/twapOrder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type TwapOrderParameters, TwapOrderRequest } from "@nktkas/hyperliquid/api/exchange";
import { formatSize } from "@nktkas/hyperliquid/utils";
import { formatPrice, formatSize } from "@nktkas/hyperliquid/utils";
import * as v from "@valibot/valibot";
import { schemaCoverage } from "../_utils/schemaCoverage.ts";
import { typeToJsonSchema } from "../_utils/typeToJsonSchema.ts";
Expand All @@ -20,12 +20,20 @@ runTest({
const midPx = allMids["SOL"];

const sz = formatSize(110 / parseFloat(midPx), szDecimals);
const pxUp = formatPrice(parseFloat(midPx) * 1.5, szDecimals);
const pxDown = formatPrice(parseFloat(midPx) * 0.5, szDecimals);

const params: TwapOrderParameters[] = [
// b=true | r=false | t=false
{ twap: { a: id, b: true, s: sz, r: false, m: 5, t: false } },
// b=false | t=true
{ twap: { a: id, b: false, s: sz, r: false, m: 5, t: true } },
// details.t | a=true
{ twap: { a: id, b: true, s: sz, r: false, m: 5, t: false }, details: { t: { p: pxUp, a: true }, s: null } },
// details.t | a=false
{ twap: { a: id, b: false, s: sz, r: false, m: 5, t: false }, details: { t: { p: pxDown, a: false }, s: null } },
// details.s
{ twap: { a: id, b: true, s: sz, r: false, m: 5, t: false }, details: { t: null, s: pxUp } },
];

const data = await Promise.all(params.map((p) => exchClient.twapOrder(p)));
Expand Down