Skip to content

Commit e0e3a9a

Browse files
authored
fix: don't poll for order inclusion on chain and also log time for market orders to fill (#1932)
1 parent 8eabbf4 commit e0e3a9a

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"@cosmjs/stargate": "^0.32.1",
5555
"@cosmjs/tendermint-rpc": "^0.32.1",
5656
"@datadog/browser-logs": "^5.23.3",
57-
"@dydxprotocol/v4-client-js": "3.0.3",
57+
"@dydxprotocol/v4-client-js": "3.0.5",
5858
"@dydxprotocol/v4-localization": "1.1.334",
5959
"@dydxprotocol/v4-proto": "^7.0.0-dev.0",
6060
"@emotion/is-prop-valid": "^1.3.0",

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bonsai/AccountTransactionSupervisor.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { logBonsaiError, logBonsaiInfo } from '@/bonsai/logs';
1313
import { BonsaiCore } from '@/bonsai/ontology';
1414
import { OrderStatus, SubaccountOrder } from '@/bonsai/types/summaryTypes';
1515
import { IndexedTx } from '@cosmjs/stargate';
16+
import { Method } from '@cosmjs/tendermint-rpc';
1617
import {
1718
CompositeClient,
1819
LocalWallet,
@@ -31,6 +32,7 @@ import {
3132
TransactionMemo,
3233
} from '@/constants/analytics';
3334
import { STRING_KEYS } from '@/constants/localization';
35+
import { timeUnits } from '@/constants/time';
3436
import {
3537
MARKET_ORDER_MAX_SLIPPAGE,
3638
POST_TRANSFER_PLACE_ORDER_DELAY,
@@ -80,6 +82,12 @@ interface TransactionSupervisorShared {
8082
stateNotifier: StateConditionNotifier;
8183
}
8284

85+
const selectOrdersAndFills = createAppSelector(
86+
BonsaiCore.account.allOrders.data,
87+
BonsaiCore.account.fills.data,
88+
(orders, fills) => ({ orders, fills })
89+
);
90+
8391
interface CancelOrderPayload {
8492
clientId: number;
8593
orderFlags: OrderFlags;
@@ -407,6 +415,9 @@ export class AccountTransactionSupervisor {
407415
}
408416

409417
private async executePlaceOrder(payload: PlaceOrderPayload): Promise<OperationResult<any>> {
418+
const totalTimer = startTimer();
419+
const afterSubmitTimer = createTimer();
420+
410421
const placeOrderResult = await this.wrapOperation(
411422
'AccountTransactionSupervisor/placeOrder',
412423
payload,
@@ -459,7 +470,8 @@ export class AccountTransactionSupervisor {
459470
marketInfo ?? undefined,
460471
currentHeight ?? undefined,
461472
goodTilBlock ?? undefined,
462-
memo
473+
memo,
474+
Method.BroadcastTxSync
463475
);
464476

465477
if ((tx as IndexedTx | undefined)?.code !== 0) {
@@ -489,6 +501,43 @@ export class AccountTransactionSupervisor {
489501
);
490502
}
491503

504+
// Log market order fills
505+
if (isOperationSuccess(placeOrderResult) && payload.type === OrderType.MARKET) {
506+
afterSubmitTimer.start();
507+
this.shared.stateNotifier.notifyWhenTrue(
508+
selectOrdersAndFills,
509+
({ orders, fills }) => {
510+
const matchingOrder = orders.find((order) => order.clientId === `${payload.clientId}`);
511+
if (matchingOrder?.id == null) {
512+
return undefined;
513+
}
514+
515+
const matchingFill = fills.find((fill) => fill.orderId === matchingOrder.id);
516+
if (matchingFill != null) {
517+
return { order: matchingOrder, fill: matchingFill };
518+
}
519+
520+
return undefined;
521+
},
522+
(result) => {
523+
if (result != null) {
524+
logBonsaiInfo('AccountTransactionSupervisor/placeOrder', 'Market order filled', {
525+
payload,
526+
order: purgeBigNumbers(result.order),
527+
fill: purgeBigNumbers(result.fill),
528+
totalTimeToFill: totalTimer.elapsed(),
529+
timeToFillAfterSubmit: afterSubmitTimer.elapsed(),
530+
});
531+
} else {
532+
logBonsaiInfo('AccountTransactionSupervisor/placeOrder', 'Market order never filled', {
533+
payload,
534+
});
535+
}
536+
},
537+
10 * timeUnits.second
538+
);
539+
}
540+
492541
return placeOrderResult;
493542
}
494543

@@ -1193,7 +1242,8 @@ export class AccountTransactionSupervisor {
11931242
cancelRawOrderPayloads,
11941243
transferToSubaccountPayload,
11951244
payload.placePayloads,
1196-
TransactionMemo.placeOrder
1245+
TransactionMemo.placeOrder,
1246+
Method.BroadcastTxSync
11971247
);
11981248

11991249
if ((tx as IndexedTx | undefined)?.code !== 0) {

0 commit comments

Comments
 (0)