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 walletsarrow-up-right can sign on behalf of your master account. Use the agent's private key instead of master account's:

Create an agent via approveAgent method or through the Hyperliquid UIarrow-up-right.

How to trade on behalf of a Vault or Sub-Account?

Pass vault or sub-account addressarrow-up-right via vaultAddress option:

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 Walletarrow-up-right for trading. Master account signs once to approve the agent.

Last updated