grpc order subscriber

This commit is contained in:
Nour Alharithi
2023-12-06 15:39:55 -08:00
parent 368ced8a24
commit 2aff4a29e2
6 changed files with 514 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
import { DLOB, OrderSubscriber, UserAccount, UserMap } from '@drift-labs/sdk';
import { PublicKey } from '@solana/web3.js';
import { GeyserOrderSubscriber } from './grpc/OrderSubscriberGRPC';
export type DLOBProvider = {
subscribe(): Promise<void>;
@@ -82,3 +83,41 @@ export function getDLOBProviderFromOrderSubscriber(
},
};
}
export function getDLOBProviderFromGrpcOrderSubscriber(
orderSubscriber: GeyserOrderSubscriber
): DLOBProvider {
return {
subscribe: async () => {
await orderSubscriber.subscribe();
},
getDLOB: async (slot: number) => {
return await orderSubscriber.getDLOB(slot);
},
getUniqueAuthorities: () => {
const authorities = new Set<PublicKey>();
for (const { userAccount } of orderSubscriber.usersAccounts.values()) {
authorities.add(userAccount.authority);
}
return Array.from(authorities.values());
},
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();
},
};
}