@dydxprotocol/v4-client-js 1.1.12 → 1.1.14

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 (150) hide show
  1. package/.eslintrc.js +3 -5
  2. package/.prettierignore +3 -0
  3. package/.prettierrc.json +6 -0
  4. package/.telescope.json +2 -4
  5. package/.vscode/launch.json +10 -10
  6. package/README.md +3 -1
  7. package/__native__/__ios__/v4-native-client.js +2648 -806
  8. package/__tests__/helpers/constants.ts +10 -8
  9. package/__tests__/lib/helpers.test.ts +22 -23
  10. package/__tests__/lib/util.test.ts +2 -8
  11. package/__tests__/lib/validation.test.ts +23 -26
  12. package/__tests__/modules/client/Transfers.test.ts +4 -19
  13. package/__tests__/modules/client/ValidatorPostEndpoints.test.ts +2 -5
  14. package/__tests__/modules/client/constants.ts +4 -2
  15. package/__tests__/modules/onboarding.test.ts +9 -7
  16. package/build/__tests__/helpers/constants.js +7 -7
  17. package/build/__tests__/lib/helpers.test.js +2 -6
  18. package/build/__tests__/lib/util.test.js +1 -1
  19. package/build/__tests__/lib/validation.test.js +3 -11
  20. package/build/__tests__/modules/client/Transfers.test.js +1 -1
  21. package/build/__tests__/modules/client/ValidatorPostEndpoints.test.js +2 -2
  22. package/build/__tests__/modules/client/constants.js +1 -1
  23. package/build/__tests__/modules/onboarding.test.js +2 -3
  24. package/build/examples/account_endpoints.js +4 -3
  25. package/build/examples/composite_example.js +5 -4
  26. package/build/examples/constants.js +1 -1
  27. package/build/examples/faucet_endpoint.js +4 -3
  28. package/build/examples/long_term_order_cancel_example.js +4 -3
  29. package/build/examples/markets_endpoints.js +4 -3
  30. package/build/examples/native_examples.js +4 -3
  31. package/build/examples/noble_example.js +1 -1
  32. package/build/examples/optimal_node.js +4 -12
  33. package/build/examples/short_term_order_cancel_example.js +4 -3
  34. package/build/examples/short_term_order_composite_example.js +4 -3
  35. package/build/examples/test.js +5 -4
  36. package/build/examples/transfer_example_deposit.js +4 -3
  37. package/build/examples/transfer_example_send.js +1 -1
  38. package/build/examples/transfer_example_subaccount_transfer.js +4 -3
  39. package/build/examples/transfer_example_withdraw.js +4 -3
  40. package/build/examples/transfer_example_withdraw_other.js +1 -1
  41. package/build/examples/utility_endpoints.js +4 -3
  42. package/build/examples/validator_get_example.js +15 -9
  43. package/build/examples/validator_post_example.js +4 -3
  44. package/build/examples/wallet_address.js +5 -4
  45. package/build/examples/websocket_example.js +1 -1
  46. package/build/src/clients/composite-client.d.ts +225 -224
  47. package/build/src/clients/composite-client.js +242 -237
  48. package/build/src/clients/constants.d.ts +3 -0
  49. package/build/src/clients/constants.js +6 -2
  50. package/build/src/clients/faucet-client.d.ts +4 -4
  51. package/build/src/clients/faucet-client.js +5 -5
  52. package/build/src/clients/helpers/chain-helpers.js +1 -1
  53. package/build/src/clients/helpers/request-helpers.js +5 -4
  54. package/build/src/clients/indexer-client.js +1 -1
  55. package/build/src/clients/lib/axios/axiosRequest.js +1 -1
  56. package/build/src/clients/lib/axios/errors.js +1 -1
  57. package/build/src/clients/lib/cctpProto.d.ts +2 -2
  58. package/build/src/clients/lib/cctpProto.js +287 -237
  59. package/build/src/clients/lib/errors.js +1 -1
  60. package/build/src/clients/modules/account.js +1 -1
  61. package/build/src/clients/modules/composer.d.ts +4 -0
  62. package/build/src/clients/modules/composer.js +39 -3
  63. package/build/src/clients/modules/get.d.ts +10 -3
  64. package/build/src/clients/modules/get.js +41 -54
  65. package/build/src/clients/modules/local-wallet.js +1 -1
  66. package/build/src/clients/modules/post.d.ts +3 -0
  67. package/build/src/clients/modules/post.js +34 -18
  68. package/build/src/clients/modules/proto-includes.d.ts +1 -0
  69. package/build/src/clients/modules/proto-includes.js +3 -2
  70. package/build/src/clients/modules/rest.js +1 -1
  71. package/build/src/clients/modules/signer.js +1 -1
  72. package/build/src/clients/modules/tendermintClient.d.ts +5 -5
  73. package/build/src/clients/modules/tendermintClient.js +6 -6
  74. package/build/src/clients/native.js +4 -7
  75. package/build/src/clients/noble-client.js +1 -1
  76. package/build/src/clients/socket-client.js +1 -1
  77. package/build/src/clients/subaccount.js +1 -1
  78. package/build/src/clients/types.d.ts +1 -1
  79. package/build/src/clients/types.js +1 -1
  80. package/build/src/clients/validator-client.d.ts +12 -12
  81. package/build/src/clients/validator-client.js +13 -13
  82. package/build/src/lib/errors.js +1 -1
  83. package/build/src/lib/onboarding.js +1 -1
  84. package/build/src/lib/utils.js +3 -3
  85. package/build/src/lib/validation.js +5 -4
  86. package/build/src/network_optimizer.js +10 -11
  87. package/build/src/types.d.ts +1 -1
  88. package/build/src/types.js +1 -1
  89. package/examples/account_endpoints.ts +5 -4
  90. package/examples/composite_example.ts +12 -7
  91. package/examples/constants.ts +10 -4
  92. package/examples/faucet_endpoint.ts +5 -4
  93. package/examples/human_readable_orders.json +85 -85
  94. package/examples/human_readable_short_term_orders.json +41 -41
  95. package/examples/long_term_order_cancel_example.ts +11 -6
  96. package/examples/markets_endpoints.ts +5 -4
  97. package/examples/native_examples.ts +8 -7
  98. package/examples/noble_example.ts +11 -23
  99. package/examples/optimal_node.ts +4 -14
  100. package/examples/raw_orders.json +128 -128
  101. package/examples/short_term_order_cancel_example.ts +7 -8
  102. package/examples/short_term_order_composite_example.ts +7 -8
  103. package/examples/test.ts +12 -7
  104. package/examples/transfer_example_deposit.ts +6 -9
  105. package/examples/transfer_example_send.ts +2 -10
  106. package/examples/transfer_example_subaccount_transfer.ts +6 -11
  107. package/examples/transfer_example_withdraw.ts +6 -9
  108. package/examples/transfer_example_withdraw_other.ts +1 -5
  109. package/examples/utility_endpoints.ts +5 -4
  110. package/examples/validator_get_example.ts +19 -10
  111. package/examples/validator_post_example.ts +7 -9
  112. package/examples/wallet_address.ts +6 -5
  113. package/examples/websocket_example.ts +5 -1
  114. package/package.json +4 -1
  115. package/src/clients/composite-client.ts +279 -330
  116. package/src/clients/constants.ts +38 -26
  117. package/src/clients/faucet-client.ts +5 -8
  118. package/src/clients/helpers/chain-helpers.ts +10 -6
  119. package/src/clients/helpers/request-helpers.ts +6 -5
  120. package/src/clients/indexer-client.ts +34 -34
  121. package/src/clients/lib/axios/axiosRequest.ts +4 -7
  122. package/src/clients/lib/axios/errors.ts +16 -21
  123. package/src/clients/lib/cctpProto.ts +655 -387
  124. package/src/clients/lib/errors.ts +8 -17
  125. package/src/clients/modules/account.ts +4 -12
  126. package/src/clients/modules/composer.ts +61 -20
  127. package/src/clients/modules/get.ts +71 -89
  128. package/src/clients/modules/local-wallet.ts +41 -56
  129. package/src/clients/modules/post.ts +664 -634
  130. package/src/clients/modules/proto-includes.ts +1 -0
  131. package/src/clients/modules/rest.ts +23 -26
  132. package/src/clients/modules/signer.ts +8 -22
  133. package/src/clients/modules/tendermintClient.ts +13 -36
  134. package/src/clients/native.ts +73 -139
  135. package/src/clients/noble-client.ts +1 -5
  136. package/src/clients/socket-client.ts +216 -216
  137. package/src/clients/subaccount.ts +16 -16
  138. package/src/clients/types.ts +14 -10
  139. package/src/clients/validator-client.ts +14 -16
  140. package/src/codegen/helpers.ts +149 -150
  141. package/src/codegen/index.ts +4 -5
  142. package/src/lib/errors.ts +3 -9
  143. package/src/lib/onboarding.ts +7 -2
  144. package/src/lib/utils.ts +5 -12
  145. package/src/lib/validation.ts +21 -19
  146. package/src/network_optimizer.ts +78 -65
  147. package/src/types.ts +5 -6
  148. package/tasks.json +15 -15
  149. package/tsconfig.eslint.json +1 -4
  150. package/tsconfig.json +2 -6
@@ -1,9 +1,14 @@
1
1
  import { EncodeObject } from '@cosmjs/proto-signing';
2
+ import { Account, GasPrice, IndexedTx, StdFee } from '@cosmjs/stargate';
3
+ import { Method } from '@cosmjs/tendermint-rpc';
2
4
  import {
3
- Account, GasPrice, IndexedTx, StdFee,
4
- } from '@cosmjs/stargate';
5
- import { BroadcastTxAsyncResponse, BroadcastTxSyncResponse } from '@cosmjs/tendermint-rpc/build/tendermint37';
6
- import { Order_ConditionType, Order_TimeInForce } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/order';
5
+ BroadcastTxAsyncResponse,
6
+ BroadcastTxSyncResponse,
7
+ } from '@cosmjs/tendermint-rpc/build/tendermint37';
8
+ import {
9
+ Order_ConditionType,
10
+ Order_TimeInForce,
11
+ } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/order';
7
12
  import { parseUnits } from 'ethers';
8
13
  import Long from 'long';
9
14
  import protobuf from 'protobufjs';
@@ -35,6 +40,7 @@ import { UserError } from './lib/errors';
35
40
  import { generateRegistry } from './lib/registry';
36
41
  import LocalWallet from './modules/local-wallet';
37
42
  import { SubaccountInfo } from './subaccount';
43
+ import { BroadcastMode } from './types';
38
44
  import { ValidatorClient } from './validator-client';
39
45
 
40
46
  // Required for encoding and decoding queries that are of type Long.
@@ -64,15 +70,9 @@ export class CompositeClient {
64
70
  return client;
65
71
  }
66
72
 
67
- private constructor(
68
- network: Network,
69
- apiTimeout?: number,
70
- ) {
73
+ private constructor(network: Network, apiTimeout?: number) {
71
74
  this.network = network;
72
- this._indexerClient = new IndexerClient(
73
- network.indexerConfig,
74
- apiTimeout,
75
- );
75
+ this._indexerClient = new IndexerClient(network.indexerConfig, apiTimeout);
76
76
  }
77
77
 
78
78
  private async initialize(): Promise<void> {
@@ -104,13 +104,13 @@ export class CompositeClient {
104
104
  }
105
105
 
106
106
  /**
107
- * @description Sign a list of messages with a wallet.
108
- * the calling function is responsible for creating the messages.
109
- *
110
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
111
- * at any point.
112
- * @returns The Signature.
113
- */
107
+ * @description Sign a list of messages with a wallet.
108
+ * the calling function is responsible for creating the messages.
109
+ *
110
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
111
+ * at any point.
112
+ * @returns The Signature.
113
+ */
114
114
  async sign(
115
115
  wallet: LocalWallet,
116
116
  messaging: () => Promise<EncodeObject[]>,
@@ -119,30 +119,24 @@ export class CompositeClient {
119
119
  memo?: string,
120
120
  account?: () => Promise<Account>,
121
121
  ): Promise<Uint8Array> {
122
- return this.validatorClient.post.sign(
123
- wallet,
124
- messaging,
125
- zeroFee,
126
- gasPrice,
127
- memo,
128
- account,
129
- );
122
+ return this.validatorClient.post.sign(wallet, messaging, zeroFee, gasPrice, memo, account);
130
123
  }
131
124
 
132
125
  /**
133
- * @description Send a list of messages with a wallet.
134
- * the calling function is responsible for creating the messages.
135
- *
136
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
137
- * at any point.
138
- * @returns The Transaction Hash.
139
- */
126
+ * @description Send a list of messages with a wallet.
127
+ * the calling function is responsible for creating the messages.
128
+ *
129
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
130
+ * at any point.
131
+ * @returns The Transaction Hash.
132
+ */
140
133
  async send(
141
134
  wallet: LocalWallet,
142
135
  messaging: () => Promise<EncodeObject[]>,
143
136
  zeroFee: boolean,
144
137
  gasPrice?: GasPrice,
145
138
  memo?: string,
139
+ broadcastMode?: BroadcastMode,
146
140
  account?: () => Promise<Account>,
147
141
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
148
142
  return this.validatorClient.post.send(
@@ -151,20 +145,20 @@ export class CompositeClient {
151
145
  zeroFee,
152
146
  gasPrice,
153
147
  memo,
154
- undefined,
148
+ broadcastMode,
155
149
  account,
156
150
  );
157
151
  }
158
152
 
159
153
  /**
160
- * @description Send a signed transaction.
161
- *
162
- * @param signedTransaction The signed transaction to send.
163
- *
164
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
165
- * at any point.
166
- * @returns The Transaction Hash.
167
- */
154
+ * @description Send a signed transaction.
155
+ *
156
+ * @param signedTransaction The signed transaction to send.
157
+ *
158
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
159
+ * at any point.
160
+ * @returns The Transaction Hash.
161
+ */
168
162
  async sendSignedTransaction(
169
163
  signedTransaction: Uint8Array,
170
164
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
@@ -172,18 +166,18 @@ export class CompositeClient {
172
166
  }
173
167
 
174
168
  /**
175
- * @description Simulate a list of messages with a wallet.
176
- * the calling function is responsible for creating the messages.
177
- *
178
- * To send multiple messages with gas estimate:
179
- * 1. Client is responsible for creating the messages.
180
- * 2. Call simulate() to get the gas estimate.
181
- * 3. Call send() to send the messages.
182
- *
183
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
184
- * at any point.
185
- * @returns The gas estimate.
186
- */
169
+ * @description Simulate a list of messages with a wallet.
170
+ * the calling function is responsible for creating the messages.
171
+ *
172
+ * To send multiple messages with gas estimate:
173
+ * 1. Client is responsible for creating the messages.
174
+ * 2. Call simulate() to get the gas estimate.
175
+ * 3. Call send() to send the messages.
176
+ *
177
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
178
+ * at any point.
179
+ * @returns The gas estimate.
180
+ */
187
181
  async simulate(
188
182
  wallet: LocalWallet,
189
183
  messaging: () => Promise<EncodeObject[]>,
@@ -191,22 +185,16 @@ export class CompositeClient {
191
185
  memo?: string,
192
186
  account?: () => Promise<Account>,
193
187
  ): Promise<StdFee> {
194
- return this.validatorClient.post.simulate(
195
- wallet,
196
- messaging,
197
- gasPrice,
198
- memo,
199
- account,
200
- );
188
+ return this.validatorClient.post.simulate(wallet, messaging, gasPrice, memo, account);
201
189
  }
202
190
 
203
191
  /**
204
- * @description Calculate the goodTilBlock value for a SHORT_TERM order
205
- *
206
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
207
- * at any point.
208
- * @returns The goodTilBlock value
209
- */
192
+ * @description Calculate the goodTilBlock value for a SHORT_TERM order
193
+ *
194
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
195
+ * at any point.
196
+ * @returns The goodTilBlock value
197
+ */
210
198
 
211
199
  private async calculateGoodTilBlock(
212
200
  orderFlags: OrderFlags,
@@ -217,7 +205,7 @@ export class CompositeClient {
217
205
  if (goodTilBlock !== undefined && goodTilBlock !== 0) {
218
206
  return Promise.resolve(goodTilBlock);
219
207
  } else {
220
- const height = currentHeight ?? await this.validatorClient.get.latestBlockHeight();
208
+ const height = currentHeight ?? (await this.validatorClient.get.latestBlockHeight());
221
209
  return height + SHORT_BLOCK_FORWARD;
222
210
  }
223
211
  } else {
@@ -247,15 +235,15 @@ export class CompositeClient {
247
235
  }
248
236
 
249
237
  /**
250
- * @description Calculate the goodTilBlockTime value for a LONG_TERM order
251
- * the calling function is responsible for creating the messages.
252
- *
253
- * @param goodTilTimeInSeconds The goodTilTimeInSeconds of the order to place.
254
- *
255
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
256
- * at any point.
257
- * @returns The goodTilBlockTime value
258
- */
238
+ * @description Calculate the goodTilBlockTime value for a LONG_TERM order
239
+ * the calling function is responsible for creating the messages.
240
+ *
241
+ * @param goodTilTimeInSeconds The goodTilTimeInSeconds of the order to place.
242
+ *
243
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
244
+ * at any point.
245
+ * @returns The goodTilBlockTime value
246
+ */
259
247
  private calculateGoodTilBlockTime(goodTilTimeInSeconds: number): number {
260
248
  const now = new Date();
261
249
  const millisecondsPerSecond = 1000;
@@ -309,10 +297,12 @@ export class CompositeClient {
309
297
  timeInForce,
310
298
  reduceOnly,
311
299
  );
312
- msg.then((it) => resolve([it])).catch((err) => {
313
- console.log(err);
314
- reject(err);
315
- });
300
+ msg
301
+ .then((it) => resolve([it]))
302
+ .catch((err) => {
303
+ console.log(err);
304
+ reject(err);
305
+ });
316
306
  });
317
307
  const account: Promise<Account> = this.validatorClient.post.account(
318
308
  subaccount.address,
@@ -324,41 +314,42 @@ export class CompositeClient {
324
314
  true,
325
315
  undefined,
326
316
  memo,
317
+ undefined,
327
318
  () => account,
328
319
  );
329
320
  }
330
321
 
331
322
  /**
332
- * @description Place an order with human readable input.
333
- *
334
- * Only MARKET and LIMIT types are supported right now
335
- * Use human readable form of input, including price and size
336
- * The quantum and subticks are calculated and submitted
337
- *
338
- * @param subaccount The subaccount to place the order on.
339
- * @param marketId The market to place the order on.
340
- * @param type The type of order to place.
341
- * @param side The side of the order to place.
342
- * @param price The price of the order to place.
343
- * @param size The size of the order to place.
344
- * @param clientId The client id of the order to place.
345
- * @param timeInForce The time in force of the order to place.
346
- * @param goodTilTimeInSeconds The goodTilTimeInSeconds of the order to place.
347
- * @param execution The execution of the order to place.
348
- * @param postOnly The postOnly of the order to place.
349
- * @param reduceOnly The reduceOnly of the order to place.
350
- * @param triggerPrice The trigger price of conditional orders.
351
- * @param marketInfo optional market information for calculating quantums and subticks.
352
- * This can be constructed from Indexer API. If set to null, additional round
353
- * trip to Indexer API will be made.
354
- * @param currentHeight Current block height. This can be obtained from ValidatorClient.
355
- * If set to null, additional round trip to ValidatorClient will be made.
356
- *
357
- *
358
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
359
- * at any point.
360
- * @returns The transaction hash.
361
- */
323
+ * @description Place an order with human readable input.
324
+ *
325
+ * Only MARKET and LIMIT types are supported right now
326
+ * Use human readable form of input, including price and size
327
+ * The quantum and subticks are calculated and submitted
328
+ *
329
+ * @param subaccount The subaccount to place the order on.
330
+ * @param marketId The market to place the order on.
331
+ * @param type The type of order to place.
332
+ * @param side The side of the order to place.
333
+ * @param price The price of the order to place.
334
+ * @param size The size of the order to place.
335
+ * @param clientId The client id of the order to place.
336
+ * @param timeInForce The time in force of the order to place.
337
+ * @param goodTilTimeInSeconds The goodTilTimeInSeconds of the order to place.
338
+ * @param execution The execution of the order to place.
339
+ * @param postOnly The postOnly of the order to place.
340
+ * @param reduceOnly The reduceOnly of the order to place.
341
+ * @param triggerPrice The trigger price of conditional orders.
342
+ * @param marketInfo optional market information for calculating quantums and subticks.
343
+ * This can be constructed from Indexer API. If set to null, additional round
344
+ * trip to Indexer API will be made.
345
+ * @param currentHeight Current block height. This can be obtained from ValidatorClient.
346
+ * If set to null, additional round trip to ValidatorClient will be made.
347
+ *
348
+ *
349
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
350
+ * at any point.
351
+ * @returns The transaction hash.
352
+ */
362
353
  async placeOrder(
363
354
  subaccount: SubaccountInfo,
364
355
  marketId: string,
@@ -385,7 +376,6 @@ export class CompositeClient {
385
376
  type,
386
377
  side,
387
378
  price,
388
- // trigger_price: number, // not used for MARKET and LIMIT
389
379
  size,
390
380
  clientId,
391
381
  timeInForce,
@@ -398,9 +388,11 @@ export class CompositeClient {
398
388
  currentHeight,
399
389
  goodTilBlock,
400
390
  );
401
- msg.then((it) => resolve([it])).catch((err) => {
402
- console.log(err);
403
- });
391
+ msg
392
+ .then((it) => resolve([it]))
393
+ .catch((err) => {
394
+ console.log(err);
395
+ });
404
396
  });
405
397
  const orderFlags = calculateOrderFlags(type, timeInForce);
406
398
  const account: Promise<Account> = this.validatorClient.post.account(
@@ -413,35 +405,36 @@ export class CompositeClient {
413
405
  true,
414
406
  undefined,
415
407
  memo,
408
+ undefined,
416
409
  () => account,
417
410
  );
418
411
  }
419
412
 
420
413
  /**
421
- * @description Calculate and create the place order message
422
- *
423
- * Only MARKET and LIMIT types are supported right now
424
- * Use human readable form of input, including price and size
425
- * The quantum and subticks are calculated and submitted
426
- *
427
- * @param subaccount The subaccount to place the order under
428
- * @param marketId The market to place the order on
429
- * @param type The type of order to place
430
- * @param side The side of the order to place
431
- * @param price The price of the order to place
432
- * @param size The size of the order to place
433
- * @param clientId The client id of the order to place
434
- * @param timeInForce The time in force of the order to place
435
- * @param goodTilTimeInSeconds The goodTilTimeInSeconds of the order to place
436
- * @param execution The execution of the order to place
437
- * @param postOnly The postOnly of the order to place
438
- * @param reduceOnly The reduceOnly of the order to place
439
- *
440
- *
441
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
442
- * at any point.
443
- * @returns The message to be passed into the protocol
444
- */
414
+ * @description Calculate and create the place order message
415
+ *
416
+ * Only MARKET and LIMIT types are supported right now
417
+ * Use human readable form of input, including price and size
418
+ * The quantum and subticks are calculated and submitted
419
+ *
420
+ * @param subaccount The subaccount to place the order under
421
+ * @param marketId The market to place the order on
422
+ * @param type The type of order to place
423
+ * @param side The side of the order to place
424
+ * @param price The price of the order to place
425
+ * @param size The size of the order to place
426
+ * @param clientId The client id of the order to place
427
+ * @param timeInForce The time in force of the order to place
428
+ * @param goodTilTimeInSeconds The goodTilTimeInSeconds of the order to place
429
+ * @param execution The execution of the order to place
430
+ * @param postOnly The postOnly of the order to place
431
+ * @param reduceOnly The reduceOnly of the order to place
432
+ *
433
+ *
434
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
435
+ * at any point.
436
+ * @returns The message to be passed into the protocol
437
+ */
445
438
  private async placeOrderMessage(
446
439
  subaccount: SubaccountInfo,
447
440
  marketId: string,
@@ -466,8 +459,7 @@ export class CompositeClient {
466
459
  const result = await Promise.all([
467
460
  this.calculateGoodTilBlock(orderFlags, currentHeight, goodTilBlock),
468
461
  this.retrieveMarketInfo(marketId, marketInfo),
469
- ],
470
- );
462
+ ]);
471
463
  const desiredGoodTilBlock = result[0];
472
464
  const clobPairId = result[1].clobPairId;
473
465
  const atomicResolution = result[1].atomicResolution;
@@ -475,11 +467,7 @@ export class CompositeClient {
475
467
  const quantumConversionExponent = result[1].quantumConversionExponent;
476
468
  const subticksPerTick = result[1].subticksPerTick;
477
469
  const orderSide = calculateSide(side);
478
- const quantums = calculateQuantums(
479
- size,
480
- atomicResolution,
481
- stepBaseQuantums,
482
- );
470
+ const quantums = calculateQuantums(size, atomicResolution, stepBaseQuantums);
483
471
  const subticks = calculateSubticks(
484
472
  price,
485
473
  atomicResolution,
@@ -502,7 +490,8 @@ export class CompositeClient {
502
490
  atomicResolution,
503
491
  quantumConversionExponent,
504
492
  subticksPerTick,
505
- triggerPrice);
493
+ triggerPrice,
494
+ );
506
495
  return this.validatorClient.post.composer.composeMsgPlaceOrder(
507
496
  subaccount.address,
508
497
  subaccount.subaccountNumber,
@@ -544,26 +533,26 @@ export class CompositeClient {
544
533
  }
545
534
 
546
535
  /**
547
- * @description Calculate and create the short term place order message
548
- *
549
- * Use human readable form of input, including price and size
550
- * The quantum and subticks are calculated and submitted
551
- *
552
- * @param subaccount The subaccount to place the order under
553
- * @param marketId The market to place the order on
554
- * @param side The side of the order to place
555
- * @param price The price of the order to place
556
- * @param size The size of the order to place
557
- * @param clientId The client id of the order to place
558
- * @param timeInForce The time in force of the order to place
559
- * @param goodTilBlock The goodTilBlock of the order to place
560
- * @param reduceOnly The reduceOnly of the order to place
561
- *
562
- *
563
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
564
- * at any point.
565
- * @returns The message to be passed into the protocol
566
- */
536
+ * @description Calculate and create the short term place order message
537
+ *
538
+ * Use human readable form of input, including price and size
539
+ * The quantum and subticks are calculated and submitted
540
+ *
541
+ * @param subaccount The subaccount to place the order under
542
+ * @param marketId The market to place the order on
543
+ * @param side The side of the order to place
544
+ * @param price The price of the order to place
545
+ * @param size The size of the order to place
546
+ * @param clientId The client id of the order to place
547
+ * @param timeInForce The time in force of the order to place
548
+ * @param goodTilBlock The goodTilBlock of the order to place
549
+ * @param reduceOnly The reduceOnly of the order to place
550
+ *
551
+ *
552
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
553
+ * at any point.
554
+ * @returns The message to be passed into the protocol
555
+ */
567
556
  private async placeShortTermOrderMessage(
568
557
  subaccount: SubaccountInfo,
569
558
  marketId: string,
@@ -585,11 +574,7 @@ export class CompositeClient {
585
574
  const quantumConversionExponent = market.quantumConversionExponent;
586
575
  const subticksPerTick = market.subticksPerTick;
587
576
  const orderSide = calculateSide(side);
588
- const quantums = calculateQuantums(
589
- size,
590
- atomicResolution,
591
- stepBaseQuantums,
592
- );
577
+ const quantums = calculateQuantums(size, atomicResolution, stepBaseQuantums);
593
578
  const subticks = calculateSubticks(
594
579
  price,
595
580
  atomicResolution,
@@ -617,19 +602,19 @@ export class CompositeClient {
617
602
  }
618
603
 
619
604
  /**
620
- * @description Cancel an order with order information from web socket or REST.
621
- *
622
- * @param subaccount The subaccount to cancel the order from
623
- * @param clientId The client id of the order to cancel
624
- * @param orderFlags The order flags of the order to cancel
625
- * @param clobPairId The clob pair id of the order to cancel
626
- * @param goodTilBlock The goodTilBlock of the order to cancel
627
- * @param goodTilBlockTime The goodTilBlockTime of the order to cancel
628
- *
629
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
630
- * at any point.
631
- * @returns The transaction hash.
632
- */
605
+ * @description Cancel an order with order information from web socket or REST.
606
+ *
607
+ * @param subaccount The subaccount to cancel the order from
608
+ * @param clientId The client id of the order to cancel
609
+ * @param orderFlags The order flags of the order to cancel
610
+ * @param clobPairId The clob pair id of the order to cancel
611
+ * @param goodTilBlock The goodTilBlock of the order to cancel
612
+ * @param goodTilBlockTime The goodTilBlockTime of the order to cancel
613
+ *
614
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
615
+ * at any point.
616
+ * @returns The transaction hash.
617
+ */
633
618
  async cancelRawOrder(
634
619
  subaccount: SubaccountInfo,
635
620
  clientId: number,
@@ -649,19 +634,19 @@ export class CompositeClient {
649
634
  }
650
635
 
651
636
  /**
652
- * @description Cancel an order with human readable input.
653
- *
654
- * @param subaccount The subaccount to cancel the order from
655
- * @param clientId The client id of the order to cancel
656
- * @param orderFlags The order flags of the order to cancel
657
- * @param marketId The market to cancel the order on
658
- * @param goodTilBlock The goodTilBlock of the order to cancel
659
- * @param goodTilBlockTime The goodTilBlockTime of the order to cancel
660
- *
661
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
662
- * at any point.
663
- * @returns The transaction hash.
664
- */
637
+ * @description Cancel an order with human readable input.
638
+ *
639
+ * @param subaccount The subaccount to cancel the order from
640
+ * @param clientId The client id of the order to cancel
641
+ * @param orderFlags The order flags of the order to cancel
642
+ * @param marketId The market to cancel the order on
643
+ * @param goodTilBlock The goodTilBlock of the order to cancel
644
+ * @param goodTilBlockTime The goodTilBlockTime of the order to cancel
645
+ *
646
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
647
+ * at any point.
648
+ * @returns The transaction hash.
649
+ */
665
650
  async cancelOrder(
666
651
  subaccount: SubaccountInfo,
667
652
  clientId: number,
@@ -670,7 +655,6 @@ export class CompositeClient {
670
655
  goodTilBlock?: number,
671
656
  goodTilTimeInSeconds?: number,
672
657
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
673
-
674
658
  const marketsResponse = await this.indexerClient.markets.getPerpetualMarkets(marketId);
675
659
  const market = marketsResponse.markets[marketId];
676
660
  const clobPairId = market.clobPairId;
@@ -687,7 +671,7 @@ export class CompositeClient {
687
671
  if (goodTilBlock !== 0) {
688
672
  throw new Error(
689
673
  'goodTilBlock should be zero since LONG_TERM or CONDITIONAL orders ' +
690
- 'use goodTilTimeInSeconds instead of goodTilBlock.',
674
+ 'use goodTilTimeInSeconds instead of goodTilBlock.',
691
675
  );
692
676
  }
693
677
  goodTilBlockTime = this.calculateGoodTilBlockTime(goodTilTimeInSeconds);
@@ -696,7 +680,9 @@ export class CompositeClient {
696
680
  throw new Error('goodTilBlock must be non-zero for SHORT_TERM orders');
697
681
  }
698
682
  if (goodTilTimeInSeconds !== undefined && goodTilTimeInSeconds !== 0) {
699
- throw new Error('goodTilTimeInSeconds should be zero since SHORT_TERM orders use goodTilBlock instead of goodTilTimeInSeconds.');
683
+ throw new Error(
684
+ 'goodTilTimeInSeconds should be zero since SHORT_TERM orders use goodTilBlock instead of goodTilTimeInSeconds.',
685
+ );
700
686
  }
701
687
  }
702
688
 
@@ -711,23 +697,24 @@ export class CompositeClient {
711
697
  }
712
698
 
713
699
  /**
714
- * @description Transfer from a subaccount to another subaccount
715
- *
716
- * @param subaccount The subaccount to transfer from
717
- * @param recipientAddress The recipient address
718
- * @param recipientSubaccountNumber The recipient subaccount number
719
- * @param amount The amount to transfer
720
- *
721
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
722
- * at any point.
723
- * @returns The transaction hash.
724
- */
700
+ * @description Transfer from a subaccount to another subaccount
701
+ *
702
+ * @param subaccount The subaccount to transfer from
703
+ * @param recipientAddress The recipient address
704
+ * @param recipientSubaccountNumber The recipient subaccount number
705
+ * @param amount The amount to transfer
706
+ *
707
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
708
+ * at any point.
709
+ * @returns The transaction hash.
710
+ */
725
711
  async transferToSubaccount(
726
712
  subaccount: SubaccountInfo,
727
713
  recipientAddress: string,
728
714
  recipientSubaccountNumber: number,
729
715
  amount: string,
730
716
  memo?: string,
717
+ broadcastMode?: BroadcastMode,
731
718
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
732
719
  const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
733
720
  const msg = this.transferToSubaccountMessage(
@@ -744,22 +731,23 @@ export class CompositeClient {
744
731
  false,
745
732
  undefined,
746
733
  memo,
734
+ broadcastMode ?? Method.BroadcastTxCommit,
747
735
  );
748
736
  }
749
737
 
750
738
  /**
751
- * @description Create message to transfer from a subaccount to another subaccount
752
- *
753
- * @param subaccount The subaccount to transfer from
754
- * @param recipientAddress The recipient address
755
- * @param recipientSubaccountNumber The recipient subaccount number
756
- * @param amount The amount to transfer
757
- *
758
- *
759
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
760
- * at any point.
761
- * @returns The message
762
- */
739
+ * @description Create message to transfer from a subaccount to another subaccount
740
+ *
741
+ * @param subaccount The subaccount to transfer from
742
+ * @param recipientAddress The recipient address
743
+ * @param recipientSubaccountNumber The recipient subaccount number
744
+ * @param amount The amount to transfer
745
+ *
746
+ *
747
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
748
+ * at any point.
749
+ * @returns The message
750
+ */
763
751
  transferToSubaccountMessage(
764
752
  subaccount: SubaccountInfo,
765
753
  recipientAddress: string,
@@ -789,49 +777,38 @@ export class CompositeClient {
789
777
  }
790
778
 
791
779
  /**
792
- * @description Deposit from wallet to subaccount
793
- *
794
- * @param subaccount The subaccount to deposit to
795
- * @param amount The amount to deposit
796
- *
797
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
798
- * at any point.
799
- * @returns The transaction hash.
800
- */
780
+ * @description Deposit from wallet to subaccount
781
+ *
782
+ * @param subaccount The subaccount to deposit to
783
+ * @param amount The amount to deposit
784
+ *
785
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
786
+ * at any point.
787
+ * @returns The transaction hash.
788
+ */
801
789
  async depositToSubaccount(
802
790
  subaccount: SubaccountInfo,
803
791
  amount: string,
804
792
  memo?: string,
805
793
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
806
794
  const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
807
- const msg = this.depositToSubaccountMessage(
808
- subaccount,
809
- amount,
810
- );
795
+ const msg = this.depositToSubaccountMessage(subaccount, amount);
811
796
  resolve([msg]);
812
797
  });
813
- return this.validatorClient.post.send(subaccount.wallet,
814
- () => msgs,
815
- false,
816
- undefined,
817
- memo,
818
- );
798
+ return this.validatorClient.post.send(subaccount.wallet, () => msgs, false, undefined, memo);
819
799
  }
820
800
 
821
801
  /**
822
- * @description Create message to deposit from wallet to subaccount
823
- *
824
- * @param subaccount The subaccount to deposit to
825
- * @param amount The amount to deposit
826
- *
827
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
828
- * at any point.
829
- * @returns The message
830
- */
831
- depositToSubaccountMessage(
832
- subaccount: SubaccountInfo,
833
- amount: string,
834
- ): EncodeObject {
802
+ * @description Create message to deposit from wallet to subaccount
803
+ *
804
+ * @param subaccount The subaccount to deposit to
805
+ * @param amount The amount to deposit
806
+ *
807
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
808
+ * at any point.
809
+ * @returns The message
810
+ */
811
+ depositToSubaccountMessage(subaccount: SubaccountInfo, amount: string): EncodeObject {
835
812
  const validatorClient = this._validatorClient;
836
813
  if (validatorClient === undefined) {
837
814
  throw new Error('validatorClient not set');
@@ -853,16 +830,16 @@ export class CompositeClient {
853
830
  }
854
831
 
855
832
  /**
856
- * @description Withdraw from subaccount to wallet
857
- *
858
- * @param subaccount The subaccount to withdraw from
859
- * @param amount The amount to withdraw
860
- * @param recipient The recipient address, default to subaccount address
861
- *
862
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
863
- * at any point.
864
- * @returns The transaction hash
865
- */
833
+ * @description Withdraw from subaccount to wallet
834
+ *
835
+ * @param subaccount The subaccount to withdraw from
836
+ * @param amount The amount to withdraw
837
+ * @param recipient The recipient address, default to subaccount address
838
+ *
839
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
840
+ * at any point.
841
+ * @returns The transaction hash
842
+ */
866
843
  async withdrawFromSubaccount(
867
844
  subaccount: SubaccountInfo,
868
845
  amount: string,
@@ -870,34 +847,24 @@ export class CompositeClient {
870
847
  memo?: string,
871
848
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
872
849
  const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
873
- const msg = this.withdrawFromSubaccountMessage(
874
- subaccount,
875
- amount,
876
- recipient,
877
- );
850
+ const msg = this.withdrawFromSubaccountMessage(subaccount, amount, recipient);
878
851
  resolve([msg]);
879
852
  });
880
- return this.send(
881
- subaccount.wallet,
882
- () => msgs,
883
- false,
884
- undefined,
885
- memo,
886
- );
853
+ return this.send(subaccount.wallet, () => msgs, false, undefined, memo);
887
854
  }
888
855
 
889
856
  /**
890
- * @description Create message to withdraw from subaccount to wallet
891
- * with human readable input.
892
- *
893
- * @param subaccount The subaccount to withdraw from
894
- * @param amount The amount to withdraw
895
- * @param recipient The recipient address
896
- *
897
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
898
- * at any point.
899
- * @returns The message
900
- */
857
+ * @description Create message to withdraw from subaccount to wallet
858
+ * with human readable input.
859
+ *
860
+ * @param subaccount The subaccount to withdraw from
861
+ * @param amount The amount to withdraw
862
+ * @param recipient The recipient address
863
+ *
864
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
865
+ * at any point.
866
+ * @returns The message
867
+ */
901
868
  withdrawFromSubaccountMessage(
902
869
  subaccount: SubaccountInfo,
903
870
  amount: string,
@@ -925,30 +892,24 @@ export class CompositeClient {
925
892
  }
926
893
 
927
894
  /**
928
- * @description Create message to send chain token from subaccount to wallet
929
- * with human readable input.
930
- *
931
- * @param subaccount The subaccount to withdraw from
932
- * @param amount The amount to withdraw
933
- * @param recipient The recipient address
934
- *
935
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
936
- * at any point.
937
- * @returns The message
938
- */
939
- sendTokenMessage(
940
- wallet: LocalWallet,
941
- amount: string,
942
- recipient: string,
943
- ): EncodeObject {
895
+ * @description Create message to send chain token from subaccount to wallet
896
+ * with human readable input.
897
+ *
898
+ * @param subaccount The subaccount to withdraw from
899
+ * @param amount The amount to withdraw
900
+ * @param recipient The recipient address
901
+ *
902
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
903
+ * at any point.
904
+ * @returns The message
905
+ */
906
+ sendTokenMessage(wallet: LocalWallet, amount: string, recipient: string): EncodeObject {
944
907
  const address = wallet.address;
945
908
  if (address === undefined) {
946
909
  throw new UserError('wallet address is not set. Call connectWallet() first');
947
910
  }
948
- const {
949
- CHAINTOKEN_DENOM: chainTokenDenom,
950
- CHAINTOKEN_DECIMALS: chainTokenDecimals,
951
- } = this._validatorClient?.config.denoms || {};
911
+ const { CHAINTOKEN_DENOM: chainTokenDenom, CHAINTOKEN_DECIMALS: chainTokenDecimals } =
912
+ this._validatorClient?.config.denoms || {};
952
913
 
953
914
  if (chainTokenDenom === undefined || chainTokenDecimals === undefined) {
954
915
  throw new Error('Chain token denom not set in validator config');
@@ -995,15 +956,13 @@ export class CompositeClient {
995
956
  postOnly,
996
957
  reduceOnly,
997
958
  );
998
- msg.then((it) => resolve([it])).catch((err) => {
999
- console.log(err);
1000
- });
959
+ msg
960
+ .then((it) => resolve([it]))
961
+ .catch((err) => {
962
+ console.log(err);
963
+ });
1001
964
  });
1002
- const signature = await this.sign(
1003
- wallet,
1004
- () => msgs,
1005
- true,
1006
- );
965
+ const signature = await this.sign(wallet, () => msgs, true);
1007
966
 
1008
967
  return Buffer.from(signature).toString('base64');
1009
968
  }
@@ -1028,11 +987,7 @@ export class CompositeClient {
1028
987
  );
1029
988
  resolve([msg]);
1030
989
  });
1031
- const signature = await this.sign(
1032
- subaccount.wallet,
1033
- () => msgs,
1034
- true,
1035
- );
990
+ const signature = await this.sign(subaccount.wallet, () => msgs, true);
1036
991
 
1037
992
  return Buffer.from(signature).toString('base64');
1038
993
  }
@@ -1130,12 +1085,6 @@ export class CompositeClient {
1130
1085
  resolve([submitProposal]);
1131
1086
  });
1132
1087
 
1133
- return this.send(
1134
- wallet,
1135
- () => msg,
1136
- false,
1137
- undefined,
1138
- memo,
1139
- );
1088
+ return this.send(wallet, () => msg, false, undefined, memo);
1140
1089
  }
1141
1090
  }