Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/rfq-close-reconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@polymarket/client": patch
---

Clear cached RFQ quoter sessions immediately after unexpected websocket disconnects.
10 changes: 9 additions & 1 deletion packages/client/src/websockets/rfq/quoter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export class RfqQuoterWebSocketManager {
}

async connect(): Promise<RfqSession> {
if (this.#session !== undefined) return this.#session;
if (this.#session !== undefined) {
if (!this.#session.closed) return this.#session;
this.#session = undefined;
}
if (this.#connecting !== undefined) return this.#connecting;

const session = new RfqWebSocketSession({
Expand Down Expand Up @@ -185,6 +188,10 @@ class RfqWebSocketSession implements RfqSession, RfqEventController {
this.#url = options.url;
}

get closed(): boolean {
return this.#closing !== undefined;
}

async connect(): Promise<void> {
const auth = createPending<void>();
this.#auth = auth;
Expand Down Expand Up @@ -311,6 +318,7 @@ class RfqWebSocketSession implements RfqSession, RfqEventController {
}

#handleClose(): void {
this.#onClose();
void this.close();
}

Expand Down
58 changes: 58 additions & 0 deletions packages/client/tests/integration/rfq.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,64 @@ describe('RFQ sessions', () => {
});
});

describe('when reopening immediately after an unexpected close', () => {
beforeEach(() => {
server.resetHandlers();
server.use(
rfq.addEventListener('connection', ({ client: socket }) => {
connectionCount += 1;

socket.addEventListener('message', (event) => {
const frame = recordOutboundFrame(event.data, outboundFrames);

if (frame.type === 'auth') {
socket.send(authAckMessage());
socket.send(quoteRequestMessage());
return;
}

if (frame.type === 'RFQ_QUOTE') {
quoteAmounts(frame);
socket.close();
}
});
}),
);
});

it('opens a fresh session instead of returning the closed session', async ({
secureClientWithDepositWallet,
}) => {
const session = await secureClientWithDepositWallet.openRfqSession();

try {
const iterator = session[Symbol.asyncIterator]();
const next = await iterator.next();

if (next.done === true || next.value.type !== 'quote_request') {
throw new Error('Expected RFQ quote request.');
}

const quote = next.value.quote({ price: 0.45 });
const reopened = quote.then(
() => {
throw new Error('Expected RFQ quote rejection.');
},
() => secureClientWithDepositWallet.openRfqSession(),
);

const nextSession = await reopened;

expect(nextSession).not.toBe(session);
expect(connectionCount).toBe(2);

await nextSession.close();
} finally {
await secureClientWithDepositWallet.closeSubscriptions();
}
});
});

describe('when the connection closes before confirmation acknowledgement', () => {
beforeEach(() => {
server.resetHandlers();
Expand Down
Loading