@totalis/hip4-client 0.128.0 → 0.130.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,108 @@
3
3
  `@totalis/hip4-client` follows [semantic versioning](https://semver.org).
4
4
  The contract version the generated types come from is `sdkVersion` in `./version`.
5
5
 
6
+ ## 0.130.0
7
+
8
+ ### Removed
9
+
10
+ Contract `285.0.0-no-mark-runs`, live lane:
11
+
12
+ - `markSnapshot` no longer carries `sealed_through`, `runs_from` or `runs`.
13
+ They have been null and empty since recorded mark runs stopped; read a
14
+ perp's mark history from `GET /v1/charts/mark:{coin}`.
15
+ - The `markSealed` frame is removed. It was no longer sent.
16
+
17
+ ## 0.129.0
18
+
19
+ ### Added
20
+
21
+ Contract `284.0.0-light-renditions`:
22
+
23
+ - `getMarketDisplay` takes `light=true`, and a discovery or search page
24
+ `light=true` beside `display=true`. Asked so, a subject image whose artwork
25
+ vanishes on a light ground (a white mark) carries `light_variants`, the same
26
+ 64, 128 and 256 pixel sizes drawn on light grounds instead of `variants`.
27
+ Without it the answer is unchanged.
28
+
29
+ - The money rules the Totalis app prices singles, Combos and cash moves
30
+ with, as pure functions. Nothing existing changes.
31
+ - Cents and units: `usdCents(kind, amount, decimals?)` and
32
+ `centAmount` round to the cent by what the amount is (`UsdKind`: money
33
+ `received` and `spendable` down, a `fee` up, a `cost` or money `held` to
34
+ the nearest); `decimalUnits` converts decimal text exactly or returns
35
+ `null`; `coreUnits` and `coreAtomsFloor` convert between USDC base units
36
+ and HyperCore's 8 decimals.
37
+ - Outcome fees: `parseUserFees` reads Hyperliquid's `userFees`, refusing a
38
+ referral discount over 100%;
39
+ `closingFees(account, { global, creator })` gives the taker and maker
40
+ rates; `settledPayout`, `pairPayout`, `saleRate`, `saleProceeds`,
41
+ `withBuilderFee` and `beforeBuilderFee` apply them. `guestFees` is the
42
+ base schedule.
43
+ - Sizing and the book walk: `limitPreview` and `bookPreview` size a limit
44
+ or market single from a budget or whole shares, judge the 1 USDC
45
+ minimum (`minOrderNotional`) and sign a market order's IOC at the worst
46
+ level its walk reaches, at any price Hyperliquid takes (5 significant
47
+ figures, up to 8 decimals). A refusal is a reason code (`BELOW_MINIMUM`
48
+ with `leastStake` and `leastShares`, `BOOK_TOO_THIN` with what fills,
49
+ `PRICE_UNAVAILABLE`, `NO_ASKS`, `NO_BIDS`, `NO_AMOUNT`,
50
+ `LESS_THAN_SHARE`). `othersBook` takes the account's own resting orders
51
+ out of the book it walks. `buyCost` states a buy's expected cost;
52
+ `sellProceeds` a sale's walk net of fees. `maxCost` (the most a buy can
53
+ spend) and `minProceeds` (the least a sale lands if every share fills)
54
+ take the terms finally signed (`SignedTerms`) and the builder fee in the
55
+ signed action, and hold whatever discount or volume tier applies when the
56
+ order fills and however it splits into fills: a buy pays no venue fee,
57
+ and `minProceeds` takes the market's fee scales and charges the base
58
+ schedule's undiscounted taker rate, so it sits a few bps under a
59
+ forecast at the account's own rate. A successful
60
+ preview names its direction (`buy`). `bookTop`, `priceUnits` and
61
+ `wholeShares` read books and sizes.
62
+ - Receipts: `orderReceipt` states a fill's cost, payout or proceeds,
63
+ rounded once; `settledFill`, `actualOrderFill`, `executionPrice` and
64
+ `filledCoreUnits` complete a fill from `userFills`.
65
+ - Shown terms: `ShownHistory` records what a control showed on a clock you
66
+ pass in; `shownFloor` is the least a press may take and `boundLimit` the
67
+ best limit shown, applied to the final limit before signing; state
68
+ `maxCost` and `minProceeds` from the limit it returns.
69
+ - Funding: `maxStake` and `fundable(stake, sources)` over a Combo's cash
70
+ sources (`ComboSources`), where an unread source is unknown, never 0.
71
+ `fundable` is capacity, not readiness: an entry still needs the reads
72
+ its funding route uses, so don't gate a press on it alone;
73
+ `orderCoreNeed`, `orderVaultNeed` and `coreShortfall` for moving vault
74
+ cash to HyperCore before a buy; `activationTopUp`, `afterActivation`,
75
+ `sendableAfterSetup`, `stakeAfterSetup`, `directBuyVault` and
76
+ `directBuyBudget` for Hyperliquid's setup fee; `withdrawRoom` for a
77
+ send's own fees (unknown while the setup fee is unread); `sweepAmount`, `walletDepositable` and
78
+ `walletSweepMinimum` for moving wallet USDC into the vault.
79
+ - `bestNetQuote()` chooses the entry quote paying the taker the most net of
80
+ fees (`netPayout`), ties to the lowest receipt sequence then quote ID.
81
+ `bestQuote()` still ranks by gross payout.
82
+ - `singleOrderAction(order, { builder: false })` builds an order without the
83
+ Totalis builder fee (`SingleOrderActionWithoutBuilder`), for an account that has
84
+ not approved it; `signL1Action` and `submitSingleOrder` take it. The
85
+ builder stays attached by default.
86
+ - `readSingleOrder`'s `null` is documented as a hint: it never proves an
87
+ order did not land, even after its `expiresAfter`.
88
+
89
+ - `GET /v1/hypercore/orders/{cloid}?coin=&nonce=&expires_after=&after_height=`
90
+ settles a HIP-4 limit IOC whose answer was lost. Pass the order's coin, the
91
+ signed action's `nonce` and `expiresAfter`, and `after_height`, a height you
92
+ read from `GET /v1/hypercore/order-history` before signing. Any earlier
93
+ height works too, as an older one only widens the search, so keep the latest
94
+ from background reads rather than reading one before each order. `FOUND`
95
+ carries the order's status and, once indexed, its fills. `ABSENT` means the
96
+ order never reached matching and never will. `UNPROVEN` says why not yet:
97
+ `BEHIND` (read again shortly after `expires_after`), `GAP` or `UNCOVERED`
98
+ (this window can't be proven).
99
+
100
+ - `getPolymarketMarket` (`GET /v1/polymarket/markets/{outcome_id}`, contract
101
+ `283.0.0-polymarket-market`) returns a listed Polymarket market: its title,
102
+ rules, image, sides with their Polymarket token ids, its registry entry
103
+ (`version`, `ref`, `closes_at`, `question`), `state` (`ACTIVE`, `CLOSED`,
104
+ `RESULT_PENDING` or `SETTLED`) and its `result` once settled. A Polymarket
105
+ market's outcome id is 2147483648 (2^31) or above; any other id is
106
+ `NOT_FOUND`. Prices come on the market stream under the same id.
107
+
6
108
  ## 0.128.0
7
109
 
8
110
  No change notes for this release.
package/README.md CHANGED
@@ -146,7 +146,13 @@ if (answer.kind === "unknown") await readSingleOrder(network, wallet.address, si
146
146
  `cancelSingles` cancels resting singles by cloid or oid. Browser wallets often refuse to sign orders (their
147
147
  signing domain names chain 1337), so in a browser approve an agent key once with `approveHyperliquidAgent` and
148
148
  sign orders with that. The agent can trade but never withdraw. `singleOrderAction`, `signL1Action` and
149
- `submitSingleOrder` are the parts, if you send through your own Hyperliquid client.
149
+ `submitSingleOrder` are the parts, if you send through your own Hyperliquid client;
150
+ `singleOrderAction(order, { builder: false })` leaves the builder fee off for an account that has not approved it.
151
+
152
+ To size an order as the Totalis app does, walk the book with `bookPreview` (or `limitPreview` at your own
153
+ price): it returns the shares and the IOC limit to sign, the worst level the walk reaches, or a reason code such
154
+ as `BELOW_MINIMUM`. `buyCost` and `sellProceeds` state what it is expected to spend or land net of
155
+ `closingFees`; `maxCost` and `minProceeds` bound it from the limit you finally sign.
150
156
 
151
157
  ## Make markets
152
158
 
@@ -0,0 +1,221 @@
1
+ import { type FeeRate, type MarketFeeScale, type OutcomeFees } from "./outcome-fees.js";
2
+ import { type OutcomeSide } from "./singles.js";
3
+ /** Hyperliquid refuses a single whose traded notional is under 1 USDC, in USDC base units. */
4
+ export declare const minOrderNotional = 1000000n;
5
+ /** Whole shares in a size: the fraction dropped, empty read as 0. HIP-4 sizes are whole. */
6
+ export declare const wholeShares: (amount: string) => bigint;
7
+ /** A price in 1e-8 USDC per share, so units times shares is HyperCore USDC units; `null` outside (0, 1) or past 8 decimals. */
8
+ export declare function priceUnits(value: string): bigint | null;
9
+ /** One level of an outcome book as Hyperliquid's `l2Book` sends it. */
10
+ export type BookLevel = {
11
+ readonly px: string;
12
+ readonly sz: string;
13
+ readonly n?: number;
14
+ };
15
+ /** An outcome side's book: bids, then asks, as `l2Book`'s `levels`. Only a book proven current may price an order. */
16
+ export type SingleBook = {
17
+ readonly levels: readonly [readonly BookLevel[], readonly BookLevel[]];
18
+ };
19
+ /** A single's signed numbers: `buy` or a sale, `price` and `shares` as signed, `notional` their product floored to the USDC base unit, `payout` what a buy pays if it wins (a dollar a share) or what a sale takes. */
20
+ export type PricedOrder = {
21
+ readonly ok: true;
22
+ readonly buy: boolean;
23
+ readonly price: string;
24
+ readonly shares: string;
25
+ readonly notional: bigint;
26
+ readonly payout: bigint;
27
+ };
28
+ /**
29
+ * Why no order can be built:
30
+ * - `PRICE_UNAVAILABLE`: no price Hyperliquid takes: above 0, below 1, at
31
+ * most 5 significant figures and 8 decimals.
32
+ * - `BELOW_MINIMUM`: its traded notional is under `minOrderNotional`;
33
+ * `leastStake` (whole cents) or `leastShares` clears it, and its numbers ride along.
34
+ * - `NO_AMOUNT`: a sale of no shares.
35
+ * - `NO_ASKS` / `NO_BIDS`: the book side it takes is empty.
36
+ * - `BOOK_TOO_THIN`: the book takes less than asked: `fillableUsdc` of a buy
37
+ * (floored to the cent), `fillableShares` of a sale.
38
+ * - `LESS_THAN_SHARE`: the budget buys no whole share.
39
+ */
40
+ export type OrderRefusal = {
41
+ readonly ok: false;
42
+ readonly reason: "PRICE_UNAVAILABLE" | "NO_AMOUNT" | "NO_ASKS" | "NO_BIDS" | "LESS_THAN_SHARE";
43
+ } | {
44
+ readonly ok: false;
45
+ readonly reason: "BELOW_MINIMUM";
46
+ readonly leastStake: bigint;
47
+ readonly leastShares: bigint;
48
+ readonly price: string;
49
+ readonly shares: string;
50
+ readonly notional: bigint;
51
+ readonly payout: bigint;
52
+ } | {
53
+ readonly ok: false;
54
+ readonly reason: "BOOK_TOO_THIN";
55
+ readonly fillableUsdc: bigint;
56
+ } | {
57
+ readonly ok: false;
58
+ readonly reason: "BOOK_TOO_THIN";
59
+ readonly fillableShares: bigint;
60
+ };
61
+ export type OrderPreview = PricedOrder | OrderRefusal;
62
+ /**
63
+ * A market order walked off a book. It signs an IOC at `price`, the worst
64
+ * level the walk reaches, so it never fills worse than that. `averagePrice`
65
+ * and `cost` (a buy's spend; `null` for a sale, whose `payout` is the walk's
66
+ * proceeds) are what the walk executes at, `bestPrice` the top of the side.
67
+ */
68
+ export type BookPreview = (PricedOrder & {
69
+ readonly averagePrice: string;
70
+ readonly bestPrice: string;
71
+ readonly cost: bigint | null;
72
+ }) | OrderRefusal;
73
+ /**
74
+ * A limit order at `price`: a buy of the most whole shares whose price times
75
+ * size fits `budget` (HyperCore units, 8 decimals), or a sale of `shares`.
76
+ * Its minimum is judged on price times size.
77
+ */
78
+ export declare function limitPreview(order: {
79
+ readonly buy: true;
80
+ readonly price: string;
81
+ readonly budget: bigint;
82
+ } | {
83
+ readonly buy: false;
84
+ readonly price: string;
85
+ readonly shares: bigint;
86
+ }): OrderPreview;
87
+ /** The best price on the side an order takes: the lowest ask to buy, the highest bid to sell; `null` when that side is empty. */
88
+ export declare function bookTop(book: SingleBook, buy: boolean): string | null;
89
+ /**
90
+ * A market order walked off `book`: a buy of the most whole shares whose
91
+ * signed limit times size fits `budget` (HyperCore units, 8 decimals), or a
92
+ * sale of `shares`. Its IOC limit is the worst level the walk reaches; its
93
+ * minimum is judged on what the walk trades, as Hyperliquid judges an IOC.
94
+ */
95
+ export declare function bookPreview(order: {
96
+ readonly buy: true;
97
+ readonly budget: bigint;
98
+ } | {
99
+ readonly buy: false;
100
+ readonly shares: bigint;
101
+ }, book: SingleBook): BookPreview;
102
+ /** A resting order of the account's own, as `GET /v1/hypercore/orders` lists it. */
103
+ export type OwnOrder = {
104
+ readonly outcome_id: string;
105
+ readonly market_side: OutcomeSide;
106
+ readonly direction: "BUY" | "SELL";
107
+ readonly limit_price: string;
108
+ readonly remaining_size: string;
109
+ };
110
+ /**
111
+ * `side`'s book of `outcomeId` less the account's own resting orders, which
112
+ * Hyperliquid never fills against the account (it cancels the resting one),
113
+ * so a walk must not count them. An order on the other side rests here
114
+ * mirrored: a No buy at p is a Yes ask at 1 − p.
115
+ */
116
+ export declare function othersBook<Book extends SingleBook>(book: Book, outcomeId: string, side: OutcomeSide, orders: readonly OwnOrder[]): Book;
117
+ /**
118
+ * The terms an order signs: its limit and whole shares. A priced preview is
119
+ * one; once `boundLimit` moves the limit, pass the moved one, since these
120
+ * bounds hold only for what is signed.
121
+ */
122
+ export type SignedTerms = {
123
+ readonly price: string;
124
+ readonly shares: string | bigint;
125
+ };
126
+ /**
127
+ * The most a buy on these terms can take from cash: its limit times size,
128
+ * rounded up to the USDC base unit, with the builder fee rounded up, then
129
+ * rounded up to the cent. Buying outcome shares pays no venue fee, only the
130
+ * builder fee, and `signedBuilderDecibps` is the one in the signed action
131
+ * (0 without the builder), so no rate read before the press can move it.
132
+ */
133
+ export declare const maxCost: (order: SignedTerms, signedBuilderDecibps: number) => bigint;
134
+ /**
135
+ * The least a sale on these terms lands if every share fills, whatever
136
+ * discount or volume tier applies when it executes: its limit times size
137
+ * less the venue fee at the highest taker rate `market` can charge (the base
138
+ * schedule, `guestFees`, with no discount) and the builder fee in the signed
139
+ * action, charged as `saleProceeds` charges them. It takes the market's
140
+ * scales, never an account's rate, which a referral discount ending or a
141
+ * tier changing can raise before the order fills; so it sits a few bps under
142
+ * a forecast at the account's own rate. An IOC can fill fewer shares or
143
+ * none, so it bounds each share's price, not the total.
144
+ */
145
+ export declare const minProceeds: (order: SignedTerms, market: MarketFeeScale, signedBuilderDecibps: number) => bigint;
146
+ /**
147
+ * What a priced buy takes from cash, with the builder fee (decibps): `cost`
148
+ * what it is expected to spend (the walk's cost, or price times size),
149
+ * rounded to the nearest cent; `maxCost` as `maxCost` states it for the
150
+ * preview's own limit; `leftover` what of `stake` stays in cash after that
151
+ * cost and any `setupFee`. `null` for a refusal or a sale.
152
+ */
153
+ export declare function buyCost(preview: OrderPreview | BookPreview, stake: bigint, builderDecibps: number, setupFee?: bigint): {
154
+ cost: bigint;
155
+ shares: bigint;
156
+ leftover: bigint;
157
+ maxCost: bigint;
158
+ } | null;
159
+ /** What a priced sale lands in cash: the walk's proceeds (or price times size at a limit) less the fee at `rate` and the builder fee; 0 for a refusal or a buy. */
160
+ export declare const sellProceeds: (preview: OrderPreview | BookPreview, rate: FeeRate, builderDecibps: number) => bigint;
161
+ /** What a priced buy needs on HyperCore spot, in HyperCore units: its signed price times size, with the builder fee (decibps). 0 for a refusal or a sale. */
162
+ export declare const orderCoreNeed: (preview: OrderPreview | BookPreview, builderDecibps?: number) => bigint;
163
+ /** The vault cash (6 decimals) that covers what `spot` (HyperCore units) lacks of `orderCoreNeed`: exact in 8 decimals, only the shortfall rounded up. */
164
+ export declare const orderVaultNeed: (preview: OrderPreview | BookPreview, spot?: bigint, builderDecibps?: number) => bigint;
165
+ /**
166
+ * What a single actually did, from its answer and fills: `shares` traded,
167
+ * `averagePrice` the price they executed at (`null` where only sizes are
168
+ * known), `fee` the venue's in HyperCore units, signed as charged (`null`
169
+ * until every fill of the order is in).
170
+ */
171
+ export type OrderFill = {
172
+ readonly kind: "filled" | "partial" | "resting";
173
+ readonly shares: bigint;
174
+ readonly averagePrice: string | null;
175
+ readonly fee: bigint | null;
176
+ };
177
+ /** What a fill moved in HyperCore units: shares times price, exact, plus `fee` (a buy pays it on top, so pass it negated for a sale). `null` for a price that is not decimal text. */
178
+ export declare function filledCoreUnits(shares: bigint, price: string, fee?: bigint): bigint | null;
179
+ /**
180
+ * Settled money from a fill, never the preview, rounded once to the cent: a
181
+ * buy's `cost` to the nearest, its `payout` if it wins at `fees` (`null`
182
+ * while they are unread), and `setup` when a setup fee was proven paid; a
183
+ * sale's `received` down. `null` where the fill's price or fee is not in yet.
184
+ */
185
+ export declare function orderReceipt(fill: OrderFill, buy: boolean, fees: OutcomeFees | null, setupFee?: bigint): {
186
+ cost: bigint | null;
187
+ payout: bigint | null;
188
+ setup?: bigint;
189
+ } | {
190
+ received: bigint | null;
191
+ };
192
+ /** One fill as Hyperliquid's `userFills` and the account read report it. */
193
+ export type Fill = {
194
+ readonly oid: number;
195
+ readonly size: string;
196
+ readonly price?: string;
197
+ readonly fee?: string;
198
+ readonly fee_token?: string;
199
+ };
200
+ /** The size-weighted price `fills` executed at; `null` where any lacks a price. */
201
+ export declare function executionPrice(fills: readonly Pick<Fill, "size" | "price">[]): string | null;
202
+ /**
203
+ * What the venue charged and executed at over every fill of order `oid`,
204
+ * once they add up to exactly `expectedSize`: a resting order keeps filling
205
+ * and old fills drop out, so fills that come to more or less belong to
206
+ * another total. `fee` is decimal text in `token`, negative for a rebate;
207
+ * `price` is `executionPrice`. `null` until then.
208
+ */
209
+ export declare function actualOrderFill(fills: readonly Fill[], oid: number, expectedSize: string): {
210
+ fee: string;
211
+ token: string;
212
+ price: string | null;
213
+ } | null;
214
+ /**
215
+ * A fill whose price or fee is missing, completed from the order's `fills`
216
+ * once they add up to exactly its shares: the venue's own answer wins where
217
+ * it gave one, and the fee must be USDC. Otherwise the same fill.
218
+ */
219
+ export declare function settledFill<F extends OrderFill & {
220
+ readonly oid?: number;
221
+ }>(fill: F, fills: readonly Fill[]): F;