openbroker 1.9.5 → 1.10.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 +19 -0
- package/README.md +6 -4
- package/SKILL.md +14 -5
- package/dist/core/client.d.ts +63 -3
- package/dist/core/client.d.ts.map +1 -1
- package/dist/core/client.js +246 -11
- package/dist/core/utils.d.ts +29 -0
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/core/utils.js +27 -0
- package/dist/lib.d.ts +4 -1
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +2 -1
- package/dist/operations/advanced-orders.test.d.ts +2 -0
- package/dist/operations/advanced-orders.test.d.ts.map +1 -0
- package/dist/operations/advanced-orders.test.js +478 -0
- package/dist/operations/bracket.d.ts +55 -3
- package/dist/operations/bracket.d.ts.map +1 -1
- package/dist/operations/bracket.js +324 -117
- package/dist/operations/chase.d.ts +20 -1
- package/dist/operations/chase.d.ts.map +1 -1
- package/dist/operations/chase.js +169 -67
- package/dist/operations/execution.d.ts +53 -0
- package/dist/operations/execution.d.ts.map +1 -0
- package/dist/operations/execution.js +106 -0
- package/dist/operations/scale.d.ts +50 -1
- package/dist/operations/scale.d.ts.map +1 -1
- package/dist/operations/scale.js +152 -105
- package/dist/operations/set-tpsl.js +69 -57
- package/dist/operations/trigger-order.js +18 -6
- package/dist/operations/twap.js +13 -1
- package/package.json +3 -2
- package/scripts/core/client.ts +320 -10
- package/scripts/core/utils.ts +37 -0
- package/scripts/lib.ts +6 -0
- package/scripts/operations/advanced-orders.test.ts +542 -0
- package/scripts/operations/bracket.ts +350 -115
- package/scripts/operations/chase.ts +183 -73
- package/scripts/operations/execution.ts +138 -0
- package/scripts/operations/scale.ts +195 -131
- package/scripts/operations/set-tpsl.ts +62 -57
- package/scripts/operations/trigger-order.ts +20 -6
- package/scripts/operations/twap.ts +14 -1
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { runBracket, type BracketClient } from './bracket.js';
|
|
4
|
+
import { runChase, type ChaseClient } from './chase.js';
|
|
5
|
+
import { runScale, type ScaleClient } from './scale.js';
|
|
6
|
+
import { UserFillWatcher, type FillSummary, type FillWatcher } from './execution.js';
|
|
7
|
+
import type { CancelResponse, OpenOrder, OrderResponse } from '../core/types.js';
|
|
8
|
+
|
|
9
|
+
const okOrder = (statuses: OrderResponse['response'] extends infer R
|
|
10
|
+
? R extends { data: { statuses: infer S } } ? S : never
|
|
11
|
+
: never): OrderResponse => ({
|
|
12
|
+
status: 'ok',
|
|
13
|
+
response: { type: 'order', data: { statuses } },
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const okCancel = (): CancelResponse => ({
|
|
17
|
+
status: 'ok',
|
|
18
|
+
response: { type: 'cancel', data: { statuses: ['success'] } },
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
class StaticFillWatcher implements FillWatcher {
|
|
22
|
+
constructor(private readonly fills: Map<number, FillSummary>) {}
|
|
23
|
+
async start(): Promise<void> {}
|
|
24
|
+
async stop(): Promise<void> {}
|
|
25
|
+
getFilled(oid: number): FillSummary {
|
|
26
|
+
return this.fills.get(oid) ?? { size: 0, notional: 0 };
|
|
27
|
+
}
|
|
28
|
+
async waitForFill(oid: number): Promise<FillSummary> {
|
|
29
|
+
return this.getFilled(oid);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function bracketClientStub(overrides: Partial<BracketClient> = {}): BracketClient {
|
|
34
|
+
return {
|
|
35
|
+
verbose: false,
|
|
36
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
37
|
+
async getAllMids() {
|
|
38
|
+
return { ETH: '1000' };
|
|
39
|
+
},
|
|
40
|
+
async marketOrder() {
|
|
41
|
+
throw new Error('marketOrder should not be called');
|
|
42
|
+
},
|
|
43
|
+
async limitOrder() {
|
|
44
|
+
throw new Error('limitOrder should not be called');
|
|
45
|
+
},
|
|
46
|
+
async tpslOrders() {
|
|
47
|
+
throw new Error('tpslOrders should not be called');
|
|
48
|
+
},
|
|
49
|
+
async bracketOrder() {
|
|
50
|
+
throw new Error('bracketOrder should not be called');
|
|
51
|
+
},
|
|
52
|
+
async cancel() {
|
|
53
|
+
throw new Error('cancel should not be called');
|
|
54
|
+
},
|
|
55
|
+
async getUserFills() {
|
|
56
|
+
return [];
|
|
57
|
+
},
|
|
58
|
+
...overrides,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
test('scale rejects invalid levels and rolls back resting orders on partial ladder placement', async () => {
|
|
63
|
+
const cancelled: Array<{ coin: string; oid: number }> = [];
|
|
64
|
+
const client: ScaleClient = {
|
|
65
|
+
verbose: false,
|
|
66
|
+
async getAllMids() {
|
|
67
|
+
return { ETH: '1000' };
|
|
68
|
+
},
|
|
69
|
+
async bulkOrder() {
|
|
70
|
+
return okOrder([
|
|
71
|
+
{ resting: { oid: 101 } },
|
|
72
|
+
{ error: 'insufficient margin' },
|
|
73
|
+
{ resting: { oid: 103 } },
|
|
74
|
+
]);
|
|
75
|
+
},
|
|
76
|
+
async bulkCancel(cancels) {
|
|
77
|
+
cancelled.push(...cancels);
|
|
78
|
+
return okCancel();
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
await assert.rejects(
|
|
83
|
+
() => runScale({
|
|
84
|
+
coin: 'ETH',
|
|
85
|
+
side: 'buy',
|
|
86
|
+
size: 1,
|
|
87
|
+
levels: 0,
|
|
88
|
+
rangePct: 2,
|
|
89
|
+
client,
|
|
90
|
+
output: () => {},
|
|
91
|
+
}),
|
|
92
|
+
/levels must be a positive integer/,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
const result = await runScale({
|
|
96
|
+
coin: 'ETH',
|
|
97
|
+
side: 'buy',
|
|
98
|
+
size: 1,
|
|
99
|
+
levels: 3,
|
|
100
|
+
rangePct: 2,
|
|
101
|
+
client,
|
|
102
|
+
output: () => {},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
assert.equal(result.status, 'partial');
|
|
106
|
+
assert.equal(result.rolledBack, true);
|
|
107
|
+
assert.deepEqual(cancelled, [
|
|
108
|
+
{ coin: 'ETH', oid: 101 },
|
|
109
|
+
{ coin: 'ETH', oid: 103 },
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('scale pre-checks the $10 exchange minimum on the thinnest level (waived for reduce-only)', async () => {
|
|
114
|
+
let placed = false;
|
|
115
|
+
const client: ScaleClient = {
|
|
116
|
+
verbose: false,
|
|
117
|
+
async getAllMids() {
|
|
118
|
+
return { ETH: '1000' };
|
|
119
|
+
},
|
|
120
|
+
async bulkOrder(orders) {
|
|
121
|
+
placed = true;
|
|
122
|
+
return okOrder(orders.map((_, i) => ({ resting: { oid: 200 + i } })));
|
|
123
|
+
},
|
|
124
|
+
async bulkCancel() {
|
|
125
|
+
return okCancel();
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// Total ~$30 over 3 linear levels → thinnest level ~$5 < $10.
|
|
130
|
+
await assert.rejects(
|
|
131
|
+
() => runScale({
|
|
132
|
+
coin: 'ETH',
|
|
133
|
+
side: 'buy',
|
|
134
|
+
size: 0.03,
|
|
135
|
+
levels: 3,
|
|
136
|
+
rangePct: 2,
|
|
137
|
+
client,
|
|
138
|
+
output: () => {},
|
|
139
|
+
}),
|
|
140
|
+
/below the \$10 exchange minimum/,
|
|
141
|
+
);
|
|
142
|
+
assert.equal(placed, false);
|
|
143
|
+
|
|
144
|
+
// Same ladder reduce-only is allowed (exchange waives the minimum).
|
|
145
|
+
const result = await runScale({
|
|
146
|
+
coin: 'ETH',
|
|
147
|
+
side: 'buy',
|
|
148
|
+
size: 0.03,
|
|
149
|
+
levels: 3,
|
|
150
|
+
rangePct: 2,
|
|
151
|
+
reduceOnly: true,
|
|
152
|
+
client,
|
|
153
|
+
output: () => {},
|
|
154
|
+
});
|
|
155
|
+
assert.equal(result.status, 'complete');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test('bracket limit entry defaults to one atomic normalTpsl batch', async () => {
|
|
159
|
+
const calls: Array<{ coin: string; isBuy: boolean; size: number; entryPrice: number; opts: Record<string, unknown> }> = [];
|
|
160
|
+
const client = bracketClientStub({
|
|
161
|
+
async bracketOrder(coin, isBuy, size, entryPrice, opts) {
|
|
162
|
+
calls.push({ coin, isBuy, size, entryPrice, opts: opts ?? {} });
|
|
163
|
+
return okOrder([
|
|
164
|
+
{ resting: { oid: 500 } },
|
|
165
|
+
'waitingForFill' as never,
|
|
166
|
+
'waitingForFill' as never,
|
|
167
|
+
]);
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const result = await runBracket({
|
|
172
|
+
coin: 'ETH',
|
|
173
|
+
side: 'buy',
|
|
174
|
+
size: 1,
|
|
175
|
+
tpPct: 5,
|
|
176
|
+
slPct: 2,
|
|
177
|
+
entryType: 'limit',
|
|
178
|
+
entryPrice: 990,
|
|
179
|
+
client,
|
|
180
|
+
output: () => {},
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
assert.equal(result.status, 'armed');
|
|
184
|
+
assert.equal(result.entryOid, 500);
|
|
185
|
+
assert.equal(calls.length, 1);
|
|
186
|
+
assert.equal(calls[0].isBuy, true);
|
|
187
|
+
assert.equal(calls[0].entryPrice, 990);
|
|
188
|
+
assert.equal(calls[0].opts.takeProfitPrice, 990 * 1.05);
|
|
189
|
+
assert.equal(calls[0].opts.stopLossPrice, 990 * 0.98);
|
|
190
|
+
assert.equal(calls[0].opts.stopLossIsMarket, true);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('bracket atomic batch rolls back resting orders when a leg is rejected', async () => {
|
|
194
|
+
const cancelled: number[] = [];
|
|
195
|
+
const client = bracketClientStub({
|
|
196
|
+
async bracketOrder() {
|
|
197
|
+
return okOrder([
|
|
198
|
+
{ resting: { oid: 600 } },
|
|
199
|
+
'waitingForFill' as never,
|
|
200
|
+
{ error: 'Invalid TP/SL price.' },
|
|
201
|
+
]);
|
|
202
|
+
},
|
|
203
|
+
async cancel(_coin, oid) {
|
|
204
|
+
cancelled.push(oid);
|
|
205
|
+
return okCancel();
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const result = await runBracket({
|
|
210
|
+
coin: 'ETH',
|
|
211
|
+
side: 'buy',
|
|
212
|
+
size: 1,
|
|
213
|
+
tpPct: 5,
|
|
214
|
+
slPct: 2,
|
|
215
|
+
entryType: 'limit',
|
|
216
|
+
entryPrice: 990,
|
|
217
|
+
client,
|
|
218
|
+
output: () => {},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
assert.equal(result.status, 'entry_failed');
|
|
222
|
+
assert.match(result.reason ?? '', /Invalid TP\/SL price/);
|
|
223
|
+
assert.deepEqual(cancelled, [600]);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test('bracket with --no-atomic waits for limit-entry fill and arms positionTpsl for confirmed size', async () => {
|
|
227
|
+
const pairCalls: Array<{ size: number; isBuy: boolean; opts: Record<string, unknown> }> = [];
|
|
228
|
+
const client = bracketClientStub({
|
|
229
|
+
async limitOrder() {
|
|
230
|
+
return okOrder([{ resting: { oid: 777 } }]);
|
|
231
|
+
},
|
|
232
|
+
async tpslOrders(_coin, isBuy, size, opts) {
|
|
233
|
+
pairCalls.push({ size, isBuy, opts: opts ?? {} });
|
|
234
|
+
return okOrder([{ resting: { oid: 778 } }, { resting: { oid: 779 } }]);
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const fillWatcher = new StaticFillWatcher(new Map([
|
|
239
|
+
[777, { size: 0.4, notional: 392, avgPrice: 980 }],
|
|
240
|
+
]));
|
|
241
|
+
|
|
242
|
+
const result = await runBracket({
|
|
243
|
+
coin: 'ETH',
|
|
244
|
+
side: 'buy',
|
|
245
|
+
size: 1,
|
|
246
|
+
tpPct: 5,
|
|
247
|
+
slPct: 2,
|
|
248
|
+
entryType: 'limit',
|
|
249
|
+
entryPrice: 990,
|
|
250
|
+
entryTimeoutSec: 5,
|
|
251
|
+
atomic: false,
|
|
252
|
+
client,
|
|
253
|
+
fillWatcher,
|
|
254
|
+
output: () => {},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
assert.equal(result.status, 'complete');
|
|
258
|
+
assert.equal(result.protectedSize, 0.4);
|
|
259
|
+
assert.equal(result.entryPrice, 980);
|
|
260
|
+
assert.equal(pairCalls.length, 1);
|
|
261
|
+
assert.equal(pairCalls[0].size, 0.4);
|
|
262
|
+
assert.equal(pairCalls[0].isBuy, false);
|
|
263
|
+
assert.equal(pairCalls[0].opts.grouping, 'positionTpsl');
|
|
264
|
+
assert.equal(pairCalls[0].opts.takeProfitPrice, 1029);
|
|
265
|
+
assert.equal(pairCalls[0].opts.stopLossPrice, 960.4);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test('bracket market entry supports a one-sided TP sized to the actual fill', async () => {
|
|
269
|
+
const calls: Array<{ isBuy: boolean; size: number; opts: Record<string, unknown> }> = [];
|
|
270
|
+
const client = bracketClientStub({
|
|
271
|
+
async marketOrder() {
|
|
272
|
+
return okOrder([{ filled: { totalSz: '0.8', avgPx: '1001', oid: 1 } }]);
|
|
273
|
+
},
|
|
274
|
+
async tpslOrders(_coin, isBuy, size, opts) {
|
|
275
|
+
calls.push({ isBuy, size, opts: opts ?? {} });
|
|
276
|
+
return okOrder([{ resting: { oid: 2 } }]);
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const result = await runBracket({
|
|
281
|
+
coin: 'ETH',
|
|
282
|
+
side: 'buy',
|
|
283
|
+
size: 1,
|
|
284
|
+
tpPct: 5,
|
|
285
|
+
entryType: 'market',
|
|
286
|
+
client,
|
|
287
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
288
|
+
output: () => {},
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
assert.equal(result.status, 'complete');
|
|
292
|
+
assert.equal(result.protectedSize, 0.8);
|
|
293
|
+
assert.equal(calls.length, 1);
|
|
294
|
+
assert.equal(calls[0].size, 0.8);
|
|
295
|
+
assert.equal(calls[0].isBuy, false);
|
|
296
|
+
assert.equal(calls[0].opts.grouping, 'positionTpsl');
|
|
297
|
+
assert.equal(calls[0].opts.takeProfitPrice, 1001 * 1.05);
|
|
298
|
+
assert.equal(calls[0].opts.stopLossPrice, undefined);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('bracket validates targets before any order goes out', async () => {
|
|
302
|
+
// SL% ≥ 100 resolves to a negative price.
|
|
303
|
+
await assert.rejects(
|
|
304
|
+
() => runBracket({
|
|
305
|
+
coin: 'ETH',
|
|
306
|
+
side: 'buy',
|
|
307
|
+
size: 1,
|
|
308
|
+
tpPct: 5,
|
|
309
|
+
slPct: 150,
|
|
310
|
+
client: bracketClientStub(),
|
|
311
|
+
output: () => {},
|
|
312
|
+
}),
|
|
313
|
+
/SL must resolve to a positive price/,
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
// Absolute TP on the wrong side of the entry.
|
|
317
|
+
await assert.rejects(
|
|
318
|
+
() => runBracket({
|
|
319
|
+
coin: 'ETH',
|
|
320
|
+
side: 'buy',
|
|
321
|
+
size: 1,
|
|
322
|
+
tpPrice: 900,
|
|
323
|
+
entryType: 'limit',
|
|
324
|
+
entryPrice: 990,
|
|
325
|
+
client: bracketClientStub(),
|
|
326
|
+
output: () => {},
|
|
327
|
+
}),
|
|
328
|
+
/TP price must be above entry/,
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
// No target at all.
|
|
332
|
+
await assert.rejects(
|
|
333
|
+
() => runBracket({
|
|
334
|
+
coin: 'ETH',
|
|
335
|
+
side: 'buy',
|
|
336
|
+
size: 1,
|
|
337
|
+
client: bracketClientStub(),
|
|
338
|
+
output: () => {},
|
|
339
|
+
}),
|
|
340
|
+
/provide a TP target, an SL target, or both/,
|
|
341
|
+
);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test('chase requotes only the remaining size after a partial fill', async () => {
|
|
345
|
+
const limitSizes: number[] = [];
|
|
346
|
+
const cancelled: number[] = [];
|
|
347
|
+
const fills = new Map<number, FillSummary>();
|
|
348
|
+
let orderCount = 0;
|
|
349
|
+
let mid = 1000;
|
|
350
|
+
|
|
351
|
+
const client: ChaseClient = {
|
|
352
|
+
verbose: false,
|
|
353
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
354
|
+
async getAllMids() {
|
|
355
|
+
mid += 2;
|
|
356
|
+
return { ETH: String(mid) };
|
|
357
|
+
},
|
|
358
|
+
async getOpenOrders(): Promise<OpenOrder[]> {
|
|
359
|
+
return [];
|
|
360
|
+
},
|
|
361
|
+
async getUserFills() {
|
|
362
|
+
return [];
|
|
363
|
+
},
|
|
364
|
+
async limitOrder(_coin, _isBuy, size) {
|
|
365
|
+
limitSizes.push(size);
|
|
366
|
+
orderCount += 1;
|
|
367
|
+
const oid = 900 + orderCount;
|
|
368
|
+
if (orderCount === 1) {
|
|
369
|
+
fills.set(oid, { size: 0.4, notional: 400, avgPrice: 1000 });
|
|
370
|
+
} else {
|
|
371
|
+
fills.set(oid, { size, notional: size * 1002, avgPrice: 1002 });
|
|
372
|
+
}
|
|
373
|
+
return okOrder([{ resting: { oid } }]);
|
|
374
|
+
},
|
|
375
|
+
async cancel(_coin, oid) {
|
|
376
|
+
cancelled.push(oid);
|
|
377
|
+
return okCancel();
|
|
378
|
+
},
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
const result = await runChase({
|
|
382
|
+
coin: 'ETH',
|
|
383
|
+
side: 'buy',
|
|
384
|
+
size: 1,
|
|
385
|
+
offsetBps: 5,
|
|
386
|
+
timeoutSec: 2,
|
|
387
|
+
intervalMs: 1,
|
|
388
|
+
maxChaseBps: 1_000,
|
|
389
|
+
client,
|
|
390
|
+
fillWatcher: new StaticFillWatcher(fills),
|
|
391
|
+
output: () => {},
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
assert.equal(result.status, 'filled');
|
|
395
|
+
assert.equal(limitSizes.length, 2);
|
|
396
|
+
assert.equal(limitSizes[0], 1);
|
|
397
|
+
assert(Math.abs(limitSizes[1] - 0.6) < 1e-9);
|
|
398
|
+
assert.deepEqual(cancelled, [901]);
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
test('chase reprices and continues after a post-only rejection', async () => {
|
|
402
|
+
const fills = new Map<number, FillSummary>();
|
|
403
|
+
let orderCount = 0;
|
|
404
|
+
|
|
405
|
+
const client: ChaseClient = {
|
|
406
|
+
verbose: false,
|
|
407
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
408
|
+
async getAllMids() {
|
|
409
|
+
return { ETH: '1000' };
|
|
410
|
+
},
|
|
411
|
+
async getOpenOrders(): Promise<OpenOrder[]> {
|
|
412
|
+
return [];
|
|
413
|
+
},
|
|
414
|
+
async getUserFills() {
|
|
415
|
+
return [];
|
|
416
|
+
},
|
|
417
|
+
async limitOrder(_coin, _isBuy, size) {
|
|
418
|
+
orderCount += 1;
|
|
419
|
+
if (orderCount === 1) {
|
|
420
|
+
return okOrder([{ error: 'Post only order would have immediately matched, bbo was 999.9@1000.1' }]);
|
|
421
|
+
}
|
|
422
|
+
const oid = 900 + orderCount;
|
|
423
|
+
fills.set(oid, { size, notional: size * 1000, avgPrice: 1000 });
|
|
424
|
+
return okOrder([{ resting: { oid } }]);
|
|
425
|
+
},
|
|
426
|
+
async cancel() {
|
|
427
|
+
return okCancel();
|
|
428
|
+
},
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const result = await runChase({
|
|
432
|
+
coin: 'ETH',
|
|
433
|
+
side: 'buy',
|
|
434
|
+
size: 1,
|
|
435
|
+
offsetBps: 5,
|
|
436
|
+
timeoutSec: 2,
|
|
437
|
+
intervalMs: 1,
|
|
438
|
+
maxChaseBps: 1_000,
|
|
439
|
+
client,
|
|
440
|
+
fillWatcher: new StaticFillWatcher(fills),
|
|
441
|
+
output: () => {},
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
assert.equal(result.status, 'filled');
|
|
445
|
+
assert.equal(orderCount, 2);
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
test('chase cancels the resting order even when the loop throws', async () => {
|
|
449
|
+
const cancelled: number[] = [];
|
|
450
|
+
|
|
451
|
+
const client: ChaseClient = {
|
|
452
|
+
verbose: false,
|
|
453
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
454
|
+
async getAllMids() {
|
|
455
|
+
return { ETH: '1000' };
|
|
456
|
+
},
|
|
457
|
+
async getOpenOrders(): Promise<OpenOrder[]> {
|
|
458
|
+
throw new Error('open-orders lookup exploded');
|
|
459
|
+
},
|
|
460
|
+
async getUserFills() {
|
|
461
|
+
return [];
|
|
462
|
+
},
|
|
463
|
+
async limitOrder() {
|
|
464
|
+
return okOrder([{ resting: { oid: 950 } }]);
|
|
465
|
+
},
|
|
466
|
+
async cancel(_coin, oid) {
|
|
467
|
+
cancelled.push(oid);
|
|
468
|
+
return okCancel();
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
await assert.rejects(
|
|
473
|
+
() => runChase({
|
|
474
|
+
coin: 'ETH',
|
|
475
|
+
side: 'buy',
|
|
476
|
+
size: 1,
|
|
477
|
+
offsetBps: 5,
|
|
478
|
+
timeoutSec: 2,
|
|
479
|
+
intervalMs: 1,
|
|
480
|
+
maxChaseBps: 1_000,
|
|
481
|
+
client,
|
|
482
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
483
|
+
output: () => {},
|
|
484
|
+
}),
|
|
485
|
+
/open-orders lookup exploded/,
|
|
486
|
+
);
|
|
487
|
+
|
|
488
|
+
assert.deepEqual(cancelled, [950]);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
test('chase rejects a start size below the $10 exchange minimum', async () => {
|
|
492
|
+
const client: ChaseClient = {
|
|
493
|
+
verbose: false,
|
|
494
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
495
|
+
async getAllMids() {
|
|
496
|
+
return { ETH: '1000' };
|
|
497
|
+
},
|
|
498
|
+
async getOpenOrders(): Promise<OpenOrder[]> {
|
|
499
|
+
return [];
|
|
500
|
+
},
|
|
501
|
+
async getUserFills() {
|
|
502
|
+
return [];
|
|
503
|
+
},
|
|
504
|
+
async limitOrder() {
|
|
505
|
+
throw new Error('limitOrder should not be called');
|
|
506
|
+
},
|
|
507
|
+
async cancel() {
|
|
508
|
+
return okCancel();
|
|
509
|
+
},
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
await assert.rejects(
|
|
513
|
+
() => runChase({
|
|
514
|
+
coin: 'ETH',
|
|
515
|
+
side: 'buy',
|
|
516
|
+
size: 0.005,
|
|
517
|
+
client,
|
|
518
|
+
fillWatcher: new StaticFillWatcher(new Map()),
|
|
519
|
+
output: () => {},
|
|
520
|
+
}),
|
|
521
|
+
/below the \$10 exchange minimum/,
|
|
522
|
+
);
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
test('user fill watcher de-duplicates repeated REST fallback fills', async () => {
|
|
526
|
+
const fillTime = Date.now();
|
|
527
|
+
const watcher = new UserFillWatcher({
|
|
528
|
+
verbose: false,
|
|
529
|
+
address: '0x0000000000000000000000000000000000000001',
|
|
530
|
+
async getUserFills() {
|
|
531
|
+
return [
|
|
532
|
+
{ coin: 'ETH', px: '1000', sz: '0.25', time: fillTime, oid: 123 },
|
|
533
|
+
];
|
|
534
|
+
},
|
|
535
|
+
}, { ws: null, sinceMs: fillTime - 1000 });
|
|
536
|
+
|
|
537
|
+
await watcher.start();
|
|
538
|
+
await watcher.waitForFill(123, 1, 1, { coin: 'ETH', pollMs: 1 });
|
|
539
|
+
await watcher.waitForFill(123, 1, 1, { coin: 'ETH', pollMs: 1 });
|
|
540
|
+
|
|
541
|
+
assert.equal(watcher.getFilled(123).size, 0.25);
|
|
542
|
+
});
|