Before you start
- Node.js 22 or later. The package is ESM and ships its signing core as WebAssembly.
- A HyperCore key that signs the escrows. Use it for Biloa swaps and nothing else.
- USDC on HyperCore, on perp in standard mode: the amount of each swap to Elysium plus 1 USDC for its new escrow account, and 1.01 USDC to open the escrow of each swap to HyperCore.
- USDC on Elysium for swaps to HyperCore.
- A little HYPE on Elysium for gas, in both directions: the taker claims the lock in a swap to Elysium and locks in a swap to HyperCore.
- An Elysium mainnet RPC URL.
Install
npm install @biloa/sdk viem
Configure the taker
import { Clock, MAINNET, elysiumMainnet, toStandardPerp } from "@biloa/sdk";
import { type Hex, createPublicClient, createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
const rpc = process.env.ELYSIUM_RPC_URL!;
const chain = elysiumMainnet(rpc);
const account = privateKeyToAccount(process.env.ELYSIUM_KEY as Hex);
const taker = {
network: MAINNET.network,
// HyperCore key that signs the escrow; keep it for Biloa swaps only.
coreKey: process.env.HYPERCORE_KEY as Hex,
elysium: createWalletClient({ account, chain, transport: http(rpc) }),
publicClient: createPublicClient({ chain, transport: http(rpc) }),
lock: MAINNET.lock,
clock: await Clock.of(MAINNET.network),
log: (line: string) => console.log(line),
// Escrow keys and secrets of every swap. Keep it, and keep it private.
journal: "./biloa-journal.json",
};
// Swaps pay from HyperCore perp in standard mode. Run once; it does nothing when already set.
await toStandardPerp(MAINNET.network, taker.coreKey, taker.clock);
| Field | What it is |
|---|---|
network |
"Mainnet" or "Testnet" for HyperCore. MAINNET.network is "Mainnet". |
coreKey |
The HyperCore key that signs and leads the escrow’s transfers. |
elysium |
A viem wallet client on Elysium. It locks, claims and refunds. |
publicClient |
A viem public client on Elysium for reads. |
lock |
The BiloaLock address, MAINNET.lock. |
clock |
HyperCore’s clock, from Clock.of(network). Nonces come from it, so they never repeat. |
log |
Receives one line per step of each swap. |
journal |
Path of the journal file, written with mode 0600. |
limits |
Optional bounds on a quote’s timing, described below. |
The journal holds what the taker needs to finish or refund every swap it started. Back it up with the rest of the agent's state and never delete it while a swap is open.
Swap to Elysium
import { MAINNET, swapBest } from "@biloa/sdk";
import { parseUnits } from "viem";
// 10 USDC from HyperCore to Elysium; at least 9.95 USDC must arrive.
const out = await swapBest(
taker,
{ dir: "toElysium", token: MAINNET.usdc, usdc: "10", minNet: parseUnits("9.95", 6) },
{ registry: MAINNET.registry },
);
console.log(out.result, out.lp); // "swapped" or "refunded", and the LP that took it
usdc is the amount paid on HyperCore, as a decimal string. minNet is the least the taker accepts on Elysium after BiloaLock’s 3 bps fee, in token units (USDC has 6 decimals). Quotes below it are refused before anything is paid.
Swap to HyperCore
import { MAINNET, swapBest } from "@biloa/sdk";
import { parseUnits } from "viem";
// 10 USDC from Elysium to HyperCore; pay at most 10.05 USDC on Elysium.
const out = await swapBest(
taker,
{ dir: "toCore", token: MAINNET.usdc, usdc: "10", maxAmount: parseUnits("10.05", 6) },
{ registry: MAINNET.registry },
);
Here usdc is the amount received on HyperCore, and maxAmount caps what the taker locks on Elysium.
What swapBest returns
swapBest resolves to { result, lp, amount, offers, refused }: the outcome, the LP that took the swap, the amount it quoted, how many offers were ranked, and the LPs passed over with the reason for each.
result |
Meaning |
|---|---|
"swapped" |
Both sides settled and the funds arrived. |
"refunded" |
The swap did not complete, and the taker’s payment came back. |
"abandoned" |
Swaps to HyperCore only: the lock never landed on Elysium, so nothing was paid there. |
"unconfirmed" |
Swaps to HyperCore only: a claim was sent and the escrow is now empty, but the SDK cannot tell whether that claim or the LP’s refund took it. Check the HyperCore balance. |
When no listed LP offers an acceptable quote, swapBest throws an error that names each LP and why it was refused. An error thrown after the taker has paid leaves the swap open in the journal; run resumeSwaps to finish it.
A swap that ends in a refund resolves only once its refund can be sent. For the escrow that is when the refund opens on HyperCore, about 15 minutes after the quote with the timing biloa-lp uses.
How the SDK picks an LP
swapBest reads the LPs listed in BiloaRegistry and their records in BiloaLock, asks each one for a quote, and ranks the answers:
- An LP that let a signed lock expire in the past week goes after every LP that did not.
- Among the rest, the better price wins: more to receive in a swap to Elysium, less to pay in a swap to HyperCore.
- The LP’s record breaks ties: more claimed locks first, then fewer expired ones.
The taker takes the first offer whose LP sets its funds aside for the swap. An LP that turns the swap down before the taker has paid, for example because it is busy, is passed over for the next one. Once the taker has paid, the swap stays with that LP.
The third argument tunes the search:
| Option | Default | What it does |
|---|---|---|
registry |
required | The BiloaRegistry address, MAINNET.registry. |
recentSeconds |
604800n (7 days) |
How recent an expired lock must be to send an LP to the back, as a bigint. |
maxListed |
200n |
Registry entries read, as a bigint. |
windowMs |
8,000 | Time for all quotes. An LP that has not answered by then is skipped. |
parallel |
16 | LPs asked at once. |
wrap |
none | Wraps the transport of the chosen LP, for example to log its messages. |
The SDK accepts only public HTTPS endpoints from the registry and refuses private and loopback addresses.
Resume after a crash
Before the swap amount leaves the taker, everything needed to finish or refund the swap is in the journal. If the process stops in the middle of a swap, run this on the next start:
import { resumeSwaps } from "@biloa/sdk";
for (const r of await resumeSwaps(taker)) console.log(r.id, r.result);
It finishes each open swap on chain without the LP and returns one result per swap, or failed: with the reason. Run it within the swap’s claim window, a few minutes, so a side the taker is owed is still claimable.
Limits on a quote’s timing
The taker checks the timing each LP quotes and refuses a quote outside these bounds. To change them, pass all three in limits in the taker config.
| Limit | Default | Meaning |
|---|---|---|
maxRefundAfterMs |
30 minutes | Latest time after the quote at which the escrow refund may open. |
maxLockSeconds |
600 | Longest lock the taker accepts on Elysium. |
minClaimWindowMs |
4 minutes | Least time between a lock’s deadline and the escrow refund, in which the party owed the escrow can still claim it. |
What a swap costs
- BiloaLock’s protocol fee of 3 bps, taken from the payout of each claim.
- The LP’s spread, which each LP sets.
biloa-lpstarts at 20 bps. - HyperCore’s 1 USDC fee for the new escrow account, paid by the taker (1.01 USDC in swaps to HyperCore, which includes a little to activate the escrow).
- Gas in HYPE for each lock, claim or refund on Elysium.
A single swap is capped at 1,000 USDC by BiloaLock. Each LP sets its own range within that, and biloa-lp starts at 10 to 200 USDC.