From 377a08d53c9d7f503e52b4ccc59d450db0fbc29a Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 28 Sep 2023 11:55:38 -0400 Subject: [PATCH] Address PR comments --- v4-client-js/examples/cancel_example.ts | 8 ++++++++ v4-client-js/package-lock.json | 4 ++-- v4-client-js/package.json | 2 +- v4-client-js/src/clients/composite-client.ts | 6 +++++- v4-client-js/src/lib/validation.ts | 5 +++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/v4-client-js/examples/cancel_example.ts b/v4-client-js/examples/cancel_example.ts index 0500a38c..4537821e 100644 --- a/v4-client-js/examples/cancel_example.ts +++ b/v4-client-js/examples/cancel_example.ts @@ -21,6 +21,14 @@ async function test(): Promise { console.log(client); const subaccount = new Subaccount(wallet, 0); + /* + Note this example places a stateful order. + Programmatic traders should generally not use stateful orders for following reasons: + - Stateful orders received out of order by validators will fail sequence number validation and be dropped. + - Stateful orders have worse time priority since they are only matched after they are included on the block. + - Stateful order rate limits are more restrictive than Short-Term orders, specifically max 2 per block / 20 per 100 blocks. + - Stateful orders can only be canceled after they’ve been included in a block. + */ const longTermOrderClientId = randomInt(MAX_CLIENT_ID); try { // place a long term order diff --git a/v4-client-js/package-lock.json b/v4-client-js/package-lock.json index 189c4c5a..9f43e208 100644 --- a/v4-client-js/package-lock.json +++ b/v4-client-js/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dydxprotocol/v4-client-js", - "version": "0.38.1", + "version": "0.38.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@dydxprotocol/v4-client-js", - "version": "0.38.1", + "version": "0.38.2", "license": "BSL-1.1", "dependencies": { "@cosmjs/amino": "^0.30.1", diff --git a/v4-client-js/package.json b/v4-client-js/package.json index 3534e35d..da0bfab1 100644 --- a/v4-client-js/package.json +++ b/v4-client-js/package.json @@ -1,6 +1,6 @@ { "name": "@dydxprotocol/v4-client-js", - "version": "0.38.1", + "version": "0.38.2", "description": "General client library for the new dYdX system (v4 decentralized)", "main": "build/src/index.js", "scripts": { diff --git a/v4-client-js/src/clients/composite-client.ts b/v4-client-js/src/clients/composite-client.ts index 8ca60ab0..ca39e6df 100644 --- a/v4-client-js/src/clients/composite-client.ts +++ b/v4-client-js/src/clients/composite-client.ts @@ -5,7 +5,7 @@ import { Order_ConditionType, Order_TimeInForce } from '@dydxprotocol/v4-proto/s import Long from 'long'; import protobuf from 'protobufjs'; -import { isStatefulOrder } from '../lib/validation'; +import { isStatefulOrder, verifyOrderFlags } from '../lib/validation'; import { OrderFlags } from '../types'; import { DYDX_DENOM, @@ -519,6 +519,10 @@ export class CompositeClient { const market = marketsResponse.markets[marketId]; const clobPairId = market.clobPairId; + if (!verifyOrderFlags(orderFlags)) { + throw new Error('Invalid order flags: ' + orderFlags); + } + let goodTilBlockTime; if (isStatefulOrder(orderFlags)) { if (goodTilTimeInSeconds === 0) { diff --git a/v4-client-js/src/lib/validation.ts b/v4-client-js/src/lib/validation.ts index 962ddd04..3a2763ff 100644 --- a/v4-client-js/src/lib/validation.ts +++ b/v4-client-js/src/lib/validation.ts @@ -131,6 +131,11 @@ function verifyNumberIsUint32(num: number): boolean { return num >= 0 && num <= MAX_UINT_32; } +export function verifyOrderFlags(orderFlags: OrderFlags): boolean { + return orderFlags === OrderFlags.SHORT_TERM || + orderFlags === OrderFlags.LONG_TERM || orderFlags === OrderFlags.CONDITIONAL; +} + export function isStatefulOrder(orderFlags: OrderFlags): boolean { return orderFlags === OrderFlags.LONG_TERM || orderFlags === OrderFlags.CONDITIONAL; }