@stryke-xyz/premarket-sdk 1.2.1 → 1.2.2
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/dist/cjs/config/index.js +3 -3
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/sync/clients/activity-client.js +325 -454
- package/dist/cjs/sync/clients/order-client.js +609 -429
- package/dist/esm/config/index.js +3 -3
- package/dist/esm/package.json +1 -1
- package/dist/esm/sync/clients/activity-client.js +325 -454
- package/dist/esm/sync/clients/order-client.js +609 -429
- package/dist/types/sync/clients/activity-client.d.ts +39 -32
- package/dist/types/sync/clients/order-client.d.ts +79 -58
- package/dist/types/sync/index.d.ts +2 -2
- package/package.json +58 -58
- package/dist/cjs/api/README.md +0 -296
- package/dist/cjs/config/README.md +0 -112
- package/dist/cjs/exchange/README.md +0 -280
- package/dist/cjs/registry/README.md +0 -150
- package/dist/cjs/shared/README.md +0 -235
- package/dist/cjs/shared/utils.js +0 -1
- package/dist/cjs/sync/README.md +0 -215
- package/dist/cjs/sync/clients/base-client.js +0 -518
- package/dist/cjs/sync/redis-ws-client.js +0 -235
- package/dist/cjs/utils/README.md +0 -89
- package/dist/cjs/vault/README.md +0 -268
- package/dist/esm/api/README.md +0 -296
- package/dist/esm/config/README.md +0 -112
- package/dist/esm/exchange/README.md +0 -280
- package/dist/esm/registry/README.md +0 -150
- package/dist/esm/shared/README.md +0 -235
- package/dist/esm/shared/utils.js +0 -0
- package/dist/esm/sync/README.md +0 -215
- package/dist/esm/sync/clients/base-client.js +0 -508
- package/dist/esm/sync/redis-ws-client.js +0 -225
- package/dist/esm/utils/README.md +0 -89
- package/dist/esm/vault/README.md +0 -268
- package/dist/types/shared/utils.d.ts +0 -0
- package/dist/types/sync/clients/base-client.d.ts +0 -56
- package/dist/types/sync/redis-ws-client.d.ts +0 -21
package/dist/cjs/api/README.md
DELETED
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
# API Guide
|
|
2
|
-
|
|
3
|
-
The `api` module gives the SDK two high-level integration surfaces:
|
|
4
|
-
|
|
5
|
-
- `OrderHelper`
|
|
6
|
-
- a workflow helper for building, hashing, serializing, signing, and
|
|
7
|
-
recovering orders
|
|
8
|
-
- `OrderbookApi`
|
|
9
|
-
- the HTTP client for orderbook, market, user, history, and analytics reads
|
|
10
|
-
|
|
11
|
-
This is the module most frontend and backend product teams use directly.
|
|
12
|
-
|
|
13
|
-
## Source map
|
|
14
|
-
|
|
15
|
-
- [`index.ts`](./index.ts) re-exports the public API surface
|
|
16
|
-
- [`order-helper.ts`](./order-helper.ts) wraps exchange helpers into an
|
|
17
|
-
integration-friendly class
|
|
18
|
-
- [`orderbook-api/index.ts`](./orderbook-api/index.ts) implements the HTTP
|
|
19
|
-
client
|
|
20
|
-
- [`orderbook-api/deserializers.ts`](./orderbook-api/deserializers.ts) converts
|
|
21
|
-
string-heavy responses into bigint-friendly client-side objects
|
|
22
|
-
- [`../shared/types.ts`](../shared/types.ts) defines the request and response
|
|
23
|
-
DTOs consumed by `OrderbookApi`
|
|
24
|
-
|
|
25
|
-
## OrderHelper
|
|
26
|
-
|
|
27
|
-
`OrderHelper` exists so most consumers do not need to manually stitch together
|
|
28
|
-
`buildExchangeOrder`, `getExchangeTypedData`, `hashExchangeOrder`, and the
|
|
29
|
-
serialization helpers.
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
import {
|
|
33
|
-
OrderHelper,
|
|
34
|
-
SignatureType,
|
|
35
|
-
TradeType,
|
|
36
|
-
} from "@stryke-xyz/premarket-sdk";
|
|
37
|
-
|
|
38
|
-
const helper = new OrderHelper({
|
|
39
|
-
chainId: 4326,
|
|
40
|
-
exchangeAddress: "0xCf24f40D2dd88084e9C28FE34Ba9E24AFDACb7C2",
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const order = helper.buildOrder({
|
|
44
|
-
maker: "0x1111111111111111111111111111111111111111",
|
|
45
|
-
receiver: "0x1111111111111111111111111111111111111111",
|
|
46
|
-
nonce: 0n,
|
|
47
|
-
marketId: 1n,
|
|
48
|
-
makingAmount: 1_000_000n,
|
|
49
|
-
takingAmount: 500_000n,
|
|
50
|
-
deadline: 1_900_000_000n,
|
|
51
|
-
tradeType: TradeType.SELL,
|
|
52
|
-
signatureType: SignatureType.EIP712,
|
|
53
|
-
tokenId: 42n,
|
|
54
|
-
});
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### Constructor config
|
|
58
|
-
|
|
59
|
-
`OrderHelperConfig`:
|
|
60
|
-
|
|
61
|
-
- `chainId`
|
|
62
|
-
- used for hashing and typed-data generation
|
|
63
|
-
- `exchangeAddress`
|
|
64
|
-
- the verifying contract used in EIP-712 signatures
|
|
65
|
-
|
|
66
|
-
### Public methods
|
|
67
|
-
|
|
68
|
-
- `buildOrder(params)`
|
|
69
|
-
- generic normalized order builder
|
|
70
|
-
- `buildSellOrder(params)`
|
|
71
|
-
- convenience helper that pins `tradeType = SELL`
|
|
72
|
-
- `buildBuyOrder(params)`
|
|
73
|
-
- convenience helper that pins `tradeType = BUY`
|
|
74
|
-
- `serializeOrder(order)`
|
|
75
|
-
- converts `ExchangeOrder` into `SerializedExchangeOrder`
|
|
76
|
-
- `hashOrder(order)`
|
|
77
|
-
- EIP-712 digest for the configured chain and exchange address
|
|
78
|
-
- `getTypedData(order)`
|
|
79
|
-
- typed-data payload suitable for a wallet signer
|
|
80
|
-
- `signEip712Order(order, walletClient)`
|
|
81
|
-
- only valid for `SignatureType.EIP712`
|
|
82
|
-
- `signSimpleAccountOrder(order, ownerWalletClient)`
|
|
83
|
-
- only valid for `SignatureType.ERC1271`
|
|
84
|
-
- intended for `SimpleAccount`-compatible flows where the maker contract
|
|
85
|
-
validates a raw owner signature against the native order hash
|
|
86
|
-
- `signOrder(order, walletClient)`
|
|
87
|
-
- compatibility alias for `signEip712Order`
|
|
88
|
-
- `recoverOrderSigner(order, signature)`
|
|
89
|
-
- recovers the address that signed the order payload
|
|
90
|
-
|
|
91
|
-
```ts
|
|
92
|
-
const signature = await helper.signEip712Order(order, walletClient);
|
|
93
|
-
const payloadOrder = helper.serializeOrder(order);
|
|
94
|
-
const signer = await helper.recoverOrderSigner(order, signature);
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
## OrderbookApi
|
|
98
|
-
|
|
99
|
-
`OrderbookApi` is a fetch-based client that understands the backend's envelope
|
|
100
|
-
format, normalizes URLs, surfaces clearer errors, and exposes typed return
|
|
101
|
-
values for the main public read and write flows.
|
|
102
|
-
|
|
103
|
-
```ts
|
|
104
|
-
import { OrderbookApi } from "@stryke-xyz/premarket-sdk";
|
|
105
|
-
|
|
106
|
-
const api = new OrderbookApi({
|
|
107
|
-
baseUrl: "https://example.stryke.xyz",
|
|
108
|
-
});
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### Constructor config
|
|
112
|
-
|
|
113
|
-
`OrderbookApiConfig` from [`../shared/types.ts`](../shared/types.ts):
|
|
114
|
-
|
|
115
|
-
- `baseUrl`
|
|
116
|
-
- root URL for the backend
|
|
117
|
-
- `fetchFn?`
|
|
118
|
-
- optional injected `fetch` implementation for tests or custom runtimes
|
|
119
|
-
|
|
120
|
-
## Order endpoints
|
|
121
|
-
|
|
122
|
-
Public order methods:
|
|
123
|
-
|
|
124
|
-
- `createOrder(params, bearerToken)`
|
|
125
|
-
- creates a new order with bearer auth
|
|
126
|
-
- `getOrder(orderHash)`
|
|
127
|
-
- returns `StoredOrder | null`
|
|
128
|
-
- `queryOrders(params)`
|
|
129
|
-
- list endpoint for orderbook snapshots
|
|
130
|
-
- `getUserOrders(maker, marketId)`
|
|
131
|
-
- market-scoped user order lookup
|
|
132
|
-
- `getDepthSnapshot(marketId, tokenId)`
|
|
133
|
-
- point-in-time depth snapshot for a market and token pair
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
const api = new OrderbookApi({ baseUrl: "https://example.stryke.xyz" });
|
|
137
|
-
|
|
138
|
-
await api.createOrder(
|
|
139
|
-
{
|
|
140
|
-
marketId: payloadOrder.marketId,
|
|
141
|
-
order: payloadOrder,
|
|
142
|
-
signature,
|
|
143
|
-
timeInForce: "GTC",
|
|
144
|
-
postOnly: false,
|
|
145
|
-
},
|
|
146
|
-
bearerToken,
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
const snapshot = await api.queryOrders({ marketId: "1", limit: 50 });
|
|
150
|
-
const mine = await api.getUserOrders(
|
|
151
|
-
"0x1111111111111111111111111111111111111111",
|
|
152
|
-
"1",
|
|
153
|
-
);
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
Important integration note:
|
|
157
|
-
|
|
158
|
-
- `getUserOrders` enforces `marketId` at runtime
|
|
159
|
-
- `queryOrders` accepts an optional `marketId` in its TypeScript type, but the
|
|
160
|
-
documented backend contract expects callers to provide one for reliable
|
|
161
|
-
integration behavior
|
|
162
|
-
|
|
163
|
-
## Market endpoints
|
|
164
|
-
|
|
165
|
-
Public market methods:
|
|
166
|
-
|
|
167
|
-
- `getMarkets()`
|
|
168
|
-
- returns `MarketsResponse["data"]`
|
|
169
|
-
- `getMarketRecentTrades(marketId, limit?)`
|
|
170
|
-
- returns `MarketTradeItem[]`
|
|
171
|
-
- `getMarket(marketId)`
|
|
172
|
-
- returns `MarketResponse["data"] | null`
|
|
173
|
-
|
|
174
|
-
```ts
|
|
175
|
-
const markets = await api.getMarkets();
|
|
176
|
-
const market = await api.getMarket("12");
|
|
177
|
-
const trades = await api.getMarketRecentTrades("12", 25);
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
## User positions and PnL
|
|
181
|
-
|
|
182
|
-
Public user analytics methods:
|
|
183
|
-
|
|
184
|
-
- `getUserPositions(userAddress)`
|
|
185
|
-
- `getUserTradingPnL(userAddress)`
|
|
186
|
-
- `getUserPnL(userAddress)`
|
|
187
|
-
- `getTokenPnL(userAddress, tokenId)`
|
|
188
|
-
- `getErc20PnL(userAddress, tokenAddress)`
|
|
189
|
-
|
|
190
|
-
These methods return the shared transport DTOs documented in the
|
|
191
|
-
[Shared types guide](../shared/README.md).
|
|
192
|
-
|
|
193
|
-
```ts
|
|
194
|
-
const positions = await api.getUserPositions(userAddress);
|
|
195
|
-
const trading = await api.getUserTradingPnL(userAddress);
|
|
196
|
-
const summary = await api.getUserPnL(userAddress);
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
## Histories
|
|
200
|
-
|
|
201
|
-
Public history methods:
|
|
202
|
-
|
|
203
|
-
- `getUserHistories(userAddress, limit?)`
|
|
204
|
-
- `getMintHistory(userAddress, limit?)`
|
|
205
|
-
- `getRedeemHistory(userAddress, limit?)`
|
|
206
|
-
- `getUnwindHistory(userAddress, limit?)`
|
|
207
|
-
- `getTransferHistory(userAddress, limit?)`
|
|
208
|
-
- `getFillHistory(userAddress, limit?)`
|
|
209
|
-
|
|
210
|
-
These methods are useful when a frontend needs user-facing activity feeds, or
|
|
211
|
-
when a backend wants typed access to grouped historical data without hand-rolled
|
|
212
|
-
endpoint wrappers.
|
|
213
|
-
|
|
214
|
-
## Deserializers
|
|
215
|
-
|
|
216
|
-
The API returns string-heavy DTOs because JSON cannot safely transport `bigint`
|
|
217
|
-
values. [`orderbook-api/deserializers.ts`](./orderbook-api/deserializers.ts)
|
|
218
|
-
provides opt-in conversion helpers for clients that prefer `bigint` values.
|
|
219
|
-
|
|
220
|
-
Public helpers:
|
|
221
|
-
|
|
222
|
-
- `marketInstrumentToBigInt`
|
|
223
|
-
- `marketToBigInt`
|
|
224
|
-
- `marketsToBigInt`
|
|
225
|
-
- `positionToBigInt`
|
|
226
|
-
- `tradingPnLToBigInt`
|
|
227
|
-
- `mintHistoryToBigInt`
|
|
228
|
-
- `redeemHistoryToBigInt`
|
|
229
|
-
- `unwindHistoryToBigInt`
|
|
230
|
-
- `transferHistoryToBigInt`
|
|
231
|
-
- `fillHistoryToBigInt`
|
|
232
|
-
|
|
233
|
-
```ts
|
|
234
|
-
import {
|
|
235
|
-
marketsToBigInt,
|
|
236
|
-
OrderbookApi,
|
|
237
|
-
} from "@stryke-xyz/premarket-sdk";
|
|
238
|
-
|
|
239
|
-
const api = new OrderbookApi({ baseUrl: "https://example.stryke.xyz" });
|
|
240
|
-
const marketData = await api.getMarkets();
|
|
241
|
-
const bigintMarketData = marketsToBigInt(marketData);
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
## Public request and response types
|
|
245
|
-
|
|
246
|
-
The API client relies on the DTOs exported from [`../shared/types.ts`](../shared/types.ts).
|
|
247
|
-
The most important public request and response shapes are:
|
|
248
|
-
|
|
249
|
-
- order write and read types
|
|
250
|
-
- `CreateOrderParams`
|
|
251
|
-
- `CreateOrderRequest`
|
|
252
|
-
- `StoredOrder`
|
|
253
|
-
- `OrderQueryParams`
|
|
254
|
-
- `OrdersSnapshot`
|
|
255
|
-
- `QueryOrdersResponse`
|
|
256
|
-
- `DepthSnapshot`
|
|
257
|
-
- market types
|
|
258
|
-
- `Market`
|
|
259
|
-
- `MarketInstrument`
|
|
260
|
-
- `MarketResponse`
|
|
261
|
-
- `MarketsResponse`
|
|
262
|
-
- `MarketTradeItem`
|
|
263
|
-
- user and analytics types
|
|
264
|
-
- `UserPosition`
|
|
265
|
-
- `TradingPnL`
|
|
266
|
-
- `UserPnL`
|
|
267
|
-
- `TokenPnL`
|
|
268
|
-
- `Erc20PnL`
|
|
269
|
-
- `UserHistories`
|
|
270
|
-
- `MintHistoryItem`
|
|
271
|
-
- `RedeemHistoryItem`
|
|
272
|
-
- `UnwindHistoryItem`
|
|
273
|
-
- `TransferHistoryItem`
|
|
274
|
-
- `OrderFillHistoryItem`
|
|
275
|
-
|
|
276
|
-
Those interfaces are broken down field-by-field in the
|
|
277
|
-
[Shared types guide](../shared/README.md).
|
|
278
|
-
|
|
279
|
-
## Restricted and sensitive surfaces
|
|
280
|
-
|
|
281
|
-
`OrderbookApi` also exposes auth helpers:
|
|
282
|
-
|
|
283
|
-
- `getChallenge({ address, chainId })`
|
|
284
|
-
- `verifyAuth({ account, nonce, signature, chainId, expiresAt })`
|
|
285
|
-
|
|
286
|
-
These methods are part of the public SDK surface, but this document does not
|
|
287
|
-
provide a field-by-field auth payload reference. The goal of the new docs is to
|
|
288
|
-
cover the general integration contract well without turning the repository docs
|
|
289
|
-
into an auth operations manual.
|
|
290
|
-
|
|
291
|
-
## How to choose between exchange and api modules
|
|
292
|
-
|
|
293
|
-
- use the [Exchange guide](../exchange/README.md) when you need low-level order
|
|
294
|
-
math, typed-data primitives, or calldata builders
|
|
295
|
-
- use this module when you want the ergonomics most applications need:
|
|
296
|
-
`OrderHelper`, `OrderbookApi`, and optional bigint deserializers
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
# Config Guide
|
|
2
|
-
|
|
3
|
-
The `config` module is the SDK's deployment and chain registry. It provides the
|
|
4
|
-
named chain objects, token metadata, and contract addresses that other modules
|
|
5
|
-
expect callers to use instead of hardcoding environment-specific values in each
|
|
6
|
-
app.
|
|
7
|
-
|
|
8
|
-
## Source map
|
|
9
|
-
|
|
10
|
-
- [`index.ts`](./index.ts) exports token metadata, contract addresses, and the
|
|
11
|
-
`CHAIN_ID_TO_CHAIN` lookup
|
|
12
|
-
- [`chains.ts`](./chains.ts) defines custom chain objects and the
|
|
13
|
-
`SUPPORTED_CHAINS` type
|
|
14
|
-
|
|
15
|
-
## What this module is for
|
|
16
|
-
|
|
17
|
-
In plain language:
|
|
18
|
-
|
|
19
|
-
- it centralizes deployed addresses
|
|
20
|
-
- it gives frontend and backend code a common view of supported chains
|
|
21
|
-
- it keeps order builders, API clients, and smart-account helpers pointed at
|
|
22
|
-
the same runtime contracts
|
|
23
|
-
|
|
24
|
-
## Chains
|
|
25
|
-
|
|
26
|
-
[`chains.ts`](./chains.ts) exports:
|
|
27
|
-
|
|
28
|
-
- `megaETH`
|
|
29
|
-
- custom viem chain definition for chain id `4326`
|
|
30
|
-
- `SUPPORTED_CHAINS`
|
|
31
|
-
- TypeScript union of the currently supported chain ids
|
|
32
|
-
|
|
33
|
-
[`index.ts`](./index.ts) then maps those ids into a runtime lookup:
|
|
34
|
-
|
|
35
|
-
- `CHAIN_ID_TO_CHAIN`
|
|
36
|
-
|
|
37
|
-
```ts
|
|
38
|
-
import {
|
|
39
|
-
CHAIN_ID_TO_CHAIN,
|
|
40
|
-
megaETH,
|
|
41
|
-
} from "@stryke-xyz/premarket-sdk";
|
|
42
|
-
|
|
43
|
-
const chain = CHAIN_ID_TO_CHAIN[megaETH.id];
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Token metadata
|
|
47
|
-
|
|
48
|
-
`Token` is the shared metadata shape for config entries:
|
|
49
|
-
|
|
50
|
-
```ts
|
|
51
|
-
export interface Token {
|
|
52
|
-
name: string;
|
|
53
|
-
symbol: string;
|
|
54
|
-
address: `0x${string}`;
|
|
55
|
-
decimals: number;
|
|
56
|
-
logoURI?: string;
|
|
57
|
-
}
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
Public token maps:
|
|
61
|
-
|
|
62
|
-
- `WETH`
|
|
63
|
-
- `USDC`
|
|
64
|
-
- `USDM`
|
|
65
|
-
- `USDT0`
|
|
66
|
-
|
|
67
|
-
Important nuance:
|
|
68
|
-
|
|
69
|
-
- `USDM`, `USDT0`, `MARKETS_REGISTRY`, `FEE_REGISTRY`, and
|
|
70
|
-
`ERC_TOKENS_RESTRICTION_MODULE` are partial maps because those deployments do
|
|
71
|
-
not exist on every supported chain
|
|
72
|
-
|
|
73
|
-
## Contract address maps
|
|
74
|
-
|
|
75
|
-
Public address maps:
|
|
76
|
-
|
|
77
|
-
- `PERMIT2_ADDRESS`
|
|
78
|
-
- `OPTION_MARKET_VAULT`
|
|
79
|
-
- `EXCHANGE`
|
|
80
|
-
- `MARKETS_REGISTRY`
|
|
81
|
-
- `ENTRY_POINT`
|
|
82
|
-
- `SIMPLE_ACCOUNT_FACTORY`
|
|
83
|
-
- `FEE_REGISTRY`
|
|
84
|
-
- `ERC_TOKENS_RESTRICTION_MODULE`
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
import {
|
|
88
|
-
EXCHANGE,
|
|
89
|
-
OPTION_MARKET_VAULT,
|
|
90
|
-
SIMPLE_ACCOUNT_FACTORY,
|
|
91
|
-
} from "@stryke-xyz/premarket-sdk";
|
|
92
|
-
|
|
93
|
-
const chainId = 4326;
|
|
94
|
-
|
|
95
|
-
const exchangeAddress = EXCHANGE[chainId];
|
|
96
|
-
const vaultAddress = OPTION_MARKET_VAULT[chainId];
|
|
97
|
-
const factoryAddress = SIMPLE_ACCOUNT_FACTORY[chainId];
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
## How this module fits with the rest of the SDK
|
|
101
|
-
|
|
102
|
-
- the [Exchange guide](../exchange/README.md) uses `EXCHANGE`
|
|
103
|
-
- the [Vault guide](../vault/README.md) uses `OPTION_MARKET_VAULT`
|
|
104
|
-
- the [Registry guide](../registry/README.md) uses `MARKETS_REGISTRY`
|
|
105
|
-
- the root package's smart-account helpers use `SIMPLE_ACCOUNT_FACTORY` and
|
|
106
|
-
`ENTRY_POINT`
|
|
107
|
-
|
|
108
|
-
## Operational guidance
|
|
109
|
-
|
|
110
|
-
If upstream deployments change, this module should be updated before app teams
|
|
111
|
-
scatter new addresses across their own repositories. This is one of the main
|
|
112
|
-
places where the SDK adds value as an integration boundary.
|
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
# Exchange Guide
|
|
2
|
-
|
|
3
|
-
The `exchange` module is the trading core of the SDK. It models the native
|
|
4
|
-
order format used by Stryke's `Exchange` contract, reproduces its EIP-712
|
|
5
|
-
signing schema, mirrors the most important fee and crossing math, and encodes
|
|
6
|
-
calldata for fills, matches, cancellation, and batching.
|
|
7
|
-
|
|
8
|
-
For most trading integrations this is the first module to understand.
|
|
9
|
-
|
|
10
|
-
## Source map
|
|
11
|
-
|
|
12
|
-
- [`index.ts`](./index.ts) re-exports the public exchange surface
|
|
13
|
-
- [`types.ts`](./types.ts) defines in-memory and transport-safe order types
|
|
14
|
-
- [`order.ts`](./order.ts) builds and validates orders
|
|
15
|
-
- [`eip712.ts`](./eip712.ts) defines the domain, typed data, hashing, and signer
|
|
16
|
-
recovery helpers
|
|
17
|
-
- [`math.ts`](./math.ts) mirrors pricing, fee, and crossing math
|
|
18
|
-
- [`exchange-contract.ts`](./exchange-contract.ts) encodes calldata for the
|
|
19
|
-
shipped `Exchange` ABI
|
|
20
|
-
- [`errors.ts`](./errors.ts) decodes custom errors from core protocol ABIs
|
|
21
|
-
|
|
22
|
-
## What this module is for
|
|
23
|
-
|
|
24
|
-
In plain language:
|
|
25
|
-
|
|
26
|
-
- it tells wallets exactly what they are signing
|
|
27
|
-
- it gives resolvers and frontends a stable order shape
|
|
28
|
-
- it lets backend and UI code compute prices and fill amounts the same way the
|
|
29
|
-
contract does
|
|
30
|
-
- it builds transaction payloads without each consumer bundling its own ABI
|
|
31
|
-
|
|
32
|
-
## Core order model
|
|
33
|
-
|
|
34
|
-
The canonical in-memory order type is [`ExchangeOrder`](./types.ts). Every
|
|
35
|
-
numeric field is a `bigint`, so callers do not lose precision between order
|
|
36
|
-
construction and signature generation.
|
|
37
|
-
|
|
38
|
-
```ts
|
|
39
|
-
export interface ExchangeOrder {
|
|
40
|
-
salt: bigint;
|
|
41
|
-
nonce: bigint;
|
|
42
|
-
marketId: bigint;
|
|
43
|
-
makingAmount: bigint;
|
|
44
|
-
takingAmount: bigint;
|
|
45
|
-
deadline: bigint;
|
|
46
|
-
maker: Address;
|
|
47
|
-
receiver: Address;
|
|
48
|
-
tradeType: TradeType;
|
|
49
|
-
signatureType: SignatureType;
|
|
50
|
-
tokenId: bigint;
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
Related public types:
|
|
55
|
-
|
|
56
|
-
- `TradeType`
|
|
57
|
-
- `BUY = 0`
|
|
58
|
-
- `SELL = 1`
|
|
59
|
-
- `SignatureType`
|
|
60
|
-
- `EIP712 = 0`
|
|
61
|
-
- `ERC1271 = 1`
|
|
62
|
-
- `SerializedExchangeOrder`
|
|
63
|
-
- string-safe transport version for HTTP or persistence
|
|
64
|
-
- `ExchangeOrderStatus`
|
|
65
|
-
- `{ isFilledOrCancelled, remaining }`
|
|
66
|
-
- `SerializedExchangeOrderStatus`
|
|
67
|
-
- string-safe status payload
|
|
68
|
-
- `MulticallResult`
|
|
69
|
-
- helper type for multicall consumers
|
|
70
|
-
|
|
71
|
-
## Building orders
|
|
72
|
-
|
|
73
|
-
`buildExchangeOrder` in [`order.ts`](./order.ts) normalizes an order before it
|
|
74
|
-
is signed or serialized:
|
|
75
|
-
|
|
76
|
-
- `salt` defaults to a random 96-bit-compatible value
|
|
77
|
-
- `receiver` defaults to `maker`
|
|
78
|
-
- `signatureType` defaults to `SignatureType.EIP712`
|
|
79
|
-
- `makingAmount`, `takingAmount`, and `deadline` must all be positive
|
|
80
|
-
|
|
81
|
-
```ts
|
|
82
|
-
import {
|
|
83
|
-
SignatureType,
|
|
84
|
-
TradeType,
|
|
85
|
-
buildExchangeOrder,
|
|
86
|
-
} from "@stryke-xyz/premarket-sdk";
|
|
87
|
-
|
|
88
|
-
const order = buildExchangeOrder({
|
|
89
|
-
maker: "0x1111111111111111111111111111111111111111",
|
|
90
|
-
nonce: 12n,
|
|
91
|
-
marketId: 7n,
|
|
92
|
-
makingAmount: 1_000_000n,
|
|
93
|
-
takingAmount: 500_000n,
|
|
94
|
-
deadline: 1_900_000_000n,
|
|
95
|
-
tradeType: TradeType.SELL,
|
|
96
|
-
signatureType: SignatureType.EIP712,
|
|
97
|
-
tokenId: 123456n,
|
|
98
|
-
});
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
Public builder and validation helpers:
|
|
102
|
-
|
|
103
|
-
- `buildExchangeOrder(params)`
|
|
104
|
-
- returns a normalized `ExchangeOrder`
|
|
105
|
-
- `validateExchangeOrder(order)`
|
|
106
|
-
- enforces the minimal numeric invariants expected by the contract
|
|
107
|
-
- `isOrderExpired(order, nowSec?)`
|
|
108
|
-
- compares `deadline` against a supplied or current unix timestamp
|
|
109
|
-
- `getExecutableMakingAmount(order, status)`
|
|
110
|
-
- resolves the important status edge case where
|
|
111
|
-
`remaining === 0 && !isFilledOrCancelled` means the order is still fully
|
|
112
|
-
open
|
|
113
|
-
- `getExchangeOrderHash(order, chainId, exchangeAddress)`
|
|
114
|
-
- convenience wrapper around the typed-data hashing helper
|
|
115
|
-
|
|
116
|
-
## Signing and hashing
|
|
117
|
-
|
|
118
|
-
The EIP-712 implementation in [`eip712.ts`](./eip712.ts) is the contract-aligned
|
|
119
|
-
source of truth for signed order payloads.
|
|
120
|
-
|
|
121
|
-
Important constants:
|
|
122
|
-
|
|
123
|
-
- `EXCHANGE_EIP712_NAME = "Exchange"`
|
|
124
|
-
- `EXCHANGE_EIP712_VERSION = "1"`
|
|
125
|
-
- `EXCHANGE_ORDER_TYPES`
|
|
126
|
-
- the exact typed-data fields and enum serialization used in signatures
|
|
127
|
-
|
|
128
|
-
Public helpers:
|
|
129
|
-
|
|
130
|
-
- `getExchangeDomain(chainId, verifyingContract)`
|
|
131
|
-
- `getExchangeTypedData(order, chainId, verifyingContract)`
|
|
132
|
-
- `hashExchangeOrder(order, chainId, verifyingContract)`
|
|
133
|
-
- `recoverExchangeOrderSigner(order, signature, chainId, verifyingContract)`
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
import {
|
|
137
|
-
getExchangeTypedData,
|
|
138
|
-
hashExchangeOrder,
|
|
139
|
-
} from "@stryke-xyz/premarket-sdk";
|
|
140
|
-
|
|
141
|
-
const typedData = getExchangeTypedData(order, 4326, exchangeAddress);
|
|
142
|
-
const digest = hashExchangeOrder(order, 4326, exchangeAddress);
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Use these helpers whenever you need exact parity with onchain signature checks.
|
|
146
|
-
That includes CI, frontend signing, backend verification, and smart-account
|
|
147
|
-
flows that still sign the native `Exchange` order shape.
|
|
148
|
-
|
|
149
|
-
## Serialization helpers
|
|
150
|
-
|
|
151
|
-
`serializeExchangeOrder` and `deserializeExchangeOrder` in [`types.ts`](./types.ts)
|
|
152
|
-
bridge the two most common representations:
|
|
153
|
-
|
|
154
|
-
- `ExchangeOrder` for local calculations and signing
|
|
155
|
-
- `SerializedExchangeOrder` for APIs, storage, and JSON payloads
|
|
156
|
-
|
|
157
|
-
Likewise, `serializeOrderStatus` and `deserializeOrderStatus` make it safe to
|
|
158
|
-
transport status objects without losing `bigint` precision.
|
|
159
|
-
|
|
160
|
-
## Math helpers
|
|
161
|
-
|
|
162
|
-
[`math.ts`](./math.ts) contains the SDK's exchange-side arithmetic helpers.
|
|
163
|
-
These are intentionally small, deterministic, and contract-aligned.
|
|
164
|
-
|
|
165
|
-
Public constants:
|
|
166
|
-
|
|
167
|
-
- `EXCHANGE_ONE = 10n ** 18n`
|
|
168
|
-
- `FEE_RATE_BASE = 1_000_000n`
|
|
169
|
-
|
|
170
|
-
Public math functions:
|
|
171
|
-
|
|
172
|
-
- `getTakingAmount(fillMakingAmount, orderMakingAmount, orderTakingAmount)`
|
|
173
|
-
- floor division from maker-side fill amount to taker-side fill amount
|
|
174
|
-
- `getMakingAmount(fillTakingAmount, orderMakingAmount, orderTakingAmount)`
|
|
175
|
-
- inverse of the above, also using floor division
|
|
176
|
-
- `calculateFee(grossAmount, feeRate)`
|
|
177
|
-
- validates `feeRate` is within `[0, 1e6]`
|
|
178
|
-
- `applyFee(grossAmount, feeRate)`
|
|
179
|
-
- returns `{ fee, net }`
|
|
180
|
-
- `getOrderPriceWad(order)`
|
|
181
|
-
- normalizes price into 1e18 precision
|
|
182
|
-
- `optionPrmToPrmId(tokenId)`
|
|
183
|
-
- canonicalizes `oPRM` token ids to the underlying even `PRM` id
|
|
184
|
-
- `isCrossing(orderA, orderB)`
|
|
185
|
-
- detects valid crosses for complementary trades and mint/merge style matches
|
|
186
|
-
- `hasValidTokenPairForMatch(orderA, orderB)`
|
|
187
|
-
- enforces token pairing rules for cross, mint, and merge cases
|
|
188
|
-
|
|
189
|
-
```ts
|
|
190
|
-
import {
|
|
191
|
-
applyFee,
|
|
192
|
-
getTakingAmount,
|
|
193
|
-
isCrossing,
|
|
194
|
-
} from "@stryke-xyz/premarket-sdk";
|
|
195
|
-
|
|
196
|
-
const takingAmount = getTakingAmount(250_000n, order.makingAmount, order.takingAmount);
|
|
197
|
-
const { fee, net } = applyFee(takingAmount, 2_000n);
|
|
198
|
-
const crossing = isCrossing(buyOrder, sellOrder);
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
## Calldata builders
|
|
202
|
-
|
|
203
|
-
`ExchangeContract` in [`exchange-contract.ts`](./exchange-contract.ts) wraps the
|
|
204
|
-
shipped ABI and returns either raw calldata or a minimal transaction envelope.
|
|
205
|
-
|
|
206
|
-
```ts
|
|
207
|
-
import { ExchangeContract } from "@stryke-xyz/premarket-sdk";
|
|
208
|
-
|
|
209
|
-
const exchange = new ExchangeContract(exchangeAddress);
|
|
210
|
-
|
|
211
|
-
const fillTx = exchange.buildFillOrderTx(order, 500_000n, signature);
|
|
212
|
-
const matchData = exchange.getMatchOrderCalldata(
|
|
213
|
-
takerOrder,
|
|
214
|
-
takerSignature,
|
|
215
|
-
makerOrder,
|
|
216
|
-
makerSignature,
|
|
217
|
-
100_000n,
|
|
218
|
-
100_000n,
|
|
219
|
-
);
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
Public methods:
|
|
223
|
-
|
|
224
|
-
- `getFillOrderCalldata(order, fillAmount, signature)`
|
|
225
|
-
- `buildFillOrderTx(order, fillAmount, signature)`
|
|
226
|
-
- `getMatchOrderCalldata(takerOrder, takerSignature, makerOrder, makerSignature, takerFillAmount, makerFillAmount)`
|
|
227
|
-
- `buildMatchOrderTx(takerOrder, takerSignature, makerOrder, makerSignature, takerFillAmount, makerFillAmount)`
|
|
228
|
-
- `getCancelOrderCalldata(order)`
|
|
229
|
-
- `getIncrementNonceCalldata()`
|
|
230
|
-
- `getSetResolverWhitelistCalldata(resolver, isWhitelisted)`
|
|
231
|
-
- `getSetFeeReceiverCalldata(newFeeReceiver)`
|
|
232
|
-
- `getPauseCalldata()`
|
|
233
|
-
- `getUnpauseCalldata()`
|
|
234
|
-
- `getMulticallCalldata(data, allowFailure?)`
|
|
235
|
-
|
|
236
|
-
`buildFillOrderTx` and `buildMatchOrderTx` are the only helpers that currently
|
|
237
|
-
return a full `{ to, data, value }` transaction object. The remaining methods
|
|
238
|
-
return encoded calldata so the caller can place them inside a broader tx or
|
|
239
|
-
user operation.
|
|
240
|
-
|
|
241
|
-
Restricted/admin surfaces:
|
|
242
|
-
|
|
243
|
-
- `getSetResolverWhitelistCalldata`
|
|
244
|
-
- `getSetFeeReceiverCalldata`
|
|
245
|
-
- `getPauseCalldata`
|
|
246
|
-
- `getUnpauseCalldata`
|
|
247
|
-
|
|
248
|
-
These are documented here because they are part of the export surface, but they
|
|
249
|
-
are operationally restricted and should not be treated as general-user flows.
|
|
250
|
-
|
|
251
|
-
## Error decoding
|
|
252
|
-
|
|
253
|
-
`decodeContractError` in [`errors.ts`](./errors.ts) attempts to decode a revert
|
|
254
|
-
payload against the shipped `Exchange`, `OptionMarketVault`, and
|
|
255
|
-
`MarketsRegistry` ABIs.
|
|
256
|
-
|
|
257
|
-
It returns either `null` or:
|
|
258
|
-
|
|
259
|
-
```ts
|
|
260
|
-
interface DecodedContractError {
|
|
261
|
-
contract: "exchange" | "optionMarketVault" | "marketsRegistry";
|
|
262
|
-
name: string;
|
|
263
|
-
signature: string;
|
|
264
|
-
args: readonly unknown[];
|
|
265
|
-
}
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
This is useful for frontend error messaging, backend diagnostics, and test
|
|
269
|
-
assertions where raw revert bytes are not enough.
|
|
270
|
-
|
|
271
|
-
## Recommended integration pattern
|
|
272
|
-
|
|
273
|
-
1. Build a normalized order with `buildExchangeOrder` or `OrderHelper`.
|
|
274
|
-
2. Hash or sign using the typed-data helpers.
|
|
275
|
-
3. Serialize the order before sending it to the API.
|
|
276
|
-
4. Use `ExchangeContract` to build fill or match calldata.
|
|
277
|
-
5. Use `decodeContractError` when surfacing or logging contract reverts.
|
|
278
|
-
|
|
279
|
-
If you want the higher-level workflow wrapper that bundles these steps together,
|
|
280
|
-
continue with the [API guide](../api/README.md), which documents `OrderHelper`.
|