@drift-labs/sdk-browser
Version:
SDK for Drift Protocol
49 lines (48 loc) • 2.09 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.OneShotUserStatsAccountSubscriber = void 0;
const basicUserStatsAccountSubscriber_1 = require("./basicUserStatsAccountSubscriber");
/**
* Simple implementation of UserStatsAccountSubscriber. It will fetch the UserStatsAccount
* data on subscribe (or call to fetch) if no account data is provided on init.
* Expect to use only 1 RPC call unless you call fetch repeatedly.
*/
class OneShotUserStatsAccountSubscriber extends basicUserStatsAccountSubscriber_1.BasicUserStatsAccountSubscriber {
constructor(program, userStatsAccountPublicKey, data, slot, commitment) {
super(userStatsAccountPublicKey, data, slot);
this.program = program;
this.commitment = commitment !== null && commitment !== void 0 ? commitment : 'confirmed';
}
async subscribe(userStatsAccount) {
if (userStatsAccount) {
this.userStats = { data: userStatsAccount, slot: this.userStats.slot };
return true;
}
await this.fetchIfUnloaded();
if (this.doesAccountExist()) {
this.eventEmitter.emit('update');
}
return true;
}
async fetchIfUnloaded() {
if (this.userStats.data === undefined) {
await this.fetch();
}
}
async fetch() {
var _a, _b;
try {
const dataAndContext = await this.program.account.userStats.fetchAndContext(this.userStatsAccountPublicKey, this.commitment);
if (dataAndContext.context.slot > ((_b = (_a = this.userStats) === null || _a === void 0 ? void 0 : _a.slot) !== null && _b !== void 0 ? _b : 0)) {
this.userStats = {
data: dataAndContext.data,
slot: dataAndContext.context.slot,
};
}
}
catch (e) {
console.error(`OneShotUserStatsAccountSubscriber.fetch() UserStatsAccount does not exist: ${e.message}`);
}
}
}
exports.OneShotUserStatsAccountSubscriber = OneShotUserStatsAccountSubscriber;
;