Skip to content

Commit 630ea36

Browse files
authored
fix: add timestamp nonce adjustment config (#342)
1 parent a2c7adc commit 630ea36

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

v4-client-js/src/clients/constants.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ export const TYPE_URL_MSG_WITHDRAW_DELEGATOR_REWARD =
117117
'/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward';
118118

119119
// x/accountplus
120-
export const TYPE_URL_MSG_ADD_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgAddAuthenticator'
121-
export const TYPE_URL_MSG_REMOVE_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgRemoveAuthenticator'
120+
export const TYPE_URL_MSG_ADD_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgAddAuthenticator';
121+
export const TYPE_URL_MSG_REMOVE_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgRemoveAuthenticator';
122122

123123
// ------------ Chain Constants ------------
124124
// The following are same across different networks / deployments.
@@ -257,6 +257,7 @@ export class ValidatorConfig {
257257
public broadcastOptions?: BroadcastOptions;
258258
public defaultClientMemo?: string;
259259
public useTimestampNonce?: boolean;
260+
public timestampNonceOffsetMs?: number;
260261

261262
constructor(
262263
restEndpoint: string,
@@ -265,6 +266,7 @@ export class ValidatorConfig {
265266
broadcastOptions?: BroadcastOptions,
266267
defaultClientMemo?: string,
267268
useTimestampNonce?: boolean,
269+
timestampNonceOffsetMs?: number,
268270
) {
269271
this.restEndpoint = restEndpoint?.endsWith('/') ? restEndpoint.slice(0, -1) : restEndpoint;
270272
this.chainId = chainId;
@@ -273,6 +275,7 @@ export class ValidatorConfig {
273275
this.broadcastOptions = broadcastOptions;
274276
this.defaultClientMemo = defaultClientMemo;
275277
this.useTimestampNonce = useTimestampNonce;
278+
this.timestampNonceOffsetMs = timestampNonceOffsetMs;
276279
}
277280
}
278281

v4-client-js/src/clients/modules/post.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class Post {
5656
public readonly defaultDydxGasPrice: GasPrice;
5757

5858
public useTimestampNonce: boolean = false;
59+
public timestampNonceOffsetMs: number = 0;
5960
private accountNumberCache: Map<string, Account> = new Map();
6061

6162
constructor(
@@ -64,6 +65,7 @@ export class Post {
6465
denoms: DenomConfig,
6566
defaultClientMemo?: string,
6667
useTimestampNonce?: boolean,
68+
timestampNonceOffsetMs?: number,
6769
) {
6870
this.get = get;
6971
this.chainId = chainId;
@@ -81,7 +83,10 @@ export class Post {
8183
: denoms.CHAINTOKEN_DENOM
8284
}`,
8385
);
84-
if (useTimestampNonce === true) this.useTimestampNonce = useTimestampNonce;
86+
if (useTimestampNonce === true) {
87+
this.useTimestampNonce = useTimestampNonce;
88+
this.timestampNonceOffsetMs = timestampNonceOffsetMs ?? this.timestampNonceOffsetMs;
89+
}
8590
}
8691

8792
/**
@@ -243,7 +248,9 @@ export class Post {
243248
authenticators?: Long[],
244249
): Promise<Uint8Array> {
245250
// protocol expects timestamp nonce in UTC milliseconds, which is the unit returned by Date.now()
246-
const sequence = this.useTimestampNonce ? Date.now() : account.sequence;
251+
const sequence = this.useTimestampNonce
252+
? Date.now() + this.timestampNonceOffsetMs
253+
: account.sequence;
247254
// Simulate transaction if no fee is specified.
248255
const fee: StdFee = zeroFee
249256
? {
@@ -956,7 +963,11 @@ export class Post {
956963
authenticatorType: AuthenticatorType,
957964
data: Uint8Array,
958965
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
959-
const msg = this.composer.composeMsgAddAuthenticator(subaccount.address, authenticatorType, data);
966+
const msg = this.composer.composeMsgAddAuthenticator(
967+
subaccount.address,
968+
authenticatorType,
969+
data,
970+
);
960971

961972
return this.send(
962973
subaccount.wallet,

v4-client-js/src/clients/validator-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export class ValidatorClient {
6969
this._post.setSelectedGasDenom(gasDenom);
7070
}
7171

72-
7372
/**
7473
* @description populate account number cache in the Post module for performance.
7574
*/
@@ -98,6 +97,7 @@ export class ValidatorClient {
9897
this.config.denoms,
9998
this.config.defaultClientMemo,
10099
this.config.useTimestampNonce,
100+
this.config.timestampNonceOffsetMs,
101101
);
102102
}
103103
}

v4-client-js/src/lib/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,17 @@ export function getGovAddNewMarketTitle(ticker: string): string {
6060
export function getGovAddNewMarketSummary(ticker: string, delayBlocks: number): string {
6161
return `Add the x/prices, x/perpetuals and x/clob parameters needed for a ${ticker} perpetual market. Create the market in INITIALIZING status and transition it to ACTIVE status after ${delayBlocks} blocks.`;
6262
}
63+
64+
export function calculateClockOffsetFromFetchDateHeader(
65+
clientRequestStartTime: number,
66+
serverReportedTime: number,
67+
clientRequestEndTime: number,
68+
): number {
69+
// use midpoint, so assume that time for request reach server === time for response to reach us
70+
const estimatedLocalTimeAtServerArrival = (clientRequestStartTime + clientRequestEndTime) / 2;
71+
72+
// we need an offset such that estimatedLocalTimeAtServerArrival + offset = serverReportedTime
73+
const offset = serverReportedTime - estimatedLocalTimeAtServerArrival;
74+
75+
return offset;
76+
}

0 commit comments

Comments
 (0)