@dydxprotocol/v4-client-js 1.6.0 → 1.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dydxprotocol/v4-client-js",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "General client library for the new dYdX system (v4 decentralized)",
5
5
  "main": "build/src/index.js",
6
6
  "scripts": {
@@ -220,6 +220,7 @@ export class ValidatorConfig {
220
220
  public denoms: DenomConfig;
221
221
  public broadcastOptions?: BroadcastOptions;
222
222
  public defaultClientMemo?: string;
223
+ public useTimestampNonce?: boolean;
223
224
 
224
225
  constructor(
225
226
  restEndpoint: string,
@@ -227,6 +228,7 @@ export class ValidatorConfig {
227
228
  denoms: DenomConfig,
228
229
  broadcastOptions?: BroadcastOptions,
229
230
  defaultClientMemo?: string,
231
+ useTimestampNonce?: boolean,
230
232
  ) {
231
233
  this.restEndpoint = restEndpoint?.endsWith('/') ? restEndpoint.slice(0, -1) : restEndpoint;
232
234
  this.chainId = chainId;
@@ -234,6 +236,7 @@ export class ValidatorConfig {
234
236
  this.denoms = denoms;
235
237
  this.broadcastOptions = broadcastOptions;
236
238
  this.defaultClientMemo = defaultClientMemo;
239
+ this.useTimestampNonce = useTimestampNonce;
237
240
  }
238
241
  }
239
242
 
@@ -48,6 +48,7 @@ import {
48
48
  TYPE_URL_MSG_WITHDRAW_DELEGATOR_REWARD,
49
49
  TYPE_URL_BATCH_CANCEL,
50
50
  TYPE_URL_MSG_DEPOSIT_TO_MEGAVAULT,
51
+ TYPE_URL_MSG_WITHDRAW_FROM_MEGAVAULT,
51
52
  } from '../constants';
52
53
  import { DenomConfig } from '../types';
53
54
  import {
@@ -490,7 +491,7 @@ export class Composer {
490
491
  };
491
492
 
492
493
  return {
493
- typeUrl: TYPE_URL_MSG_DEPOSIT_TO_MEGAVAULT,
494
+ typeUrl: TYPE_URL_MSG_WITHDRAW_FROM_MEGAVAULT,
494
495
  value: msg,
495
496
  };
496
497
  }
@@ -55,9 +55,10 @@ export class Post {
55
55
  public readonly defaultGasPrice: GasPrice;
56
56
  public readonly defaultDydxGasPrice: GasPrice;
57
57
 
58
+ public useTimestampNonce: boolean = false;
58
59
  private accountNumberCache: Map<string, Account> = new Map();
59
60
 
60
- constructor(get: Get, chainId: string, denoms: DenomConfig, defaultClientMemo?: string) {
61
+ constructor(get: Get, chainId: string, denoms: DenomConfig, defaultClientMemo?: string, useTimestampNonce?: boolean) {
61
62
  this.get = get;
62
63
  this.chainId = chainId;
63
64
  this.registry = generateRegistry();
@@ -74,6 +75,7 @@ export class Post {
74
75
  : denoms.CHAINTOKEN_DENOM
75
76
  }`,
76
77
  );
78
+ if (useTimestampNonce === true) this.useTimestampNonce = useTimestampNonce;
77
79
  }
78
80
 
79
81
  /**
@@ -113,14 +115,23 @@ export class Post {
113
115
  memo?: string,
114
116
  account?: () => Promise<Account>,
115
117
  ): Promise<StdFee> {
116
- const msgsPromise = messaging();
117
- const accountPromise = account ? await account() : this.account(wallet.address!);
118
- const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
119
- const msgs = msgsAndAccount[0];
118
+ let msgs: EncodeObject[];
119
+ // protocol expects timestamp nonce in UTC milliseconds, which is the unit returned by Date.now()
120
+ let sequence = Date.now();
121
+
122
+ if (this.useTimestampNonce) {
123
+ msgs = await messaging();
124
+ } else {
125
+ const msgsPromise = messaging();
126
+ const accountPromise = account ? await account() : this.account(wallet.address!);
127
+ const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
128
+ msgs = msgsAndAccount[0];
129
+ sequence = msgsAndAccount[1].sequence;
130
+ }
120
131
 
121
132
  return this.simulateTransaction(
122
133
  wallet.pubKey!,
123
- msgsAndAccount[1].sequence,
134
+ sequence,
124
135
  msgs,
125
136
  gasPrice,
126
137
  memo,
@@ -225,16 +236,18 @@ export class Post {
225
236
  gasPrice: GasPrice = this.getGasPrice(),
226
237
  memo?: string,
227
238
  ): Promise<Uint8Array> {
239
+ // protocol expects timestamp nonce in UTC milliseconds, which is the unit returned by Date.now()
240
+ const sequence = this.useTimestampNonce ? Date.now() : account.sequence;
228
241
  // Simulate transaction if no fee is specified.
229
242
  const fee: StdFee = zeroFee
230
243
  ? {
231
244
  amount: [],
232
245
  gas: '1000000',
233
246
  }
234
- : await this.simulateTransaction(wallet.pubKey!, account.sequence, messages, gasPrice, memo);
247
+ : await this.simulateTransaction(wallet.pubKey!, sequence, messages, gasPrice, memo);
235
248
 
236
249
  const txOptions: TransactionOptions = {
237
- sequence: account.sequence,
250
+ sequence,
238
251
  accountNumber: account.accountNumber,
239
252
  chainId: this.chainId,
240
253
  };
@@ -246,11 +259,12 @@ export class Post {
246
259
  * @description Retrieve an account structure for transactions.
247
260
  * For short term orders, the sequence doesn't matter. Use cached if available.
248
261
  * For long term and conditional orders, a round trip to validator must be made.
262
+ * when timestamp nonce is supported, no need to fetch account sequence number
249
263
  */
250
264
  public async account(address: string, orderFlags?: OrderFlags): Promise<Account> {
251
- if (orderFlags === OrderFlags.SHORT_TERM) {
265
+ if (orderFlags === OrderFlags.SHORT_TERM || this.useTimestampNonce) {
252
266
  if (this.accountNumberCache.has(address)) {
253
- // For SHORT_TERM orders, the sequence doesn't matter
267
+ // If order is SHORT_TERM or if timestamp nonce is enabled, the sequence doesn't matter
254
268
  return this.accountNumberCache.get(address)!;
255
269
  }
256
270
  }
@@ -97,6 +97,7 @@ export class ValidatorClient {
97
97
  this.config.chainId,
98
98
  this.config.denoms,
99
99
  this.config.defaultClientMemo,
100
+ this.config.useTimestampNonce,
100
101
  );
101
102
  }
102
103
  }