@dydxprotocol/v4-client-js 0.38.1 → 0.38.3

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.
Files changed (32) hide show
  1. package/__native__/__ios__/v4-native-client.js +247 -753
  2. package/__native__/__ios__/v4-native-client.js.map +1 -1
  3. package/build/examples/composite_example.js +2 -2
  4. package/build/examples/constants.d.ts +1 -0
  5. package/build/examples/constants.js +3 -2
  6. package/build/examples/long_term_order_cancel_example.js +62 -0
  7. package/build/examples/short_term_order_cancel_example.d.ts +1 -0
  8. package/build/examples/short_term_order_cancel_example.js +53 -0
  9. package/build/examples/short_term_order_composite_example.d.ts +1 -0
  10. package/build/examples/{short_term_composite_example.js → short_term_order_composite_example.js} +1 -1
  11. package/build/examples/test.d.ts +1 -0
  12. package/build/examples/test.js +49 -0
  13. package/build/src/clients/composite-client.d.ts +5 -5
  14. package/build/src/clients/composite-client.js +33 -7
  15. package/build/src/clients/native.d.ts +3 -3
  16. package/build/src/clients/native.js +21 -6
  17. package/build/src/lib/utils.d.ts +6 -0
  18. package/build/src/lib/utils.js +11 -2
  19. package/build/src/lib/validation.d.ts +3 -1
  20. package/build/src/lib/validation.js +8 -2
  21. package/examples/composite_example.ts +2 -2
  22. package/examples/constants.ts +2 -0
  23. package/examples/long_term_order_cancel_example.ts +82 -0
  24. package/examples/short_term_order_cancel_example.ts +69 -0
  25. package/examples/test.ts +60 -0
  26. package/package.json +1 -1
  27. package/src/clients/composite-client.ts +39 -8
  28. package/src/clients/native.ts +20 -5
  29. package/src/lib/utils.ts +9 -0
  30. package/src/lib/validation.ts +6 -1
  31. /package/build/examples/{short_term_composite_example.d.ts → long_term_order_cancel_example.d.ts} +0 -0
  32. /package/examples/{short_term_composite_example.ts → short_term_order_composite_example.ts} +0 -0
@@ -5,6 +5,7 @@ import { Order_ConditionType, Order_TimeInForce } from '@dydxprotocol/v4-proto/s
5
5
  import Long from 'long';
6
6
  import protobuf from 'protobufjs';
7
7
 
8
+ import { isStatefulOrder, verifyOrderFlags } from '../lib/validation';
8
9
  import { OrderFlags } from '../types';
9
10
  import {
10
11
  DYDX_DENOM,
@@ -160,13 +161,13 @@ export class CompositeClient {
160
161
  }
161
162
 
162
163
  /**
163
- * @description Calculate the goodTilBlock value for a SHORT_TERM order
164
+ * @description Validate the goodTilBlock value for a SHORT_TERM order
164
165
  *
165
166
  * @param goodTilBlock Number of blocks from the current block height the order will
166
167
  * be valid for.
167
168
  *
168
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
169
- * at any point.
169
+ * @throws UserError if the goodTilBlock value is not valid given latest block height and
170
+ * SHORT_BLOCK_WINDOW.
170
171
  */
171
172
  private async validateGoodTilBlock(goodTilBlock: number): Promise<void> {
172
173
  const height = await this.validatorClient.get.latestBlockHeight();
@@ -238,8 +239,8 @@ export class CompositeClient {
238
239
  price,
239
240
  size,
240
241
  clientId,
241
- timeInForce,
242
242
  goodTilBlock,
243
+ timeInForce,
243
244
  reduceOnly,
244
245
  );
245
246
  msg.then((it) => resolve([it])).catch((err) => {
@@ -497,7 +498,7 @@ export class CompositeClient {
497
498
  * @param subaccount The subaccount to cancel the order from
498
499
  * @param clientId The client id of the order to cancel
499
500
  * @param orderFlags The order flags of the order to cancel
500
- * @param clobPairId The clob pair id of the order to cancel
501
+ * @param marketId The market to cancel the order on
501
502
  * @param goodTilBlock The goodTilBlock of the order to cancel
502
503
  * @param goodTilBlockTime The goodTilBlockTime of the order to cancel
503
504
  *
@@ -509,10 +510,40 @@ export class CompositeClient {
509
510
  subaccount: Subaccount,
510
511
  clientId: number,
511
512
  orderFlags: OrderFlags,
512
- clobPairId: number,
513
- goodTilBlock?: number,
514
- goodTilBlockTime?: number,
513
+ marketId: string,
514
+ goodTilBlock: number,
515
+ goodTilTimeInSeconds: number,
515
516
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
517
+
518
+ const marketsResponse = await this.indexerClient.markets.getPerpetualMarkets(marketId);
519
+ const market = marketsResponse.markets[marketId];
520
+ const clobPairId = market.clobPairId;
521
+
522
+ if (!verifyOrderFlags(orderFlags)) {
523
+ throw new Error(`Invalid order flags: ${orderFlags}`);
524
+ }
525
+
526
+ let goodTilBlockTime;
527
+ if (isStatefulOrder(orderFlags)) {
528
+ if (goodTilTimeInSeconds === 0) {
529
+ throw new Error('goodTilTimeInSeconds must be set for LONG_TERM or CONDITIONAL order');
530
+ }
531
+ if (goodTilBlock !== 0) {
532
+ throw new Error(
533
+ 'goodTilBlock should be zero since LONG_TERM or CONDITIONAL orders ' +
534
+ 'use goodTilTimeInSeconds instead of goodTilBlock.',
535
+ );
536
+ }
537
+ goodTilBlockTime = this.calculateGoodTilBlockTime(goodTilTimeInSeconds);
538
+ } else {
539
+ if (goodTilBlock === 0) {
540
+ throw new Error('goodTilBlock must be non-zero for SHORT_TERM orders');
541
+ }
542
+ if (goodTilTimeInSeconds !== 0) {
543
+ throw new Error('goodTilTimeInSeconds should be zero since SHORT_TERM orders use goodTilBlock instead of goodTilTimeInSeconds.');
544
+ }
545
+ }
546
+
516
547
  return this.validatorClient.post.cancelOrder(
517
548
  subaccount,
518
549
  clientId,
@@ -901,15 +901,20 @@ export async function getRewardsParams(): Promise<string> {
901
901
  }
902
902
 
903
903
  export async function getDelegatorDelegations(
904
- delegatorAddress: string,
904
+ payload: string,
905
905
  ): Promise<string> {
906
906
  try {
907
907
  const client = globalThis.client;
908
908
  if (client === undefined) {
909
909
  throw new UserError('client is not connected. Call connectClient() first');
910
910
  }
911
+ const json = JSON.parse(payload);
912
+ const address = json.address;
913
+ if (address === undefined) {
914
+ throw new UserError('address is not set');
915
+ }
911
916
  const delegations = await globalThis
912
- .client?.validatorClient.get.getDelegatorDelegations(delegatorAddress);
917
+ .client?.validatorClient.get.getDelegatorDelegations(address);
913
918
  return encodeJson(delegations);
914
919
  } catch (e) {
915
920
  return wrappedError(e);
@@ -917,15 +922,20 @@ export async function getDelegatorDelegations(
917
922
  }
918
923
 
919
924
  export async function getDelegatorUnbondingDelegations(
920
- delegatorAddress: string,
925
+ payload: string,
921
926
  ): Promise<string> {
922
927
  try {
923
928
  const client = globalThis.client;
924
929
  if (client === undefined) {
925
930
  throw new UserError('client is not connected. Call connectClient() first');
926
931
  }
932
+ const json = JSON.parse(payload);
933
+ const address = json.address;
934
+ if (address === undefined) {
935
+ throw new UserError('address is not set');
936
+ }
927
937
  const delegations = await globalThis
928
- .client?.validatorClient.get.getDelegatorUnbondingDelegations(delegatorAddress);
938
+ .client?.validatorClient.get.getDelegatorUnbondingDelegations(address);
929
939
  return encodeJson(delegations);
930
940
  } catch (e) {
931
941
  return wrappedError(e);
@@ -933,13 +943,18 @@ export async function getDelegatorUnbondingDelegations(
933
943
  }
934
944
 
935
945
  export async function getMarketPrice(
936
- marketId: number,
946
+ payload: string,
937
947
  ): Promise<string> {
938
948
  try {
939
949
  const client = globalThis.client;
940
950
  if (client === undefined) {
941
951
  throw new UserError('client is not connected. Call connectClient() first');
942
952
  }
953
+ const json = JSON.parse(payload);
954
+ const marketId = json.marketId;
955
+ if (marketId === undefined) {
956
+ throw new UserError('marketId is not set');
957
+ }
943
958
  const marketPrice = await client.validatorClient.get.getPrice(marketId);
944
959
  return encodeJson(marketPrice);
945
960
  } catch (e) {
package/src/lib/utils.ts CHANGED
@@ -34,3 +34,12 @@ export function clientIdFromString(
34
34
  // We must coerce this into a 32-bit unsigned integer.
35
35
  return hash + (2 ** 31);
36
36
  }
37
+
38
+ /**
39
+ * Pauses the execution of the program for a specified time.
40
+ * @param ms - The number of milliseconds to pause the program.
41
+ * @returns A promise that resolves after the specified number of milliseconds.
42
+ */
43
+ export async function sleep(ms: number): Promise<void> {
44
+ return new Promise((resolve) => setTimeout(resolve, ms));
45
+ }
@@ -131,7 +131,12 @@ function verifyNumberIsUint32(num: number): boolean {
131
131
  return num >= 0 && num <= MAX_UINT_32;
132
132
  }
133
133
 
134
- function isStatefulOrder(orderFlags: OrderFlags): boolean {
134
+ export function verifyOrderFlags(orderFlags: OrderFlags): boolean {
135
+ return orderFlags === OrderFlags.SHORT_TERM ||
136
+ orderFlags === OrderFlags.LONG_TERM || orderFlags === OrderFlags.CONDITIONAL;
137
+ }
138
+
139
+ export function isStatefulOrder(orderFlags: OrderFlags): boolean {
135
140
  return orderFlags === OrderFlags.LONG_TERM || orderFlags === OrderFlags.CONDITIONAL;
136
141
  }
137
142