Skip to content

fix: add timestamp nonce adjustment config #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ export const TYPE_URL_MSG_WITHDRAW_DELEGATOR_REWARD =
'/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward';

// x/accountplus
export const TYPE_URL_MSG_ADD_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgAddAuthenticator'
export const TYPE_URL_MSG_REMOVE_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgRemoveAuthenticator'
export const TYPE_URL_MSG_ADD_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgAddAuthenticator';
export const TYPE_URL_MSG_REMOVE_AUTHENTICATOR = '/dydxprotocol.accountplus.MsgRemoveAuthenticator';

// ------------ Chain Constants ------------
// The following are same across different networks / deployments.
Expand Down Expand Up @@ -257,6 +257,7 @@ export class ValidatorConfig {
public broadcastOptions?: BroadcastOptions;
public defaultClientMemo?: string;
public useTimestampNonce?: boolean;
public timestampNonceOffsetMs?: number;

constructor(
restEndpoint: string,
Expand All @@ -265,6 +266,7 @@ export class ValidatorConfig {
broadcastOptions?: BroadcastOptions,
defaultClientMemo?: string,
useTimestampNonce?: boolean,
timestampNonceOffsetMs?: number,
) {
this.restEndpoint = restEndpoint?.endsWith('/') ? restEndpoint.slice(0, -1) : restEndpoint;
this.chainId = chainId;
Expand All @@ -273,6 +275,7 @@ export class ValidatorConfig {
this.broadcastOptions = broadcastOptions;
this.defaultClientMemo = defaultClientMemo;
this.useTimestampNonce = useTimestampNonce;
this.timestampNonceOffsetMs = timestampNonceOffsetMs;
}
}

Expand Down
17 changes: 14 additions & 3 deletions v4-client-js/src/clients/modules/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class Post {
public readonly defaultDydxGasPrice: GasPrice;

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

constructor(
Expand All @@ -64,6 +65,7 @@ export class Post {
denoms: DenomConfig,
defaultClientMemo?: string,
useTimestampNonce?: boolean,
timestampNonceOffsetMs?: number,
) {
this.get = get;
this.chainId = chainId;
Expand All @@ -81,7 +83,10 @@ export class Post {
: denoms.CHAINTOKEN_DENOM
}`,
);
if (useTimestampNonce === true) this.useTimestampNonce = useTimestampNonce;
if (useTimestampNonce === true) {
this.useTimestampNonce = useTimestampNonce;
this.timestampNonceOffsetMs = timestampNonceOffsetMs ?? this.timestampNonceOffsetMs;
}
}

/**
Expand Down Expand Up @@ -243,7 +248,9 @@ export class Post {
authenticators?: Long[],
): Promise<Uint8Array> {
// protocol expects timestamp nonce in UTC milliseconds, which is the unit returned by Date.now()
const sequence = this.useTimestampNonce ? Date.now() : account.sequence;
const sequence = this.useTimestampNonce
? Date.now() + this.timestampNonceOffsetMs
: account.sequence;
// Simulate transaction if no fee is specified.
const fee: StdFee = zeroFee
? {
Expand Down Expand Up @@ -956,7 +963,11 @@ export class Post {
authenticatorType: AuthenticatorType,
data: Uint8Array,
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
const msg = this.composer.composeMsgAddAuthenticator(subaccount.address, authenticatorType, data);
const msg = this.composer.composeMsgAddAuthenticator(
subaccount.address,
authenticatorType,
data,
);

return this.send(
subaccount.wallet,
Expand Down
2 changes: 1 addition & 1 deletion v4-client-js/src/clients/validator-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export class ValidatorClient {
this._post.setSelectedGasDenom(gasDenom);
}


/**
* @description populate account number cache in the Post module for performance.
*/
Expand Down Expand Up @@ -98,6 +97,7 @@ export class ValidatorClient {
this.config.denoms,
this.config.defaultClientMemo,
this.config.useTimestampNonce,
this.config.timestampNonceOffsetMs,
);
}
}
14 changes: 14 additions & 0 deletions v4-client-js/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ export function getGovAddNewMarketTitle(ticker: string): string {
export function getGovAddNewMarketSummary(ticker: string, delayBlocks: number): string {
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.`;
}

export function calculateClockOffsetFromFetchDateHeader(
clientRequestStartTime: number,
serverReportedTime: number,
clientRequestEndTime: number,
): number {
// use midpoint, so assume that time for request reach server === time for response to reach us
const estimatedLocalTimeAtServerArrival = (clientRequestStartTime + clientRequestEndTime) / 2;

// we need an offset such that estimatedLocalTimeAtServerArrival + offset = serverReportedTime
const offset = serverReportedTime - estimatedLocalTimeAtServerArrival;

return offset;
}