@dydxprotocol/v4-client-js 2.7.0 → 3.0.0
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/CHANGELOG.md +7 -2
- package/__tests__/modules/client/Transfers.test.ts +1 -1
- package/__tests__/modules/client/ValidatorPostEndpoints.test.ts +1 -1
- package/build/cjs/__tests__/modules/client/Transfers.test.js +2 -2
- package/build/cjs/__tests__/modules/client/ValidatorPostEndpoints.test.js +2 -2
- package/build/cjs/examples/batch_cancel_orders_example.js +2 -2
- package/build/cjs/examples/composite_example.js +2 -2
- package/build/cjs/examples/long_term_order_cancel_example.js +2 -2
- package/build/cjs/examples/noble_example.js +3 -2
- package/build/cjs/examples/permissioned_keys_example.js +5 -8
- package/build/cjs/examples/short_term_order_cancel_example.js +2 -2
- package/build/cjs/examples/short_term_order_composite_example.js +2 -2
- package/build/cjs/examples/test.js +2 -2
- package/build/cjs/examples/transfer_example_deposit.js +2 -2
- package/build/cjs/examples/transfer_example_send.js +3 -3
- package/build/cjs/examples/transfer_example_subaccount_transfer.js +2 -2
- package/build/cjs/examples/transfer_example_withdraw.js +2 -2
- package/build/cjs/examples/transfer_example_withdraw_other.js +3 -3
- package/build/cjs/examples/validator_post_example.js +2 -2
- package/build/cjs/src/clients/composite-client.js +22 -27
- package/build/cjs/src/clients/modules/local-wallet.js +12 -1
- package/build/cjs/src/clients/modules/post.js +32 -32
- package/build/cjs/src/clients/native.js +23 -22
- package/build/cjs/src/clients/subaccount.js +19 -10
- package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/build/esm/__tests__/modules/client/Transfers.test.js +2 -2
- package/build/esm/__tests__/modules/client/ValidatorPostEndpoints.test.js +2 -2
- package/build/esm/examples/batch_cancel_orders_example.js +2 -2
- package/build/esm/examples/composite_example.js +2 -2
- package/build/esm/examples/long_term_order_cancel_example.js +2 -2
- package/build/esm/examples/noble_example.js +3 -2
- package/build/esm/examples/permissioned_keys_example.js +5 -8
- package/build/esm/examples/short_term_order_cancel_example.js +2 -2
- package/build/esm/examples/short_term_order_composite_example.js +2 -2
- package/build/esm/examples/test.js +2 -2
- package/build/esm/examples/transfer_example_deposit.js +2 -2
- package/build/esm/examples/transfer_example_send.js +3 -3
- package/build/esm/examples/transfer_example_subaccount_transfer.js +2 -2
- package/build/esm/examples/transfer_example_withdraw.js +2 -2
- package/build/esm/examples/transfer_example_withdraw_other.js +3 -3
- package/build/esm/examples/validator_post_example.js +2 -2
- package/build/esm/src/clients/composite-client.d.ts +4 -8
- package/build/esm/src/clients/composite-client.d.ts.map +1 -1
- package/build/esm/src/clients/composite-client.js +22 -27
- package/build/esm/src/clients/modules/local-wallet.d.ts +2 -0
- package/build/esm/src/clients/modules/local-wallet.d.ts.map +1 -1
- package/build/esm/src/clients/modules/local-wallet.js +13 -2
- package/build/esm/src/clients/modules/post.d.ts +3 -4
- package/build/esm/src/clients/modules/post.d.ts.map +1 -1
- package/build/esm/src/clients/modules/post.js +32 -32
- package/build/esm/src/clients/native.d.ts.map +1 -1
- package/build/esm/src/clients/native.js +23 -22
- package/build/esm/src/clients/subaccount.d.ts +9 -3
- package/build/esm/src/clients/subaccount.d.ts.map +1 -1
- package/build/esm/src/clients/subaccount.js +19 -10
- package/build/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/examples/batch_cancel_orders_example.ts +3 -3
- package/examples/composite_example.ts +1 -1
- package/examples/long_term_order_cancel_example.ts +1 -1
- package/examples/noble_example.ts +2 -1
- package/examples/permissioned_keys_example.ts +9 -8
- package/examples/short_term_order_cancel_example.ts +1 -1
- package/examples/short_term_order_composite_example.ts +1 -1
- package/examples/test.ts +1 -1
- package/examples/transfer_example_deposit.ts +1 -1
- package/examples/transfer_example_send.ts +2 -2
- package/examples/transfer_example_subaccount_transfer.ts +1 -1
- package/examples/transfer_example_withdraw.ts +1 -1
- package/examples/transfer_example_withdraw_other.ts +2 -2
- package/examples/validator_post_example.ts +1 -1
- package/package.json +1 -1
- package/src/clients/composite-client.ts +21 -42
- package/src/clients/modules/local-wallet.ts +14 -0
- package/src/clients/modules/post.ts +37 -37
- package/src/clients/native.ts +39 -39
- package/src/clients/subaccount.ts +42 -10
|
@@ -26,7 +26,6 @@ import {
|
|
|
26
26
|
} from '../types';
|
|
27
27
|
import { Composer } from './composer';
|
|
28
28
|
import { Get } from './get';
|
|
29
|
-
import LocalWallet from './local-wallet';
|
|
30
29
|
import {
|
|
31
30
|
Order_Side,
|
|
32
31
|
Order_TimeInForce,
|
|
@@ -121,7 +120,7 @@ export class Post {
|
|
|
121
120
|
* @returns The Fee for broadcasting a transaction.
|
|
122
121
|
*/
|
|
123
122
|
async simulate(
|
|
124
|
-
|
|
123
|
+
subaccount: SubaccountInfo,
|
|
125
124
|
messaging: () => Promise<EncodeObject[]>,
|
|
126
125
|
gasPrice: GasPrice = this.getGasPrice(),
|
|
127
126
|
memo?: string,
|
|
@@ -135,13 +134,19 @@ export class Post {
|
|
|
135
134
|
msgs = await messaging();
|
|
136
135
|
} else {
|
|
137
136
|
const msgsPromise = messaging();
|
|
138
|
-
const accountPromise = account ? await account() : this.account(
|
|
137
|
+
const accountPromise = account ? await account() : this.account(subaccount.address);
|
|
139
138
|
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
140
139
|
msgs = msgsAndAccount[0];
|
|
141
140
|
sequence = msgsAndAccount[1].sequence;
|
|
142
141
|
}
|
|
143
142
|
|
|
144
|
-
return this.simulateTransaction(
|
|
143
|
+
return this.simulateTransaction(
|
|
144
|
+
subaccount.signingWallet.pubKey!,
|
|
145
|
+
sequence,
|
|
146
|
+
msgs,
|
|
147
|
+
gasPrice,
|
|
148
|
+
memo,
|
|
149
|
+
);
|
|
145
150
|
}
|
|
146
151
|
|
|
147
152
|
/**
|
|
@@ -153,7 +158,7 @@ export class Post {
|
|
|
153
158
|
* @returns The Signature.
|
|
154
159
|
*/
|
|
155
160
|
async sign(
|
|
156
|
-
|
|
161
|
+
subaccount: SubaccountInfo,
|
|
157
162
|
messaging: () => Promise<EncodeObject[]>,
|
|
158
163
|
zeroFee: boolean,
|
|
159
164
|
gasPrice: GasPrice = this.getGasPrice(),
|
|
@@ -161,10 +166,10 @@ export class Post {
|
|
|
161
166
|
account?: () => Promise<Account>,
|
|
162
167
|
): Promise<Uint8Array> {
|
|
163
168
|
const msgsPromise = await messaging();
|
|
164
|
-
const accountPromise = account ? await account() : this.account(
|
|
169
|
+
const accountPromise = account ? await account() : this.account(subaccount.address);
|
|
165
170
|
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
166
171
|
const msgs = msgsAndAccount[0];
|
|
167
|
-
return this.signTransaction(
|
|
172
|
+
return this.signTransaction(subaccount, msgs, msgsAndAccount[1], zeroFee, gasPrice, memo);
|
|
168
173
|
}
|
|
169
174
|
|
|
170
175
|
/**
|
|
@@ -176,7 +181,7 @@ export class Post {
|
|
|
176
181
|
* @returns The Tx Hash.
|
|
177
182
|
*/
|
|
178
183
|
async send(
|
|
179
|
-
|
|
184
|
+
subaccount: SubaccountInfo,
|
|
180
185
|
messaging: () => Promise<EncodeObject[]>,
|
|
181
186
|
zeroFee: boolean,
|
|
182
187
|
gasPrice: GasPrice = this.getGasPrice(),
|
|
@@ -184,15 +189,14 @@ export class Post {
|
|
|
184
189
|
broadcastMode?: BroadcastMode,
|
|
185
190
|
account?: () => Promise<Account>,
|
|
186
191
|
gasAdjustment: number = GAS_MULTIPLIER,
|
|
187
|
-
authenticators?: Long[],
|
|
188
192
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
189
193
|
const msgsPromise = messaging();
|
|
190
|
-
const accountPromise = account ? await account() : this.account(
|
|
194
|
+
const accountPromise = account ? await account() : this.account(subaccount.address);
|
|
191
195
|
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
|
|
192
196
|
const msgs = msgsAndAccount[0];
|
|
193
197
|
|
|
194
198
|
return this.signAndSendTransaction(
|
|
195
|
-
|
|
199
|
+
subaccount,
|
|
196
200
|
msgsAndAccount[1],
|
|
197
201
|
msgs,
|
|
198
202
|
zeroFee,
|
|
@@ -200,7 +204,6 @@ export class Post {
|
|
|
200
204
|
memo ?? this.defaultClientMemo,
|
|
201
205
|
broadcastMode ?? this.defaultBroadcastMode(msgs),
|
|
202
206
|
gasAdjustment,
|
|
203
|
-
authenticators,
|
|
204
207
|
);
|
|
205
208
|
}
|
|
206
209
|
|
|
@@ -239,14 +242,13 @@ export class Post {
|
|
|
239
242
|
* @returns The Tx Response.
|
|
240
243
|
*/
|
|
241
244
|
private async signTransaction(
|
|
242
|
-
|
|
245
|
+
subaccount: SubaccountInfo,
|
|
243
246
|
messages: EncodeObject[],
|
|
244
247
|
account: Account,
|
|
245
248
|
zeroFee: boolean,
|
|
246
249
|
gasPrice: GasPrice = this.getGasPrice(),
|
|
247
250
|
memo?: string,
|
|
248
251
|
gasAdjustment: number = GAS_MULTIPLIER,
|
|
249
|
-
authenticators?: Long[],
|
|
250
252
|
): Promise<Uint8Array> {
|
|
251
253
|
// protocol expects timestamp nonce in UTC milliseconds, which is the unit returned by Date.now()
|
|
252
254
|
const sequence = this.useTimestampNonce
|
|
@@ -259,7 +261,7 @@ export class Post {
|
|
|
259
261
|
gas: '1000000',
|
|
260
262
|
}
|
|
261
263
|
: await this.simulateTransaction(
|
|
262
|
-
|
|
264
|
+
subaccount.signingWallet.pubKey!,
|
|
263
265
|
sequence,
|
|
264
266
|
messages,
|
|
265
267
|
gasPrice,
|
|
@@ -271,10 +273,10 @@ export class Post {
|
|
|
271
273
|
sequence,
|
|
272
274
|
accountNumber: account.accountNumber,
|
|
273
275
|
chainId: this.chainId,
|
|
274
|
-
authenticators,
|
|
276
|
+
authenticators: subaccount.authenticators,
|
|
275
277
|
};
|
|
276
278
|
// Generate signed transaction.
|
|
277
|
-
return
|
|
279
|
+
return subaccount.signingWallet.signTransaction(messages, txOptions, fee, memo);
|
|
278
280
|
}
|
|
279
281
|
|
|
280
282
|
/**
|
|
@@ -301,7 +303,7 @@ export class Post {
|
|
|
301
303
|
* @returns The Tx Response.
|
|
302
304
|
*/
|
|
303
305
|
private async signAndSendTransaction(
|
|
304
|
-
|
|
306
|
+
subaccount: SubaccountInfo,
|
|
305
307
|
account: Account,
|
|
306
308
|
messages: EncodeObject[],
|
|
307
309
|
zeroFee: boolean,
|
|
@@ -309,17 +311,15 @@ export class Post {
|
|
|
309
311
|
memo?: string,
|
|
310
312
|
broadcastMode?: BroadcastMode,
|
|
311
313
|
gasAdjustment: number = GAS_MULTIPLIER,
|
|
312
|
-
authenticators?: Long[],
|
|
313
314
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
314
315
|
const signedTransaction = await this.signTransaction(
|
|
315
|
-
|
|
316
|
+
subaccount,
|
|
316
317
|
messages,
|
|
317
318
|
account,
|
|
318
319
|
zeroFee,
|
|
319
320
|
gasPrice,
|
|
320
321
|
memo,
|
|
321
322
|
gasAdjustment,
|
|
322
|
-
authenticators,
|
|
323
323
|
);
|
|
324
324
|
return this.sendSignedTransaction(signedTransaction, broadcastMode);
|
|
325
325
|
}
|
|
@@ -440,7 +440,7 @@ export class Post {
|
|
|
440
440
|
);
|
|
441
441
|
const account: Promise<Account> = this.account(subaccount.address, orderFlags);
|
|
442
442
|
return this.send(
|
|
443
|
-
subaccount
|
|
443
|
+
subaccount,
|
|
444
444
|
() => Promise.resolve([msg]),
|
|
445
445
|
true,
|
|
446
446
|
undefined,
|
|
@@ -541,7 +541,7 @@ export class Post {
|
|
|
541
541
|
goodTilBlockTime ?? 0,
|
|
542
542
|
);
|
|
543
543
|
return this.send(
|
|
544
|
-
subaccount
|
|
544
|
+
subaccount,
|
|
545
545
|
() => Promise.resolve([msg]),
|
|
546
546
|
true,
|
|
547
547
|
undefined,
|
|
@@ -602,7 +602,7 @@ export class Post {
|
|
|
602
602
|
goodTilBlock,
|
|
603
603
|
);
|
|
604
604
|
return this.send(
|
|
605
|
-
subaccount
|
|
605
|
+
subaccount,
|
|
606
606
|
() => Promise.resolve([msg]),
|
|
607
607
|
true,
|
|
608
608
|
undefined,
|
|
@@ -645,7 +645,7 @@ export class Post {
|
|
|
645
645
|
amount,
|
|
646
646
|
);
|
|
647
647
|
return this.send(
|
|
648
|
-
subaccount
|
|
648
|
+
subaccount,
|
|
649
649
|
() => Promise.resolve([msg]),
|
|
650
650
|
false,
|
|
651
651
|
undefined,
|
|
@@ -688,7 +688,7 @@ export class Post {
|
|
|
688
688
|
quantums,
|
|
689
689
|
);
|
|
690
690
|
return this.send(
|
|
691
|
-
subaccount
|
|
691
|
+
subaccount,
|
|
692
692
|
() => Promise.resolve([msg]),
|
|
693
693
|
false,
|
|
694
694
|
undefined,
|
|
@@ -729,7 +729,7 @@ export class Post {
|
|
|
729
729
|
recipient,
|
|
730
730
|
);
|
|
731
731
|
return this.send(
|
|
732
|
-
subaccount
|
|
732
|
+
subaccount,
|
|
733
733
|
() => Promise.resolve([msg]),
|
|
734
734
|
false,
|
|
735
735
|
undefined,
|
|
@@ -767,7 +767,7 @@ export class Post {
|
|
|
767
767
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
768
768
|
const msg = await this.sendTokenMsg(subaccount.address, recipient, coinDenom, quantums);
|
|
769
769
|
return this.send(
|
|
770
|
-
subaccount
|
|
770
|
+
subaccount,
|
|
771
771
|
() => Promise.resolve([msg]),
|
|
772
772
|
zeroFee,
|
|
773
773
|
coinDenom === this.denoms.CHAINTOKEN_DENOM ? this.defaultDydxGasPrice : this.defaultGasPrice,
|
|
@@ -804,7 +804,7 @@ export class Post {
|
|
|
804
804
|
amount,
|
|
805
805
|
});
|
|
806
806
|
return this.send(
|
|
807
|
-
subaccount
|
|
807
|
+
subaccount,
|
|
808
808
|
() => Promise.resolve([msg]),
|
|
809
809
|
false,
|
|
810
810
|
this.defaultDydxGasPrice,
|
|
@@ -832,7 +832,7 @@ export class Post {
|
|
|
832
832
|
amount,
|
|
833
833
|
});
|
|
834
834
|
return this.send(
|
|
835
|
-
subaccount
|
|
835
|
+
subaccount,
|
|
836
836
|
() => Promise.resolve([msg]),
|
|
837
837
|
false,
|
|
838
838
|
this.defaultDydxGasPrice,
|
|
@@ -856,7 +856,7 @@ export class Post {
|
|
|
856
856
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
857
857
|
const msg = this.composer.composeMsgWithdrawDelegatorReward(delegator, validator);
|
|
858
858
|
return this.send(
|
|
859
|
-
subaccount
|
|
859
|
+
subaccount,
|
|
860
860
|
() => Promise.resolve([msg]),
|
|
861
861
|
false,
|
|
862
862
|
this.defaultGasPrice,
|
|
@@ -881,7 +881,7 @@ export class Post {
|
|
|
881
881
|
quoteQuantums,
|
|
882
882
|
);
|
|
883
883
|
return this.send(
|
|
884
|
-
subaccount
|
|
884
|
+
subaccount,
|
|
885
885
|
() => Promise.resolve([msg]),
|
|
886
886
|
false,
|
|
887
887
|
undefined,
|
|
@@ -909,7 +909,7 @@ export class Post {
|
|
|
909
909
|
minQuoteQuantums,
|
|
910
910
|
);
|
|
911
911
|
return this.send(
|
|
912
|
-
subaccount
|
|
912
|
+
subaccount,
|
|
913
913
|
() => Promise.resolve([msg]),
|
|
914
914
|
false,
|
|
915
915
|
undefined,
|
|
@@ -932,7 +932,7 @@ export class Post {
|
|
|
932
932
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
933
933
|
const msg = this.registerAffiliateMsg(subaccount.address, affiliate);
|
|
934
934
|
return this.send(
|
|
935
|
-
subaccount
|
|
935
|
+
subaccount,
|
|
936
936
|
() => Promise.resolve([msg]),
|
|
937
937
|
false,
|
|
938
938
|
undefined,
|
|
@@ -963,7 +963,7 @@ export class Post {
|
|
|
963
963
|
const msg = this.launchMarketMsg(subaccount.address, ticker, subaccount.subaccountNumber);
|
|
964
964
|
|
|
965
965
|
return this.send(
|
|
966
|
-
subaccount
|
|
966
|
+
subaccount,
|
|
967
967
|
() => Promise.resolve([msg]),
|
|
968
968
|
false,
|
|
969
969
|
undefined,
|
|
@@ -986,7 +986,7 @@ export class Post {
|
|
|
986
986
|
);
|
|
987
987
|
|
|
988
988
|
return this.send(
|
|
989
|
-
subaccount
|
|
989
|
+
subaccount,
|
|
990
990
|
() => Promise.resolve([msg]),
|
|
991
991
|
false,
|
|
992
992
|
undefined,
|
|
@@ -1004,7 +1004,7 @@ export class Post {
|
|
|
1004
1004
|
const msg = this.composer.composeMsgRemoveAuthenticator(subaccount.address, id);
|
|
1005
1005
|
|
|
1006
1006
|
return this.send(
|
|
1007
|
-
subaccount
|
|
1007
|
+
subaccount,
|
|
1008
1008
|
() => Promise.resolve([msg]),
|
|
1009
1009
|
false,
|
|
1010
1010
|
undefined,
|
package/src/clients/native.ts
CHANGED
|
@@ -275,7 +275,7 @@ export async function isWalletConnected(): Promise<string> {
|
|
|
275
275
|
if (wallet === undefined) {
|
|
276
276
|
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
277
277
|
}
|
|
278
|
-
return await encodeJson(
|
|
278
|
+
return await encodeJson({ result: true });
|
|
279
279
|
} catch (e) {
|
|
280
280
|
return wrappedError(e);
|
|
281
281
|
}
|
|
@@ -333,7 +333,7 @@ export async function placeOrder(payload: string): Promise<string> {
|
|
|
333
333
|
const marketInfo = (json.marketInfo as MarketInfo) ?? undefined;
|
|
334
334
|
const currentHeight = (json.currentHeight as number) ?? undefined;
|
|
335
335
|
|
|
336
|
-
const subaccount =
|
|
336
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
337
337
|
const tx = await client.placeOrder(
|
|
338
338
|
subaccount,
|
|
339
339
|
marketId,
|
|
@@ -394,7 +394,7 @@ export async function cancelOrder(payload: string): Promise<string> {
|
|
|
394
394
|
const goodTilBlock = json.goodTilBlock;
|
|
395
395
|
const goodTilBlockTime = json.goodTilBlockTime;
|
|
396
396
|
|
|
397
|
-
const subaccount =
|
|
397
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
398
398
|
const tx = await client.cancelRawOrder(
|
|
399
399
|
subaccount,
|
|
400
400
|
clientId,
|
|
@@ -430,7 +430,7 @@ export async function deposit(payload: string): Promise<string> {
|
|
|
430
430
|
throw new UserError('amount is not set');
|
|
431
431
|
}
|
|
432
432
|
|
|
433
|
-
const subaccount =
|
|
433
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
434
434
|
const tx = await client.depositToSubaccount(subaccount, amount);
|
|
435
435
|
return encodeJson(tx);
|
|
436
436
|
} catch (error) {
|
|
@@ -459,7 +459,7 @@ export async function withdraw(payload: string): Promise<string> {
|
|
|
459
459
|
throw new UserError('amount is not set');
|
|
460
460
|
}
|
|
461
461
|
|
|
462
|
-
const subaccount =
|
|
462
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
463
463
|
const tx = await client.withdrawFromSubaccount(subaccount, amount, json.recipient, json.memo);
|
|
464
464
|
return encodeJson(tx);
|
|
465
465
|
} catch (error) {
|
|
@@ -532,14 +532,14 @@ export async function withdrawToIBC(
|
|
|
532
532
|
},
|
|
533
533
|
};
|
|
534
534
|
|
|
535
|
-
const subaccount =
|
|
535
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
536
536
|
const subaccountMsg = client.withdrawFromSubaccountMessage(subaccount, amount);
|
|
537
537
|
|
|
538
538
|
const msgs = [subaccountMsg, ibcMsg];
|
|
539
539
|
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
540
540
|
|
|
541
541
|
const tx = await client.send(
|
|
542
|
-
|
|
542
|
+
subaccount,
|
|
543
543
|
() => {
|
|
544
544
|
return encodeObjects;
|
|
545
545
|
},
|
|
@@ -575,7 +575,7 @@ export async function transferNativeToken(payload: string): Promise<string> {
|
|
|
575
575
|
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
576
576
|
|
|
577
577
|
const tx = await client.send(
|
|
578
|
-
wallet,
|
|
578
|
+
SubaccountInfo.forLocalWallet(wallet),
|
|
579
579
|
() => {
|
|
580
580
|
return encodeObjects;
|
|
581
581
|
},
|
|
@@ -669,12 +669,12 @@ export async function simulateDeposit(payload: string): Promise<string> {
|
|
|
669
669
|
throw new UserError('amount is not set');
|
|
670
670
|
}
|
|
671
671
|
|
|
672
|
-
const subaccount =
|
|
672
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
673
673
|
const msg: EncodeObject = client.depositToSubaccountMessage(subaccount, amount);
|
|
674
674
|
const msgs: EncodeObject[] = [msg];
|
|
675
675
|
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
676
676
|
|
|
677
|
-
const stdFee = await client.simulate(
|
|
677
|
+
const stdFee = await client.simulate(subaccount, () => {
|
|
678
678
|
return encodeObjects;
|
|
679
679
|
});
|
|
680
680
|
return JSON.stringify(stdFee);
|
|
@@ -703,7 +703,7 @@ export async function simulateWithdraw(payload: string): Promise<string> {
|
|
|
703
703
|
throw new UserError('amount is not set');
|
|
704
704
|
}
|
|
705
705
|
|
|
706
|
-
const subaccount =
|
|
706
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
707
707
|
const msg: EncodeObject = client.withdrawFromSubaccountMessage(
|
|
708
708
|
subaccount,
|
|
709
709
|
amount,
|
|
@@ -712,7 +712,7 @@ export async function simulateWithdraw(payload: string): Promise<string> {
|
|
|
712
712
|
const msgs: EncodeObject[] = [msg];
|
|
713
713
|
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
714
714
|
|
|
715
|
-
const stdFee = await client.simulate(
|
|
715
|
+
const stdFee = await client.simulate(subaccount, () => {
|
|
716
716
|
return encodeObjects;
|
|
717
717
|
});
|
|
718
718
|
return encodeJson(stdFee);
|
|
@@ -746,7 +746,7 @@ export async function simulateTransferNativeToken(payload: string): Promise<stri
|
|
|
746
746
|
const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
|
|
747
747
|
|
|
748
748
|
const stdFee = await client.simulate(
|
|
749
|
-
|
|
749
|
+
SubaccountInfo.forLocalWallet(wallet),
|
|
750
750
|
() => {
|
|
751
751
|
return encodeObjects;
|
|
752
752
|
},
|
|
@@ -800,7 +800,11 @@ export async function signRawPlaceOrder(
|
|
|
800
800
|
);
|
|
801
801
|
resolve([msg]);
|
|
802
802
|
});
|
|
803
|
-
const signed = await client.sign(
|
|
803
|
+
const signed = await client.sign(
|
|
804
|
+
SubaccountInfo.forLocalWallet(wallet, subaccountNumber),
|
|
805
|
+
() => msgs,
|
|
806
|
+
true,
|
|
807
|
+
);
|
|
804
808
|
return Buffer.from(signed).toString('base64');
|
|
805
809
|
} catch (error) {
|
|
806
810
|
return wrappedError(error);
|
|
@@ -832,7 +836,7 @@ export async function signPlaceOrder(
|
|
|
832
836
|
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
833
837
|
}
|
|
834
838
|
|
|
835
|
-
const subaccount =
|
|
839
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
836
840
|
const signed = await client.signPlaceOrder(
|
|
837
841
|
subaccount,
|
|
838
842
|
marketId,
|
|
@@ -871,7 +875,7 @@ export async function signCancelOrder(
|
|
|
871
875
|
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
872
876
|
}
|
|
873
877
|
|
|
874
|
-
const subaccount =
|
|
878
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
875
879
|
const signed = await client.signCancelOrder(
|
|
876
880
|
subaccount,
|
|
877
881
|
clientId,
|
|
@@ -1128,8 +1132,9 @@ export async function withdrawToNobleIBC(payload: string): Promise<String> {
|
|
|
1128
1132
|
|
|
1129
1133
|
const parsedIbcPayload: SquidIBCPayload = JSON.parse(decoded);
|
|
1130
1134
|
|
|
1135
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1131
1136
|
const msg = client.withdrawFromSubaccountMessage(
|
|
1132
|
-
|
|
1137
|
+
subaccount,
|
|
1133
1138
|
parseFloat(amount).toFixed(client.validatorClient.config.denoms.USDC_DECIMALS),
|
|
1134
1139
|
);
|
|
1135
1140
|
const ibcMsg: MsgTransferEncodeObject = {
|
|
@@ -1143,7 +1148,7 @@ export async function withdrawToNobleIBC(payload: string): Promise<String> {
|
|
|
1143
1148
|
},
|
|
1144
1149
|
};
|
|
1145
1150
|
|
|
1146
|
-
const tx = await client.send(
|
|
1151
|
+
const tx = await client.send(subaccount, () => Promise.resolve([msg, ibcMsg]), false);
|
|
1147
1152
|
|
|
1148
1153
|
return encodeJson({
|
|
1149
1154
|
txHash: `0x${Buffer.from(tx?.hash).toString('hex')}`,
|
|
@@ -1187,11 +1192,10 @@ export async function cctpWithdraw(squidPayload: string): Promise<String> {
|
|
|
1187
1192
|
}
|
|
1188
1193
|
}
|
|
1189
1194
|
|
|
1190
|
-
|
|
1191
1195
|
export async function cctpMultiMsgWithdraw(cosmosPayload: string): Promise<string> {
|
|
1192
1196
|
try {
|
|
1193
1197
|
const client = globalThis.nobleClient;
|
|
1194
|
-
const messages: { typeUrl:string
|
|
1198
|
+
const messages: { typeUrl: string; value: { amount: string } }[] = JSON.parse(cosmosPayload);
|
|
1195
1199
|
if (client === undefined || !client.isConnected) {
|
|
1196
1200
|
throw new UserError('client is not connected.');
|
|
1197
1201
|
}
|
|
@@ -1217,7 +1221,7 @@ export async function cctpMultiMsgWithdraw(cosmosPayload: string): Promise<strin
|
|
|
1217
1221
|
|
|
1218
1222
|
return encodeJson(tx);
|
|
1219
1223
|
} catch (error) {
|
|
1220
|
-
return wrappedError(error)
|
|
1224
|
+
return wrappedError(error);
|
|
1221
1225
|
}
|
|
1222
1226
|
}
|
|
1223
1227
|
|
|
@@ -1247,7 +1251,8 @@ export async function getWithdrawalAndTransferGatingStatus(perpetualId: number):
|
|
|
1247
1251
|
throw new UserError('client is not connected. Call connectClient() first');
|
|
1248
1252
|
}
|
|
1249
1253
|
|
|
1250
|
-
const response =
|
|
1254
|
+
const response =
|
|
1255
|
+
await client.validatorClient.get.getWithdrawalAndTransferGatingStatus(perpetualId);
|
|
1251
1256
|
return encodeJson(response);
|
|
1252
1257
|
} catch (error) {
|
|
1253
1258
|
return wrappedError(error);
|
|
@@ -1285,7 +1290,7 @@ export async function subaccountTransfer(payload: string): Promise<string> {
|
|
|
1285
1290
|
throw new UserError('destinationSubaccountNumber is not set');
|
|
1286
1291
|
}
|
|
1287
1292
|
|
|
1288
|
-
const subaccount =
|
|
1293
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1289
1294
|
const tx = await client.transferToSubaccount(
|
|
1290
1295
|
subaccount,
|
|
1291
1296
|
destinationAddress,
|
|
@@ -1335,7 +1340,9 @@ export async function signCompliancePayload(payload: string): Promise<string> {
|
|
|
1335
1340
|
}
|
|
1336
1341
|
}
|
|
1337
1342
|
|
|
1338
|
-
export async function signPushNotificationTokenRegistrationPayload(
|
|
1343
|
+
export async function signPushNotificationTokenRegistrationPayload(
|
|
1344
|
+
payload: string,
|
|
1345
|
+
): Promise<string> {
|
|
1339
1346
|
try {
|
|
1340
1347
|
const json = JSON.parse(payload);
|
|
1341
1348
|
const message = json.message;
|
|
@@ -1387,17 +1394,14 @@ export async function getMegavaultOwnerShares(payload: string): Promise<string>
|
|
|
1387
1394
|
if (address === undefined) {
|
|
1388
1395
|
throw new UserError('address is not set');
|
|
1389
1396
|
}
|
|
1390
|
-
const response =
|
|
1391
|
-
await globalThis.client?.validatorClient.get.getMegavaultOwnerShares(address);
|
|
1397
|
+
const response = await globalThis.client?.validatorClient.get.getMegavaultOwnerShares(address);
|
|
1392
1398
|
return encodeJson(parseToPrimitives(response));
|
|
1393
1399
|
} catch (e) {
|
|
1394
1400
|
return wrappedError(e);
|
|
1395
1401
|
}
|
|
1396
1402
|
}
|
|
1397
1403
|
|
|
1398
|
-
export async function getMegavaultWithdrawalInfo(
|
|
1399
|
-
sharesToWithdraw: bigint
|
|
1400
|
-
): Promise<string> {
|
|
1404
|
+
export async function getMegavaultWithdrawalInfo(sharesToWithdraw: bigint): Promise<string> {
|
|
1401
1405
|
try {
|
|
1402
1406
|
const client = globalThis.client;
|
|
1403
1407
|
if (client === undefined) {
|
|
@@ -1405,7 +1409,7 @@ export async function getMegavaultWithdrawalInfo(
|
|
|
1405
1409
|
}
|
|
1406
1410
|
const response =
|
|
1407
1411
|
await globalThis.client?.validatorClient.get.getMegavaultWithdrawalInfo(sharesToWithdraw);
|
|
1408
|
-
|
|
1412
|
+
return encodeJson(parseToPrimitives(response));
|
|
1409
1413
|
} catch (e) {
|
|
1410
1414
|
return wrappedError(e);
|
|
1411
1415
|
}
|
|
@@ -1413,7 +1417,7 @@ export async function getMegavaultWithdrawalInfo(
|
|
|
1413
1417
|
|
|
1414
1418
|
export async function depositToMegavault(
|
|
1415
1419
|
subaccountNumber: number,
|
|
1416
|
-
amountUsdc: number
|
|
1420
|
+
amountUsdc: number,
|
|
1417
1421
|
): Promise<string> {
|
|
1418
1422
|
try {
|
|
1419
1423
|
const client = globalThis.client;
|
|
@@ -1424,12 +1428,8 @@ export async function depositToMegavault(
|
|
|
1424
1428
|
if (wallet === undefined) {
|
|
1425
1429
|
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
1426
1430
|
}
|
|
1427
|
-
const subaccount =
|
|
1428
|
-
const tx = await client.depositToMegavault(
|
|
1429
|
-
subaccount,
|
|
1430
|
-
amountUsdc,
|
|
1431
|
-
Method.BroadcastTxCommit,
|
|
1432
|
-
);
|
|
1431
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1432
|
+
const tx = await client.depositToMegavault(subaccount, amountUsdc, Method.BroadcastTxCommit);
|
|
1433
1433
|
return encodeJson(parseToPrimitives(tx));
|
|
1434
1434
|
} catch (error) {
|
|
1435
1435
|
return wrappedError(error);
|
|
@@ -1450,7 +1450,7 @@ export async function withdrawFromMegavault(
|
|
|
1450
1450
|
if (wallet === undefined) {
|
|
1451
1451
|
throw new UserError('wallet is not set. Call connectWallet() first');
|
|
1452
1452
|
}
|
|
1453
|
-
const subaccount =
|
|
1453
|
+
const subaccount = SubaccountInfo.forLocalWallet(wallet, subaccountNumber);
|
|
1454
1454
|
const tx = await client.withdrawFromMegavault(
|
|
1455
1455
|
subaccount,
|
|
1456
1456
|
shares,
|
|
@@ -1461,4 +1461,4 @@ export async function withdrawFromMegavault(
|
|
|
1461
1461
|
} catch (error) {
|
|
1462
1462
|
return wrappedError(error);
|
|
1463
1463
|
}
|
|
1464
|
-
}
|
|
1464
|
+
}
|
|
@@ -1,25 +1,57 @@
|
|
|
1
|
+
import Long from 'long';
|
|
2
|
+
|
|
1
3
|
import { MAX_SUBACCOUNT_NUMBER } from './constants';
|
|
2
4
|
import LocalWallet from './modules/local-wallet';
|
|
3
5
|
|
|
4
6
|
export class SubaccountInfo {
|
|
5
|
-
readonly
|
|
6
|
-
// TODO, change address to Wallet object when implementing validator functions
|
|
7
|
+
readonly address: string;
|
|
7
8
|
readonly subaccountNumber: number;
|
|
9
|
+
readonly signingWallet: LocalWallet;
|
|
10
|
+
readonly authenticators?: Long[];
|
|
8
11
|
|
|
9
|
-
constructor(
|
|
12
|
+
private constructor(
|
|
13
|
+
signingWallet: LocalWallet,
|
|
14
|
+
address: string,
|
|
15
|
+
subaccountNumber: number = 0,
|
|
16
|
+
authenticators?: Long[],
|
|
17
|
+
) {
|
|
10
18
|
if (subaccountNumber < 0 || subaccountNumber > MAX_SUBACCOUNT_NUMBER) {
|
|
11
19
|
throw new Error(`Subaccount number must be between 0 and ${MAX_SUBACCOUNT_NUMBER}`);
|
|
12
20
|
}
|
|
13
|
-
|
|
21
|
+
|
|
22
|
+
this.address = address;
|
|
14
23
|
this.subaccountNumber = subaccountNumber;
|
|
24
|
+
this.signingWallet = signingWallet;
|
|
25
|
+
this.authenticators = authenticators;
|
|
15
26
|
}
|
|
16
27
|
|
|
17
|
-
|
|
18
|
-
const address =
|
|
19
|
-
if (address
|
|
20
|
-
|
|
21
|
-
} else {
|
|
22
|
-
throw new Error('Address not set');
|
|
28
|
+
static forLocalWallet(wallet: LocalWallet, subaccountNumber: number = 0): SubaccountInfo {
|
|
29
|
+
const address = wallet.address;
|
|
30
|
+
if (address === undefined) {
|
|
31
|
+
throw new Error('Address not available from wallet');
|
|
23
32
|
}
|
|
33
|
+
return new SubaccountInfo(wallet, address, subaccountNumber);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static forPermissionedWallet(
|
|
37
|
+
signingWallet: LocalWallet,
|
|
38
|
+
accountAddress: string,
|
|
39
|
+
subaccountNumber: number = 0,
|
|
40
|
+
authenticators: Long[],
|
|
41
|
+
): SubaccountInfo {
|
|
42
|
+
return new SubaccountInfo(signingWallet, accountAddress, subaccountNumber, authenticators);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get isPermissionedWallet(): boolean {
|
|
46
|
+
return this.address !== this.signingWallet.address;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
cloneWithSubaccount(subaccountNumber: number): SubaccountInfo {
|
|
50
|
+
return new SubaccountInfo(
|
|
51
|
+
this.signingWallet,
|
|
52
|
+
this.address,
|
|
53
|
+
subaccountNumber,
|
|
54
|
+
this.authenticators,
|
|
55
|
+
);
|
|
24
56
|
}
|
|
25
57
|
}
|