@drift-labs/sdk
Version:
SDK for Drift Protocol
112 lines (111 loc) • 4.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PollingHighLeverageModeConfigAccountSubscriber = void 0;
const types_1 = require("./types");
const events_1 = require("events");
class PollingHighLeverageModeConfigAccountSubscriber {
constructor(program, publicKey, accountLoader) {
this.isSubscribed = false;
this.program = program;
this.highLeverageModeConfigAccountPublicKey = publicKey;
this.accountLoader = accountLoader;
this.eventEmitter = new events_1.EventEmitter();
}
async subscribe(highLeverageModeConfig) {
if (this.isSubscribed) {
return true;
}
if (highLeverageModeConfig) {
this.highLeverageModeConfigAccountAndSlot = {
data: highLeverageModeConfig,
slot: undefined,
};
}
await this.addToAccountLoader();
await this.fetchIfUnloaded();
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.highLeverageModeConfigAccountPublicKey, (buffer, slot) => {
if (!buffer) {
return;
}
if (this.highLeverageModeConfigAccountAndSlot &&
this.highLeverageModeConfigAccountAndSlot.slot > slot) {
return;
}
const account = this.program.account.user.coder.accounts.decode('HighLeverageModeConfig', buffer);
this.highLeverageModeConfigAccountAndSlot = { data: account, slot };
this.eventEmitter.emit('highLeverageModeConfigAccountUpdate', account);
this.eventEmitter.emit('update');
});
this.errorCallbackId = this.accountLoader.addErrorCallbacks((error) => {
this.eventEmitter.emit('error', error);
});
}
async fetchIfUnloaded() {
if (this.highLeverageModeConfigAccountAndSlot === undefined) {
await this.fetch();
}
}
async fetch() {
var _a, _b;
try {
const dataAndContext = await this.program.account.highLeverageModeConfig.fetchAndContext(this.highLeverageModeConfigAccountPublicKey, this.accountLoader.commitment);
if (dataAndContext.context.slot >
((_b = (_a = this.highLeverageModeConfigAccountAndSlot) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0)) {
this.highLeverageModeConfigAccountAndSlot = {
data: dataAndContext.data,
slot: dataAndContext.context.slot,
};
}
}
catch (e) {
console.log(`PollingHighLeverageModeConfigAccountSubscriber.fetch() HighLeverageModeConfig does not exist: ${e.message}`);
}
}
doesAccountExist() {
return this.highLeverageModeConfigAccountAndSlot !== undefined;
}
async unsubscribe() {
if (!this.isSubscribed) {
return;
}
this.accountLoader.removeAccount(this.highLeverageModeConfigAccountPublicKey, 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');
}
}
getHighLeverageModeConfigAccountAndSlot() {
this.assertIsSubscribed();
return this.highLeverageModeConfigAccountAndSlot;
}
didSubscriptionSucceed() {
return !!this.highLeverageModeConfigAccountAndSlot;
}
updateData(highLeverageModeConfig, slot) {
if (!this.highLeverageModeConfigAccountAndSlot ||
this.highLeverageModeConfigAccountAndSlot.slot < slot) {
this.highLeverageModeConfigAccountAndSlot = {
data: highLeverageModeConfig,
slot,
};
this.eventEmitter.emit('highLeverageModeConfigAccountUpdate', highLeverageModeConfig);
this.eventEmitter.emit('update');
}
}
}
exports.PollingHighLeverageModeConfigAccountSubscriber = PollingHighLeverageModeConfigAccountSubscriber;