@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.
Files changed (151) 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 +2680 -799
  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 +222 -222
  47. package/build/src/clients/composite-client.js +234 -228
  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 +6 -0
  67. package/build/src/clients/modules/post.js +50 -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.d.ts +5 -0
  75. package/build/src/clients/native.js +44 -9
  76. package/build/src/clients/noble-client.js +1 -1
  77. package/build/src/clients/socket-client.js +1 -1
  78. package/build/src/clients/subaccount.js +1 -1
  79. package/build/src/clients/types.d.ts +1 -1
  80. package/build/src/clients/types.js +1 -1
  81. package/build/src/clients/validator-client.d.ts +12 -12
  82. package/build/src/clients/validator-client.js +13 -13
  83. package/build/src/lib/errors.js +1 -1
  84. package/build/src/lib/onboarding.js +1 -1
  85. package/build/src/lib/utils.js +3 -3
  86. package/build/src/lib/validation.js +5 -4
  87. package/build/src/network_optimizer.js +10 -11
  88. package/build/src/types.d.ts +1 -1
  89. package/build/src/types.js +1 -1
  90. package/examples/account_endpoints.ts +5 -4
  91. package/examples/composite_example.ts +12 -7
  92. package/examples/constants.ts +10 -4
  93. package/examples/faucet_endpoint.ts +5 -4
  94. package/examples/human_readable_orders.json +85 -85
  95. package/examples/human_readable_short_term_orders.json +41 -41
  96. package/examples/long_term_order_cancel_example.ts +11 -6
  97. package/examples/markets_endpoints.ts +5 -4
  98. package/examples/native_examples.ts +8 -7
  99. package/examples/noble_example.ts +11 -23
  100. package/examples/optimal_node.ts +4 -14
  101. package/examples/raw_orders.json +128 -128
  102. package/examples/short_term_order_cancel_example.ts +7 -8
  103. package/examples/short_term_order_composite_example.ts +7 -8
  104. package/examples/test.ts +12 -7
  105. package/examples/transfer_example_deposit.ts +6 -9
  106. package/examples/transfer_example_send.ts +2 -10
  107. package/examples/transfer_example_subaccount_transfer.ts +6 -11
  108. package/examples/transfer_example_withdraw.ts +6 -9
  109. package/examples/transfer_example_withdraw_other.ts +1 -5
  110. package/examples/utility_endpoints.ts +5 -4
  111. package/examples/validator_get_example.ts +19 -10
  112. package/examples/validator_post_example.ts +7 -9
  113. package/examples/wallet_address.ts +6 -5
  114. package/examples/websocket_example.ts +5 -1
  115. package/package.json +4 -1
  116. package/src/clients/composite-client.ts +272 -329
  117. package/src/clients/constants.ts +38 -26
  118. package/src/clients/faucet-client.ts +5 -8
  119. package/src/clients/helpers/chain-helpers.ts +10 -6
  120. package/src/clients/helpers/request-helpers.ts +6 -5
  121. package/src/clients/indexer-client.ts +34 -34
  122. package/src/clients/lib/axios/axiosRequest.ts +4 -7
  123. package/src/clients/lib/axios/errors.ts +16 -21
  124. package/src/clients/lib/cctpProto.ts +655 -387
  125. package/src/clients/lib/errors.ts +8 -17
  126. package/src/clients/modules/account.ts +4 -12
  127. package/src/clients/modules/composer.ts +61 -20
  128. package/src/clients/modules/get.ts +71 -89
  129. package/src/clients/modules/local-wallet.ts +41 -56
  130. package/src/clients/modules/post.ts +684 -634
  131. package/src/clients/modules/proto-includes.ts +1 -0
  132. package/src/clients/modules/rest.ts +23 -26
  133. package/src/clients/modules/signer.ts +8 -22
  134. package/src/clients/modules/tendermintClient.ts +13 -36
  135. package/src/clients/native.ts +119 -140
  136. package/src/clients/noble-client.ts +1 -5
  137. package/src/clients/socket-client.ts +216 -216
  138. package/src/clients/subaccount.ts +16 -16
  139. package/src/clients/types.ts +14 -10
  140. package/src/clients/validator-client.ts +14 -16
  141. package/src/codegen/helpers.ts +149 -150
  142. package/src/codegen/index.ts +4 -5
  143. package/src/lib/errors.ts +3 -9
  144. package/src/lib/onboarding.ts +7 -2
  145. package/src/lib/utils.ts +5 -12
  146. package/src/lib/validation.ts +21 -19
  147. package/src/network_optimizer.ts +78 -65
  148. package/src/types.ts +5 -6
  149. package/tasks.json +15 -15
  150. package/tsconfig.eslint.json +1 -4
  151. package/tsconfig.json +2 -6
@@ -1,19 +1,8 @@
1
1
  import { Coin, Secp256k1Pubkey } from '@cosmjs/amino';
2
2
  import { Uint53 } from '@cosmjs/math';
3
- import {
4
- EncodeObject,
5
- Registry,
6
- } from '@cosmjs/proto-signing';
7
- import {
8
- Account,
9
- calculateFee,
10
- GasPrice,
11
- IndexedTx,
12
- StdFee,
13
- } from '@cosmjs/stargate';
14
- import {
15
- Method,
16
- } from '@cosmjs/tendermint-rpc';
3
+ import { EncodeObject, Registry } from '@cosmjs/proto-signing';
4
+ import { Account, calculateFee, GasPrice, IndexedTx, StdFee } from '@cosmjs/stargate';
5
+ import { Method } from '@cosmjs/tendermint-rpc';
17
6
  import {
18
7
  BroadcastTxAsyncResponse,
19
8
  BroadcastTxSyncResponse,
@@ -38,7 +27,12 @@ import { Composer } from './composer';
38
27
  import { Get } from './get';
39
28
  import LocalWallet from './local-wallet';
40
29
  import {
41
- Order_Side, Order_TimeInForce, Any, MsgPlaceOrder, MsgCancelOrder, Order_ConditionType,
30
+ Order_Side,
31
+ Order_TimeInForce,
32
+ Any,
33
+ MsgPlaceOrder,
34
+ MsgCancelOrder,
35
+ Order_ConditionType,
42
36
  } from './proto-includes';
43
37
 
44
38
  // Required for encoding and decoding queries that are of type Long.
@@ -49,667 +43,723 @@ protobuf.util.Long = Long;
49
43
  protobuf.configure();
50
44
 
51
45
  export class Post {
52
- public readonly composer: Composer;
53
- private readonly registry: Registry;
54
- private readonly chainId: string;
55
- public readonly get: Get;
56
- public readonly denoms: DenomConfig;
57
- public readonly defaultClientMemo?: string;
58
-
59
- public selectedGasDenom: SelectedGasDenom = SelectedGasDenom.USDC;
60
- public readonly defaultGasPrice: GasPrice;
61
- public readonly defaultDydxGasPrice: GasPrice;
62
-
63
- private accountNumberCache: Map<string, Account> = new Map();
64
-
65
- constructor(
66
- get: Get,
67
- chainId: string,
68
- denoms: DenomConfig,
69
- defaultClientMemo?: string,
46
+ public readonly composer: Composer;
47
+ private readonly registry: Registry;
48
+ private readonly chainId: string;
49
+ public readonly get: Get;
50
+ public readonly denoms: DenomConfig;
51
+ public readonly defaultClientMemo?: string;
52
+
53
+ public selectedGasDenom: SelectedGasDenom = SelectedGasDenom.USDC;
54
+ public readonly defaultGasPrice: GasPrice;
55
+ public readonly defaultDydxGasPrice: GasPrice;
56
+
57
+ private accountNumberCache: Map<string, Account> = new Map();
58
+
59
+ constructor(get: Get, chainId: string, denoms: DenomConfig, defaultClientMemo?: string) {
60
+ this.get = get;
61
+ this.chainId = chainId;
62
+ this.registry = generateRegistry();
63
+ this.composer = new Composer();
64
+ this.denoms = denoms;
65
+ this.defaultClientMemo = defaultClientMemo;
66
+ this.defaultGasPrice = GasPrice.fromString(
67
+ `0.025${denoms.USDC_GAS_DENOM !== undefined ? denoms.USDC_GAS_DENOM : denoms.USDC_DENOM}`,
68
+ );
69
+ this.defaultDydxGasPrice = GasPrice.fromString(
70
+ `25000000000${
71
+ denoms.CHAINTOKEN_GAS_DENOM !== undefined
72
+ ? denoms.CHAINTOKEN_GAS_DENOM
73
+ : denoms.CHAINTOKEN_DENOM
74
+ }`,
75
+ );
76
+ }
77
+
78
+ setSelectedGasDenom(selectedGasDenom: SelectedGasDenom): void {
79
+ this.selectedGasDenom = selectedGasDenom;
80
+ }
81
+
82
+ getGasPrice(): GasPrice {
83
+ return this.selectedGasDenom === SelectedGasDenom.USDC
84
+ ? this.defaultGasPrice
85
+ : this.defaultDydxGasPrice;
86
+ }
87
+
88
+ /**
89
+ * @description Simulate a transaction
90
+ * the calling function is responsible for creating the messages.
91
+ *
92
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
93
+ * at any point.
94
+ * @returns The Fee for broadcasting a transaction.
95
+ */
96
+ async simulate(
97
+ wallet: LocalWallet,
98
+ messaging: () => Promise<EncodeObject[]>,
99
+ gasPrice: GasPrice = this.getGasPrice(),
100
+ memo?: string,
101
+ account?: () => Promise<Account>,
102
+ ): Promise<StdFee> {
103
+ const msgsPromise = messaging();
104
+ const accountPromise = account ? await account() : this.account(wallet.address!);
105
+ const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
106
+ const msgs = msgsAndAccount[0];
107
+
108
+ return this.simulateTransaction(
109
+ wallet.pubKey!,
110
+ msgsAndAccount[1].sequence,
111
+ msgs,
112
+ gasPrice,
113
+ memo,
114
+ );
115
+ }
116
+
117
+ /**
118
+ * @description Sign a transaction
119
+ * the calling function is responsible for creating the messages.
120
+ *
121
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
122
+ * at any point.
123
+ * @returns The Signature.
124
+ */
125
+ async sign(
126
+ wallet: LocalWallet,
127
+ messaging: () => Promise<EncodeObject[]>,
128
+ zeroFee: boolean,
129
+ gasPrice: GasPrice = this.getGasPrice(),
130
+ memo?: string,
131
+ account?: () => Promise<Account>,
132
+ ): Promise<Uint8Array> {
133
+ const msgsPromise = await messaging();
134
+ const accountPromise = account ? await account() : this.account(wallet.address!);
135
+ const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
136
+ const msgs = msgsAndAccount[0];
137
+ return this.signTransaction(wallet, msgs, msgsAndAccount[1], zeroFee, gasPrice, memo);
138
+ }
139
+
140
+ /**
141
+ * @description Send a transaction
142
+ * the calling function is responsible for creating the messages.
143
+ *
144
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
145
+ * at any point.
146
+ * @returns The Tx Hash.
147
+ */
148
+ async send(
149
+ wallet: LocalWallet,
150
+ messaging: () => Promise<EncodeObject[]>,
151
+ zeroFee: boolean,
152
+ gasPrice: GasPrice = this.getGasPrice(),
153
+ memo?: string,
154
+ broadcastMode?: BroadcastMode,
155
+ account?: () => Promise<Account>,
156
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
157
+ const msgsPromise = messaging();
158
+ const accountPromise = account ? await account() : this.account(wallet.address!);
159
+ const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
160
+ const msgs = msgsAndAccount[0];
161
+
162
+ return this.signAndSendTransaction(
163
+ wallet,
164
+ msgsAndAccount[1],
165
+ msgs,
166
+ zeroFee,
167
+ gasPrice,
168
+ memo ?? this.defaultClientMemo,
169
+ broadcastMode ?? this.defaultBroadcastMode(msgs),
170
+ );
171
+ }
172
+
173
+ /**
174
+ * @description Calculate the default broadcast mode.
175
+ */
176
+ private defaultBroadcastMode(msgs: EncodeObject[]): BroadcastMode {
177
+ if (
178
+ msgs.length === 1 &&
179
+ (msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder' ||
180
+ msgs[0].typeUrl === '/dydxprotocol.clob.MsgCancelOrder')
70
181
  ) {
71
- this.get = get;
72
- this.chainId = chainId;
73
- this.registry = generateRegistry();
74
- this.composer = new Composer();
75
- this.denoms = denoms;
76
- this.defaultClientMemo = defaultClientMemo;
77
- this.defaultGasPrice = GasPrice
78
- .fromString(`0.025${denoms.USDC_GAS_DENOM !== undefined ? denoms.USDC_GAS_DENOM : denoms.USDC_DENOM}`);
79
- this.defaultDydxGasPrice = GasPrice
80
- .fromString(`25000000000${denoms.CHAINTOKEN_GAS_DENOM !== undefined ? denoms.CHAINTOKEN_GAS_DENOM : denoms.CHAINTOKEN_DENOM}`);
81
- }
82
-
83
- setSelectedGasDenom(selectedGasDenom: SelectedGasDenom): void {
84
- this.selectedGasDenom = selectedGasDenom;
85
- }
86
-
87
- getGasPrice(): GasPrice {
88
- return this.selectedGasDenom === SelectedGasDenom.USDC
89
- ? this.defaultGasPrice
90
- : this.defaultDydxGasPrice;
91
- }
92
-
93
- /**
94
- * @description Simulate a transaction
95
- * the calling function is responsible for creating the messages.
96
- *
97
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
98
- * at any point.
99
- * @returns The Fee for broadcasting a transaction.
100
- */
101
- async simulate(
102
- wallet: LocalWallet,
103
- messaging: () => Promise<EncodeObject[]>,
104
- gasPrice: GasPrice = this.getGasPrice(),
105
- memo?: string,
106
- account?: () => Promise<Account>,
107
- ): Promise<StdFee> {
108
- const msgsPromise = messaging();
109
- const accountPromise = account ? (await account()) : this.account(wallet.address!);
110
- const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
111
- const msgs = msgsAndAccount[0];
112
-
113
- return this.simulateTransaction(
114
- wallet.pubKey!,
115
- msgsAndAccount[1].sequence,
116
- msgs,
117
- gasPrice,
118
- memo,
119
- );
120
- }
121
-
122
- /**
123
- * @description Sign a transaction
124
- * the calling function is responsible for creating the messages.
125
- *
126
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
127
- * at any point.
128
- * @returns The Signature.
129
- */
130
- async sign(
131
- wallet: LocalWallet,
132
- messaging: () => Promise<EncodeObject[]>,
133
- zeroFee: boolean,
134
- gasPrice: GasPrice = this.getGasPrice(),
135
- memo?: string,
136
- account?: () => Promise<Account>,
137
- ): Promise<Uint8Array> {
138
- const msgsPromise = await messaging();
139
- const accountPromise = account ? (await account()) : this.account(wallet.address!);
140
- const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
141
- const msgs = msgsAndAccount[0];
142
- return this.signTransaction(wallet, msgs, msgsAndAccount[1], zeroFee, gasPrice, memo);
143
- }
144
-
145
- /**
146
- * @description Send a transaction
147
- * the calling function is responsible for creating the messages.
148
- *
149
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
150
- * at any point.
151
- * @returns The Tx Hash.
152
- */
153
- async send(
154
- wallet: LocalWallet,
155
- messaging: () => Promise<EncodeObject[]>,
156
- zeroFee: boolean,
157
- gasPrice: GasPrice = this.getGasPrice(),
158
- memo?: string,
159
- broadcastMode?: BroadcastMode,
160
- account?: () => Promise<Account>,
161
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
162
- const msgsPromise = messaging();
163
- const accountPromise = account ? (await account()) : this.account(wallet.address!);
164
- const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
165
- const msgs = msgsAndAccount[0];
166
-
167
- return this.signAndSendTransaction(
168
- wallet,
169
- msgsAndAccount[1],
170
- msgs,
171
- zeroFee,
172
- gasPrice,
173
- memo ?? this.defaultClientMemo,
174
- broadcastMode ?? this.defaultBroadcastMode(msgs),
175
- );
176
- }
177
-
178
- /**
179
- * @description Calculate the default broadcast mode.
180
- */
181
- private defaultBroadcastMode(msgs: EncodeObject[]): BroadcastMode {
182
- if (
183
- msgs.length === 1 &&
184
- (msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder' ||
185
- msgs[0].typeUrl === '/dydxprotocol.clob.MsgCancelOrder')
186
- ) {
187
- const orderFlags = msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder'
182
+ const orderFlags =
183
+ msgs[0].typeUrl === '/dydxprotocol.clob.MsgPlaceOrder'
188
184
  ? (msgs[0].value as MsgPlaceOrder).order?.orderId?.orderFlags
189
185
  : (msgs[0].value as MsgCancelOrder).orderId?.orderFlags;
190
186
 
191
- switch (orderFlags) {
192
- case OrderFlags.SHORT_TERM:
193
- return Method.BroadcastTxSync;
187
+ switch (orderFlags) {
188
+ case OrderFlags.SHORT_TERM:
189
+ return Method.BroadcastTxSync;
194
190
 
195
- case OrderFlags.LONG_TERM:
196
- case OrderFlags.CONDITIONAL:
197
- return Method.BroadcastTxCommit;
191
+ case OrderFlags.LONG_TERM:
192
+ case OrderFlags.CONDITIONAL:
193
+ return Method.BroadcastTxCommit;
198
194
 
199
- default:
200
- break;
201
- }
195
+ default:
196
+ break;
202
197
  }
203
- return Method.BroadcastTxSync;
204
- }
205
-
206
- /**
207
- * @description Sign and send a message
208
- *
209
- * @returns The Tx Response.
210
- */
211
- private async signTransaction(
212
- wallet: LocalWallet,
213
- messages: EncodeObject[],
214
- account: Account,
215
- zeroFee: boolean,
216
- gasPrice: GasPrice = this.getGasPrice(),
217
- memo?: string,
218
- ): Promise<Uint8Array> {
219
- // Simulate transaction if no fee is specified.
220
- const fee: StdFee = zeroFee ? {
221
- amount: [],
222
- gas: '1000000',
223
- } : await this.simulateTransaction(
224
- wallet.pubKey!,
225
- account.sequence,
226
- messages,
227
- gasPrice,
228
- memo,
229
- );
230
-
231
- const txOptions: TransactionOptions = {
232
- sequence: account.sequence,
233
- accountNumber: account.accountNumber,
234
- chainId: this.chainId,
235
- };
236
- // Generate signed transaction.
237
- return wallet.signTransaction(
238
- messages,
239
- txOptions,
240
- fee,
241
- memo,
242
- );
243
198
  }
244
-
245
- /**
246
- * @description Retrieve an account structure for transactions.
247
- * For short term orders, the sequence doesn't matter. Use cached if available.
248
- * For long term and conditional orders, a round trip to validator must be made.
249
- */
250
- public async account(address: string, orderFlags?: OrderFlags): Promise<Account> {
251
- if (orderFlags === OrderFlags.SHORT_TERM) {
252
- if (this.accountNumberCache.has(address)) {
253
- // For SHORT_TERM orders, the sequence doesn't matter
254
- return this.accountNumberCache.get(address)!;
199
+ return Method.BroadcastTxSync;
200
+ }
201
+
202
+ /**
203
+ * @description Sign and send a message
204
+ *
205
+ * @returns The Tx Response.
206
+ */
207
+ private async signTransaction(
208
+ wallet: LocalWallet,
209
+ messages: EncodeObject[],
210
+ account: Account,
211
+ zeroFee: boolean,
212
+ gasPrice: GasPrice = this.getGasPrice(),
213
+ memo?: string,
214
+ ): Promise<Uint8Array> {
215
+ // Simulate transaction if no fee is specified.
216
+ const fee: StdFee = zeroFee
217
+ ? {
218
+ amount: [],
219
+ gas: '1000000',
255
220
  }
221
+ : await this.simulateTransaction(wallet.pubKey!, account.sequence, messages, gasPrice, memo);
222
+
223
+ const txOptions: TransactionOptions = {
224
+ sequence: account.sequence,
225
+ accountNumber: account.accountNumber,
226
+ chainId: this.chainId,
227
+ };
228
+ // Generate signed transaction.
229
+ return wallet.signTransaction(messages, txOptions, fee, memo);
230
+ }
231
+
232
+ /**
233
+ * @description Retrieve an account structure for transactions.
234
+ * For short term orders, the sequence doesn't matter. Use cached if available.
235
+ * For long term and conditional orders, a round trip to validator must be made.
236
+ */
237
+ public async account(address: string, orderFlags?: OrderFlags): Promise<Account> {
238
+ if (orderFlags === OrderFlags.SHORT_TERM) {
239
+ if (this.accountNumberCache.has(address)) {
240
+ // For SHORT_TERM orders, the sequence doesn't matter
241
+ return this.accountNumberCache.get(address)!;
256
242
  }
257
- const account = await this.get.getAccount(address);
258
- this.accountNumberCache.set(address, account);
259
- return account;
260
243
  }
261
-
262
- /**
263
- * @description Sign and send a message
264
- *
265
- * @returns The Tx Response.
266
- */
267
- private async signAndSendTransaction(
268
- wallet: LocalWallet,
269
- account: Account,
270
- messages: EncodeObject[],
271
- zeroFee: boolean,
272
- gasPrice: GasPrice = this.getGasPrice(),
273
- memo?: string,
274
- broadcastMode?: BroadcastMode,
275
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
276
- const signedTransaction = await this.signTransaction(
277
- wallet,
278
- messages,
279
- account,
280
- zeroFee,
281
- gasPrice,
282
- memo,
283
- );
284
- return this.sendSignedTransaction(signedTransaction, broadcastMode);
244
+ const account = await this.get.getAccount(address);
245
+ this.accountNumberCache.set(address, account);
246
+ return account;
247
+ }
248
+
249
+ /**
250
+ * @description Sign and send a message
251
+ *
252
+ * @returns The Tx Response.
253
+ */
254
+ private async signAndSendTransaction(
255
+ wallet: LocalWallet,
256
+ account: Account,
257
+ messages: EncodeObject[],
258
+ zeroFee: boolean,
259
+ gasPrice: GasPrice = this.getGasPrice(),
260
+ memo?: string,
261
+ broadcastMode?: BroadcastMode,
262
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
263
+ const signedTransaction = await this.signTransaction(
264
+ wallet,
265
+ messages,
266
+ account,
267
+ zeroFee,
268
+ gasPrice,
269
+ memo,
270
+ );
271
+ return this.sendSignedTransaction(signedTransaction, broadcastMode);
272
+ }
273
+
274
+ /**
275
+ * @description Send signed transaction.
276
+ *
277
+ * @returns The Tx Response.
278
+ */
279
+ async sendSignedTransaction(
280
+ signedTransaction: Uint8Array,
281
+ broadcastMode?: BroadcastMode,
282
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
283
+ return this.get.tendermintClient.broadcastTransaction(
284
+ signedTransaction,
285
+ broadcastMode !== undefined ? broadcastMode : Method.BroadcastTxSync,
286
+ );
287
+ }
288
+
289
+ /**
290
+ * @description Simulate broadcasting a transaction.
291
+ *
292
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
293
+ * at any point.
294
+ * @returns The Fee for broadcasting a transaction.
295
+ */
296
+ private async simulateTransaction(
297
+ pubKey: Secp256k1Pubkey,
298
+ sequence: number,
299
+ messages: readonly EncodeObject[],
300
+ gasPrice: GasPrice = this.getGasPrice(),
301
+ memo?: string,
302
+ ): Promise<StdFee> {
303
+ // Get simulated response.
304
+ const encodedMessages: Any[] = messages.map((message: EncodeObject) =>
305
+ this.registry.encodeAsAny(message),
306
+ );
307
+ const simulationResponse = await this.get.stargateQueryClient.tx.simulate(
308
+ encodedMessages,
309
+ memo,
310
+ pubKey,
311
+ sequence,
312
+ );
313
+
314
+ // The promise should have been rejected if the gasInfo was undefined.
315
+ if (simulationResponse.gasInfo === undefined) {
316
+ throw new UnexpectedClientError();
285
317
  }
286
318
 
287
- /**
288
- * @description Send signed transaction.
289
- *
290
- * @returns The Tx Response.
291
- */
292
- async sendSignedTransaction(
293
- signedTransaction: Uint8Array,
294
- broadcastMode?: BroadcastMode,
295
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
296
- return this.get.tendermintClient.broadcastTransaction(
297
- signedTransaction,
298
- broadcastMode !== undefined
299
- ? broadcastMode
300
- : Method.BroadcastTxSync,
301
- );
302
- }
303
-
304
- /**
305
- * @description Simulate broadcasting a transaction.
306
- *
307
- * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
308
- * at any point.
309
- * @returns The Fee for broadcasting a transaction.
310
- */
311
- private async simulateTransaction(
312
- pubKey: Secp256k1Pubkey,
313
- sequence: number,
314
- messages: readonly EncodeObject[],
315
- gasPrice: GasPrice = this.getGasPrice(),
316
- memo?: string,
317
- ): Promise<StdFee> {
318
- // Get simulated response.
319
- const encodedMessages: Any[] = messages.map(
320
- (message: EncodeObject) => this.registry.encodeAsAny(message),
321
- );
322
- const simulationResponse = await this.get.stargateQueryClient.tx.simulate(
323
- encodedMessages,
324
- memo,
325
- pubKey,
326
- sequence,
327
- );
328
-
329
- // The promise should have been rejected if the gasInfo was undefined.
330
- if (simulationResponse.gasInfo === undefined) {
331
- throw new UnexpectedClientError();
319
+ // Calculate and return fee from gasEstimate.
320
+ const gasEstimate: number = Uint53.fromString(
321
+ simulationResponse.gasInfo.gasUsed.toString(),
322
+ ).toNumber();
323
+ const fee = calculateFee(Math.floor(gasEstimate * GAS_MULTIPLIER), gasPrice);
324
+
325
+ // TODO(TRCL-2550): Temporary workaround before IBC denom is supported in '@cosmjs/stargate'.
326
+ // The '@cosmjs/stargate' does not support denom with '/', so currently GAS_PRICE is
327
+ // represented in 'uusdc', and the output of `calculateFee` is in '', which is replaced
328
+ // below by USDC_DENOM string.
329
+ const amount: Coin[] = _.map(fee.amount, (coin: Coin) => {
330
+ if (coin.denom === 'uusdc') {
331
+ return {
332
+ amount: coin.amount,
333
+ denom: this.denoms.USDC_DENOM,
334
+ };
332
335
  }
333
-
334
- // Calculate and return fee from gasEstimate.
335
- const gasEstimate: number = Uint53.fromString(
336
- simulationResponse.gasInfo.gasUsed.toString(),
337
- ).toNumber();
338
- const fee = calculateFee(
339
- Math.floor(gasEstimate * GAS_MULTIPLIER),
340
- gasPrice,
341
- );
342
-
343
- // TODO(TRCL-2550): Temporary workaround before IBC denom is supported in '@cosmjs/stargate'.
344
- // The '@cosmjs/stargate' does not support denom with '/', so currently GAS_PRICE is
345
- // represented in 'uusdc', and the output of `calculateFee` is in '', which is replaced
346
- // below by USDC_DENOM string.
347
- const amount: Coin[] = _.map(fee.amount, (coin: Coin) => {
348
- if (coin.denom === 'uusdc') {
349
- return {
350
- amount: coin.amount,
351
- denom: this.denoms.USDC_DENOM,
352
- };
353
- }
354
- return coin;
355
- });
356
-
357
- return {
358
- ...fee,
359
- amount,
360
- };
361
- }
362
-
363
- // ------ State-Changing Requests ------ //
364
-
365
- async placeOrder(
366
- subaccount: SubaccountInfo,
367
- clientId: number,
368
- clobPairId: number,
369
- side: Order_Side,
370
- quantums: Long,
371
- subticks: Long,
372
- timeInForce: Order_TimeInForce,
373
- orderFlags: number,
374
- reduceOnly: boolean,
375
- goodTilBlock?: number,
376
- goodTilBlockTime?: number,
377
- clientMetadata: number = 0,
378
- conditionType: Order_ConditionType = Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
379
- conditionalOrderTriggerSubticks: Long = Long.fromInt(0),
380
- broadcastMode?: BroadcastMode,
381
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
382
- const msg = await this.placeOrderMsg(
383
- subaccount.address,
384
- subaccount.subaccountNumber,
336
+ return coin;
337
+ });
338
+
339
+ return {
340
+ ...fee,
341
+ amount,
342
+ };
343
+ }
344
+
345
+ // ------ State-Changing Requests ------ //
346
+
347
+ async placeOrder(
348
+ subaccount: SubaccountInfo,
349
+ clientId: number,
350
+ clobPairId: number,
351
+ side: Order_Side,
352
+ quantums: Long,
353
+ subticks: Long,
354
+ timeInForce: Order_TimeInForce,
355
+ orderFlags: number,
356
+ reduceOnly: boolean,
357
+ goodTilBlock?: number,
358
+ goodTilBlockTime?: number,
359
+ clientMetadata: number = 0,
360
+ conditionType: Order_ConditionType = Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
361
+ conditionalOrderTriggerSubticks: Long = Long.fromInt(0),
362
+ broadcastMode?: BroadcastMode,
363
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
364
+ const msg = await this.placeOrderMsg(
365
+ subaccount.address,
366
+ subaccount.subaccountNumber,
367
+ clientId,
368
+ clobPairId,
369
+ side,
370
+ quantums,
371
+ subticks,
372
+ timeInForce,
373
+ orderFlags,
374
+ reduceOnly,
375
+ goodTilBlock,
376
+ goodTilBlockTime,
377
+ clientMetadata,
378
+ conditionType,
379
+ conditionalOrderTriggerSubticks,
380
+ );
381
+ const account: Promise<Account> = this.account(subaccount.address, orderFlags);
382
+ return this.send(
383
+ subaccount.wallet,
384
+ () => Promise.resolve([msg]),
385
+ true,
386
+ undefined,
387
+ undefined,
388
+ broadcastMode,
389
+ () => account,
390
+ );
391
+ }
392
+
393
+ async placeOrderMsg(
394
+ address: string,
395
+ subaccountNumber: number,
396
+ clientId: number,
397
+ clobPairId: number,
398
+ side: Order_Side,
399
+ quantums: Long,
400
+ subticks: Long,
401
+ timeInForce: Order_TimeInForce,
402
+ orderFlags: number,
403
+ reduceOnly: boolean,
404
+ goodTilBlock?: number,
405
+ goodTilBlockTime?: number,
406
+ clientMetadata: number = 0,
407
+ conditionType: Order_ConditionType = Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
408
+ conditionalOrderTriggerSubticks: Long = Long.fromInt(0),
409
+ ): Promise<EncodeObject> {
410
+ return new Promise((resolve) => {
411
+ const msg = this.composer.composeMsgPlaceOrder(
412
+ address,
413
+ subaccountNumber,
385
414
  clientId,
386
415
  clobPairId,
416
+ orderFlags,
417
+ goodTilBlock ?? 0,
418
+ goodTilBlockTime ?? 0,
387
419
  side,
388
420
  quantums,
389
421
  subticks,
390
422
  timeInForce,
391
- orderFlags,
392
423
  reduceOnly,
393
- goodTilBlock,
394
- goodTilBlockTime,
395
424
  clientMetadata,
396
425
  conditionType,
397
426
  conditionalOrderTriggerSubticks,
398
427
  );
399
- const account: Promise<Account> = this.account(subaccount.address, orderFlags);
400
- return this.send(
401
- subaccount.wallet,
402
- () => Promise.resolve([msg]),
403
- true,
404
- undefined,
405
- undefined,
406
- broadcastMode,
407
- () => account,
408
- );
409
- }
410
-
411
- async placeOrderMsg(
412
- address: string,
413
- subaccountNumber: number,
414
- clientId: number,
415
- clobPairId: number,
416
- side: Order_Side,
417
- quantums: Long,
418
- subticks: Long,
419
- timeInForce: Order_TimeInForce,
420
- orderFlags: number,
421
- reduceOnly: boolean,
422
- goodTilBlock?: number,
423
- goodTilBlockTime?: number,
424
- clientMetadata: number = 0,
425
- conditionType: Order_ConditionType = Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
426
- conditionalOrderTriggerSubticks: Long = Long.fromInt(0),
427
- ): Promise<EncodeObject> {
428
- return new Promise((resolve) => {
429
- const msg = this.composer.composeMsgPlaceOrder(
430
- address,
431
- subaccountNumber,
432
- clientId,
433
- clobPairId,
434
- orderFlags,
435
- goodTilBlock ?? 0,
436
- goodTilBlockTime ?? 0,
437
- side,
438
- quantums,
439
- subticks,
440
- timeInForce,
441
- reduceOnly,
442
- clientMetadata,
443
- conditionType,
444
- conditionalOrderTriggerSubticks,
445
- );
446
- resolve(msg);
447
- });
448
- }
449
-
450
- async placeOrderObject(
451
- subaccount: SubaccountInfo,
452
- placeOrder: IPlaceOrder,
453
- broadcastMode?: BroadcastMode,
454
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
455
- return this.placeOrder(
456
- subaccount,
457
- placeOrder.clientId,
458
- placeOrder.clobPairId,
459
- placeOrder.side,
460
- placeOrder.quantums,
461
- placeOrder.subticks,
462
- placeOrder.timeInForce,
463
- placeOrder.orderFlags,
464
- placeOrder.reduceOnly,
465
- placeOrder.goodTilBlock,
466
- placeOrder.goodTilBlockTime,
467
- placeOrder.clientMetadata,
468
- placeOrder.conditionType ?? Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
469
- placeOrder.conditionalOrderTriggerSubticks ?? Long.fromInt(0),
470
- broadcastMode,
471
- );
472
- }
473
-
474
- async cancelOrder(
475
- subaccount: SubaccountInfo,
476
- clientId: number,
477
- orderFlags: OrderFlags,
478
- clobPairId: number,
479
- goodTilBlock?: number,
480
- goodTilBlockTime?: number,
481
- broadcastMode?: BroadcastMode,
482
- ) : Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
483
- const msg = await this.cancelOrderMsg(
484
- subaccount.address,
485
- subaccount.subaccountNumber,
428
+ resolve(msg);
429
+ });
430
+ }
431
+
432
+ async placeOrderObject(
433
+ subaccount: SubaccountInfo,
434
+ placeOrder: IPlaceOrder,
435
+ broadcastMode?: BroadcastMode,
436
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
437
+ return this.placeOrder(
438
+ subaccount,
439
+ placeOrder.clientId,
440
+ placeOrder.clobPairId,
441
+ placeOrder.side,
442
+ placeOrder.quantums,
443
+ placeOrder.subticks,
444
+ placeOrder.timeInForce,
445
+ placeOrder.orderFlags,
446
+ placeOrder.reduceOnly,
447
+ placeOrder.goodTilBlock,
448
+ placeOrder.goodTilBlockTime,
449
+ placeOrder.clientMetadata,
450
+ placeOrder.conditionType ?? Order_ConditionType.CONDITION_TYPE_UNSPECIFIED,
451
+ placeOrder.conditionalOrderTriggerSubticks ?? Long.fromInt(0),
452
+ broadcastMode,
453
+ );
454
+ }
455
+
456
+ async cancelOrder(
457
+ subaccount: SubaccountInfo,
458
+ clientId: number,
459
+ orderFlags: OrderFlags,
460
+ clobPairId: number,
461
+ goodTilBlock?: number,
462
+ goodTilBlockTime?: number,
463
+ broadcastMode?: BroadcastMode,
464
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
465
+ const msg = await this.cancelOrderMsg(
466
+ subaccount.address,
467
+ subaccount.subaccountNumber,
468
+ clientId,
469
+ orderFlags,
470
+ clobPairId,
471
+ goodTilBlock ?? 0,
472
+ goodTilBlockTime ?? 0,
473
+ );
474
+ return this.send(
475
+ subaccount.wallet,
476
+ () => Promise.resolve([msg]),
477
+ true,
478
+ undefined,
479
+ undefined,
480
+ broadcastMode,
481
+ );
482
+ }
483
+
484
+ async cancelOrderMsg(
485
+ address: string,
486
+ subaccountNumber: number,
487
+ clientId: number,
488
+ orderFlags: OrderFlags,
489
+ clobPairId: number,
490
+ goodTilBlock?: number,
491
+ goodTilBlockTime?: number,
492
+ ): Promise<EncodeObject> {
493
+ return new Promise((resolve) => {
494
+ const msg = this.composer.composeMsgCancelOrder(
495
+ address,
496
+ subaccountNumber,
486
497
  clientId,
487
- orderFlags,
488
498
  clobPairId,
499
+ orderFlags,
489
500
  goodTilBlock ?? 0,
490
501
  goodTilBlockTime ?? 0,
491
502
  );
492
- return this.send(
493
- subaccount.wallet,
494
- () => Promise.resolve([msg]),
495
- true,
496
- undefined,
497
- undefined,
498
- broadcastMode);
499
- }
500
-
501
- async cancelOrderMsg(
502
- address: string,
503
- subaccountNumber: number,
504
- clientId: number,
505
- orderFlags: OrderFlags,
506
- clobPairId: number,
507
- goodTilBlock?: number,
508
- goodTilBlockTime?: number,
509
- ) : Promise<EncodeObject> {
510
- return new Promise((resolve) => {
511
- const msg = this.composer.composeMsgCancelOrder(
512
- address,
513
- subaccountNumber,
514
- clientId,
515
- clobPairId,
516
- orderFlags,
517
- goodTilBlock ?? 0,
518
- goodTilBlockTime ?? 0,
519
- );
520
- resolve(msg);
521
- });
522
- }
523
-
524
- async cancelOrderObject(
525
- subaccount: SubaccountInfo,
526
- cancelOrder: ICancelOrder,
527
- broadcastMode?: BroadcastMode,
528
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
529
- return this.cancelOrder(
530
- subaccount,
531
- cancelOrder.clientId,
532
- cancelOrder.orderFlags,
533
- cancelOrder.clobPairId,
534
- cancelOrder.goodTilBlock,
535
- cancelOrder.goodTilBlockTime,
536
- broadcastMode,
537
- );
538
- }
539
-
540
- async transfer(
541
- subaccount: SubaccountInfo,
542
- recipientAddress: string,
543
- recipientSubaccountNumber: number,
544
- assetId: number,
545
- amount: Long,
546
- broadcastMode?: BroadcastMode,
547
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
548
- const msg = await this.transferMsg(
549
- subaccount.address,
550
- subaccount.subaccountNumber,
503
+ resolve(msg);
504
+ });
505
+ }
506
+
507
+ async cancelOrderObject(
508
+ subaccount: SubaccountInfo,
509
+ cancelOrder: ICancelOrder,
510
+ broadcastMode?: BroadcastMode,
511
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
512
+ return this.cancelOrder(
513
+ subaccount,
514
+ cancelOrder.clientId,
515
+ cancelOrder.orderFlags,
516
+ cancelOrder.clobPairId,
517
+ cancelOrder.goodTilBlock,
518
+ cancelOrder.goodTilBlockTime,
519
+ broadcastMode,
520
+ );
521
+ }
522
+
523
+ async transfer(
524
+ subaccount: SubaccountInfo,
525
+ recipientAddress: string,
526
+ recipientSubaccountNumber: number,
527
+ assetId: number,
528
+ amount: Long,
529
+ broadcastMode?: BroadcastMode,
530
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
531
+ const msg = await this.transferMsg(
532
+ subaccount.address,
533
+ subaccount.subaccountNumber,
534
+ recipientAddress,
535
+ recipientSubaccountNumber,
536
+ assetId,
537
+ amount,
538
+ );
539
+ return this.send(
540
+ subaccount.wallet,
541
+ () => Promise.resolve([msg]),
542
+ false,
543
+ undefined,
544
+ undefined,
545
+ broadcastMode,
546
+ );
547
+ }
548
+
549
+ async transferMsg(
550
+ address: string,
551
+ subaccountNumber: number,
552
+ recipientAddress: string,
553
+ recipientSubaccountNumber: number,
554
+ assetId: number,
555
+ amount: Long,
556
+ ): Promise<EncodeObject> {
557
+ return new Promise((resolve) => {
558
+ const msg = this.composer.composeMsgTransfer(
559
+ address,
560
+ subaccountNumber,
551
561
  recipientAddress,
552
562
  recipientSubaccountNumber,
553
563
  assetId,
554
564
  amount,
555
565
  );
556
- return this.send(
557
- subaccount.wallet,
558
- () => Promise.resolve([msg]),
559
- false,
560
- undefined,
561
- undefined,
562
- broadcastMode,
563
- );
564
- }
565
-
566
- async transferMsg(
567
- address: string,
568
- subaccountNumber: number,
569
- recipientAddress: string,
570
- recipientSubaccountNumber: number,
571
- assetId: number,
572
- amount: Long,
573
- ): Promise<EncodeObject> {
574
- return new Promise((resolve) => {
575
- const msg = this.composer.composeMsgTransfer(
576
- address,
577
- subaccountNumber,
578
- recipientAddress,
579
- recipientSubaccountNumber,
580
- assetId,
581
- amount,
582
- );
583
- resolve(msg);
584
- });
585
- }
586
-
587
- async deposit(
588
- subaccount: SubaccountInfo,
589
- assetId: number,
590
- quantums: Long,
591
- broadcastMode?: BroadcastMode,
592
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
593
- const msg = await this.depositMsg(
594
- subaccount.address,
595
- subaccount.subaccountNumber,
566
+ resolve(msg);
567
+ });
568
+ }
569
+
570
+ async deposit(
571
+ subaccount: SubaccountInfo,
572
+ assetId: number,
573
+ quantums: Long,
574
+ broadcastMode?: BroadcastMode,
575
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
576
+ const msg = await this.depositMsg(
577
+ subaccount.address,
578
+ subaccount.subaccountNumber,
579
+ assetId,
580
+ quantums,
581
+ );
582
+ return this.send(
583
+ subaccount.wallet,
584
+ () => Promise.resolve([msg]),
585
+ false,
586
+ undefined,
587
+ undefined,
588
+ broadcastMode,
589
+ );
590
+ }
591
+
592
+ async depositMsg(
593
+ address: string,
594
+ subaccountNumber: number,
595
+ assetId: number,
596
+ quantums: Long,
597
+ ): Promise<EncodeObject> {
598
+ return new Promise((resolve) => {
599
+ const msg = this.composer.composeMsgDepositToSubaccount(
600
+ address,
601
+ subaccountNumber,
596
602
  assetId,
597
603
  quantums,
598
604
  );
599
- return this.send(
600
- subaccount.wallet,
601
- () => Promise.resolve([msg]),
602
- false,
603
- undefined,
604
- undefined,
605
- broadcastMode,
606
- );
607
- }
608
-
609
- async depositMsg(
610
- address: string,
611
- subaccountNumber: number,
612
- assetId: number,
613
- quantums: Long,
614
- ): Promise<EncodeObject> {
615
- return new Promise((resolve) => {
616
- const msg = this.composer.composeMsgDepositToSubaccount(
617
- address,
618
- subaccountNumber,
619
- assetId,
620
- quantums,
621
- );
622
- resolve(msg);
623
- });
624
- }
625
-
626
- async withdraw(
627
- subaccount: SubaccountInfo,
628
- assetId: number,
629
- quantums: Long,
630
- recipient?: string,
631
- broadcastMode?: BroadcastMode,
632
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
633
- const msg = await this.withdrawMsg(
634
- subaccount.address,
635
- subaccount.subaccountNumber,
605
+ resolve(msg);
606
+ });
607
+ }
608
+
609
+ async withdraw(
610
+ subaccount: SubaccountInfo,
611
+ assetId: number,
612
+ quantums: Long,
613
+ recipient?: string,
614
+ broadcastMode?: BroadcastMode,
615
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
616
+ const msg = await this.withdrawMsg(
617
+ subaccount.address,
618
+ subaccount.subaccountNumber,
619
+ assetId,
620
+ quantums,
621
+ recipient,
622
+ );
623
+ return this.send(
624
+ subaccount.wallet,
625
+ () => Promise.resolve([msg]),
626
+ false,
627
+ undefined,
628
+ undefined,
629
+ broadcastMode,
630
+ );
631
+ }
632
+
633
+ async withdrawMsg(
634
+ address: string,
635
+ subaccountNumber: number,
636
+ assetId: number,
637
+ quantums: Long,
638
+ recipient?: string,
639
+ ): Promise<EncodeObject> {
640
+ return new Promise((resolve) => {
641
+ const msg = this.composer.composeMsgWithdrawFromSubaccount(
642
+ address,
643
+ subaccountNumber,
636
644
  assetId,
637
645
  quantums,
638
646
  recipient,
639
647
  );
640
- return this.send(
641
- subaccount.wallet,
642
- () => Promise.resolve([msg]),
643
- false,
644
- undefined,
645
- undefined,
646
- broadcastMode,
647
- );
648
- }
649
-
650
- async withdrawMsg(
651
- address: string,
652
- subaccountNumber: number,
653
- assetId: number,
654
- quantums: Long,
655
- recipient?: string,
656
- ): Promise<EncodeObject> {
657
- return new Promise((resolve) => {
658
- const msg = this.composer.composeMsgWithdrawFromSubaccount(
659
- address,
660
- subaccountNumber,
661
- assetId,
662
- quantums,
663
- recipient,
664
- );
665
- resolve(msg);
666
- });
667
- }
668
-
669
- async sendToken(
670
- subaccount: SubaccountInfo,
671
- recipient: string,
672
- coinDenom: string,
673
- quantums: string,
674
- zeroFee: boolean = true,
675
- broadcastMode?: BroadcastMode,
676
- ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
677
- const msg = await this.sendTokenMsg(
678
- subaccount.address,
679
- recipient,
680
- coinDenom,
681
- quantums,
682
- );
683
- return this.send(
684
- subaccount.wallet,
685
- () => Promise.resolve([msg]),
686
- zeroFee,
687
- coinDenom === this.denoms.CHAINTOKEN_DENOM
688
- ? this.defaultDydxGasPrice
689
- : this.defaultGasPrice,
690
- undefined,
691
- broadcastMode,
692
- );
648
+ resolve(msg);
649
+ });
650
+ }
651
+
652
+ async sendToken(
653
+ subaccount: SubaccountInfo,
654
+ recipient: string,
655
+ coinDenom: string,
656
+ quantums: string,
657
+ zeroFee: boolean = true,
658
+ broadcastMode?: BroadcastMode,
659
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
660
+ const msg = await this.sendTokenMsg(subaccount.address, recipient, coinDenom, quantums);
661
+ return this.send(
662
+ subaccount.wallet,
663
+ () => Promise.resolve([msg]),
664
+ zeroFee,
665
+ coinDenom === this.denoms.CHAINTOKEN_DENOM ? this.defaultDydxGasPrice : this.defaultGasPrice,
666
+ undefined,
667
+ broadcastMode,
668
+ );
669
+ }
670
+
671
+ async sendTokenMsg(
672
+ address: string,
673
+ recipient: string,
674
+ coinDenom: string,
675
+ quantums: string,
676
+ ): Promise<EncodeObject> {
677
+ if (coinDenom !== this.denoms.CHAINTOKEN_DENOM && coinDenom !== this.denoms.USDC_DENOM) {
678
+ throw new Error('Unsupported coinDenom');
693
679
  }
694
680
 
695
- async sendTokenMsg(
696
- address: string,
697
- recipient: string,
698
- coinDenom: string,
699
- quantums: string,
700
- ): Promise<EncodeObject> {
701
- if (coinDenom !== this.denoms.CHAINTOKEN_DENOM && coinDenom !== this.denoms.USDC_DENOM) {
702
- throw new Error('Unsupported coinDenom');
703
- }
704
-
705
- return new Promise((resolve) => {
706
- const msg = this.composer.composeMsgSendToken(
707
- address,
708
- recipient,
709
- coinDenom,
710
- quantums,
711
- );
712
- resolve(msg);
713
- });
714
- }
681
+ return new Promise((resolve) => {
682
+ const msg = this.composer.composeMsgSendToken(address, recipient, coinDenom, quantums);
683
+ resolve(msg);
684
+ });
685
+ }
686
+
687
+ async delegate(
688
+ subaccount: SubaccountInfo,
689
+ delegator: string,
690
+ validator: string,
691
+ amount: string,
692
+ broadcastMode?: BroadcastMode,
693
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
694
+ const msg = this.composer.composeMsgDelegate(delegator, validator, {
695
+ denom: this.denoms.CHAINTOKEN_DENOM,
696
+ amount,
697
+ });
698
+ return this.send(
699
+ subaccount.wallet,
700
+ () => Promise.resolve([msg]),
701
+ false,
702
+ this.defaultDydxGasPrice,
703
+ undefined,
704
+ broadcastMode,
705
+ );
706
+ }
707
+
708
+ delegateMsg(delegator: string, validator: string, amount: string): EncodeObject {
709
+ return this.composer.composeMsgDelegate(delegator, validator, {
710
+ denom: this.denoms.CHAINTOKEN_DENOM,
711
+ amount,
712
+ });
713
+ }
714
+
715
+ async undelegate(
716
+ subaccount: SubaccountInfo,
717
+ delegator: string,
718
+ validator: string,
719
+ amount: string,
720
+ broadcastMode?: BroadcastMode,
721
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
722
+ const msg = this.composer.composeMsgUndelegate(delegator, validator, {
723
+ denom: this.denoms.CHAINTOKEN_DENOM,
724
+ amount,
725
+ });
726
+ return this.send(
727
+ subaccount.wallet,
728
+ () => Promise.resolve([msg]),
729
+ false,
730
+ this.defaultDydxGasPrice,
731
+ undefined,
732
+ broadcastMode,
733
+ );
734
+ }
735
+
736
+ undelegateMsg(delegator: string, validator: string, amount: string): EncodeObject {
737
+ return this.composer.composeMsgUndelegate(delegator, validator, {
738
+ denom: this.denoms.CHAINTOKEN_DENOM,
739
+ amount,
740
+ });
741
+ }
742
+
743
+ async withdrawDelegatorReward(
744
+ subaccount: SubaccountInfo,
745
+ delegator: string,
746
+ validator: string,
747
+ broadcastMode?: BroadcastMode,
748
+ ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
749
+ const msg = this.composer.composeMsgWithdrawDelegatorReward(delegator, validator);
750
+ return this.send(
751
+ subaccount.wallet,
752
+ () => Promise.resolve([msg]),
753
+ false,
754
+ undefined,
755
+ undefined,
756
+ broadcastMode,
757
+ );
758
+ }
759
+
760
+ async withdrawDelegatorRewardMsg(delegator: string, validator: string): Promise<EncodeObject> {
761
+ const msg = this.composer.composeMsgWithdrawDelegatorReward(delegator, validator);
762
+
763
+ return Promise.resolve(msg);
764
+ }
715
765
  }