Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sdk): pass string to getOrderSignature #793

Merged
merged 2 commits into from
Dec 28, 2023
Merged
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
Binary file modified sdk/bun.lockb
Binary file not shown.
38 changes: 23 additions & 15 deletions sdk/src/dlob/DLOB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ export class DLOB {
for (const user of userMap.values()) {
const userAccount = user.getUserAccount();
const userAccountPubkey = user.getUserAccountPublicKey();
const userAccountPubkeyString = userAccountPubkey.toString();

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

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

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

this.initialized = true;
return true;
}

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

public handleOrderActionRecord(
Expand Down Expand Up @@ -249,7 +250,7 @@ export class DLOB {

public insertOrder(
order: Order,
userAccount: PublicKey,
userAccount: string,
slot: number,
onInsert?: OrderBookCallback
): void {
Expand Down Expand Up @@ -327,7 +328,7 @@ export class DLOB {
};
newOrder.baseAssetAmountFilled = cumulativeBaseAssetAmountFilled;

this.getListForOrder(order, slot)?.update(newOrder, userAccount);
this.getListForOrder(order, slot)?.update(newOrder, userAccount.toString());

if (onUpdate) {
onUpdate();
Expand All @@ -354,9 +355,13 @@ export class DLOB {

const triggerList = this.orderLists.get(marketType).get(order.marketIndex)
.trigger[isVariant(order.triggerCondition, 'above') ? 'above' : 'below'];
triggerList.remove(order, userAccount);
triggerList.remove(order, userAccount.toString());

this.getListForOrder(order, slot)?.insert(order, marketType, userAccount);
this.getListForOrder(order, slot)?.insert(
order,
marketType,
userAccount.toString()
);
if (onTrigger) {
onTrigger();
}
Expand All @@ -374,7 +379,7 @@ export class DLOB {

this.updateRestingLimitOrders(slot);

this.getListForOrder(order, slot)?.remove(order, userAccount);
this.getListForOrder(order, slot)?.remove(order, userAccount.toString());
if (onDelete) {
onDelete();
}
Expand Down Expand Up @@ -472,7 +477,7 @@ export class DLOB {
}

public getOrder(orderId: number, userAccount: PublicKey): Order | undefined {
const orderSignature = getOrderSignature(orderId, userAccount);
const orderSignature = getOrderSignature(orderId, userAccount.toString());
for (const nodeList of this.getNodeLists()) {
const node = nodeList.get(orderSignature);
if (node) {
Expand Down Expand Up @@ -852,7 +857,7 @@ export class DLOB {

for (const makerNode of makerNodeGenerator) {
// Can't match orders from the same user
const sameUser = takerNode.userAccount.equals(makerNode.userAccount);
const sameUser = takerNode.userAccount === makerNode.userAccount;
if (sameUser) {
continue;
}
Expand Down Expand Up @@ -1377,7 +1382,7 @@ export class DLOB {
const askOrder = askNode.order;

// Can't match orders from the same user
const sameUser = bidNode.userAccount.equals(askNode.userAccount);
const sameUser = bidNode.userAccount === askNode.userAccount;
if (sameUser) {
continue;
}
Expand Down Expand Up @@ -1760,7 +1765,7 @@ export class DLOB {
for (const nodeList of this.getNodeLists()) {
for (const node of nodeList.getGenerator()) {
dlobOrders.push({
user: node.userAccount,
user: new PublicKey(node.userAccount),
order: node.order,
});
}
Expand Down Expand Up @@ -1902,7 +1907,7 @@ export class DLOB {
asks.push({
price: ask.getPrice(oraclePriceData, slot),
size: ask.order.baseAssetAmount.sub(ask.order.baseAssetAmountFilled),
maker: ask.userAccount,
maker: new PublicKey(ask.userAccount),
orderId: ask.order.orderId,
});
}
Expand All @@ -1918,7 +1923,7 @@ export class DLOB {
bids.push({
price: bid.getPrice(oraclePriceData, slot),
size: bid.order.baseAssetAmount.sub(bid.order.baseAssetAmountFilled),
maker: bid.userAccount,
maker: new PublicKey(bid.userAccount),
orderId: bid.order.orderId,
});
}
Expand Down Expand Up @@ -2028,7 +2033,10 @@ export class DLOB {

for (const node of generator) {
if (!makers.has(node.userAccount.toString())) {
makers.set(node.userAccount.toString(), node.userAccount);
makers.set(
node.userAccount.toString(),
new PublicKey(node.userAccount)
);
}

if (makers.size === numMakers) {
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/dlob/DLOBNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Order,
ZERO,
} from '..';
import { PublicKey } from '@solana/web3.js';
// import { PublicKey } from '@solana/web3.js';
import { getOrderSignature } from './NodeList';

export interface DLOBNode {
Expand All @@ -18,17 +18,17 @@ export interface DLOBNode {
order: Order | undefined;
isBaseFilled(): boolean;
haveFilled: boolean;
userAccount: PublicKey | undefined;
userAccount: string | undefined;
}

export abstract class OrderNode implements DLOBNode {
order: Order;
userAccount: PublicKey;
userAccount: string;
sortValue: BN;
haveFilled = false;
haveTrigger = false;

constructor(order: Order, userAccount: PublicKey) {
constructor(order: Order, userAccount: string) {
// Copy the order over to the node
this.order = { ...order };
this.userAccount = userAccount;
Expand Down Expand Up @@ -140,7 +140,7 @@ export type DLOBNodeType =
export function createNode<T extends DLOBNodeType>(
nodeType: T,
order: Order,
userAccount: PublicKey
userAccount: string
): DLOBNodeMap[T] {
switch (nodeType) {
case 'floatingLimit':
Expand Down
12 changes: 6 additions & 6 deletions sdk/src/dlob/NodeList.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BN, isVariant, MarketTypeStr, Order } from '..';
import { PublicKey } from '@solana/web3.js';
// import { PublicKey } from '@solana/web3.js';
import { createNode, DLOBNode, DLOBNodeMap } from './DLOBNode';

export type SortDirection = 'asc' | 'desc';

export function getOrderSignature(
orderId: number,
userAccount: PublicKey
userAccount: string
): string {
return `${userAccount.toString()}-${orderId.toString()}`;
}
Expand Down Expand Up @@ -36,7 +36,7 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
public insert(
order: Order,
marketType: MarketTypeStr,
userAccount: PublicKey
userAccount: string
): void {
if (isVariant(order.status, 'init')) {
return;
Expand Down Expand Up @@ -101,7 +101,7 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
}
}

public update(order: Order, userAccount: PublicKey): void {
public update(order: Order, userAccount: string): void {
const orderId = getOrderSignature(order.orderId, userAccount);
if (this.nodeMap.has(orderId)) {
const node = this.nodeMap.get(orderId);
Expand All @@ -110,7 +110,7 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
}
}

public remove(order: Order, userAccount: PublicKey): void {
public remove(order: Order, userAccount: string): void {
const orderId = getOrderSignature(order.orderId, userAccount);
if (this.nodeMap.has(orderId)) {
const node = this.nodeMap.get(orderId);
Expand Down Expand Up @@ -142,7 +142,7 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
}
}

public has(order: Order, userAccount: PublicKey): boolean {
public has(order: Order, userAccount: string): boolean {
return this.nodeMap.has(getOrderSignature(order.orderId, userAccount));
}

Expand Down
3 changes: 1 addition & 2 deletions sdk/src/orderSubscriber/OrderSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ export class OrderSubscriber {
public async getDLOB(slot: number): Promise<DLOB> {
const dlob = new DLOB();
for (const [key, { userAccount }] of this.usersAccounts.entries()) {
const userAccountPubkey = new PublicKey(key);
for (const order of userAccount.orders) {
dlob.insertOrder(order, userAccountPubkey, slot);
dlob.insertOrder(order, key, slot);
}
}
return dlob;
Expand Down
7 changes: 4 additions & 3 deletions sdk/src/phoenix/phoenixFulfillmentConfigMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export class PhoenixFulfillmentConfigMap {
marketIndex: number,
phoenixMarketAddress: PublicKey
): Promise<void> {
const account = await this.driftClient.getPhoenixV1FulfillmentConfig(
phoenixMarketAddress
);
const account =
await this.driftClient.getPhoenixV1FulfillmentConfig(
phoenixMarketAddress
);
this.map.set(marketIndex, account);
}

Expand Down
5 changes: 2 additions & 3 deletions sdk/src/serum/serumFulfillmentConfigMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ export class SerumFulfillmentConfigMap {
marketIndex: number,
serumMarketAddress: PublicKey
): Promise<void> {
const account = await this.driftClient.getSerumV3FulfillmentConfig(
serumMarketAddress
);
const account =
await this.driftClient.getSerumV3FulfillmentConfig(serumMarketAddress);
this.map.set(marketIndex, account);
}

Expand Down
Loading
Loading