UNPKG

@sova-labs/sdk

Version:
87 lines 2.83 kB
import path from "node:path"; import * as grpc from '@grpc/grpc-js'; import * as protoLoader from '@grpc/proto-loader'; import { promisify } from 'node:util'; import { ChannelCredentials } from "@grpc/grpc-js"; const protoPath = path.join(__dirname, '..', 'sova-grpc-proto', 'proto', 'searcher.proto'); const packageDefinition = protoLoader.loadSync(protoPath, { keepCase: false, longs: String, enums: String, oneofs: true, }); const proto = grpc.loadPackageDefinition(packageDefinition); const pkg = proto.searcher; export class SearcherService { constructor(url, secureConnection, accessToken) { this.accessToken = accessToken; const metadata = new grpc.Metadata(); if (this.accessToken) { metadata.add('authorization', `Bearer ${this.accessToken.value}`); } this.client = new pkg.SearcherService(url, secureConnection ? ChannelCredentials.createSsl() : grpc.credentials.createInsecure(), { metadata }); } subscribeBundleResults() { return this.client.SubscribeBundleResults({}, {}); } sendBundle(messages, expireAt) { let expirationNs = undefined; if (expireAt) { const milliseconds = expireAt.getTime(); expirationNs = { seconds: Math.floor(milliseconds / 1000), nanos: (milliseconds % 1000) * 1e6 }; } const request = { message: messages, expirationNs, }; return promisify(this.client.SendBundle.bind(this.client, request))(); } getTipAddresses() { return promisify(this.client.GetTipAddresses.bind(this.client, {}))(); } subscribeMempool(subscription) { return this.client.subscribeMempool(subscription); } subscribeByAddresses(addresses) { return this.subscribeMempool({ addresses: { address: addresses, } }); } subscribeByWorkchain(workchainId) { return this.subscribeMempool({ workchain: { workchainId, } }); } subscribeByWorkchainShard(workchainId, shard) { return this.subscribeMempool({ workchainShard: { workchainId, shard, } }); } subscribeByExternalOutMessageOpcode(workchainId, shard, opcode) { return this.subscribeMempool({ externalOutMessageBodyOpcode: { workchainId, shard, opcode } }); } subscribeByInternalMessageOpcode(workchainId, shard, opcode) { return this.subscribeMempool({ internalMessageBodyOpcode: { opcode } }); } } //# sourceMappingURL=searcher.service.js.map