Skip to content

Instantly share code, notes, and snippets.

@maxsap
Last active June 22, 2023 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxsap/abb055741654688d355fed63c12f7b5f to your computer and use it in GitHub Desktop.
Save maxsap/abb055741654688d355fed63c12f7b5f to your computer and use it in GitHub Desktop.
Substreams node client
version: v1
managed:
enabled: true
plugins:
- name: es
path: ./node_modules/.bin/protoc-gen-es
out: src/generated
opt: target=ts
- name: connect-es
path: ./node_modules/.bin/protoc-gen-connect-es
out: src/generated
opt: target=ts
import { Config } from '../constants/config.constants';
import { Package } from '../generated/sf/substreams/v1/package_pb';
import path from 'path';
import { createPromiseClient } from '@bufbuild/connect';
import { createGrpcTransport } from '@bufbuild/connect-node';
import { Stream } from '../generated/sf/substreams/rpc/v2/service_connect';
import { Request, Response } from '../generated/sf/substreams/rpc/v2/service_pb';
const file = path.isAbsolute(packageFilename) ? packageFilename : path.resolve(process.cwd(), packageFilename);
const pkg = Package.fromBinary(readFileSync(file));
const url = 'https://polygon.streamingfast.io:443';
const transport = createGrpcTransport({
baseUrl: url,
httpVersion: '2',
});
const client = createPromiseClient(Stream, transport);
const request = new Request({
modules: pkg.modules,
startBlockNum: BigInt(startingBlock),
stopBlockNum: BigInt(Number(endBlock)),
finalBlocksOnly: true,
productionMode: true,
outputModule: 'classify_all_events',
});
const authorization = '{YOUR TOKEN HERE}}';
const responses = client.blocks(request, {
headers: { Authorization: authorization },
});
return responses;
import { Config } from '../constants/config.constants';
import { Package } from '../generated/sf/substreams/v1/package_pb';
import path from 'path';
import { createPromiseClient } from '@bufbuild/connect';
import { createGrpcTransport } from '@bufbuild/connect-node';
import { Stream } from '../generated/sf/substreams/rpc/v2/service_connect';
import { Request, Response } from '../generated/sf/substreams/rpc/v2/service_pb';
const file = path.isAbsolute(packageFilename) ? packageFilename : path.resolve(process.cwd(), packageFilename);
const pkg = Package.fromBinary(readFileSync(file));
const url = this.config.getOrThrow(Config.GRPC_TRANSPORT_BASE_URL);
const transport = createGrpcTransport({
baseUrl: url,
httpVersion: '2',
});
const client = createPromiseClient(Stream, transport);
const request = new Request({
modules: pkg.modules,
startBlockNum: BigInt(startingBlock),
stopBlockNum: BigInt(Number(endBlock)),
finalBlocksOnly: true,
productionMode: true,
outputModule: 'classify_all_events',
});
const authorization =
'{YOUR TOKEN HERE}';
// Setup Substream
const responses = client.blocks(request, {
headers: { Authorization: authorization, 'Content-Type': 'application/connect+proto' },
});
return responses;
@maxsap
Copy link
Author

maxsap commented Jun 22, 2023

In order to generate the client you will need to have the buf.gen.yml in your project, define a script on package.json which invoces buf generate buf.build/streamingfast/substreams

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment