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

Impl websocket token account subscriber #1331

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
104 changes: 104 additions & 0 deletions sdk/src/accounts/webSocketTokenAccountSubscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {
DataAndSlot,
AccountSubscriber,
NotSubscribedError,
ResubOpts,
TokenAccountEvents,
TokenAccountSubscriber,
} from './types';
import { Program } from '@coral-xyz/anchor';
import StrictEventEmitter from 'strict-event-emitter-types';
import { EventEmitter } from 'events';
import { Commitment, PublicKey } from '@solana/web3.js';
import { WebSocketAccountSubscriber } from './webSocketAccountSubscriber';
import { Account } from '@solana/spl-token';
import { parseTokenAccount } from '../token';

export class WebSocketTokenAccountSubscriber implements TokenAccountSubscriber {
isSubscribed: boolean;
resubOpts?: ResubOpts;
commitment?: Commitment;
program: Program;
eventEmitter: StrictEventEmitter<EventEmitter, TokenAccountEvents>;
tokenAccountPublicKey: PublicKey;

tokenAccountSubscriber: AccountSubscriber<Account>;

public constructor(
program: Program,
tokenAccountPublicKey: PublicKey,
resubOpts?: ResubOpts,
commitment?: Commitment
) {
this.isSubscribed = false;
this.program = program;
this.resubOpts = resubOpts;
this.tokenAccountPublicKey = tokenAccountPublicKey;
this.eventEmitter = new EventEmitter();
this.commitment = commitment;
}

async subscribe(tokenAccount?: Account): Promise<boolean> {
if (this.isSubscribed) {
return true;
}

this.tokenAccountSubscriber = new WebSocketAccountSubscriber(
'token',
this.program,
this.tokenAccountPublicKey,
(buffer) => parseTokenAccount(buffer, this.tokenAccountPublicKey),
this.resubOpts,
this.commitment
);

if (tokenAccount) {
this.tokenAccountSubscriber.setData(tokenAccount);
}

await this.tokenAccountSubscriber.subscribe((data: Account) => {
this.eventEmitter.emit('tokenAccountUpdate', data);
this.eventEmitter.emit('update');
});

this.eventEmitter.emit('update');
this.isSubscribed = true;
return true;
}

async fetch(): Promise<void> {
await Promise.all([this.tokenAccountSubscriber.fetch()]);
}

async unsubscribe(): Promise<void> {
if (!this.isSubscribed) {
return;
}

await Promise.all([this.tokenAccountSubscriber.unsubscribe()]);

this.isSubscribed = false;
}

assertIsSubscribed(): void {
if (!this.isSubscribed) {
throw new NotSubscribedError(
'You must call `subscribe` before using this function'
);
}
}

public getTokenAccountAndSlot(): DataAndSlot<Account> {
this.assertIsSubscribed();
return this.tokenAccountSubscriber.dataAndSlot;
}

public updateData(tokenAccount: Account, slot: number) {
const currentDataSlot = this.tokenAccountSubscriber.dataAndSlot?.slot || 0;
if (currentDataSlot <= slot) {
this.tokenAccountSubscriber.setData(tokenAccount, slot);
this.eventEmitter.emit('tokenAccountUpdate', tokenAccount);
this.eventEmitter.emit('update');
}
}
}
1 change: 1 addition & 0 deletions sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './accounts/fetch';
export * from './accounts/webSocketDriftClientAccountSubscriber';
export * from './accounts/webSocketInsuranceFundStakeAccountSubscriber';
export * from './accounts/webSocketHighLeverageModeConfigAccountSubscriber';
export * from './accounts/webSocketTokenAccountSubscriber';
export * from './accounts/bulkAccountLoader';
export * from './accounts/bulkUserSubscription';
export * from './accounts/bulkUserStatsSubscription';
Expand Down
Loading