FAQ
How to create a market order?
Hyperliquid doesn't have traditional market orders. Use a limit order with tif: "Ioc" (Immediate or Cancel) and a price that guarantees immediate execution:
import { ExchangeClient, HttpTransport, InfoClient } from "@nktkas/hyperliquid";
import { privateKeyToAccount } from "viem/accounts";
const info = new InfoClient({ transport: new HttpTransport() });
const exchange = new ExchangeClient({
transport: new HttpTransport(),
wallet: privateKeyToAccount("0x..."),
});
// Get current price
const mids = await info.allMids();
const currentPrice = parseFloat(mids["ETH"]);
// Buy: set price above current (e.g., +1%)
const buyPrice = currentPrice * 1.01;
// Sell: set price below current (e.g., -1%)
const sellPrice = currentPrice * 0.99;
await exchange.order({
orders: [{
a: 4,
b: true,
p: buyPrice,
s: "0.1",
r: false,
t: { limit: { tif: "Ioc" } }, // Immediate or Cancel
}],
grouping: "na",
});How to use Agent Wallet?
Agent wallets can sign on behalf of your master account. Use the agent's private key instead of master account's:
import { ExchangeClient, HttpTransport } from "@nktkas/hyperliquid";
import { privateKeyToAccount } from "viem/accounts";
const client = new ExchangeClient({
transport: new HttpTransport(),
wallet: privateKeyToAccount("0x..."), // agent's private key
});Create an agent via approveAgent method or through the Hyperliquid UI.
How to trade on behalf of a Vault or Sub-Account?
Pass vault or sub-account address via vaultAddress option:
// Per-request
await client.order({ ... }, { vaultAddress: "0x..." });
// Or set default for all requests
const client = new ExchangeClient({
transport: new HttpTransport(),
wallet: privateKeyToAccount("0x..."),
defaultVaultAddress: "0x...",
});How to sign with MetaMask or other browser wallets?
L1 actions use chain ID 1337 (dev network, not in wallets by default) and sign an action hash instead of readable order details. Users will see Agent { source: "a", connectionId: "0x..." } — not useful.
Solution: Use Agent Wallet for trading. Master account signs once to approve the agent.
Last updated