mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-09 08:18:07 +00:00
fix nonce
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user