Last active
June 22, 2023 07:48
-
-
Save maxsap/abb055741654688d355fed63c12f7b5f to your computer and use it in GitHub Desktop.
Substreams node client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 invocesbuf generate buf.build/streamingfast/substreams