• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nktkas / hyperliquid / 19514753888

19 Nov 2025 08:02PM UTC coverage: 94.811% (+0.1%) from 94.685%
19514753888

push

github

nktkas
ci: remove environment for test job

364 of 583 branches covered (62.44%)

Branch coverage included in aggregate %.

12208 of 12677 relevant lines covered (96.3%)

964.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.44
/src/api/exchange/usdClassTransfer.ts
1
import * as v from "valibot";
357✔
2

3
// ============================================================
4
// API Schemas
5
// ============================================================
6

7
import { Hex, UnsignedDecimal, UnsignedInteger } from "../_base.ts";
357✔
8
import { ErrorResponse, Signature, SuccessResponse } from "./_base/mod.ts";
357✔
9

10
/**
11
 * Transfer funds between Spot account and Perp account.
12
 * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
13
 */
14
export const UsdClassTransferRequest = /* @__PURE__ */ (() => {
357✔
15
  return v.pipe(
605✔
16
    v.object({
605✔
17
      /** Action to perform. */
18
      action: v.pipe(
605✔
19
        v.object({
605✔
20
          /** Type of action. */
21
          type: v.pipe(
605✔
22
            v.literal("usdClassTransfer"),
605✔
23
            v.description("Type of action."),
605✔
24
          ),
25
          /** Chain ID used for signing. */
26
          signatureChainId: v.pipe(
605✔
27
            Hex,
605✔
28
            v.description("Chain ID used for signing."),
605✔
29
          ),
30
          /** HyperLiquid network. */
31
          hyperliquidChain: v.pipe(
605✔
32
            v.union([v.literal("Mainnet"), v.literal("Testnet")]),
2,420✔
33
            v.description("HyperLiquid network."),
605✔
34
          ),
35
          /** Amount to transfer (1 = $1). */
36
          amount: v.pipe(
605✔
37
            UnsignedDecimal,
605✔
38
            v.description("Amount to transfer (1 = $1)."),
605✔
39
          ),
40
          /** `true` for Spot to Perp, `false` for Perp to Spot. */
41
          toPerp: v.pipe(
605✔
42
            v.boolean(),
605✔
43
            v.description("`true` for Spot to Perp, `false` for Perp to Spot."),
605✔
44
          ),
45
          /** Unique request identifier (current timestamp in ms). */
46
          nonce: v.pipe(
605✔
47
            UnsignedInteger,
605✔
48
            v.description("Unique request identifier (current timestamp in ms)."),
605✔
49
          ),
50
        }),
605✔
51
        v.description("Action to perform."),
605✔
52
      ),
53
      /** Unique request identifier (current timestamp in ms). */
54
      nonce: v.pipe(
605✔
55
        UnsignedInteger,
605✔
56
        v.description("Unique request identifier (current timestamp in ms)."),
605✔
57
      ),
58
      /** Cryptographic signature. */
59
      signature: v.pipe(
605✔
60
        Signature,
605✔
61
        v.description("Cryptographic signature."),
605✔
62
      ),
63
    }),
605✔
64
    v.description("Transfer funds between Spot account and Perp account."),
605✔
65
  );
66
})();
357✔
67
export type UsdClassTransferRequest = v.InferOutput<typeof UsdClassTransferRequest>;
68

69
/**
70
 * Successful response without specific data or error response.
71
 * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
72
 */
73
export const UsdClassTransferResponse = /* @__PURE__ */ (() => {
357✔
74
  return v.pipe(
605✔
75
    v.union([SuccessResponse, ErrorResponse]),
2,420✔
76
    v.description("Successful response without specific data or error response."),
605✔
77
  );
78
})();
357✔
79
export type UsdClassTransferResponse = v.InferOutput<typeof UsdClassTransferResponse>;
80

81
// ============================================================
82
// Execution Logic
83
// ============================================================
84

85
import { type DeepImmutable, parser } from "../_base.ts";
357✔
86
import {
357✔
87
  type ExchangeRequestConfig,
88
  type ExcludeErrorResponse,
89
  executeUserSignedAction,
357✔
90
  type ExtractRequestAction,
91
  type ExtractRequestOptions,
92
  getSignatureChainId,
357✔
93
  type MultiSignRequestConfig,
94
} from "./_base/mod.ts";
357✔
95

96
/** Action parameters for the {@linkcode usdClassTransfer} function. */
97
export type UsdClassTransferParameters = ExtractRequestAction<v.InferInput<typeof UsdClassTransferRequest>>;
98

99
/** Request options for the {@linkcode usdClassTransfer} function. */
100
export type UsdClassTransferOptions = ExtractRequestOptions<v.InferInput<typeof UsdClassTransferRequest>>;
101

102
/** Successful variant of {@linkcode UsdClassTransferResponse} without errors. */
103
export type UsdClassTransferSuccessResponse = ExcludeErrorResponse<UsdClassTransferResponse>;
104

105
/** EIP-712 types for the {@linkcode usdClassTransfer} function. */
106
export const UsdClassTransferTypes = {
357✔
107
  "HyperliquidTransaction:UsdClassTransfer": [
357✔
108
    { name: "hyperliquidChain", type: "string" },
1,428✔
109
    { name: "amount", type: "string" },
1,428✔
110
    { name: "toPerp", type: "bool" },
1,428✔
111
    { name: "nonce", type: "uint64" },
1,428✔
112
  ],
113
};
357✔
114

115
/**
116
 * Transfer funds between Spot account and Perp account.
117
 * @param config - General configuration for Exchange API requests.
118
 * @param params - Parameters specific to the API request.
119
 * @param opts - Request execution options.
120
 * @returns Successful response without specific data.
121
 *
122
 * @throws {ApiRequestError} When the API returns an unsuccessful response.
123
 * @throws {TransportError} When the transport layer throws an error.
124
 *
125
 * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
126
 * @example
127
 * ```ts
128
 * import { HttpTransport } from "@nktkas/hyperliquid";
129
 * import { usdClassTransfer } from "@nktkas/hyperliquid/api/exchange";
130
 * import { privateKeyToAccount } from "viem/accounts";
131
 *
132
 * const wallet = privateKeyToAccount("0x..."); // viem or ethers
133
 * const transport = new HttpTransport(); // or `WebSocketTransport`
134
 *
135
 * await usdClassTransfer(
136
 *   { transport, wallet },
137
 *   { amount: "1", toPerp: true },
138
 * );
139
 * ```
140
 */
141
export async function usdClassTransfer(
357✔
142
  config: ExchangeRequestConfig | MultiSignRequestConfig,
357✔
143
  params: DeepImmutable<UsdClassTransferParameters>,
357✔
144
  opts?: UsdClassTransferOptions,
357✔
145
): Promise<UsdClassTransferSuccessResponse> {
146
  const request = parser(UsdClassTransferRequest)({
360✔
147
    action: {
360✔
148
      type: "usdClassTransfer",
360✔
149
      hyperliquidChain: config.transport.isTestnet ? "Testnet" : "Mainnet",
360✔
150
      signatureChainId: await getSignatureChainId(config),
360✔
151
      nonce: 0, // Placeholder; actual nonce generated in `executeUserSignedAction` to prevent race conditions
360✔
152
      ...params,
360✔
153
    },
360✔
154
    nonce: 0, // Placeholder; actual nonce generated in `executeUserSignedAction` to prevent race conditions
360✔
155
    signature: { // Placeholder; actual signature generated in `executeUserSignedAction`
360✔
156
      r: "0x0000000000000000000000000000000000000000000000000000000000000000",
360✔
157
      s: "0x0000000000000000000000000000000000000000000000000000000000000000",
360✔
158
      v: 27,
360✔
159
    },
360✔
160
  });
360✔
161
  return await executeUserSignedAction(config, request, UsdClassTransferTypes, opts?.signal);
×
162
}
360✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc