UNPKG

@drift-labs/sdk

Version:
111 lines (110 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PollingInsuranceFundStakeAccountSubscriber = void 0; const types_1 = require("./types"); const events_1 = require("events"); class PollingInsuranceFundStakeAccountSubscriber { constructor(program, publicKey, accountLoader) { this.isSubscribed = false; this.program = program; this.insuranceFundStakeAccountPublicKey = publicKey; this.accountLoader = accountLoader; this.eventEmitter = new events_1.EventEmitter(); } async subscribe(insuranceFundStake) { if (this.isSubscribed) { return true; } if (insuranceFundStake) { this.insuranceFundStakeAccountAndSlot = { data: insuranceFundStake, slot: undefined, }; } await this.addToAccountLoader(); if (this.doesAccountExist()) { this.eventEmitter.emit('update'); } this.isSubscribed = true; return true; } async addToAccountLoader() { if (this.callbackId) { return; } this.callbackId = await this.accountLoader.addAccount(this.insuranceFundStakeAccountPublicKey, (buffer, slot) => { if (!buffer) { return; } if (this.insuranceFundStakeAccountAndSlot && this.insuranceFundStakeAccountAndSlot.slot > slot) { return; } const account = this.program.account.user.coder.accounts.decode('InsuranceFundStake', buffer); this.insuranceFundStakeAccountAndSlot = { data: account, slot }; this.eventEmitter.emit('insuranceFundStakeAccountUpdate', account); this.eventEmitter.emit('update'); }); this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => { this.eventEmitter.emit('error', error); }); } async fetchIfUnloaded() { if (this.insuranceFundStakeAccountAndSlot === undefined) { await this.fetch(); } } async fetch() { var _a, _b; try { const dataAndContext = await this.program.account.insuranceFundStake.fetchAndContext(this.insuranceFundStakeAccountPublicKey, this.accountLoader.commitment); if (dataAndContext.context.slot > ((_b = (_a = this.insuranceFundStakeAccountAndSlot) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0)) { this.insuranceFundStakeAccountAndSlot = { data: dataAndContext.data, slot: dataAndContext.context.slot, }; } } catch (e) { console.log(`PollingInsuranceFundStakeAccountSubscriber.fetch() InsuranceFundStake does not exist: ${e.message}`); } } doesAccountExist() { return this.insuranceFundStakeAccountAndSlot !== undefined; } async unsubscribe() { if (!this.isSubscribed) { return; } this.accountLoader.removeAccount(this.insuranceFundStakeAccountPublicKey, this.callbackId); this.callbackId = undefined; this.accountLoader.removeErrorCallbacks(this.errorCallbackId); this.errorCallbackId = undefined; this.isSubscribed = false; } assertIsSubscribed() { if (!this.isSubscribed) { throw new types_1.NotSubscribedError('You must call `subscribe` before using this function'); } } getInsuranceFundStakeAccountAndSlot() { this.assertIsSubscribed(); return this.insuranceFundStakeAccountAndSlot; } didSubscriptionSucceed() { return !!this.insuranceFundStakeAccountAndSlot; } updateData(insuranceFundStake, slot) { if (!this.insuranceFundStakeAccountAndSlot || this.insuranceFundStakeAccountAndSlot.slot < slot) { this.insuranceFundStakeAccountAndSlot = { data: insuranceFundStake, slot, }; this.eventEmitter.emit('insuranceFundStakeAccountUpdate', insuranceFundStake); this.eventEmitter.emit('update'); } } } exports.PollingInsuranceFundStakeAccountSubscriber = PollingInsuranceFundStakeAccountSubscriber;