Events
Protocol events are emitted in transaction logs. The SDK provides an EventSubscriber that can listen, deserialize, and emit events to your app.
View EventSubscriber import
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 ↗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 ↗| Name | Type | Default |
|---|---|---|
object | unknown | |
type | string |
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);
}
});Last updated on