Skip to content

Commit

Permalink
add protected maker to dlob
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Nov 30, 2024
1 parent 34ca52b commit 86f9979
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
24 changes: 17 additions & 7 deletions sdk/src/dlob/DLOB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
L3OrderBook,
mergeL2LevelGenerators,
} from './orderBookLevels';
import { isUserProtectedMaker } from '../math/userStatus';

export type MarketNodeLists = {
restingLimit: {
Expand Down Expand Up @@ -158,9 +159,10 @@ export class DLOB {
const userAccount = user.getUserAccount();
const userAccountPubkey = user.getUserAccountPublicKey();
const userAccountPubkeyString = userAccountPubkey.toString();
const protectedMaker = isUserProtectedMaker(userAccount);

for (const order of userAccount.orders) {
this.insertOrder(order, userAccountPubkeyString, slot);
this.insertOrder(order, userAccountPubkeyString, slot, protectedMaker);
}
}

Expand All @@ -174,15 +176,15 @@ export class DLOB {
}

for (const { user, order } of dlobOrders) {
this.insertOrder(order, user.toString(), slot);
this.insertOrder(order, user.toString(), slot, false);
}

this.initialized = true;
return true;
}

public handleOrderRecord(record: OrderRecord, slot: number): void {
this.insertOrder(record.order, record.user.toString(), slot);
this.insertOrder(record.order, record.user.toString(), slot, false);
}

public handleOrderActionRecord(
Expand All @@ -197,14 +199,14 @@ export class DLOB {
if (record.taker !== null) {
const takerOrder = this.getOrder(record.takerOrderId, record.taker);
if (takerOrder) {
this.trigger(takerOrder, record.taker, slot);
this.trigger(takerOrder, record.taker, slot, false);
}
}

if (record.maker !== null) {
const makerOrder = this.getOrder(record.makerOrderId, record.maker);
if (makerOrder) {
this.trigger(makerOrder, record.maker, slot);
this.trigger(makerOrder, record.maker, slot, false);
}
}
} else if (isVariant(record.action, 'fill')) {
Expand Down Expand Up @@ -252,6 +254,7 @@ export class DLOB {
order: Order,
userAccount: string,
slot: number,
isUserProtectedMaker: boolean,
onInsert?: OrderBookCallback
): void {
if (isVariant(order.status, 'init')) {
Expand All @@ -273,7 +276,12 @@ export class DLOB {
.get(marketType)
.add(getOrderSignature(order.orderId, userAccount));
}
this.getListForOrder(order, slot)?.insert(order, marketType, userAccount);
this.getListForOrder(order, slot)?.insert(
order,
marketType,
userAccount,
isUserProtectedMaker
);

if (onInsert) {
onInsert();
Expand Down Expand Up @@ -339,6 +347,7 @@ export class DLOB {
order: Order,
userAccount: PublicKey,
slot: number,
isUserProtectedMaker: boolean,
onTrigger?: OrderBookCallback
): void {
if (isVariant(order.status, 'init')) {
Expand All @@ -360,7 +369,8 @@ export class DLOB {
this.getListForOrder(order, slot)?.insert(
order,
marketType,
userAccount.toString()
userAccount.toString(),
isUserProtectedMaker
);
if (onTrigger) {
onTrigger();
Expand Down
31 changes: 23 additions & 8 deletions sdk/src/dlob/DLOBNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface DLOBNode {
isBaseFilled(): boolean;
haveFilled: boolean;
userAccount: string | undefined;
isUserProtectedMaker: boolean;
}

export abstract class OrderNode implements DLOBNode {
Expand All @@ -27,12 +28,17 @@ export abstract class OrderNode implements DLOBNode {
sortValue: BN;
haveFilled = false;
haveTrigger = false;

constructor(order: Order, userAccount: string) {
isUserProtectedMaker: boolean;
constructor(
order: Order,
userAccount: string,
isUserProtectedMaker: boolean
) {
// Copy the order over to the node
this.order = { ...order };
this.userAccount = userAccount;
this.sortValue = this.getSortValue(order);
this.isUserProtectedMaker = isUserProtectedMaker;
}

abstract getSortValue(order: Order): BN;
Expand Down Expand Up @@ -140,19 +146,28 @@ export type DLOBNodeType =
export function createNode<T extends DLOBNodeType>(
nodeType: T,
order: Order,
userAccount: string
userAccount: string,
isUserProtectedMaker: boolean
): DLOBNodeMap[T] {
switch (nodeType) {
case 'floatingLimit':
return new FloatingLimitOrderNode(order, userAccount);
return new FloatingLimitOrderNode(
order,
userAccount,
isUserProtectedMaker
);
case 'restingLimit':
return new RestingLimitOrderNode(order, userAccount);
return new RestingLimitOrderNode(
order,
userAccount,
isUserProtectedMaker
);
case 'takingLimit':
return new TakingLimitOrderNode(order, userAccount);
return new TakingLimitOrderNode(order, userAccount, isUserProtectedMaker);
case 'market':
return new MarketOrderNode(order, userAccount);
return new MarketOrderNode(order, userAccount, isUserProtectedMaker);
case 'trigger':
return new TriggerOrderNode(order, userAccount);
return new TriggerOrderNode(order, userAccount, isUserProtectedMaker);
default:
throw Error(`Unknown DLOBNode type ${nodeType}`);
}
Expand Down
11 changes: 9 additions & 2 deletions sdk/src/dlob/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,19 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
public insert(
order: Order,
marketType: MarketTypeStr,
userAccount: string
userAccount: string,
isUserProtectedMaker: boolean
): void {
if (isVariant(order.status, 'init')) {
return;
}

const newNode = createNode(this.nodeType, order, userAccount);
const newNode = createNode(
this.nodeType,
order,
userAccount,
isUserProtectedMaker
);

const orderSignature = getOrderSignature(order.orderId, userAccount);
if (this.nodeMap.has(orderSignature)) {
Expand Down Expand Up @@ -178,6 +184,7 @@ export function* getVammNodeGenerator(
isVammNode: () => true,
order: undefined,
userAccount: undefined,
isUserProtectedMaker: false,
isBaseFilled: () => false,
haveFilled: false,
};
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/math/userStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { UserAccount, UserStatus } from '..';

export function isUserProtectedMaker(userAccount: UserAccount): boolean {
return (userAccount.status & UserStatus.PROTECTED_MAKER) > 0;
}
4 changes: 3 additions & 1 deletion sdk/src/orderSubscriber/OrderSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { EventEmitter } from 'events';
import { BN } from '../index';
import { decodeUser } from '../decode/user';
import { grpcSubscription } from './grpcSubscription';
import { isUserProtectedMaker } from '../math/userStatus';

export class OrderSubscriber {
driftClient: DriftClient;
Expand Down Expand Up @@ -229,8 +230,9 @@ export class OrderSubscriber {
public async getDLOB(slot: number): Promise<DLOB> {
const dlob = this.createDLOB();
for (const [key, { userAccount }] of this.usersAccounts.entries()) {
const protectedMaker = isUserProtectedMaker(userAccount);
for (const order of userAccount.orders) {
dlob.insertOrder(order, key, slot);
dlob.insertOrder(order, key, slot, protectedMaker);
}
}
return dlob;
Expand Down
1 change: 1 addition & 0 deletions sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export enum UserStatus {
BANKRUPT = 2,
REDUCE_ONLY = 4,
ADVANCED_LP = 8,
PROTECTED_MAKER = 16,
}

export class MarginMode {
Expand Down

0 comments on commit 86f9979

Please sign in to comment.