@nktkas/hyperliquid
Version:
Hyperliquid API SDK for all major JS runtimes, written in TypeScript.
110 lines (109 loc) • 5.32 kB
JavaScript
import * as v from "valibot";
// ============================================================
// API Schemas
// ============================================================
import { Hex, UnsignedDecimal, UnsignedInteger } from "../../_schemas.js";
/**
* Manually split or merge outcome shares to convert between primary and dual balances.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#split-outcome
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#merge-outcome
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#merge-question
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#negate-outcome
*/ export const UserOutcomeRequest = /* @__PURE__ */ (()=>{
return v.object({
/** Outcome action. */ action: v.variant("type", [
v.object({
/** Type of action. */ type: v.literal("userOutcome"),
/** Split `X` quote tokens into `X` Yes and `X` No shares of an outcome. */ splitOutcome: v.object({
/** Outcome identifier. */ outcome: UnsignedInteger,
/** Amount of quote tokens to split. */ amount: UnsignedDecimal
})
}),
v.object({
/** Type of action. */ type: v.literal("userOutcome"),
/** Merge `X` Yes and `X` No shares of an outcome into `X` quote tokens. */ mergeOutcome: v.object({
/** Outcome identifier. */ outcome: UnsignedInteger,
/** Amount of shares to merge, or `null` for the maximum available. */ amount: v.nullable(UnsignedDecimal)
})
}),
v.object({
/** Type of action. */ type: v.literal("userOutcome"),
/** Merge `X` Yes shares from each outcome associated to the same question into `X` quote tokens. */ mergeQuestion: v.object({
/** Question identifier. */ question: UnsignedInteger,
/** Amount of shares to merge, or `null` for the maximum available. */ amount: v.nullable(UnsignedDecimal)
})
}),
v.object({
/** Type of action. */ type: v.literal("userOutcome"),
/**
* Convert `X` No shares from an outcome associated with a question into
* `X` Yes shares of every other outcome associated with the question.
*/ negateOutcome: v.object({
/** Question identifier. */ question: UnsignedInteger,
/** Outcome identifier. */ outcome: UnsignedInteger,
/** Amount of No shares to negate. */ amount: UnsignedDecimal
})
})
]),
/** Nonce (timestamp in ms) used to prevent replay attacks. */ nonce: UnsignedInteger,
/** ECDSA signature components. */ signature: v.object({
/** First 32-byte component. */ r: v.pipe(Hex, v.length(66)),
/** Second 32-byte component. */ s: v.pipe(Hex, v.length(66)),
/** Recovery identifier. */ v: v.picklist([
27,
28
])
}),
/** Expiration time of the action. */ expiresAfter: v.optional(UnsignedInteger)
});
})();
// ============================================================
// Execution Logic
// ============================================================
import { parse } from "../../../_base.js";
import { canonicalize } from "../../../signing/mod.js";
import { executeL1Action } from "./_base/mod.js";
/** Schema for action fields (excludes request-level system fields). */ const UserOutcomeActionSchema = /* @__PURE__ */ (()=>{
return v.variant("type", UserOutcomeRequest.entries.action.options);
})();
/**
* Manually split or merge outcome shares to convert between primary and dual balances.
*
* Signing: L1 Action.
*
* @param config General configuration for Exchange API requests.
* @param params Parameters specific to the API request.
* @param opts Request execution options.
* @return Successful response without specific data.
*
* @throws {ValidationError} When the request parameters fail validation (before sending).
* @throws {TransportError} When the transport layer throws an error.
* @throws {ApiRequestError} When the API returns an unsuccessful response.
*
* @example Split outcome
* ```ts
* import { HttpTransport } from "@nktkas/hyperliquid";
* import { userOutcome } from "@nktkas/hyperliquid/api/exchange";
* import { privateKeyToAccount } from "npm:viem/accounts";
*
* const wallet = privateKeyToAccount("0x..."); // viem or ethers
* const transport = new HttpTransport(); // or `WebSocketTransport`
*
* await userOutcome({ transport, wallet }, {
* splitOutcome: { outcome: 0, amount: "1" },
* });
* ```
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#split-outcome
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#merge-outcome
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#merge-question
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#negate-outcome
*/ export function userOutcome(config, params, opts) {
const action = canonicalize(UserOutcomeActionSchema, parse(UserOutcomeActionSchema, {
type: "userOutcome",
...params
}));
return executeL1Action(config, action, opts);
}
//# sourceMappingURL=userOutcome.js.map