Files
trade-drift-dlob/src/dlobProvider.ts
moosecat b3b655dc65 dlob add fetch indicative quotes (#372)
* dlob add fetch indicative quotes

* indicative ws stream

* prettify

* allow for oracle orders
2025-04-16 12:06:21 -07:00

108 lines
2.5 KiB
TypeScript

import {
DLOB,
OrderSubscriber,
ProtectMakerParamsMap,
UserAccount,
UserMap,
} from '@drift-labs/sdk';
import { PublicKey } from '@solana/web3.js';
export type DLOBProvider = {
subscribe(): Promise<void>;
getDLOB(
slot: number,
protectedMakerParamsMap?: ProtectMakerParamsMap
): Promise<DLOB>;
getUniqueAuthorities(): PublicKey[];
getUserAccounts(): Generator<{
userAccount: UserAccount;
publicKey: PublicKey;
}>;
getUserAccount(publicKey: PublicKey): UserAccount | undefined;
size(): number;
fetch(): Promise<void>;
getSlot(): number;
};
export function getDLOBProviderFromUserMap(userMap: UserMap): DLOBProvider {
return {
subscribe: async () => {
await userMap.subscribe();
},
getDLOB: async (
slot: number,
protectedMakerParamsMap?: ProtectMakerParamsMap
) => {
return await userMap.getDLOB(slot, protectedMakerParamsMap);
},
getUniqueAuthorities: () => {
return userMap.getUniqueAuthorities();
},
getUserAccounts: function* () {
for (const user of userMap.values()) {
yield {
userAccount: user.getUserAccount(),
publicKey: user.getUserAccountPublicKey(),
};
}
},
getUserAccount: (publicKey) => {
return userMap.get(publicKey.toString())?.getUserAccount();
},
size: () => {
return userMap.size();
},
fetch: () => {
return userMap.sync();
},
getSlot: () => {
return userMap.getSlot();
},
};
}
export function getDLOBProviderFromOrderSubscriber(
orderSubscriber: OrderSubscriber
): DLOBProvider {
return {
subscribe: async () => {
await orderSubscriber.subscribe();
},
getDLOB: async (
slot: number,
protectedMakerParamsMap?: ProtectMakerParamsMap
) => {
return await orderSubscriber.getDLOB(slot, protectedMakerParamsMap);
},
getUniqueAuthorities: () => {
const authorities = new Set<string>();
for (const { userAccount } of orderSubscriber.usersAccounts.values()) {
authorities.add(userAccount.authority.toBase58());
}
const pubkeys = Array.from(authorities).map((a) => new PublicKey(a));
return pubkeys;
},
getUserAccounts: function* () {
for (const [
key,
{ userAccount },
] of orderSubscriber.usersAccounts.entries()) {
yield { userAccount: userAccount, publicKey: new PublicKey(key) };
}
},
getUserAccount: (publicKey) => {
return orderSubscriber.usersAccounts.get(publicKey.toString())
?.userAccount;
},
size(): number {
return orderSubscriber.usersAccounts.size;
},
fetch() {
return orderSubscriber.fetch();
},
getSlot: () => {
return orderSubscriber.getSlot();
},
};
}