@nktkas/hyperliquid
Version:
Unofficial Hyperliquid API SDK for all major JS runtimes, written in TypeScript and provided with tests
1,361 lines (1,360 loc) • 75.3 kB
JavaScript
import { keccak_256 } from "@noble/hashes/sha3";
import { getPublicKey } from "@noble/secp256k1";
import { encodeHex } from "../../deps/jsr.io/@std/encoding/1.0.10/hex.js";
import { actionSorter, isAbstractEthersSigner, isAbstractEthersV5Signer, isAbstractViemWalletClient, isAbstractWindowEthereum, isValidPrivateKey, signL1Action, signUserSignedAction, userSignedActionEip712Types, } from "../signing/mod.js";
import { ExchangeClient, } from "./exchange.js";
/**
* Multi-signature exchange client for interacting with the Hyperliquid API.
* @typeParam T The transport used to connect to the Hyperliquid API.
* @typeParam S Array of wallets where the first wallet acts as the leader.
*/
export class MultiSignClient extends ExchangeClient {
multiSignAddress;
signers;
/**
* Initialises a new multi-signature client instance.
* @param args - The parameters for the multi-signature client.
*
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport();
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
* ```
*/
constructor(args) {
super({ ...args, wallet: args.signers[0] });
this.multiSignAddress = args.multiSignAddress;
this.signers = args.signers;
Object.defineProperty(this, "wallet", {
get() {
return this.signers[0];
},
set(value) {
this.signers[0] = value;
},
enumerable: true,
configurable: true,
});
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-an-api-wallet
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.approveAgent({ agentAddress: "0x...", agentName: "agentName" });
* ```
*/
async approveAgent(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
...actionArgs,
agentName: args.agentName ?? "",
type: "approveAgent",
hyperliquidChain: this._getHyperliquidChain(),
signatureChainId: await this._getSignatureChainId(),
nonce,
};
// Sign the action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignUserSignedAction(sortedAction, outerSigner);
if (sortedAction.agentName === "")
sortedAction.agentName = null;
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#approve-a-builder-fee
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.approveBuilderFee({ maxFeeRate: "0.01%", builder: "0x..." });
* ```
*/
async approveBuilderFee(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
...actionArgs,
type: "approveBuilderFee",
hyperliquidChain: this._getHyperliquidChain(),
signatureChainId: await this._getSignatureChainId(),
nonce,
};
// Sign the action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignUserSignedAction(sortedAction, outerSigner);
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful variant of {@link OrderResponse} without error statuses.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-multiple-orders
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.batchModify({
* modifies: [{
* oid: 123,
* order: {
* a: 0, // Asset index
* b: true, // Buy order
* p: "31000", // New price
* s: "0.2", // New size
* r: false, // Not reduce-only
* t: {
* limit: {
* tif: "Gtc", // Good-til-cancelled
* },
* },
* c: "0x...", // Client Order ID (optional)
* },
* }],
* });
* ```
*/
async batchModify(...[args, signal]) {
// Destructure the parameters
const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "batchModify",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({
action: sortedAction,
nonce,
outerSigner,
vaultAddress,
expiresAfter,
});
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
vaultAddress,
expiresAfter,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful variant of {@link CancelResponse} without error statuses.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.cancel({
* cancels: [{
* a: 0, // Asset index
* o: 123, // Order ID
* }],
* });
* ```
*/
async cancel(...[args, signal]) {
// Destructure the parameters
const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "cancel",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({
action: sortedAction,
nonce,
outerSigner,
vaultAddress,
expiresAfter,
});
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
vaultAddress,
expiresAfter,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful variant of {@link CancelResponse} without error statuses.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#cancel-order-s-by-cloid
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.cancelByCloid({
* cancels: [
* { asset: 0, cloid: "0x..." },
* ],
* });
* ```
*/
async cancelByCloid(...[args, signal]) {
// Destructure the parameters
const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "cancelByCloid",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({
action: sortedAction,
nonce,
outerSigner,
vaultAddress,
expiresAfter,
});
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
vaultAddress,
expiresAfter,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#deposit-into-staking
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.cDeposit({ wei: 1 * 1e8 });
* ```
*/
async cDeposit(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
...actionArgs,
type: "cDeposit",
hyperliquidChain: this._getHyperliquidChain(),
signatureChainId: await this._getSignatureChainId(),
nonce,
};
// Sign the action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignUserSignedAction(sortedAction, outerSigner);
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.claimRewards();
* ```
*/
async claimRewards(...[signal]) {
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "claimRewards",
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/hypercore/multi-sig
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.convertToMultiSigUser({ // convert to normal user
* authorizedUsers: [],
* threshold: 0,
* });
* ```
*/
async convertToMultiSigUser(...[args, signal]) {
// Destructure the parameters
const actionArgs = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "convertToMultiSigUser",
hyperliquidChain: this._getHyperliquidChain(),
signatureChainId: await this._getSignatureChainId(),
signers: JSON.stringify(actionArgs),
nonce,
};
// Sign the action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignUserSignedAction(sortedAction, outerSigner);
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Response for creating a sub-account.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.createSubAccount({ name: "subAccountName" });
* ```
*/
async createSubAccount(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "createSubAccount",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Response for creating a vault.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.createVault({
* name: "VaultName",
* description: "Vault description",
* initialUsd: 100 * 1e6,
* });
* ```
*/
async createVault(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "createVault",
nonce,
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
async cSignerAction(args, signal) {
// Destructure the parameters
const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "CSignerAction",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner, expiresAfter });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
expiresAfter,
}, signal);
}
async cValidatorAction(args, signal) {
// Destructure the parameters
const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "CValidatorAction",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner, expiresAfter });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
expiresAfter,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#withdraw-from-staking
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.cWithdraw({ wei: 1 * 1e8 });
* ```
*/
async cWithdraw(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
...actionArgs,
type: "cWithdraw",
hyperliquidChain: this._getHyperliquidChain(),
signatureChainId: await this._getSignatureChainId(),
nonce,
};
// Sign the action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignUserSignedAction(sortedAction, outerSigner);
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Response for creating a sub-account.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/evm/dual-block-architecture
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.evmUserModify({ usingBigBlocks: true });
* ```
*/
async evmUserModify(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "evmUserModify",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#modify-an-order
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.modify({
* oid: 123,
* order: {
* a: 0, // Asset index
* b: true, // Buy order
* p: "31000", // New price
* s: "0.2", // New size
* r: false, // Not reduce-only
* t: {
* limit: {
* tif: "Gtc", // Good-til-cancelled
* },
* },
* c: "0x...", // Client Order ID (optional)
* },
* });
* ```
*/
async modify(...[args, signal]) {
// Destructure the parameters
const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "modify",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({
action: sortedAction,
nonce,
outerSigner,
vaultAddress,
expiresAfter,
});
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
vaultAddress,
expiresAfter,
}, signal);
}
/**
* @multisign Not implemented
*/
multiSig(...[_args, _signal]) {
throw new Error("Not implemented"); // FIXME
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful variant of {@link OrderResponse} without error statuses.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#place-an-order
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.order({
* orders: [{
* a: 0, // Asset index
* b: true, // Buy order
* p: "30000", // Price
* s: "0.1", // Size
* r: false, // Not reduce-only
* t: {
* limit: {
* tif: "Gtc", // Good-til-cancelled
* },
* },
* c: "0x...", // Client Order ID (optional)
* }],
* grouping: "na", // No grouping
* });
* ```
*/
async order(...[args, signal]) {
// Destructure the parameters
const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "order",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({
action: sortedAction,
nonce,
outerSigner,
vaultAddress,
expiresAfter,
});
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
vaultAddress,
expiresAfter,
}, signal);
}
async perpDeploy(args, signal) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "perpDeploy",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#transfer-from-spot-account-to-perp-account-and-vice-versa
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.perpDexClassTransfer({
* dex: "test",
* token: "USDC",
* amount: "1",
* toPerp: true,
* });
* ```
*/
async perpDexClassTransfer(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
...actionArgs,
type: "PerpDexClassTransfer",
hyperliquidChain: this._getHyperliquidChain(),
signatureChainId: await this._getSignatureChainId(),
nonce,
};
// Sign the action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignUserSignedAction(sortedAction, outerSigner);
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.registerReferrer({ code: "TEST" });
* ```
*/
async registerReferrer(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "registerReferrer",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#reserve-additional-actions
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.reserveRequestWeight({ weight: 10 });
* ```
*/
async reserveRequestWeight(...[args, signal]) {
// Destructure the parameters
const { expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "reserveRequestWeight",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner, expiresAfter });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
expiresAfter,
}, signal);
}
async scheduleCancel(args_or_signal, maybeSignal) {
const args = args_or_signal instanceof AbortSignal ? {} : args_or_signal ?? {};
const signal = args_or_signal instanceof AbortSignal ? args_or_signal : maybeSignal;
// Destructure the parameters
const { vaultAddress = this.defaultVaultAddress, expiresAfter = await this._getDefaultExpiresAfter(), ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "scheduleCancel",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({
action: sortedAction,
nonce,
outerSigner,
vaultAddress,
expiresAfter,
});
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
vaultAddress,
expiresAfter,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.setDisplayName({ displayName: "My Name" });
* ```
*/
async setDisplayName(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "setDisplayName",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.setReferrer({ code: "TEST" });
* ```
*/
async setReferrer(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "setReferrer",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
async spotDeploy(args, signal) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "spotDeploy",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/exchange-endpoint#core-spot-transfer
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.spotSend({
* destination: "0x...",
* token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
* amount: "1",
* });
* ```
*/
async spotSend(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
...actionArgs,
type: "spotSend",
hyperliquidChain: this._getHyperliquidChain(),
signatureChainId: await this._getSignatureChainId(),
time: nonce,
};
// Sign the action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignUserSignedAction(sortedAction, outerSigner);
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.spotUser({ toggleSpotDusting: { optOut: false } });
* ```
*/
async spotUser(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "spotUser",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.subAccountSpotTransfer({
* subAccountUser: "0x...",
* isDeposit: true,
* token: "USDC:0xeb62eee3685fc4c43992febcd9e75443",
* amount: "1",
* });
* ```
*/
async subAccountSpotTransfer(...[args, signal]) {
// Destructure the parameters
const { ...actionArgs } = args;
// Construct an action
const nonce = await this.nonceManager();
const action = {
type: "subAccountSpotTransfer",
...actionArgs,
};
// Send a multi-sig action
const sortedAction = actionSorter[action.type](action);
const outerSigner = await this._getWalletAddress(this.signers[0]);
const signatures = await this._multiSignL1Action({ action: sortedAction, nonce, outerSigner });
// Send a multi-sig action
return super.multiSig({
signatures,
payload: {
multiSigUser: this.multiSignAddress,
outerSigner,
action: sortedAction,
},
nonce,
}, signal);
}
/**
* @param args - The parameters for the request.
* @param signal - An optional abort signal.
* @returns Successful response without specific data.
* @throws {ApiRequestError} When the API returns an error response.
*
* @see null - no documentation
* @example
* ```ts
* import * as hl from "@nktkas/hyperliquid";
*
* const multiSignAddress = "0x...";
* const signers = [
* "0x...", // Private key; or any other wallet libraries
* ] as const;
*
* const transport = new hl.HttpTransport(); // or `WebSocketTransport`
* const multiSignClient = new hl.MultiSignClient({ transport, multiSignAddress, signers });
*
* const data = await multiSignClient.subAccountTransfer({
* subAccountUser: "0x...",
* isDeposit: true,
* usd: 1 * 1e6,
* });
* ```