@quantform/core 0.6.5 → 0.6.7
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/adapter/adapter-aggregate.d.ts +2 -2
- package/dist/adapter/adapter-aggregate.d.ts.map +1 -1
- package/dist/adapter/adapter-aggregate.js +12 -11
- package/dist/adapter/adapter.d.ts +2 -3
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-adapter.d.ts +1 -5
- package/dist/adapter/backtester/backtester-adapter.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-adapter.js +2 -2
- package/dist/adapter/backtester/backtester-cursor.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-cursor.js +2 -2
- package/dist/adapter/backtester/backtester-cursor.spec.d.ts +2 -0
- package/dist/adapter/backtester/backtester-cursor.spec.d.ts.map +1 -0
- package/dist/adapter/backtester/backtester-cursor.spec.js +47 -0
- package/dist/adapter/backtester/backtester-streamer.d.ts +1 -1
- package/dist/adapter/backtester/backtester-streamer.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-streamer.js +4 -5
- package/dist/adapter/backtester/backtester-streamer.spec.d.ts +2 -0
- package/dist/adapter/backtester/backtester-streamer.spec.d.ts.map +1 -0
- package/dist/adapter/backtester/backtester-streamer.spec.js +45 -0
- package/dist/adapter/backtester/error.d.ts +6 -2
- package/dist/adapter/backtester/error.d.ts.map +1 -1
- package/dist/adapter/backtester/error.js +11 -7
- package/dist/adapter/backtester/index.d.ts +4 -2
- package/dist/adapter/backtester/index.d.ts.map +1 -1
- package/dist/adapter/backtester/index.js +4 -2
- package/dist/adapter/error.d.ts +9 -3
- package/dist/adapter/error.d.ts.map +1 -1
- package/dist/adapter/error.js +16 -10
- package/dist/adapter/index.d.ts +5 -4
- package/dist/adapter/index.d.ts.map +1 -1
- package/dist/adapter/index.js +5 -4
- package/dist/adapter/paper/engine/paper-engine.d.ts.map +1 -1
- package/dist/adapter/paper/engine/paper-engine.js +7 -8
- package/dist/adapter/paper/engine/paper-engine.spec.d.ts +2 -0
- package/dist/adapter/paper/engine/paper-engine.spec.d.ts.map +1 -0
- package/dist/adapter/paper/engine/paper-engine.spec.js +63 -0
- package/dist/adapter/paper/index.d.ts +2 -2
- package/dist/adapter/paper/index.d.ts.map +1 -1
- package/dist/adapter/paper/index.js +2 -2
- package/dist/adapter/paper/paper-adapter.d.ts +1 -3
- package/dist/adapter/paper/paper-adapter.d.ts.map +1 -1
- package/dist/adapter/paper/paper-adapter.js +8 -7
- package/dist/cli/build.js +1 -1
- package/dist/cli/dev.js +6 -6
- package/dist/cli/index.js +5 -5
- package/dist/cli/internal/workspace.js +1 -1
- package/dist/cli/pull.js +6 -6
- package/dist/cli/run.js +6 -6
- package/dist/cli/test.d.ts.map +1 -1
- package/dist/cli/test.js +9 -8
- package/dist/domain/asset.d.ts +5 -1
- package/dist/domain/asset.d.ts.map +1 -1
- package/dist/domain/asset.js +13 -7
- package/dist/domain/asset.spec.d.ts +2 -0
- package/dist/domain/asset.spec.d.ts.map +1 -0
- package/dist/domain/asset.spec.js +54 -0
- package/dist/domain/balance-operator.d.ts +1 -3
- package/dist/domain/balance-operator.d.ts.map +1 -1
- package/dist/domain/balance-operator.js +2 -2
- package/dist/domain/balance-operator.spec.d.ts +2 -0
- package/dist/domain/balance-operator.spec.d.ts.map +1 -0
- package/dist/domain/balance-operator.spec.js +21 -0
- package/dist/domain/balance.d.ts +14 -15
- package/dist/domain/balance.d.ts.map +1 -1
- package/dist/domain/balance.js +30 -48
- package/dist/domain/balance.spec.d.ts +2 -0
- package/dist/domain/balance.spec.d.ts.map +1 -0
- package/dist/domain/balance.spec.js +32 -0
- package/dist/domain/commission.d.ts.map +1 -1
- package/dist/domain/commission.spec.d.ts +2 -0
- package/dist/domain/commission.spec.d.ts.map +1 -0
- package/dist/domain/commission.spec.js +30 -0
- package/dist/domain/component.d.ts +2 -2
- package/dist/domain/component.d.ts.map +1 -1
- package/dist/domain/error.d.ts +15 -5
- package/dist/domain/error.d.ts.map +1 -1
- package/dist/domain/error.js +26 -16
- package/dist/domain/index.d.ts +21 -21
- package/dist/domain/index.d.ts.map +1 -1
- package/dist/domain/index.js +21 -21
- package/dist/domain/instrument-operator.d.ts +1 -2
- package/dist/domain/instrument-operator.d.ts.map +1 -1
- package/dist/domain/instrument-operator.js +3 -3
- package/dist/domain/instrument-operator.spec.d.ts +2 -0
- package/dist/domain/instrument-operator.spec.d.ts.map +1 -0
- package/dist/domain/instrument-operator.spec.js +21 -0
- package/dist/domain/instrument.d.ts +3 -3
- package/dist/domain/instrument.d.ts.map +1 -1
- package/dist/domain/instrument.js +10 -8
- package/dist/domain/instrument.spec.d.ts +2 -0
- package/dist/domain/instrument.spec.d.ts.map +1 -0
- package/dist/domain/instrument.spec.js +51 -0
- package/dist/domain/ohlc-operator.d.ts +1 -1
- package/dist/domain/ohlc-operator.d.ts.map +1 -1
- package/dist/domain/ohlc-operator.js +4 -5
- package/dist/domain/ohlc-operator.spec.d.ts +2 -0
- package/dist/domain/ohlc-operator.spec.d.ts.map +1 -0
- package/dist/domain/ohlc-operator.spec.js +110 -0
- package/dist/domain/ohlc.d.ts.map +1 -1
- package/dist/domain/ohlc.spec.d.ts +2 -0
- package/dist/domain/ohlc.spec.d.ts.map +1 -0
- package/dist/domain/ohlc.spec.js +25 -0
- package/dist/domain/order-operator.d.ts +1 -3
- package/dist/domain/order-operator.d.ts.map +1 -1
- package/dist/domain/order-operator.js +3 -3
- package/dist/domain/order-operator.spec.d.ts +2 -0
- package/dist/domain/order-operator.spec.d.ts.map +1 -0
- package/dist/domain/order-operator.spec.js +61 -0
- package/dist/domain/order.d.ts +5 -8
- package/dist/domain/order.d.ts.map +1 -1
- package/dist/domain/order.js +14 -22
- package/dist/domain/order.spec.d.ts +2 -0
- package/dist/domain/order.spec.d.ts.map +1 -0
- package/dist/domain/order.spec.js +27 -0
- package/dist/domain/orderbook-operator.d.ts +1 -3
- package/dist/domain/orderbook-operator.d.ts.map +1 -1
- package/dist/domain/orderbook-operator.js +2 -2
- package/dist/domain/orderbook-operator.spec.d.ts +2 -0
- package/dist/domain/orderbook-operator.spec.d.ts.map +1 -0
- package/dist/domain/orderbook-operator.spec.js +19 -0
- package/dist/domain/orderbook.d.ts +3 -2
- package/dist/domain/orderbook.d.ts.map +1 -1
- package/dist/domain/orderbook.js +3 -0
- package/dist/domain/orderbook.spec.d.ts +2 -0
- package/dist/domain/orderbook.spec.d.ts.map +1 -0
- package/dist/domain/orderbook.spec.js +11 -0
- package/dist/domain/position-operator.d.ts +1 -3
- package/dist/domain/position-operator.d.ts.map +1 -1
- package/dist/domain/position-operator.js +3 -4
- package/dist/domain/position-operator.spec.d.ts +2 -0
- package/dist/domain/position-operator.spec.d.ts.map +1 -0
- package/dist/domain/position-operator.spec.js +44 -0
- package/dist/domain/position.d.ts +3 -2
- package/dist/domain/position.d.ts.map +1 -1
- package/dist/domain/position.js +2 -0
- package/dist/domain/position.spec.d.ts +2 -0
- package/dist/domain/position.spec.d.ts.map +1 -0
- package/dist/domain/position.spec.js +28 -0
- package/dist/domain/session-builder.d.ts +8 -4
- package/dist/domain/session-builder.d.ts.map +1 -1
- package/dist/domain/session-builder.js +10 -15
- package/dist/domain/session.d.ts +1 -1
- package/dist/domain/session.d.ts.map +1 -1
- package/dist/domain/session.js +11 -17
- package/dist/domain/session.spec.d.ts +2 -0
- package/dist/domain/session.spec.d.ts.map +1 -0
- package/dist/domain/session.spec.js +17 -0
- package/dist/domain/trade-operator.d.ts +1 -3
- package/dist/domain/trade-operator.d.ts.map +1 -1
- package/dist/domain/trade-operator.js +2 -2
- package/dist/domain/trade-operator.spec.d.ts +2 -0
- package/dist/domain/trade-operator.spec.d.ts.map +1 -0
- package/dist/domain/trade-operator.spec.js +21 -0
- package/dist/domain/trade.d.ts +3 -2
- package/dist/domain/trade.d.ts.map +1 -1
- package/dist/domain/trade.js +3 -0
- package/dist/domain/trade.spec.d.ts +2 -0
- package/dist/domain/trade.spec.d.ts.map +1 -0
- package/dist/domain/trade.spec.js +11 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -8
- package/dist/shared/collections.spec.d.ts +2 -0
- package/dist/shared/collections.spec.d.ts.map +1 -0
- package/dist/shared/collections.spec.js +29 -0
- package/dist/shared/decimals.spec.d.ts +2 -0
- package/dist/shared/decimals.spec.d.ts.map +1 -0
- package/dist/shared/decimals.spec.js +28 -0
- package/dist/shared/hash.d.ts +2 -0
- package/dist/shared/hash.d.ts.map +1 -0
- package/dist/shared/hash.js +12 -0
- package/dist/shared/index.d.ts +7 -7
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index.js +7 -7
- package/dist/shared/logger.d.ts +8 -5
- package/dist/shared/logger.d.ts.map +1 -1
- package/dist/shared/logger.js +23 -16
- package/dist/storage/cache.d.ts +1 -1
- package/dist/storage/cache.d.ts.map +1 -1
- package/dist/storage/cache.spec.d.ts +2 -0
- package/dist/storage/cache.spec.d.ts.map +1 -0
- package/dist/storage/cache.spec.js +25 -0
- package/dist/storage/feed.d.ts +1 -1
- package/dist/storage/feed.d.ts.map +1 -1
- package/dist/storage/index.d.ts +4 -4
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +4 -4
- package/dist/storage/measurement.d.ts +1 -1
- package/dist/storage/measurement.d.ts.map +1 -1
- package/dist/store/error.d.ts +21 -7
- package/dist/store/error.d.ts.map +1 -1
- package/dist/store/error.js +36 -22
- package/dist/store/index.d.ts +10 -9
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +10 -9
- package/dist/store/store-balance-event.d.ts +10 -30
- package/dist/store/store-balance-event.d.ts.map +1 -1
- package/dist/store/store-balance-event.js +19 -94
- package/dist/store/store-balance-event.spec.d.ts +2 -0
- package/dist/store/store-balance-event.spec.d.ts.map +1 -0
- package/dist/store/store-balance-event.spec.js +24 -0
- package/dist/store/store-event.d.ts +1 -1
- package/dist/store/store-event.d.ts.map +1 -1
- package/dist/store/store-instrument-event.d.ts +1 -2
- package/dist/store/store-instrument-event.d.ts.map +1 -1
- package/dist/store/store-instrument-event.js +5 -6
- package/dist/store/store-instrument-event.spec.d.ts +2 -0
- package/dist/store/store-instrument-event.spec.d.ts.map +1 -0
- package/dist/store/store-instrument-event.spec.js +22 -0
- package/dist/store/store-order-event.d.ts +1 -2
- package/dist/store/store-order-event.d.ts.map +1 -1
- package/dist/store/store-order-event.js +60 -22
- package/dist/store/store-order-event.spec.d.ts +2 -0
- package/dist/store/store-order-event.spec.d.ts.map +1 -0
- package/dist/store/store-order-event.spec.js +23 -0
- package/dist/store/store-orderbook-event.d.ts +1 -2
- package/dist/store/store-orderbook-event.d.ts.map +1 -1
- package/dist/store/store-orderbook-event.js +3 -3
- package/dist/store/store-orderbook-event.spec.d.ts +2 -0
- package/dist/store/store-orderbook-event.spec.d.ts.map +1 -0
- package/dist/store/store-orderbook-event.spec.js +27 -0
- package/dist/store/store-position-event.d.ts +1 -2
- package/dist/store/store-position-event.d.ts.map +1 -1
- package/dist/store/store-position-event.js +5 -5
- package/dist/store/store-state.d.ts.map +1 -1
- package/dist/store/store-trade-event.d.ts +1 -2
- package/dist/store/store-trade-event.d.ts.map +1 -1
- package/dist/store/store-trade-event.js +3 -3
- package/dist/store/store-trade-event.spec.d.ts +2 -0
- package/dist/store/store-trade-event.spec.d.ts.map +1 -0
- package/dist/store/store-trade-event.spec.js +44 -0
- package/dist/store/store.d.ts +1 -2
- package/dist/store/store.d.ts.map +1 -1
- package/dist/store/store.js +5 -2
- package/dist/store/store.spec.d.ts +2 -0
- package/dist/store/store.spec.d.ts.map +1 -0
- package/dist/store/store.spec.js +122 -0
- package/jest.config.ts +23 -2
- package/package.json +2 -2
- package/src/adapter/adapter-aggregate.ts +35 -21
- package/src/adapter/adapter.ts +6 -7
- package/src/adapter/backtester/backtester-adapter.ts +12 -8
- package/src/adapter/backtester/backtester-cursor.spec.ts +6 -6
- package/src/adapter/backtester/backtester-cursor.ts +5 -5
- package/src/adapter/backtester/backtester-streamer.spec.ts +6 -6
- package/src/adapter/backtester/backtester-streamer.ts +12 -9
- package/src/adapter/backtester/error.ts +8 -4
- package/src/adapter/backtester/index.ts +4 -2
- package/src/adapter/error.ts +14 -8
- package/src/adapter/index.ts +5 -4
- package/src/adapter/paper/engine/paper-engine.spec.ts +6 -12
- package/src/adapter/paper/engine/paper-engine.ts +13 -28
- package/src/adapter/paper/index.ts +2 -2
- package/src/adapter/paper/paper-adapter.ts +18 -11
- package/src/cli/build.ts +1 -1
- package/src/cli/dev.ts +5 -5
- package/src/cli/index.ts +5 -5
- package/src/cli/internal/workspace.ts +1 -1
- package/src/cli/pull.ts +7 -7
- package/src/cli/run.ts +5 -5
- package/src/cli/test.ts +8 -7
- package/src/domain/asset.spec.ts +4 -4
- package/src/domain/asset.ts +13 -6
- package/src/domain/balance-operator.spec.ts +3 -6
- package/src/domain/balance-operator.ts +5 -6
- package/src/domain/balance.spec.ts +7 -77
- package/src/domain/balance.ts +51 -64
- package/src/domain/commission.spec.ts +3 -3
- package/src/domain/commission.ts +1 -1
- package/src/domain/component.ts +3 -3
- package/src/domain/error.ts +23 -18
- package/src/domain/index.ts +21 -21
- package/src/domain/instrument-operator.spec.ts +10 -7
- package/src/domain/instrument-operator.ts +6 -6
- package/src/domain/instrument.spec.ts +11 -6
- package/src/domain/instrument.ts +16 -7
- package/src/domain/ohlc-operator.spec.ts +5 -6
- package/src/domain/ohlc-operator.ts +2 -3
- package/src/domain/ohlc.spec.ts +2 -2
- package/src/domain/ohlc.ts +2 -2
- package/src/domain/order-operator.spec.ts +14 -10
- package/src/domain/order-operator.ts +8 -7
- package/src/domain/order.spec.ts +3 -6
- package/src/domain/order.ts +26 -29
- package/src/domain/orderbook-operator.spec.ts +11 -9
- package/src/domain/orderbook-operator.ts +5 -6
- package/src/domain/orderbook.spec.ts +3 -6
- package/src/domain/orderbook.ts +5 -3
- package/src/domain/position-operator.spec.ts +14 -11
- package/src/domain/position-operator.ts +12 -9
- package/src/domain/position.spec.ts +3 -6
- package/src/domain/position.ts +5 -3
- package/src/domain/session-builder.ts +14 -14
- package/src/domain/session.spec.ts +4 -4
- package/src/domain/session.ts +20 -15
- package/src/domain/trade-operator.spec.ts +4 -9
- package/src/domain/trade-operator.ts +5 -6
- package/src/domain/trade.spec.ts +3 -6
- package/src/domain/trade.ts +5 -3
- package/src/index.ts +15 -14
- package/src/shared/collections.spec.ts +2 -2
- package/src/shared/decimals.spec.ts +1 -1
- package/src/shared/hash.ts +10 -0
- package/src/shared/index.ts +7 -7
- package/src/shared/logger.ts +22 -15
- package/src/storage/cache.spec.ts +2 -3
- package/src/storage/cache.ts +2 -2
- package/src/storage/feed.ts +4 -4
- package/src/storage/index.ts +4 -4
- package/src/storage/measurement.ts +2 -2
- package/src/store/error.ts +33 -22
- package/src/store/index.ts +10 -9
- package/src/store/store-balance-event.spec.ts +5 -7
- package/src/store/store-balance-event.ts +20 -115
- package/src/store/store-event.ts +2 -2
- package/src/store/store-instrument-event.spec.ts +4 -5
- package/src/store/store-instrument-event.ts +13 -9
- package/src/store/store-order-event.spec.ts +6 -5
- package/src/store/store-order-event.ts +92 -23
- package/src/store/store-orderbook-event.spec.ts +4 -5
- package/src/store/store-orderbook-event.ts +11 -7
- package/src/store/store-position-event.ts +13 -9
- package/src/store/store-state.ts +2 -2
- package/src/store/store-trade-event.spec.ts +4 -5
- package/src/store/store-trade-event.ts +11 -7
- package/src/store/store.spec.ts +45 -31
- package/src/store/store.ts +6 -3
- package/tsconfig.json +7 -4
- package/dist/cli/error.d.ts +0 -2
- package/dist/cli/error.d.ts.map +0 -1
- package/dist/cli/error.js +0 -7
- package/dist/shared/pipe.d.ts +0 -5
- package/dist/shared/pipe.d.ts.map +0 -1
- package/dist/shared/pipe.js +0 -8
- package/src/cli/error.ts +0 -5
- package/src/shared/pipe.ts +0 -12
package/src/store/error.ts
CHANGED
|
@@ -1,36 +1,47 @@
|
|
|
1
|
-
import { AssetSelector, InstrumentSelector, OrderState } from '
|
|
1
|
+
import { AssetSelector, InstrumentSelector, OrderState } from '@lib/domain';
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
|
|
3
|
+
export class AssetNotSupportedError extends Error {
|
|
4
|
+
constructor(selector: AssetSelector) {
|
|
5
|
+
super(`asset ${selector.id} not supported.`);
|
|
6
|
+
}
|
|
5
7
|
}
|
|
6
8
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
+
export class InstrumentNotSubscribedError extends Error {
|
|
10
|
+
constructor(selector: InstrumentSelector) {
|
|
11
|
+
super(`trying to patch a not subscribed instrument ${selector.id}`);
|
|
12
|
+
}
|
|
9
13
|
}
|
|
10
14
|
|
|
11
|
-
export
|
|
12
|
-
|
|
15
|
+
export class InstrumentNotSupportedError extends Error {
|
|
16
|
+
constructor(selector: InstrumentSelector) {
|
|
17
|
+
super(`instrument ${selector.id} not supported.`);
|
|
18
|
+
}
|
|
13
19
|
}
|
|
14
20
|
|
|
15
|
-
export
|
|
16
|
-
|
|
21
|
+
export class LiquidationError extends Error {
|
|
22
|
+
constructor() {
|
|
23
|
+
super('you have been liquidated.');
|
|
24
|
+
}
|
|
17
25
|
}
|
|
18
26
|
|
|
19
|
-
export
|
|
20
|
-
|
|
27
|
+
export class OrderNotFoundError extends Error {
|
|
28
|
+
constructor(id: string) {
|
|
29
|
+
super(`trying to patch unknown order: ${id}`);
|
|
30
|
+
}
|
|
21
31
|
}
|
|
22
32
|
|
|
23
|
-
export
|
|
24
|
-
|
|
33
|
+
export class BalanceNotFoundError extends Error {
|
|
34
|
+
constructor(selector: AssetSelector) {
|
|
35
|
+
super(`balance not found: ${selector.id}`);
|
|
36
|
+
}
|
|
25
37
|
}
|
|
26
38
|
|
|
27
|
-
export
|
|
28
|
-
currentState: OrderState,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
);
|
|
39
|
+
export class OrderInvalidStateError extends Error {
|
|
40
|
+
constructor(currentState: OrderState, requiredStates: OrderState[]) {
|
|
41
|
+
super(
|
|
42
|
+
`order state ${currentState} is not in one of required states: ${requiredStates.join(
|
|
43
|
+
', '
|
|
44
|
+
)}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
36
47
|
}
|
package/src/store/index.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export * from '
|
|
3
|
-
export * from '
|
|
4
|
-
export * from '
|
|
5
|
-
export * from '
|
|
6
|
-
export * from '
|
|
7
|
-
export * from '
|
|
8
|
-
export * from '
|
|
9
|
-
export * from '
|
|
1
|
+
export * from '@lib/store/store';
|
|
2
|
+
export * from '@lib/store/store-state';
|
|
3
|
+
export * from '@lib/store/store-balance-event';
|
|
4
|
+
export * from '@lib/store/store-instrument-event';
|
|
5
|
+
export * from '@lib/store/store-order-event';
|
|
6
|
+
export * from '@lib/store/store-orderbook-event';
|
|
7
|
+
export * from '@lib/store/store-position-event';
|
|
8
|
+
export * from '@lib/store/store-trade-event';
|
|
9
|
+
export * from '@lib/store/store-event';
|
|
10
|
+
export * from '@lib/store/error';
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { Asset, Commission } from '
|
|
2
|
-
import { d, now } from '
|
|
3
|
-
import { Store } from '
|
|
4
|
-
import { BalancePatchEvent } from './store-balance-event';
|
|
5
|
-
import { InstrumentPatchEvent } from './store-instrument-event';
|
|
1
|
+
import { Asset, Commission } from '@lib/domain';
|
|
2
|
+
import { d, now } from '@lib/shared';
|
|
3
|
+
import { BalanceLoadEvent, InstrumentPatchEvent, Store } from '@lib/store';
|
|
6
4
|
|
|
7
|
-
describe(
|
|
5
|
+
describe(BalanceLoadEvent.name, () => {
|
|
8
6
|
test('should patch a store', () => {
|
|
9
7
|
const base = new Asset('de30', 'cex', 2);
|
|
10
8
|
const quote = new Asset('usd', 'cex', 2);
|
|
@@ -17,7 +15,7 @@ describe('BalancePatchEvent', () => {
|
|
|
17
15
|
store.dispatch(
|
|
18
16
|
new InstrumentPatchEvent(timestamp, base, quote, new Commission(d.Zero, d.Zero), '')
|
|
19
17
|
);
|
|
20
|
-
store.dispatch(new
|
|
18
|
+
store.dispatch(new BalanceLoadEvent(base, d(100), d.Zero, timestamp));
|
|
21
19
|
|
|
22
20
|
const balance = store.snapshot.balance.get(base.id) ?? fail();
|
|
23
21
|
|
|
@@ -1,22 +1,20 @@
|
|
|
1
|
-
import { AssetSelector, Balance
|
|
2
|
-
import { decimal, timestamp } from '
|
|
1
|
+
import { AssetSelector, Balance } from '@lib/domain';
|
|
2
|
+
import { decimal, timestamp } from '@lib/shared';
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from '
|
|
9
|
-
import { StoreEvent } from './store-event';
|
|
10
|
-
import { State, StateChangeTracker } from './store-state';
|
|
4
|
+
AssetNotSupportedError,
|
|
5
|
+
State,
|
|
6
|
+
StateChangeTracker,
|
|
7
|
+
StoreEvent
|
|
8
|
+
} from '@lib/store';
|
|
11
9
|
|
|
12
10
|
/**
|
|
13
11
|
* Updates the free and freezed balance of the given asset.
|
|
14
12
|
*/
|
|
15
|
-
export class
|
|
13
|
+
export class BalanceLoadEvent implements StoreEvent {
|
|
16
14
|
constructor(
|
|
17
15
|
readonly asset: AssetSelector,
|
|
18
|
-
readonly
|
|
19
|
-
readonly
|
|
16
|
+
readonly available: decimal,
|
|
17
|
+
readonly unavailable: decimal,
|
|
20
18
|
readonly timestamp: timestamp
|
|
21
19
|
) {}
|
|
22
20
|
|
|
@@ -33,7 +31,9 @@ export class BalancePatchEvent implements StoreEvent {
|
|
|
33
31
|
const balance = state.balance.tryGetOrSet(this.asset.id, () => new Balance(0, asset));
|
|
34
32
|
|
|
35
33
|
balance.timestamp = this.timestamp;
|
|
36
|
-
balance.
|
|
34
|
+
balance.available = this.available;
|
|
35
|
+
balance.unavailable = this.unavailable;
|
|
36
|
+
balance.clearTransientFunding();
|
|
37
37
|
|
|
38
38
|
state.timestamp = this.timestamp;
|
|
39
39
|
|
|
@@ -44,124 +44,29 @@ export class BalancePatchEvent implements StoreEvent {
|
|
|
44
44
|
/**
|
|
45
45
|
*
|
|
46
46
|
*/
|
|
47
|
-
export class
|
|
47
|
+
export class BalancePatchEvent implements StoreEvent {
|
|
48
48
|
constructor(
|
|
49
49
|
readonly asset: AssetSelector,
|
|
50
|
-
readonly
|
|
50
|
+
readonly available: decimal,
|
|
51
|
+
readonly unavailable: decimal,
|
|
51
52
|
readonly timestamp: timestamp
|
|
52
53
|
) {}
|
|
53
54
|
|
|
54
55
|
handle(state: State, changes: StateChangeTracker) {
|
|
55
56
|
const asset = state.universe.asset.get(this.asset.id);
|
|
56
57
|
if (!asset) {
|
|
57
|
-
throw
|
|
58
|
+
throw new AssetNotSupportedError(this.asset);
|
|
58
59
|
}
|
|
59
60
|
|
|
60
61
|
const balance = state.balance.tryGetOrSet(this.asset.id, () => new Balance(0, asset));
|
|
61
62
|
|
|
62
63
|
balance.timestamp = this.timestamp;
|
|
63
|
-
balance.
|
|
64
|
+
balance.available = this.available;
|
|
65
|
+
balance.unavailable = this.unavailable;
|
|
66
|
+
balance.clearTransientFunding();
|
|
64
67
|
|
|
65
68
|
state.timestamp = this.timestamp;
|
|
66
69
|
|
|
67
70
|
changes.commit(balance);
|
|
68
71
|
}
|
|
69
72
|
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
export class BalanceLockOrderEvent implements StoreEvent {
|
|
75
|
-
constructor(
|
|
76
|
-
readonly orderId: string,
|
|
77
|
-
readonly instrument: InstrumentSelector,
|
|
78
|
-
readonly timestamp: timestamp
|
|
79
|
-
) {}
|
|
80
|
-
|
|
81
|
-
handle(state: State, changes: StateChangeTracker) {
|
|
82
|
-
const orders = state.order.get(this.instrument.id);
|
|
83
|
-
if (!orders) {
|
|
84
|
-
throw instrumentNotSubscribedError(this.instrument);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const order = orders.get(this.orderId);
|
|
88
|
-
if (!order) {
|
|
89
|
-
throw orderNotFoundError(this.orderId);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const base = state.balance.get(order.instrument.base.id);
|
|
93
|
-
if (!base) {
|
|
94
|
-
throw balanceNotFoundError(order.instrument.base);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const quote = state.balance.get(order.instrument.quote.id);
|
|
98
|
-
if (!quote) {
|
|
99
|
-
throw balanceNotFoundError(order.instrument.quote);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const balanceToLock = order.calculateBalanceToLock(base, quote);
|
|
103
|
-
|
|
104
|
-
state.timestamp = this.timestamp;
|
|
105
|
-
|
|
106
|
-
if (balanceToLock.base?.greaterThan(0)) {
|
|
107
|
-
base.timestamp = this.timestamp;
|
|
108
|
-
base.lock(this.orderId, balanceToLock.base);
|
|
109
|
-
|
|
110
|
-
changes.commit(base);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (balanceToLock.quote?.greaterThan(0)) {
|
|
114
|
-
quote.timestamp = this.timestamp;
|
|
115
|
-
quote.lock(this.orderId, balanceToLock.quote);
|
|
116
|
-
|
|
117
|
-
changes.commit(quote);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
*
|
|
124
|
-
*/
|
|
125
|
-
export class BalanceUnlockOrderEvent implements StoreEvent {
|
|
126
|
-
constructor(
|
|
127
|
-
readonly orderId: string,
|
|
128
|
-
readonly instrument: InstrumentSelector,
|
|
129
|
-
readonly timestamp: timestamp
|
|
130
|
-
) {}
|
|
131
|
-
|
|
132
|
-
handle(state: State, changes: StateChangeTracker) {
|
|
133
|
-
const orders = state.order.get(this.instrument.id);
|
|
134
|
-
if (!orders) {
|
|
135
|
-
throw instrumentNotSubscribedError(this.instrument);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const order = orders.get(this.orderId);
|
|
139
|
-
if (!order) {
|
|
140
|
-
throw orderNotFoundError(this.orderId);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const base = state.balance.get(order.instrument.base.id);
|
|
144
|
-
if (!base) {
|
|
145
|
-
throw balanceNotFoundError(order.instrument.base);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const quote = state.balance.get(order.instrument.quote.id);
|
|
149
|
-
if (!quote) {
|
|
150
|
-
throw balanceNotFoundError(order.instrument.quote);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
state.timestamp = this.timestamp;
|
|
154
|
-
|
|
155
|
-
if (base.tryUnlock(this.orderId)) {
|
|
156
|
-
base.timestamp = this.timestamp;
|
|
157
|
-
|
|
158
|
-
changes.commit(base);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (quote.tryUnlock(this.orderId)) {
|
|
162
|
-
quote.timestamp = this.timestamp;
|
|
163
|
-
|
|
164
|
-
changes.commit(quote);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
package/src/store/store-event.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Asset, Commission } from '
|
|
2
|
-
import { d, now } from '
|
|
3
|
-
import { Store } from '
|
|
4
|
-
import { InstrumentPatchEvent } from './store-instrument-event';
|
|
1
|
+
import { Asset, Commission } from '@lib/domain';
|
|
2
|
+
import { d, now } from '@lib/shared';
|
|
3
|
+
import { InstrumentPatchEvent, Store } from '@lib/store';
|
|
5
4
|
|
|
6
|
-
describe(
|
|
5
|
+
describe(InstrumentPatchEvent.name, () => {
|
|
7
6
|
test('should patch a store', () => {
|
|
8
7
|
const timestamp = now();
|
|
9
8
|
const store = new Store();
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { Asset, Commission, Instrument, InstrumentSelector } from '
|
|
2
|
-
import { timestamp } from '
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Asset, Commission, Instrument, InstrumentSelector } from '@lib/domain';
|
|
2
|
+
import { timestamp } from '@lib/shared';
|
|
3
|
+
import {
|
|
4
|
+
AssetNotSupportedError,
|
|
5
|
+
InnerSet,
|
|
6
|
+
InstrumentNotSupportedError,
|
|
7
|
+
State,
|
|
8
|
+
StateChangeTracker,
|
|
9
|
+
StoreEvent
|
|
10
|
+
} from '@lib/store';
|
|
7
11
|
|
|
8
12
|
export class InstrumentPatchEvent implements StoreEvent {
|
|
9
13
|
constructor(
|
|
@@ -59,18 +63,18 @@ export class InstrumentSubscriptionEvent implements StoreEvent {
|
|
|
59
63
|
handle(state: State, changes: StateChangeTracker): void {
|
|
60
64
|
const instrument = state.universe.instrument.get(this.instrument.id);
|
|
61
65
|
if (!instrument) {
|
|
62
|
-
throw
|
|
66
|
+
throw new InstrumentNotSupportedError(this.instrument);
|
|
63
67
|
}
|
|
64
68
|
|
|
65
69
|
if (this.subscribed) {
|
|
66
70
|
const base = state.universe.asset.get(instrument.base.id);
|
|
67
71
|
if (!base) {
|
|
68
|
-
throw
|
|
72
|
+
throw new AssetNotSupportedError(instrument.base);
|
|
69
73
|
}
|
|
70
74
|
|
|
71
75
|
const quote = state.universe.asset.get(instrument.quote.id);
|
|
72
76
|
if (!quote) {
|
|
73
|
-
throw
|
|
77
|
+
throw new AssetNotSupportedError(instrument.quote);
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
state.subscription.instrument.upsert(instrument);
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Asset, Commission, Instrument, Order } from '
|
|
2
|
-
import { d, now } from '
|
|
3
|
-
import { Store } from '
|
|
4
|
-
import { OrderLoadEvent } from './store-order-event';
|
|
1
|
+
import { Asset, Balance, Commission, Instrument, Order } from '@lib/domain';
|
|
2
|
+
import { d, now } from '@lib/shared';
|
|
3
|
+
import { OrderLoadEvent, Store } from '@lib/store';
|
|
5
4
|
|
|
6
|
-
describe(
|
|
5
|
+
describe(OrderLoadEvent.name, () => {
|
|
7
6
|
const instrument = new Instrument(
|
|
8
7
|
0,
|
|
9
8
|
new Asset('btc', 'binance', 8),
|
|
@@ -15,12 +14,14 @@ describe('OrderLoadEvent', () => {
|
|
|
15
14
|
test('should load order to store', () => {
|
|
16
15
|
const timestamp = now();
|
|
17
16
|
const store = new Store();
|
|
17
|
+
const balance = new Balance(0, instrument.quote, d(1));
|
|
18
18
|
const order = new Order(0, '1', instrument, d(1.0), 0);
|
|
19
19
|
|
|
20
20
|
order.state = 'PENDING';
|
|
21
21
|
|
|
22
22
|
store.snapshot.universe.instrument.upsert(instrument);
|
|
23
23
|
store.snapshot.subscription.instrument.upsert(instrument);
|
|
24
|
+
store.snapshot.balance.upsert(balance);
|
|
24
25
|
|
|
25
26
|
store.dispatch(new OrderLoadEvent(order, timestamp));
|
|
26
27
|
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import { InstrumentSelector, Order } from '
|
|
2
|
-
import { decimal, timestamp } from '
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { InstrumentSelector, Order } from '@lib/domain';
|
|
2
|
+
import { decimal, timestamp } from '@lib/shared';
|
|
3
|
+
import {
|
|
4
|
+
BalanceNotFoundError,
|
|
5
|
+
InnerSet,
|
|
6
|
+
OrderInvalidStateError,
|
|
7
|
+
OrderNotFoundError,
|
|
8
|
+
State,
|
|
9
|
+
StateChangeTracker,
|
|
10
|
+
StoreEvent
|
|
11
|
+
} from '@lib/store';
|
|
6
12
|
|
|
7
13
|
/**
|
|
8
14
|
* Patches a store with an existing pending order.
|
|
@@ -28,7 +34,7 @@ export class OrderNewEvent implements StoreEvent {
|
|
|
28
34
|
|
|
29
35
|
handle(state: State, changes: StateChangeTracker): void {
|
|
30
36
|
if (this.order.state != 'NEW') {
|
|
31
|
-
throw
|
|
37
|
+
throw new OrderInvalidStateError(this.order.state, ['NEW']);
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
this.order.createdAt = this.timestamp;
|
|
@@ -41,6 +47,27 @@ export class OrderNewEvent implements StoreEvent {
|
|
|
41
47
|
|
|
42
48
|
orderByInstrument.upsert(this.order);
|
|
43
49
|
|
|
50
|
+
const base = state.balance.get(this.order.instrument.base.id);
|
|
51
|
+
const quote = state.balance.get(this.order.instrument.quote.id);
|
|
52
|
+
|
|
53
|
+
if (!base || !quote) {
|
|
54
|
+
throw new BalanceNotFoundError(
|
|
55
|
+
!base ? this.order.instrument.base : this.order.instrument.quote
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (base.tryAddTransientFunding(this.order)) {
|
|
60
|
+
base.timestamp = this.timestamp;
|
|
61
|
+
|
|
62
|
+
changes.commit(base);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (quote.tryAddTransientFunding(this.order)) {
|
|
66
|
+
quote.timestamp = this.timestamp;
|
|
67
|
+
|
|
68
|
+
changes.commit(quote);
|
|
69
|
+
}
|
|
70
|
+
|
|
44
71
|
changes.commit(this.order);
|
|
45
72
|
}
|
|
46
73
|
}
|
|
@@ -55,14 +82,14 @@ export class OrderPendingEvent implements StoreEvent {
|
|
|
55
82
|
handle(state: State, changes: StateChangeTracker): void {
|
|
56
83
|
const order = state.order
|
|
57
84
|
.tryGetOrSet(this.instrument.id, () => {
|
|
58
|
-
throw
|
|
85
|
+
throw new OrderNotFoundError(this.id);
|
|
59
86
|
})
|
|
60
87
|
.tryGetOrSet(this.id, () => {
|
|
61
|
-
throw
|
|
88
|
+
throw new OrderNotFoundError(this.id);
|
|
62
89
|
});
|
|
63
90
|
|
|
64
91
|
if (order.state != 'NEW') {
|
|
65
|
-
throw
|
|
92
|
+
throw new OrderInvalidStateError(order.state, ['NEW']);
|
|
66
93
|
}
|
|
67
94
|
|
|
68
95
|
order.state = 'PENDING';
|
|
@@ -83,14 +110,14 @@ export class OrderFilledEvent implements StoreEvent {
|
|
|
83
110
|
handle(state: State, changes: StateChangeTracker): void {
|
|
84
111
|
const order = state.order
|
|
85
112
|
.tryGetOrSet(this.instrument.id, () => {
|
|
86
|
-
throw
|
|
113
|
+
throw new OrderNotFoundError(this.id);
|
|
87
114
|
})
|
|
88
115
|
.tryGetOrSet(this.id, () => {
|
|
89
|
-
throw
|
|
116
|
+
throw new OrderNotFoundError(this.id);
|
|
90
117
|
});
|
|
91
118
|
|
|
92
119
|
if (order.state != 'PENDING' && order.state != 'CANCELING') {
|
|
93
|
-
throw
|
|
120
|
+
throw new OrderInvalidStateError(order.state, ['PENDING', 'CANCELING']);
|
|
94
121
|
}
|
|
95
122
|
|
|
96
123
|
order.state = 'FILLED';
|
|
@@ -112,10 +139,10 @@ export class OrderCancelingEvent implements StoreEvent {
|
|
|
112
139
|
handle(state: State, changes: StateChangeTracker): void {
|
|
113
140
|
const order = state.order
|
|
114
141
|
.tryGetOrSet(this.instrument.id, () => {
|
|
115
|
-
throw
|
|
142
|
+
throw new OrderNotFoundError(this.id);
|
|
116
143
|
})
|
|
117
144
|
.tryGetOrSet(this.id, () => {
|
|
118
|
-
throw
|
|
145
|
+
throw new OrderNotFoundError(this.id);
|
|
119
146
|
});
|
|
120
147
|
|
|
121
148
|
if (order.state == 'CANCELING' || order.state == 'CANCELED') {
|
|
@@ -123,7 +150,7 @@ export class OrderCancelingEvent implements StoreEvent {
|
|
|
123
150
|
}
|
|
124
151
|
|
|
125
152
|
if (order.state != 'PENDING') {
|
|
126
|
-
throw
|
|
153
|
+
throw new OrderInvalidStateError(order.state, ['PENDING']);
|
|
127
154
|
}
|
|
128
155
|
|
|
129
156
|
order.state = 'CANCELING';
|
|
@@ -143,10 +170,10 @@ export class OrderCanceledEvent implements StoreEvent {
|
|
|
143
170
|
handle(state: State, changes: StateChangeTracker): void {
|
|
144
171
|
const order = state.order
|
|
145
172
|
.tryGetOrSet(this.instrument.id, () => {
|
|
146
|
-
throw
|
|
173
|
+
throw new OrderNotFoundError(this.id);
|
|
147
174
|
})
|
|
148
175
|
.tryGetOrSet(this.id, () => {
|
|
149
|
-
throw
|
|
176
|
+
throw new OrderNotFoundError(this.id);
|
|
150
177
|
});
|
|
151
178
|
|
|
152
179
|
if (order.state == 'CANCELED') {
|
|
@@ -154,12 +181,33 @@ export class OrderCanceledEvent implements StoreEvent {
|
|
|
154
181
|
}
|
|
155
182
|
|
|
156
183
|
if (order.state != 'CANCELING') {
|
|
157
|
-
throw
|
|
184
|
+
throw new OrderInvalidStateError(order.state, ['CANCELING']);
|
|
158
185
|
}
|
|
159
186
|
|
|
160
187
|
order.state = 'CANCELED';
|
|
161
188
|
order.timestamp = this.timestamp;
|
|
162
189
|
|
|
190
|
+
const base = state.balance.get(order.instrument.base.id);
|
|
191
|
+
const quote = state.balance.get(order.instrument.quote.id);
|
|
192
|
+
|
|
193
|
+
if (!base || !quote) {
|
|
194
|
+
throw new BalanceNotFoundError(
|
|
195
|
+
!base ? order.instrument.base : order.instrument.quote
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (base.tryRemoveTransientFunding(order)) {
|
|
200
|
+
base.timestamp = this.timestamp;
|
|
201
|
+
|
|
202
|
+
changes.commit(base);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (quote.tryRemoveTransientFunding(order)) {
|
|
206
|
+
quote.timestamp = this.timestamp;
|
|
207
|
+
|
|
208
|
+
changes.commit(quote);
|
|
209
|
+
}
|
|
210
|
+
|
|
163
211
|
changes.commit(order);
|
|
164
212
|
}
|
|
165
213
|
}
|
|
@@ -174,10 +222,10 @@ export class OrderCancelFailedEvent implements StoreEvent {
|
|
|
174
222
|
handle(state: State, changes: StateChangeTracker): void {
|
|
175
223
|
const order = state.order
|
|
176
224
|
.tryGetOrSet(this.instrument.id, () => {
|
|
177
|
-
throw
|
|
225
|
+
throw new OrderNotFoundError(this.id);
|
|
178
226
|
})
|
|
179
227
|
.tryGetOrSet(this.id, () => {
|
|
180
|
-
throw
|
|
228
|
+
throw new OrderNotFoundError(this.id);
|
|
181
229
|
});
|
|
182
230
|
|
|
183
231
|
if (order.state != 'CANCELING') {
|
|
@@ -201,19 +249,40 @@ export class OrderRejectedEvent implements StoreEvent {
|
|
|
201
249
|
handle(state: State, changes: StateChangeTracker): void {
|
|
202
250
|
const order = state.order
|
|
203
251
|
.tryGetOrSet(this.instrument.id, () => {
|
|
204
|
-
throw
|
|
252
|
+
throw new OrderNotFoundError(this.id);
|
|
205
253
|
})
|
|
206
254
|
.tryGetOrSet(this.id, () => {
|
|
207
|
-
throw
|
|
255
|
+
throw new OrderNotFoundError(this.id);
|
|
208
256
|
});
|
|
209
257
|
|
|
210
258
|
if (order.state != 'NEW') {
|
|
211
|
-
throw
|
|
259
|
+
throw new OrderInvalidStateError(order.state, ['NEW']);
|
|
212
260
|
}
|
|
213
261
|
|
|
214
262
|
order.state = 'REJECTED';
|
|
215
263
|
order.timestamp = this.timestamp;
|
|
216
264
|
|
|
265
|
+
const base = state.balance.get(order.instrument.base.id);
|
|
266
|
+
const quote = state.balance.get(order.instrument.quote.id);
|
|
267
|
+
|
|
268
|
+
if (!base || !quote) {
|
|
269
|
+
throw new BalanceNotFoundError(
|
|
270
|
+
!base ? order.instrument.base : order.instrument.quote
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (base.tryRemoveTransientFunding(order)) {
|
|
275
|
+
base.timestamp = this.timestamp;
|
|
276
|
+
|
|
277
|
+
changes.commit(base);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (quote.tryRemoveTransientFunding(order)) {
|
|
281
|
+
quote.timestamp = this.timestamp;
|
|
282
|
+
|
|
283
|
+
changes.commit(quote);
|
|
284
|
+
}
|
|
285
|
+
|
|
217
286
|
changes.commit(order);
|
|
218
287
|
}
|
|
219
288
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Asset, Commission, Instrument, Liquidity } from '
|
|
2
|
-
import { d, now } from '
|
|
3
|
-
import { OrderbookPatchEvent } from '
|
|
4
|
-
import { Store } from './store';
|
|
1
|
+
import { Asset, Commission, Instrument, Liquidity } from '@lib/domain';
|
|
2
|
+
import { d, now } from '@lib/shared';
|
|
3
|
+
import { OrderbookPatchEvent, Store } from '@lib/store';
|
|
5
4
|
|
|
6
|
-
describe(
|
|
5
|
+
describe(OrderbookPatchEvent.name, () => {
|
|
7
6
|
const instrument = new Instrument(
|
|
8
7
|
0,
|
|
9
8
|
new Asset('btc', 'binance', 8),
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import { InstrumentSelector, Liquidity, Orderbook } from '
|
|
2
|
-
import { timestamp } from '
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { InstrumentSelector, Liquidity, Orderbook } from '@lib/domain';
|
|
2
|
+
import { timestamp } from '@lib/shared';
|
|
3
|
+
import {
|
|
4
|
+
InstrumentNotSupportedError,
|
|
5
|
+
LiquidationError,
|
|
6
|
+
State,
|
|
7
|
+
StateChangeTracker,
|
|
8
|
+
StoreEvent
|
|
9
|
+
} from '@lib/store';
|
|
6
10
|
|
|
7
11
|
export class OrderbookPatchEvent implements StoreEvent {
|
|
8
12
|
constructor(
|
|
@@ -15,7 +19,7 @@ export class OrderbookPatchEvent implements StoreEvent {
|
|
|
15
19
|
handle(state: State, changes: StateChangeTracker): void {
|
|
16
20
|
const instrument = state.universe.instrument.get(this.instrument.id);
|
|
17
21
|
if (!instrument) {
|
|
18
|
-
throw
|
|
22
|
+
throw new InstrumentNotSupportedError(this.instrument);
|
|
19
23
|
}
|
|
20
24
|
|
|
21
25
|
const orderbook = state.orderbook.tryGetOrSet(
|
|
@@ -47,7 +51,7 @@ export class OrderbookPatchEvent implements StoreEvent {
|
|
|
47
51
|
}
|
|
48
52
|
|
|
49
53
|
if (quote.total.lessThan(0)) {
|
|
50
|
-
throw
|
|
54
|
+
throw new LiquidationError();
|
|
51
55
|
}
|
|
52
56
|
}
|
|
53
57
|
|