@drift-labs/sdk-browser
Version:
SDK for Drift Protocol
155 lines (154 loc) • 6.55 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LaserstreamProgramAccountSubscriber = void 0;
const web3_js_1 = require("@solana/web3.js");
const Buffer = __importStar(require("buffer"));
const webSocketProgramAccountSubscriber_1 = require("./webSocketProgramAccountSubscriber");
const grpc_1 = require("../isomorphic/grpc");
class LaserstreamProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber {
constructor(laserConfig, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = { filters: [] }, resubOpts) {
super(subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
this.laserConfig = laserConfig;
this.commitmentLevel = this.toLaserCommitment(commitmentLevel);
}
static async create(grpcConfigs, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
filters: [],
}, resubOpts) {
var _a;
const laserConfig = {
apiKey: grpcConfigs.token,
endpoint: grpcConfigs.endpoint,
maxReconnectAttempts: grpcConfigs.enableReconnect ? 10 : 0,
channelOptions: {
'grpc.default_compression_algorithm': grpc_1.CompressionAlgorithms.zstd,
'grpc.max_receive_message_length': 1000000000,
},
};
const commitmentLevel = (_a = grpcConfigs.commitmentLevel) !== null && _a !== void 0 ? _a : grpc_1.CommitmentLevel.CONFIRMED;
return new LaserstreamProgramAccountSubscriber(laserConfig, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
}
async subscribe(onChange) {
var _a;
if (this.listenerId != null || this.isUnsubscribing)
return;
this.onChange = onChange;
const filters = this.options.filters.map((filter) => {
return {
memcmp: {
offset: filter.memcmp.offset,
base58: filter.memcmp.bytes,
},
};
});
const request = {
slots: {},
accounts: {
drift: {
account: [],
owner: [this.program.programId.toBase58()],
filters,
},
},
transactions: {},
blocks: {},
blocksMeta: {},
accountsDataSlice: [],
commitment: this.commitmentLevel,
entry: {},
transactionsStatus: {},
};
try {
const stream = await (0, grpc_1.LaserSubscribe)(this.laserConfig, request, async (update) => {
var _a;
if (update.account) {
const slot = Number(update.account.slot);
const acc = update.account.account;
const accountInfo = {
owner: new web3_js_1.PublicKey(acc.owner),
lamports: Number(acc.lamports),
data: Buffer.Buffer.from(acc.data),
executable: acc.executable,
rentEpoch: Number(acc.rentEpoch),
};
const payload = {
accountId: new web3_js_1.PublicKey(acc.pubkey),
accountInfo,
};
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
this.receivingData = true;
clearTimeout(this.timeoutId);
this.handleRpcResponse({ slot }, payload);
this.setTimeout();
}
else {
this.handleRpcResponse({ slot }, payload);
}
}
}, async (error) => {
console.error('LaserStream client error:', error);
throw error;
});
this.stream = stream;
this.listenerId = 1;
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
this.receivingData = true;
this.setTimeout();
}
}
catch (err) {
console.error('Failed to start LaserStream client:', err);
throw err;
}
}
async unsubscribe(onResub = false) {
if (!onResub && this.resubOpts) {
this.resubOpts.resubTimeoutMs = undefined;
}
this.isUnsubscribing = true;
clearTimeout(this.timeoutId);
this.timeoutId = undefined;
if (this.listenerId != null && this.stream) {
try {
this.stream.cancel();
}
finally {
this.listenerId = undefined;
this.isUnsubscribing = false;
}
}
else {
this.isUnsubscribing = false;
}
}
toLaserCommitment(level) {
var _a, _b;
if (typeof level === 'string') {
return ((_a = grpc_1.LaserCommitmentLevel[level.toUpperCase()]) !== null && _a !== void 0 ? _a : grpc_1.LaserCommitmentLevel.CONFIRMED);
}
return (_b = level) !== null && _b !== void 0 ? _b : grpc_1.LaserCommitmentLevel.CONFIRMED;
}
}
exports.LaserstreamProgramAccountSubscriber = LaserstreamProgramAccountSubscriber;