Skip to content

Commit

Permalink
add support for timestamp nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
aforaleka committed Sep 26, 2024
1 parent 907eb40 commit 439427f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,20 +220,23 @@ export class ValidatorConfig {
public denoms: DenomConfig;
public broadcastOptions?: BroadcastOptions;
public defaultClientMemo?: string;
public useTimestampNonce?: boolean;

constructor(
restEndpoint: string,
chainId: string,
denoms: DenomConfig,
broadcastOptions?: BroadcastOptions,
defaultClientMemo?: string,
useTimestampNonce?: boolean,
) {
this.restEndpoint = restEndpoint?.endsWith('/') ? restEndpoint.slice(0, -1) : restEndpoint;
this.chainId = chainId;

this.denoms = denoms;
this.broadcastOptions = broadcastOptions;
this.defaultClientMemo = defaultClientMemo;
this.useTimestampNonce = useTimestampNonce;
}
}

Expand Down
32 changes: 22 additions & 10 deletions v4-client-js/src/clients/modules/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ export class Post {
public readonly defaultGasPrice: GasPrice;
public readonly defaultDydxGasPrice: GasPrice;

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

constructor(get: Get, chainId: string, denoms: DenomConfig, defaultClientMemo?: string) {
constructor(get: Get, chainId: string, denoms: DenomConfig, defaultClientMemo?: string, useTimestampNonce?: boolean) {
this.get = get;
this.chainId = chainId;
this.registry = generateRegistry();
Expand All @@ -74,6 +75,7 @@ export class Post {
: denoms.CHAINTOKEN_DENOM
}`,
);
if (useTimestampNonce === true) this.useTimestampNonce = useTimestampNonce;
}

/**
Expand Down Expand Up @@ -113,14 +115,22 @@ export class Post {
memo?: string,
account?: () => Promise<Account>,
): Promise<StdFee> {
const msgsPromise = messaging();
const accountPromise = account ? await account() : this.account(wallet.address!);
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
const msgs = msgsAndAccount[0];
let msgs: EncodeObject[];
let sequence = Date.now();

if (this.useTimestampNonce) {
msgs = await messaging();
} else {
const msgsPromise = messaging();
const accountPromise = account ? await account() : this.account(wallet.address!);
const msgsAndAccount = await Promise.all([msgsPromise, accountPromise]);
msgs = msgsAndAccount[0];
sequence = msgsAndAccount[1].sequence;
}

return this.simulateTransaction(
wallet.pubKey!,
msgsAndAccount[1].sequence,
sequence,
msgs,
gasPrice,
memo,
Expand Down Expand Up @@ -225,16 +235,17 @@ export class Post {
gasPrice: GasPrice = this.getGasPrice(),
memo?: string,
): Promise<Uint8Array> {
const sequence = this.useTimestampNonce ? Date.now() : account.sequence;
// Simulate transaction if no fee is specified.
const fee: StdFee = zeroFee
? {
amount: [],
gas: '1000000',
}
: await this.simulateTransaction(wallet.pubKey!, account.sequence, messages, gasPrice, memo);
: await this.simulateTransaction(wallet.pubKey!, sequence, messages, gasPrice, memo);

const txOptions: TransactionOptions = {
sequence: account.sequence,
sequence,
accountNumber: account.accountNumber,
chainId: this.chainId,
};
Expand All @@ -246,11 +257,12 @@ export class Post {
* @description Retrieve an account structure for transactions.
* For short term orders, the sequence doesn't matter. Use cached if available.
* For long term and conditional orders, a round trip to validator must be made.
* when timestamp nonce is supported, no need to fetch account sequence number
*/
public async account(address: string, orderFlags?: OrderFlags): Promise<Account> {
if (orderFlags === OrderFlags.SHORT_TERM) {
if (orderFlags === OrderFlags.SHORT_TERM || this.useTimestampNonce) {
if (this.accountNumberCache.has(address)) {
// For SHORT_TERM orders, the sequence doesn't matter
// For SHORT_TERM orders and when timestamp nonce is enabled, the sequence doesn't matter
return this.accountNumberCache.get(address)!;
}
}
Expand Down
1 change: 1 addition & 0 deletions v4-client-js/src/clients/validator-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class ValidatorClient {
this.config.chainId,
this.config.denoms,
this.config.defaultClientMemo,
this.config.useTimestampNonce,
);
}
}

0 comments on commit 439427f

Please sign in to comment.