fix nonce

This commit is contained in:
discountry
2025-12-07 22:36:37 +08:00
parent 7612e25dd1
commit 6a6b61c491
2 changed files with 18 additions and 1 deletions
+4 -1
View File
@@ -199,6 +199,7 @@ export class LighterGateway {
private readonly l1Address: string | null;
private loggedCreateOrderPayload = false;
private readonly logTxInfo: boolean;
private readonly primaryApiKeyIndex: number;
private lastWsPositionUpdateAt = 0;
private readonly lastWsPositionByMarket = new Map<number, number>();
private httpPositionsEmptyLogged = false;
@@ -296,6 +297,7 @@ export class LighterGateway {
if (this.forcedSpotPreset && this.apiKeyIndices.length > 1) {
this.apiKeyIndices.splice(1); // stick to the first key to avoid nonce drift
}
this.primaryApiKeyIndex = this.apiKeyIndices[0]!;
this.nonceManager = new HttpNonceManager({
accountIndex: options.accountIndex,
apiKeyIndices: this.apiKeyIndices,
@@ -361,7 +363,8 @@ export class LighterGateway {
const maxAttempts = 3;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const { apiKeyIndex, nonce } = this.nonceManager.next();
const { apiKeyIndex, nonce } =
this.primaryApiKeyIndex != null ? this.nonceManager.nextFor(this.primaryApiKeyIndex) : this.nonceManager.next();
try {
const signed = await this.signer.signCreateOrder({
...signParams,
+14
View File
@@ -67,6 +67,20 @@ export class HttpNonceManager implements LighterNonceManager {
this.slots.set(apiKeyIndex, { apiKeyIndex, next: nonce, lastIssued: null });
}
nextFor(apiKeyIndex: number): { apiKeyIndex: number; nonce: bigint } {
if (!this.slots.size) {
throw new Error("Nonce manager not initialized");
}
const slot = this.slots.get(apiKeyIndex);
if (!slot) {
throw new Error(`Nonce slot for API key index ${apiKeyIndex} is not initialized`);
}
const nonce = slot.next;
slot.lastIssued = nonce;
slot.next = nonce + 1n;
return { apiKeyIndex, nonce };
}
private async refreshAll(force: boolean): Promise<void> {
await Promise.all(
this.apiKeyIndices.map(async (index) => {