@drift-labs/sdk-browser
Version:
SDK for Drift Protocol
180 lines (179 loc) • 7.2 kB
JavaScript
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.grpcProgramAccountSubscriber = void 0;
const bs58_1 = __importDefault(require("bs58"));
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 grpcProgramAccountSubscriber extends webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber {
constructor(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
filters: [],
}, resubOpts) {
super(subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
this.client = client;
this.commitmentLevel = commitmentLevel;
}
static async create(grpcConfigs, subscriptionName, accountDiscriminator, program, decodeBufferFn, options = {
filters: [],
}, resubOpts) {
var _a, _b;
const client = await (0, grpc_1.createClient)(grpcConfigs.endpoint, grpcConfigs.token, (_a = grpcConfigs.channelOptions) !== null && _a !== void 0 ? _a : {});
const commitmentLevel =
// @ts-ignore :: isomorphic exported enum fails typescript but will work at runtime
(_b = grpcConfigs.commitmentLevel) !== null && _b !== void 0 ? _b : grpc_1.CommitmentLevel.CONFIRMED;
return new grpcProgramAccountSubscriber(client, commitmentLevel, subscriptionName, accountDiscriminator, program, decodeBufferFn, options, resubOpts);
}
async subscribe(onChange) {
if (this.listenerId != null || this.isUnsubscribing) {
return;
}
this.onChange = onChange;
// Subscribe with grpc
this.stream =
(await this.client.subscribe());
const filters = this.options.filters.map((filter) => {
return {
memcmp: {
offset: filter.memcmp.offset.toString(),
bytes: bs58_1.default.decode(filter.memcmp.bytes),
},
};
});
const request = {
slots: {},
accounts: {
drift: {
account: [],
owner: [this.program.programId.toBase58()],
filters,
},
},
transactions: {},
blocks: {},
blocksMeta: {},
accountsDataSlice: [],
commitment: this.commitmentLevel,
entry: {},
transactionsStatus: {},
};
this.stream.on('data', (chunk) => {
var _a;
if (!chunk.account) {
return;
}
const slot = Number(chunk.account.slot);
const accountInfo = {
owner: new web3_js_1.PublicKey(chunk.account.account.owner),
lamports: Number(chunk.account.account.lamports),
data: Buffer.Buffer.from(chunk.account.account.data),
executable: chunk.account.account.executable,
rentEpoch: Number(chunk.account.account.rentEpoch),
};
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
this.receivingData = true;
clearTimeout(this.timeoutId);
this.handleRpcResponse({
slot,
}, {
accountId: new web3_js_1.PublicKey(chunk.account.account.pubkey),
accountInfo,
});
this.setTimeout();
}
else {
this.handleRpcResponse({
slot,
}, {
accountId: new web3_js_1.PublicKey(chunk.account.account.pubkey),
accountInfo,
});
}
});
return new Promise((resolve, reject) => {
this.stream.write(request, (err) => {
var _a;
if (err === null || err === undefined) {
this.listenerId = 1;
if ((_a = this.resubOpts) === null || _a === void 0 ? void 0 : _a.resubTimeoutMs) {
this.receivingData = true;
this.setTimeout();
}
resolve();
}
else {
reject(err);
}
});
}).catch((reason) => {
console.error(reason);
throw reason;
});
}
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) {
const promise = new Promise((resolve, reject) => {
const request = {
slots: {},
accounts: {},
transactions: {},
blocks: {},
blocksMeta: {},
accountsDataSlice: [],
entry: {},
transactionsStatus: {},
};
this.stream.write(request, (err) => {
if (err === null || err === undefined) {
this.listenerId = undefined;
this.isUnsubscribing = false;
resolve();
}
else {
reject(err);
}
});
}).catch((reason) => {
console.error(reason);
throw reason;
});
return promise;
}
else {
this.isUnsubscribing = false;
}
}
}
exports.grpcProgramAccountSubscriber = grpcProgramAccountSubscriber;
;