Events
Protocol events are emitted in transaction logs. The SDK provides an EventSubscriber that can listen, deserialize, and emit events to your app.
import { EventSubscriber } from "@drift-labs/sdk";
const options = {
// eventTypes: ["DepositRecord", "OrderRecord", ...],
commitment: "confirmed",
logProviderConfig: { type: "websocket" },
};
const eventSubscriber = new EventSubscriber(connection, driftClient.program, options);
await eventSubscriber.subscribe();
eventSubscriber.eventEmitter.on("newEvent", (event) => {
console.log(event);
});Class EventSubscriberReference ↗
Class EventSubscriberReference ↗| Property | Type | Required |
|---|---|---|
connection | any | Yes |
program | any | Yes |
options | any | Yes |
address | any | Yes |
eventListMap | any | Yes |
txEventCache | any | Yes |
awaitTxPromises | any | Yes |
awaitTxResolver | any | Yes |
logProvider | any | Yes |
_currentProviderType | any | Yes |
eventEmitter | StrictEventEmitter<EventEmitter, EventSubscriberEvents> | Yes |
lastSeenSlot | any | Yes |
lastSeenBlockTime | any | Yes |
lastSeenTxSig | string | Yes |
currentProviderType | LogProviderType | Yes |
initializeLogProvider | any | Yes |
populateInitialEventListMap | any | Yes |
updateFallbackProviderType | anyImplements fallback logic for reconnecting to LogProvider. Currently terminates at polling,
could be improved to try the original type again after some cooldown. | Yes |
subscribe | () => Promise<boolean> | Yes |
handleTxLogs | any | Yes |
fetchPreviousTx | (fetchMax?: boolean | undefined) => Promise<void> | Yes |
unsubscribe | () => Promise<boolean> | Yes |
parseEventsFromLogs | any | Yes |
awaitTx | (txSig: string) => Promise<void> | Yes |
getEventList | <Type extends keyof EventMap>(eventType: Type) => EventList<Type> | Yes |
getEventsArray | <Type extends EventType>(eventType: Type) => EventMap[Type][]This requires the EventList be cast to an array, which requires reallocation of memory.
Would bias to using getEventList over getEvents | Yes |
getEventsByTx | (txSig: string) => WrappedEvents | undefined | Yes |
Filtering Events
You can filter events by market index, event type, or action using the isVariant helper function. This is useful for focusing on specific markets or event types in your bot.
import { isVariant } from "@drift-labs/sdk";
// Example: Filter for perp fills on a specific market
const marketIndex = 0;
const isPerpFill = (event) => {
if (event.eventType !== "OrderActionRecord") return false;
if (event.marketIndex !== marketIndex) return false;
if (!isVariant(event.marketType, "perp")) return false;
if (!isVariant(event.action, "fill")) return false;
return true;
};
eventSubscriber.eventEmitter.on("newEvent", (event) => {
if (isPerpFill(event)) console.log("Perp fill on market", marketIndex, event);
});Function isVariantReference ↗
Function isVariantReference ↗| Parameter | Type | Required |
|---|---|---|
object | unknown | Yes |
type | string | Yes |
| Returns |
|---|
boolean |
Common filter patterns for bots:
// Filter by market index only
eventSubscriber.eventEmitter.on("newEvent", (event) => {
if (event.marketIndex === 0) {
console.log("Event on market 0:", event);
}
});
// Filter by event type
eventSubscriber.eventEmitter.on("newEvent", (event) => {
if (event.eventType === "DepositRecord") {
console.log("Deposit event:", event);
}
});
// Filter for liquidations
eventSubscriber.eventEmitter.on("newEvent", (event) => {
if (event.eventType === "LiquidationRecord") {
console.log("Liquidation:", event);
}
});Example Event filter patternsReference ↗
Example Event filter patternsReference ↗TypeScript docs unavailable for
Event filter patterns.Last updated on