Skip to content

Commit

Permalink
[IND-511] e2e test for limit order matching (#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill authored Jan 2, 2024
1 parent 992b2b2 commit 19313c5
Show file tree
Hide file tree
Showing 18 changed files with 653 additions and 54 deletions.
30 changes: 30 additions & 0 deletions e2e-testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,33 @@ In another terminal, run
```
pnpm build && pnpm test
```

#### Quickest way to reset the network/clear all Indexer data sources without rebuilding from scratch

Go to Docker Desktop

Stop all containers

Delete all dydxprotocold* containers

Reset the protocol by doing the following:
```
cd ../protocol
make reset-chain
```

Delete the postgres container.

Restart the Kafka container.

Clear all Kafka topics:
```
docker cp remove-all-kafka-msgs.sh <container_id>:/opt/kafka
docker exec -it <container_id> /bin/bash
./remove-all-kafka-msgs.sh
```

Restart all containers:
```
docker compose -f docker-compose-e2e-test.yml up
```
56 changes: 56 additions & 0 deletions e2e-testing/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
import {
IPlaceOrder, Order_Side, Order_TimeInForce, OrderFlags,
} from '@dydxprotocol/v4-client-js';
import Long from 'long';

import { OrderDetails } from './types';

export const DYDX_LOCAL_ADDRESS = 'dydx199tqg4wdlnu4qjlxchpd7seg454937hjrknju4';
export const DYDX_LOCAL_MNEMONIC = 'merge panther lobster crazy road hollow amused security before critic about cliff exhibit cause coyote talent happy where lion river tobacco option coconut small';
export const DYDX_LOCAL_ADDRESS_2 = 'dydx10fx7sy6ywd5senxae9dwytf8jxek3t2gcen2vs';
export const DYDX_LOCAL_MNEMONIC_2 = 'color habit donor nurse dinosaur stable wonder process post perfect raven gold census inside worth inquiry mammal panic olive toss shadow strong name drum';

export const MNEMONIC_TO_ADDRESS: Record<string, string> = {
[DYDX_LOCAL_MNEMONIC]: DYDX_LOCAL_ADDRESS,
[DYDX_LOCAL_MNEMONIC_2]: DYDX_LOCAL_ADDRESS_2,
};

export const ADDRESS_TO_MNEMONIC: Record<string, string> = {
[DYDX_LOCAL_ADDRESS]: DYDX_LOCAL_MNEMONIC,
[DYDX_LOCAL_ADDRESS_2]: DYDX_LOCAL_MNEMONIC_2,
};

export const PERPETUAL_PAIR_BTC_USD: number = 0;
export const quantums: Long = new Long(1_000_000_000);
export const subticks: Long = new Long(1_000_000_000);

export const defaultOrder: IPlaceOrder = {
clientId: 0,
orderFlags: OrderFlags.SHORT_TERM,
clobPairId: PERPETUAL_PAIR_BTC_USD,
side: Order_Side.SIDE_BUY,
quantums,
subticks,
timeInForce: Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED,
reduceOnly: false,
clientMetadata: 0,
};

export const orderDetails: OrderDetails[] = [
{
mnemonic: DYDX_LOCAL_MNEMONIC,
timeInForce: 0,
orderFlags: 64,
side: 1,
clobPairId: PERPETUAL_PAIR_BTC_USD,
quantums: 10000000,
subticks: 5000000000,
},
{
mnemonic: DYDX_LOCAL_MNEMONIC_2,
timeInForce: 0,
orderFlags: 64,
side: 2,
clobPairId: PERPETUAL_PAIR_BTC_USD,
quantums: 5000000,
subticks: 5000000000,
},
];
9 changes: 9 additions & 0 deletions e2e-testing/__tests__/helpers/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type OrderDetails = {
mnemonic: string;
timeInForce: number;
orderFlags: number;
side: number;
clobPairId: number;
quantums: number;
subticks: number;
};
37 changes: 37 additions & 0 deletions e2e-testing/__tests__/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
import { IPlaceOrder, Network, SocketClient } from '@dydxprotocol/v4-client-js';
import Long from 'long';

import { defaultOrder } from './constants';
import { OrderDetails } from './types';

export async function sleep(milliseconds: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}

export function createModifiedOrder(
order: OrderDetails,
): IPlaceOrder {
const modifiedOrder: IPlaceOrder = defaultOrder;
modifiedOrder.clientId = Math.floor(Math.random() * 1000000000);
modifiedOrder.goodTilBlock = 0;
modifiedOrder.clobPairId = order.clobPairId;
modifiedOrder.timeInForce = order.timeInForce;
modifiedOrder.reduceOnly = false;
modifiedOrder.orderFlags = order.orderFlags;
modifiedOrder.side = order.side;
modifiedOrder.quantums = Long.fromNumber(order.quantums);
modifiedOrder.subticks = Long.fromNumber(order.subticks);
return modifiedOrder;
}

export function connectAndValidateSocketClient(validateMessage: Function): void {
const mySocket = new SocketClient(
Network.local().indexerConfig,
() => {},
() => {},
(message) => {
if (typeof message.data === 'string') {
const data = JSON.parse(message.data as string);
validateMessage(data, mySocket);
}
},
);
mySocket.connect();
}
Loading

0 comments on commit 19313c5

Please sign in to comment.