Skip to content
API v1 is deprecated. It stays supported, but new integrations should use the Khalani API (v3). See Migrate to v3.
API v1 · Deprecated

Wallet execution (v1)

POST /v1/deposit/build returns a deposit plan. Switch on its kind:

  • CONTRACT_CALL: the user’s wallet runs the actions in approvals[], in order. You submit the hash of the action marked deposit: true. This page covers EVM wallets.
  • TRANSFER: the user sends a plain transfer to a deposit address. See Deposit addresses.
Field On Meaning
type Every action eip1193_request.
request EVM An EIP-1193 request: wallet_switchEthereumChain or eth_sendTransaction. Forward it unchanged.
waitForReceipt EVM, optional true on approvals. Wait for the receipt before the next action.
deposit Optional true on the one action that is the deposit. Submit its hash.

A chain-switch action has neither waitForReceipt nor deposit. Treat a missing flag as false.

An EVM plan is a list of EIP-1193 requests, so any EIP-1193 wallet can run it. Pass each request to provider.request as it is. A typical native-route plan is a chain switch, an approval when the allowance is short, and the deposit.

v1-wallet-evm.ts
// API v1 (deprecated): run the EVM wallet actions from POST /v1/deposit/build with any EIP-1193
// wallet, then submit the deposit hash. Each request is forwarded to the wallet unchanged.
import { type Hex, isHex } from "viem";
const API = "https://api.hyperstream.dev";
export type Eip1193Provider = {
request(args: { method: string; params?: readonly unknown[] }): Promise<unknown>;
};
/** The quote and route the user chose, and the wallet that sends the deposit. */
export type Selection = { quoteId: string; routeId: string; from: string };
type EvmAction = {
type: "eip1193_request";
request: { method: string; params?: readonly unknown[] };
waitForReceipt?: boolean;
deposit?: boolean;
};
type DepositPlan =
| { kind: "CONTRACT_CALL"; approvals?: EvmAction[]; buildId?: string }
| { kind: "TRANSFER"; depositAddress: string; amount: string; token: string; chainId: number }
| { kind: "PERMIT2" };
type Receipt = { status: Hex };
/** Builds, executes and submits one EVM deposit. Returns the v1 order id. */
export async function depositWithEvmWallet(provider: Eip1193Provider, selection: Selection): Promise<string> {
const plan = await call<DepositPlan>("POST", "/v1/deposit/build", selection);
if (plan.kind !== "CONTRACT_CALL") throw new Error(`Not a wallet deposit: ${plan.kind}`);
let depositTxHash: Hex | undefined;
for (const action of plan.approvals ?? []) {
// wallet_switchEthereumChain resolves to null; eth_sendTransaction to the transaction hash.
const result = await provider.request(action.request);
if (action.request.method !== "eth_sendTransaction") continue;
if (typeof result !== "string" || !isHex(result)) throw new Error("The wallet returned no transaction hash");
if (action.waitForReceipt) await waitForReceipt(provider, result);
if (action.deposit) depositTxHash = result;
}
if (!depositTxHash) throw new Error("The plan had no deposit action");
// Submit once. buildId is present only for a quote made without fromAddress.
const submitted = await call<{ orderId: string; txHash: string }>("PUT", "/v1/deposit/submit", {
txHash: depositTxHash,
quoteId: selection.quoteId,
routeId: selection.routeId,
...(plan.buildId ? { buildId: plan.buildId } : {}),
});
return submitted.orderId;
}
async function waitForReceipt(provider: Eip1193Provider, hash: Hex): Promise<void> {
for (;;) {
const receipt = (await provider.request({ method: "eth_getTransactionReceipt", params: [hash] })) as Receipt | null;
if (receipt) {
if (receipt.status !== "0x1") throw new Error(`Transaction ${hash} reverted`);
return;
}
await new Promise((resolve) => setTimeout(resolve, 2_000));
}
}
async function call<T>(method: "POST" | "PUT", path: string, body: unknown): Promise<T> {
const res = await fetch(`${API}${path}`, {
method,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const text = await res.text();
if (!res.ok) throw new Error(`${res.status} ${path}: ${text} (x-request-id ${res.headers.get("x-request-id")})`);
return JSON.parse(text) as T;
}
  • Forward every field. The deposit’s eth_sendTransaction carries value and often gas. Dropping value makes the deposit revert, because it pays the relay fee.
  • Wait where the plan says so. An approval has waitForReceipt: true. Sending the deposit before the approval confirms makes it revert.
  • A chain switch can fail when the wallet does not know the chain. Ask the user to add it with wallet_addEthereumChain, using the RPC and explorer from GET /v1/chains, then run the plan again.
  • Show a stepper. Tell the user how many signatures to expect (approve, then deposit), so no prompt arrives without context.

A TRANSFER plan has no wallet actions. It names an address and an amount:

{
"kind": "TRANSFER",
"orderId": "9f2c4e1a7b3d5f60812a4c6e8b0d2f41",
"depositAddress": "0x7d2c…e41a",
"amount": "10000000",
"token": "0xaf88d065e77c8cc2239327c5edb3a432268e5831",
"chainId": 42161,
"expiresAt": 1792443600
}

Send exactly amount of token to depositAddress on chainId, before expiresAt. Any wallet can send it. Then:

Do not call submit for this deposit-address flow. The order already exists, and the deposit address detects the transfer itself. For a quote made without a wallet, submit returns 400. Track the order with GET /v1/orders/{refundTo}?orderIds={orderId}, where the address is the order’s refund address (refundTo, or else recipient, or else from). GET /v1/orders/by-id/{orderId} does not serve these 32-character ids.

If the user’s wallet can sign but not broadcast, send the signed transaction instead of a hash, and the API broadcasts it:

{ "signedTransaction": "0x02f8…", "quoteId": "3f6c2a1e-8b4d-4e7a-9c15-2d0b7e9a4f61", "routeId": "Hyperstream" }

Use 0x-prefixed hex. Send either txHash or signedTransaction, never both. If a broadcast fails, the API derives its hash and looks it up. A lookup hit continues the normal submit and returns { orderId, txHash }.

For a failure response, branch on the top-level errorCode, not the HTTP status or message:

  • BROADCAST_REJECTED: the API proved that the signed transaction belongs to a different source chain, and its lookup succeeded without finding it. Fix the transaction and re-sign.
  • BROADCAST_OUTCOME_UNKNOWN: acceptance is unresolved. The top-level txHash, when derivable, identifies the original transaction. A timeout, duplicate or consumed nonce does not prove rejection.

reasonCategory describes the original failure: transport, duplicate, nonce_conflict, node_policy, current_state, or unknown. A proven rejection uses invalid. These categories do not prove non-acceptance.

After BROADCAST_OUTCOME_UNKNOWN, re-sign only for a positively recognized current_state failure and only if this was the first broadcast of these bytes anywhere, including the wallet. Count every earlier wallet or API broadcast, even one whose response was lost. If the history is uncertain, keep the original bytes.

Look for an existing order first with GET /v1/orders/{address}. Otherwise retry the same signed bytes or submit the returned txHash while the quote survives. Successful submit deletes the quote; storage otherwise lasts until validBefore plus 60 seconds, capped at 24 hours. Once that window closes, contact Khalani with the original hash before creating another deposit.

See Deposit for every field and Handling exceptions for the failure contract.