@dydxprotocol/v4-client-js 1.1.13 → 1.1.15
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.
- package/.eslintrc.js +3 -5
- package/.prettierignore +3 -0
- package/.prettierrc.json +6 -0
- package/.telescope.json +2 -4
- package/.vscode/launch.json +10 -10
- package/README.md +3 -1
- package/__native__/__ios__/v4-native-client.js +2680 -799
- package/__tests__/helpers/constants.ts +10 -8
- package/__tests__/lib/helpers.test.ts +22 -23
- package/__tests__/lib/util.test.ts +2 -8
- package/__tests__/lib/validation.test.ts +23 -26
- package/__tests__/modules/client/Transfers.test.ts +4 -19
- package/__tests__/modules/client/ValidatorPostEndpoints.test.ts +2 -5
- package/__tests__/modules/client/constants.ts +4 -2
- package/__tests__/modules/onboarding.test.ts +9 -7
- package/build/__tests__/helpers/constants.js +7 -7
- package/build/__tests__/lib/helpers.test.js +2 -6
- package/build/__tests__/lib/util.test.js +1 -1
- package/build/__tests__/lib/validation.test.js +3 -11
- package/build/__tests__/modules/client/Transfers.test.js +1 -1
- package/build/__tests__/modules/client/ValidatorPostEndpoints.test.js +2 -2
- package/build/__tests__/modules/client/constants.js +1 -1
- package/build/__tests__/modules/onboarding.test.js +2 -3
- package/build/examples/account_endpoints.js +4 -3
- package/build/examples/composite_example.js +5 -4
- package/build/examples/constants.js +1 -1
- package/build/examples/faucet_endpoint.js +4 -3
- package/build/examples/long_term_order_cancel_example.js +4 -3
- package/build/examples/markets_endpoints.js +4 -3
- package/build/examples/native_examples.js +4 -3
- package/build/examples/noble_example.js +1 -1
- package/build/examples/optimal_node.js +4 -12
- package/build/examples/short_term_order_cancel_example.js +4 -3
- package/build/examples/short_term_order_composite_example.js +4 -3
- package/build/examples/test.js +5 -4
- package/build/examples/transfer_example_deposit.js +4 -3
- package/build/examples/transfer_example_send.js +1 -1
- package/build/examples/transfer_example_subaccount_transfer.js +4 -3
- package/build/examples/transfer_example_withdraw.js +4 -3
- package/build/examples/transfer_example_withdraw_other.js +1 -1
- package/build/examples/utility_endpoints.js +4 -3
- package/build/examples/validator_get_example.js +15 -9
- package/build/examples/validator_post_example.js +4 -3
- package/build/examples/wallet_address.js +5 -4
- package/build/examples/websocket_example.js +1 -1
- package/build/src/clients/composite-client.d.ts +222 -222
- package/build/src/clients/composite-client.js +234 -228
- package/build/src/clients/constants.d.ts +3 -0
- package/build/src/clients/constants.js +6 -2
- package/build/src/clients/faucet-client.d.ts +4 -4
- package/build/src/clients/faucet-client.js +5 -5
- package/build/src/clients/helpers/chain-helpers.js +1 -1
- package/build/src/clients/helpers/request-helpers.js +5 -4
- package/build/src/clients/indexer-client.js +1 -1
- package/build/src/clients/lib/axios/axiosRequest.js +1 -1
- package/build/src/clients/lib/axios/errors.js +1 -1
- package/build/src/clients/lib/cctpProto.d.ts +2 -2
- package/build/src/clients/lib/cctpProto.js +287 -237
- package/build/src/clients/lib/errors.js +1 -1
- package/build/src/clients/modules/account.js +1 -1
- package/build/src/clients/modules/composer.d.ts +4 -0
- package/build/src/clients/modules/composer.js +39 -3
- package/build/src/clients/modules/get.d.ts +10 -3
- package/build/src/clients/modules/get.js +41 -54
- package/build/src/clients/modules/local-wallet.js +1 -1
- package/build/src/clients/modules/post.d.ts +6 -0
- package/build/src/clients/modules/post.js +50 -18
- package/build/src/clients/modules/proto-includes.d.ts +1 -0
- package/build/src/clients/modules/proto-includes.js +3 -2
- package/build/src/clients/modules/rest.js +1 -1
- package/build/src/clients/modules/signer.js +1 -1
- package/build/src/clients/modules/tendermintClient.d.ts +5 -5
- package/build/src/clients/modules/tendermintClient.js +6 -6
- package/build/src/clients/native.d.ts +5 -0
- package/build/src/clients/native.js +44 -9
- package/build/src/clients/noble-client.js +1 -1
- package/build/src/clients/socket-client.js +1 -1
- package/build/src/clients/subaccount.js +1 -1
- package/build/src/clients/types.d.ts +1 -1
- package/build/src/clients/types.js +1 -1
- package/build/src/clients/validator-client.d.ts +12 -12
- package/build/src/clients/validator-client.js +13 -13
- package/build/src/lib/errors.js +1 -1
- package/build/src/lib/onboarding.js +1 -1
- package/build/src/lib/utils.js +3 -3
- package/build/src/lib/validation.js +5 -4
- package/build/src/network_optimizer.js +10 -11
- package/build/src/types.d.ts +1 -1
- package/build/src/types.js +1 -1
- package/examples/account_endpoints.ts +5 -4
- package/examples/composite_example.ts +12 -7
- package/examples/constants.ts +10 -4
- package/examples/faucet_endpoint.ts +5 -4
- package/examples/human_readable_orders.json +85 -85
- package/examples/human_readable_short_term_orders.json +41 -41
- package/examples/long_term_order_cancel_example.ts +11 -6
- package/examples/markets_endpoints.ts +5 -4
- package/examples/native_examples.ts +8 -7
- package/examples/noble_example.ts +11 -23
- package/examples/optimal_node.ts +4 -14
- package/examples/raw_orders.json +128 -128
- package/examples/short_term_order_cancel_example.ts +7 -8
- package/examples/short_term_order_composite_example.ts +7 -8
- package/examples/test.ts +12 -7
- package/examples/transfer_example_deposit.ts +6 -9
- package/examples/transfer_example_send.ts +2 -10
- package/examples/transfer_example_subaccount_transfer.ts +6 -11
- package/examples/transfer_example_withdraw.ts +6 -9
- package/examples/transfer_example_withdraw_other.ts +1 -5
- package/examples/utility_endpoints.ts +5 -4
- package/examples/validator_get_example.ts +19 -10
- package/examples/validator_post_example.ts +7 -9
- package/examples/wallet_address.ts +6 -5
- package/examples/websocket_example.ts +5 -1
- package/package.json +4 -1
- package/src/clients/composite-client.ts +272 -329
- package/src/clients/constants.ts +38 -26
- package/src/clients/faucet-client.ts +5 -8
- package/src/clients/helpers/chain-helpers.ts +10 -6
- package/src/clients/helpers/request-helpers.ts +6 -5
- package/src/clients/indexer-client.ts +34 -34
- package/src/clients/lib/axios/axiosRequest.ts +4 -7
- package/src/clients/lib/axios/errors.ts +16 -21
- package/src/clients/lib/cctpProto.ts +655 -387
- package/src/clients/lib/errors.ts +8 -17
- package/src/clients/modules/account.ts +4 -12
- package/src/clients/modules/composer.ts +61 -20
- package/src/clients/modules/get.ts +71 -89
- package/src/clients/modules/local-wallet.ts +41 -56
- package/src/clients/modules/post.ts +684 -634
- package/src/clients/modules/proto-includes.ts +1 -0
- package/src/clients/modules/rest.ts +23 -26
- package/src/clients/modules/signer.ts +8 -22
- package/src/clients/modules/tendermintClient.ts +13 -36
- package/src/clients/native.ts +119 -140
- package/src/clients/noble-client.ts +1 -5
- package/src/clients/socket-client.ts +216 -216
- package/src/clients/subaccount.ts +16 -16
- package/src/clients/types.ts +14 -10
- package/src/clients/validator-client.ts +14 -16
- package/src/codegen/helpers.ts +149 -150
- package/src/codegen/index.ts +4 -5
- package/src/lib/errors.ts +3 -9
- package/src/lib/onboarding.ts +7 -2
- package/src/lib/utils.ts +5 -12
- package/src/lib/validation.ts +21 -19
- package/src/network_optimizer.ts +78 -65
- package/src/types.ts +5 -6
- package/tasks.json +15 -15
- package/tsconfig.eslint.json +1 -4
- package/tsconfig.json +2 -6
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { EncodeObject } from '@cosmjs/proto-signing';
|
|
2
|
-
import {
|
|
3
|
-
Account, GasPrice, IndexedTx, StdFee,
|
|
4
|
-
} from '@cosmjs/stargate';
|
|
2
|
+
import { Account, GasPrice, IndexedTx, StdFee } from '@cosmjs/stargate';
|
|
5
3
|
import { Method } from '@cosmjs/tendermint-rpc';
|
|
6
|
-
import {
|
|
7
|
-
|
|
4
|
+
import {
|
|
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';
|
|
8
12
|
import { parseUnits } from 'ethers';
|
|
9
13
|
import Long from 'long';
|
|
10
14
|
import protobuf from 'protobufjs';
|
|
@@ -66,15 +70,9 @@ export class CompositeClient {
|
|
|
66
70
|
return client;
|
|
67
71
|
}
|
|
68
72
|
|
|
69
|
-
private constructor(
|
|
70
|
-
network: Network,
|
|
71
|
-
apiTimeout?: number,
|
|
72
|
-
) {
|
|
73
|
+
private constructor(network: Network, apiTimeout?: number) {
|
|
73
74
|
this.network = network;
|
|
74
|
-
this._indexerClient = new IndexerClient(
|
|
75
|
-
network.indexerConfig,
|
|
76
|
-
apiTimeout,
|
|
77
|
-
);
|
|
75
|
+
this._indexerClient = new IndexerClient(network.indexerConfig, apiTimeout);
|
|
78
76
|
}
|
|
79
77
|
|
|
80
78
|
private async initialize(): Promise<void> {
|
|
@@ -106,13 +104,13 @@ export class CompositeClient {
|
|
|
106
104
|
}
|
|
107
105
|
|
|
108
106
|
/**
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
+
*/
|
|
116
114
|
async sign(
|
|
117
115
|
wallet: LocalWallet,
|
|
118
116
|
messaging: () => Promise<EncodeObject[]>,
|
|
@@ -121,24 +119,17 @@ export class CompositeClient {
|
|
|
121
119
|
memo?: string,
|
|
122
120
|
account?: () => Promise<Account>,
|
|
123
121
|
): Promise<Uint8Array> {
|
|
124
|
-
return this.validatorClient.post.sign(
|
|
125
|
-
wallet,
|
|
126
|
-
messaging,
|
|
127
|
-
zeroFee,
|
|
128
|
-
gasPrice,
|
|
129
|
-
memo,
|
|
130
|
-
account,
|
|
131
|
-
);
|
|
122
|
+
return this.validatorClient.post.sign(wallet, messaging, zeroFee, gasPrice, memo, account);
|
|
132
123
|
}
|
|
133
124
|
|
|
134
125
|
/**
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
+
*/
|
|
142
133
|
async send(
|
|
143
134
|
wallet: LocalWallet,
|
|
144
135
|
messaging: () => Promise<EncodeObject[]>,
|
|
@@ -160,14 +151,14 @@ export class CompositeClient {
|
|
|
160
151
|
}
|
|
161
152
|
|
|
162
153
|
/**
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
+
*/
|
|
171
162
|
async sendSignedTransaction(
|
|
172
163
|
signedTransaction: Uint8Array,
|
|
173
164
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
@@ -175,18 +166,18 @@ export class CompositeClient {
|
|
|
175
166
|
}
|
|
176
167
|
|
|
177
168
|
/**
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
+
*/
|
|
190
181
|
async simulate(
|
|
191
182
|
wallet: LocalWallet,
|
|
192
183
|
messaging: () => Promise<EncodeObject[]>,
|
|
@@ -194,22 +185,16 @@ export class CompositeClient {
|
|
|
194
185
|
memo?: string,
|
|
195
186
|
account?: () => Promise<Account>,
|
|
196
187
|
): Promise<StdFee> {
|
|
197
|
-
return this.validatorClient.post.simulate(
|
|
198
|
-
wallet,
|
|
199
|
-
messaging,
|
|
200
|
-
gasPrice,
|
|
201
|
-
memo,
|
|
202
|
-
account,
|
|
203
|
-
);
|
|
188
|
+
return this.validatorClient.post.simulate(wallet, messaging, gasPrice, memo, account);
|
|
204
189
|
}
|
|
205
190
|
|
|
206
191
|
/**
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
+
*/
|
|
213
198
|
|
|
214
199
|
private async calculateGoodTilBlock(
|
|
215
200
|
orderFlags: OrderFlags,
|
|
@@ -220,7 +205,7 @@ export class CompositeClient {
|
|
|
220
205
|
if (goodTilBlock !== undefined && goodTilBlock !== 0) {
|
|
221
206
|
return Promise.resolve(goodTilBlock);
|
|
222
207
|
} else {
|
|
223
|
-
const height = currentHeight ?? await this.validatorClient.get.latestBlockHeight();
|
|
208
|
+
const height = currentHeight ?? (await this.validatorClient.get.latestBlockHeight());
|
|
224
209
|
return height + SHORT_BLOCK_FORWARD;
|
|
225
210
|
}
|
|
226
211
|
} else {
|
|
@@ -250,15 +235,15 @@ export class CompositeClient {
|
|
|
250
235
|
}
|
|
251
236
|
|
|
252
237
|
/**
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
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
|
+
*/
|
|
262
247
|
private calculateGoodTilBlockTime(goodTilTimeInSeconds: number): number {
|
|
263
248
|
const now = new Date();
|
|
264
249
|
const millisecondsPerSecond = 1000;
|
|
@@ -312,10 +297,12 @@ export class CompositeClient {
|
|
|
312
297
|
timeInForce,
|
|
313
298
|
reduceOnly,
|
|
314
299
|
);
|
|
315
|
-
msg
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
300
|
+
msg
|
|
301
|
+
.then((it) => resolve([it]))
|
|
302
|
+
.catch((err) => {
|
|
303
|
+
console.log(err);
|
|
304
|
+
reject(err);
|
|
305
|
+
});
|
|
319
306
|
});
|
|
320
307
|
const account: Promise<Account> = this.validatorClient.post.account(
|
|
321
308
|
subaccount.address,
|
|
@@ -333,36 +320,36 @@ export class CompositeClient {
|
|
|
333
320
|
}
|
|
334
321
|
|
|
335
322
|
/**
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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
|
+
*/
|
|
366
353
|
async placeOrder(
|
|
367
354
|
subaccount: SubaccountInfo,
|
|
368
355
|
marketId: string,
|
|
@@ -401,9 +388,11 @@ export class CompositeClient {
|
|
|
401
388
|
currentHeight,
|
|
402
389
|
goodTilBlock,
|
|
403
390
|
);
|
|
404
|
-
msg
|
|
405
|
-
|
|
406
|
-
|
|
391
|
+
msg
|
|
392
|
+
.then((it) => resolve([it]))
|
|
393
|
+
.catch((err) => {
|
|
394
|
+
console.log(err);
|
|
395
|
+
});
|
|
407
396
|
});
|
|
408
397
|
const orderFlags = calculateOrderFlags(type, timeInForce);
|
|
409
398
|
const account: Promise<Account> = this.validatorClient.post.account(
|
|
@@ -422,30 +411,30 @@ export class CompositeClient {
|
|
|
422
411
|
}
|
|
423
412
|
|
|
424
413
|
/**
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
+
*/
|
|
449
438
|
private async placeOrderMessage(
|
|
450
439
|
subaccount: SubaccountInfo,
|
|
451
440
|
marketId: string,
|
|
@@ -470,8 +459,7 @@ export class CompositeClient {
|
|
|
470
459
|
const result = await Promise.all([
|
|
471
460
|
this.calculateGoodTilBlock(orderFlags, currentHeight, goodTilBlock),
|
|
472
461
|
this.retrieveMarketInfo(marketId, marketInfo),
|
|
473
|
-
]
|
|
474
|
-
);
|
|
462
|
+
]);
|
|
475
463
|
const desiredGoodTilBlock = result[0];
|
|
476
464
|
const clobPairId = result[1].clobPairId;
|
|
477
465
|
const atomicResolution = result[1].atomicResolution;
|
|
@@ -479,11 +467,7 @@ export class CompositeClient {
|
|
|
479
467
|
const quantumConversionExponent = result[1].quantumConversionExponent;
|
|
480
468
|
const subticksPerTick = result[1].subticksPerTick;
|
|
481
469
|
const orderSide = calculateSide(side);
|
|
482
|
-
const quantums = calculateQuantums(
|
|
483
|
-
size,
|
|
484
|
-
atomicResolution,
|
|
485
|
-
stepBaseQuantums,
|
|
486
|
-
);
|
|
470
|
+
const quantums = calculateQuantums(size, atomicResolution, stepBaseQuantums);
|
|
487
471
|
const subticks = calculateSubticks(
|
|
488
472
|
price,
|
|
489
473
|
atomicResolution,
|
|
@@ -506,7 +490,8 @@ export class CompositeClient {
|
|
|
506
490
|
atomicResolution,
|
|
507
491
|
quantumConversionExponent,
|
|
508
492
|
subticksPerTick,
|
|
509
|
-
triggerPrice
|
|
493
|
+
triggerPrice,
|
|
494
|
+
);
|
|
510
495
|
return this.validatorClient.post.composer.composeMsgPlaceOrder(
|
|
511
496
|
subaccount.address,
|
|
512
497
|
subaccount.subaccountNumber,
|
|
@@ -548,26 +533,26 @@ export class CompositeClient {
|
|
|
548
533
|
}
|
|
549
534
|
|
|
550
535
|
/**
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
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
|
+
*/
|
|
571
556
|
private async placeShortTermOrderMessage(
|
|
572
557
|
subaccount: SubaccountInfo,
|
|
573
558
|
marketId: string,
|
|
@@ -589,11 +574,7 @@ export class CompositeClient {
|
|
|
589
574
|
const quantumConversionExponent = market.quantumConversionExponent;
|
|
590
575
|
const subticksPerTick = market.subticksPerTick;
|
|
591
576
|
const orderSide = calculateSide(side);
|
|
592
|
-
const quantums = calculateQuantums(
|
|
593
|
-
size,
|
|
594
|
-
atomicResolution,
|
|
595
|
-
stepBaseQuantums,
|
|
596
|
-
);
|
|
577
|
+
const quantums = calculateQuantums(size, atomicResolution, stepBaseQuantums);
|
|
597
578
|
const subticks = calculateSubticks(
|
|
598
579
|
price,
|
|
599
580
|
atomicResolution,
|
|
@@ -621,19 +602,19 @@ export class CompositeClient {
|
|
|
621
602
|
}
|
|
622
603
|
|
|
623
604
|
/**
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
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
|
+
*/
|
|
637
618
|
async cancelRawOrder(
|
|
638
619
|
subaccount: SubaccountInfo,
|
|
639
620
|
clientId: number,
|
|
@@ -653,19 +634,19 @@ export class CompositeClient {
|
|
|
653
634
|
}
|
|
654
635
|
|
|
655
636
|
/**
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
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
|
+
*/
|
|
669
650
|
async cancelOrder(
|
|
670
651
|
subaccount: SubaccountInfo,
|
|
671
652
|
clientId: number,
|
|
@@ -674,7 +655,6 @@ export class CompositeClient {
|
|
|
674
655
|
goodTilBlock?: number,
|
|
675
656
|
goodTilTimeInSeconds?: number,
|
|
676
657
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
677
|
-
|
|
678
658
|
const marketsResponse = await this.indexerClient.markets.getPerpetualMarkets(marketId);
|
|
679
659
|
const market = marketsResponse.markets[marketId];
|
|
680
660
|
const clobPairId = market.clobPairId;
|
|
@@ -691,7 +671,7 @@ export class CompositeClient {
|
|
|
691
671
|
if (goodTilBlock !== 0) {
|
|
692
672
|
throw new Error(
|
|
693
673
|
'goodTilBlock should be zero since LONG_TERM or CONDITIONAL orders ' +
|
|
694
|
-
|
|
674
|
+
'use goodTilTimeInSeconds instead of goodTilBlock.',
|
|
695
675
|
);
|
|
696
676
|
}
|
|
697
677
|
goodTilBlockTime = this.calculateGoodTilBlockTime(goodTilTimeInSeconds);
|
|
@@ -700,7 +680,9 @@ export class CompositeClient {
|
|
|
700
680
|
throw new Error('goodTilBlock must be non-zero for SHORT_TERM orders');
|
|
701
681
|
}
|
|
702
682
|
if (goodTilTimeInSeconds !== undefined && goodTilTimeInSeconds !== 0) {
|
|
703
|
-
throw new Error(
|
|
683
|
+
throw new Error(
|
|
684
|
+
'goodTilTimeInSeconds should be zero since SHORT_TERM orders use goodTilBlock instead of goodTilTimeInSeconds.',
|
|
685
|
+
);
|
|
704
686
|
}
|
|
705
687
|
}
|
|
706
688
|
|
|
@@ -715,17 +697,17 @@ export class CompositeClient {
|
|
|
715
697
|
}
|
|
716
698
|
|
|
717
699
|
/**
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
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
|
+
*/
|
|
729
711
|
async transferToSubaccount(
|
|
730
712
|
subaccount: SubaccountInfo,
|
|
731
713
|
recipientAddress: string,
|
|
@@ -754,18 +736,18 @@ export class CompositeClient {
|
|
|
754
736
|
}
|
|
755
737
|
|
|
756
738
|
/**
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
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
|
+
*/
|
|
769
751
|
transferToSubaccountMessage(
|
|
770
752
|
subaccount: SubaccountInfo,
|
|
771
753
|
recipientAddress: string,
|
|
@@ -795,49 +777,38 @@ export class CompositeClient {
|
|
|
795
777
|
}
|
|
796
778
|
|
|
797
779
|
/**
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
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
|
+
*/
|
|
807
789
|
async depositToSubaccount(
|
|
808
790
|
subaccount: SubaccountInfo,
|
|
809
791
|
amount: string,
|
|
810
792
|
memo?: string,
|
|
811
793
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
812
794
|
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
|
|
813
|
-
const msg = this.depositToSubaccountMessage(
|
|
814
|
-
subaccount,
|
|
815
|
-
amount,
|
|
816
|
-
);
|
|
795
|
+
const msg = this.depositToSubaccountMessage(subaccount, amount);
|
|
817
796
|
resolve([msg]);
|
|
818
797
|
});
|
|
819
|
-
return this.validatorClient.post.send(subaccount.wallet,
|
|
820
|
-
() => msgs,
|
|
821
|
-
false,
|
|
822
|
-
undefined,
|
|
823
|
-
memo,
|
|
824
|
-
);
|
|
798
|
+
return this.validatorClient.post.send(subaccount.wallet, () => msgs, false, undefined, memo);
|
|
825
799
|
}
|
|
826
800
|
|
|
827
801
|
/**
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
depositToSubaccountMessage(
|
|
838
|
-
subaccount: SubaccountInfo,
|
|
839
|
-
amount: string,
|
|
840
|
-
): 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 {
|
|
841
812
|
const validatorClient = this._validatorClient;
|
|
842
813
|
if (validatorClient === undefined) {
|
|
843
814
|
throw new Error('validatorClient not set');
|
|
@@ -859,16 +830,16 @@ export class CompositeClient {
|
|
|
859
830
|
}
|
|
860
831
|
|
|
861
832
|
/**
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
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
|
+
*/
|
|
872
843
|
async withdrawFromSubaccount(
|
|
873
844
|
subaccount: SubaccountInfo,
|
|
874
845
|
amount: string,
|
|
@@ -876,34 +847,24 @@ export class CompositeClient {
|
|
|
876
847
|
memo?: string,
|
|
877
848
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
878
849
|
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
|
|
879
|
-
const msg = this.withdrawFromSubaccountMessage(
|
|
880
|
-
subaccount,
|
|
881
|
-
amount,
|
|
882
|
-
recipient,
|
|
883
|
-
);
|
|
850
|
+
const msg = this.withdrawFromSubaccountMessage(subaccount, amount, recipient);
|
|
884
851
|
resolve([msg]);
|
|
885
852
|
});
|
|
886
|
-
return this.send(
|
|
887
|
-
subaccount.wallet,
|
|
888
|
-
() => msgs,
|
|
889
|
-
false,
|
|
890
|
-
undefined,
|
|
891
|
-
memo,
|
|
892
|
-
);
|
|
853
|
+
return this.send(subaccount.wallet, () => msgs, false, undefined, memo);
|
|
893
854
|
}
|
|
894
855
|
|
|
895
856
|
/**
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
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
|
+
*/
|
|
907
868
|
withdrawFromSubaccountMessage(
|
|
908
869
|
subaccount: SubaccountInfo,
|
|
909
870
|
amount: string,
|
|
@@ -931,30 +892,24 @@ export class CompositeClient {
|
|
|
931
892
|
}
|
|
932
893
|
|
|
933
894
|
/**
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
sendTokenMessage(
|
|
946
|
-
wallet: LocalWallet,
|
|
947
|
-
amount: string,
|
|
948
|
-
recipient: string,
|
|
949
|
-
): 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 {
|
|
950
907
|
const address = wallet.address;
|
|
951
908
|
if (address === undefined) {
|
|
952
909
|
throw new UserError('wallet address is not set. Call connectWallet() first');
|
|
953
910
|
}
|
|
954
|
-
const {
|
|
955
|
-
|
|
956
|
-
CHAINTOKEN_DECIMALS: chainTokenDecimals,
|
|
957
|
-
} = this._validatorClient?.config.denoms || {};
|
|
911
|
+
const { CHAINTOKEN_DENOM: chainTokenDenom, CHAINTOKEN_DECIMALS: chainTokenDecimals } =
|
|
912
|
+
this._validatorClient?.config.denoms || {};
|
|
958
913
|
|
|
959
914
|
if (chainTokenDenom === undefined || chainTokenDecimals === undefined) {
|
|
960
915
|
throw new Error('Chain token denom not set in validator config');
|
|
@@ -1001,15 +956,13 @@ export class CompositeClient {
|
|
|
1001
956
|
postOnly,
|
|
1002
957
|
reduceOnly,
|
|
1003
958
|
);
|
|
1004
|
-
msg
|
|
1005
|
-
|
|
1006
|
-
|
|
959
|
+
msg
|
|
960
|
+
.then((it) => resolve([it]))
|
|
961
|
+
.catch((err) => {
|
|
962
|
+
console.log(err);
|
|
963
|
+
});
|
|
1007
964
|
});
|
|
1008
|
-
const signature = await this.sign(
|
|
1009
|
-
wallet,
|
|
1010
|
-
() => msgs,
|
|
1011
|
-
true,
|
|
1012
|
-
);
|
|
965
|
+
const signature = await this.sign(wallet, () => msgs, true);
|
|
1013
966
|
|
|
1014
967
|
return Buffer.from(signature).toString('base64');
|
|
1015
968
|
}
|
|
@@ -1034,11 +987,7 @@ export class CompositeClient {
|
|
|
1034
987
|
);
|
|
1035
988
|
resolve([msg]);
|
|
1036
989
|
});
|
|
1037
|
-
const signature = await this.sign(
|
|
1038
|
-
subaccount.wallet,
|
|
1039
|
-
() => msgs,
|
|
1040
|
-
true,
|
|
1041
|
-
);
|
|
990
|
+
const signature = await this.sign(subaccount.wallet, () => msgs, true);
|
|
1042
991
|
|
|
1043
992
|
return Buffer.from(signature).toString('base64');
|
|
1044
993
|
}
|
|
@@ -1136,12 +1085,6 @@ export class CompositeClient {
|
|
|
1136
1085
|
resolve([submitProposal]);
|
|
1137
1086
|
});
|
|
1138
1087
|
|
|
1139
|
-
return this.send(
|
|
1140
|
-
wallet,
|
|
1141
|
-
() => msg,
|
|
1142
|
-
false,
|
|
1143
|
-
undefined,
|
|
1144
|
-
memo,
|
|
1145
|
-
);
|
|
1088
|
+
return this.send(wallet, () => msg, false, undefined, memo);
|
|
1146
1089
|
}
|
|
1147
1090
|
}
|