Skip to content

Commit 95eb9d0

Browse files
mangasgithub-actions[bot]saihaj
authored
Filipe/substreams mappings (#1490)
* compile substreams mappings when available * chore(dependencies): updated changesets for modified dependencies * revert change to prettier * run prettier * it just works * chore(dependencies): updated changesets for modified dependencies * trigger ci * make sure to uplaod mapping * handle names of datasource gracefully * ci: fix snapshot --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Saihajpreet Singh <[email protected]>
1 parent b168be1 commit 95eb9d0

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

.changeset/nervous-windows-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphprotocol/graph-cli': minor
3+
---
4+
5+
substreams based triggers support

packages/cli/src/compiler/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ export default class Compiler {
548548
);
549549
}
550550

551-
if (protocol.name == 'substreams') {
551+
if (protocol.name == 'substreams' || protocol.name == 'substreams/triggers') {
552552
updatedDataSource = updatedDataSource
553553
// Write data source ABIs to the output directory
554554
.updateIn(['source', 'package'], (substreamsPackage: any) =>
@@ -569,6 +569,17 @@ export default class Compiler {
569569
}),
570570
);
571571

572+
if (updatedDataSource.getIn(['mapping', 'file'])) {
573+
updatedDataSource = updatedDataSource.updateIn(
574+
['mapping', 'file'],
575+
(mappingFile: string) =>
576+
path.relative(
577+
this.options.outputDir,
578+
path.resolve(this.sourceDir, mappingFile),
579+
),
580+
);
581+
}
582+
572583
return updatedDataSource;
573584
}
574585

@@ -662,7 +673,7 @@ export default class Compiler {
662673
}
663674

664675
// Upload all mappings
665-
if (this.protocol.name === 'substreams') {
676+
if (this.protocol.name === 'substreams' || this.protocol.name === 'substreams/triggers') {
666677
for (const [i, dataSource] of subgraph.get('dataSources').entries()) {
667678
updates.push({
668679
keyPath: ['dataSources', i, 'source', 'package', 'file'],
@@ -672,6 +683,17 @@ export default class Compiler {
672683
spinner,
673684
),
674685
});
686+
687+
if (dataSource.getIn(['mapping', 'file'])) {
688+
updates.push({
689+
keyPath: ['dataSources', i, 'mapping', 'file'],
690+
value: await this._uploadFileToIPFS(
691+
dataSource.getIn(['mapping', 'file']),
692+
uploadedFiles,
693+
spinner,
694+
),
695+
});
696+
}
675697
}
676698
} else {
677699
for (const [i, dataSource] of subgraph.get('dataSources').entries()) {

packages/cli/src/protocols/index.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,24 @@ const protocolDebug = debug('graph-cli:protocol');
2727

2828
export default class Protocol {
2929
static fromDataSources(dataSourcesAndTemplates: any) {
30-
const firstDataSourceKind = dataSourcesAndTemplates[0].kind;
31-
return new Protocol(firstDataSourceKind);
30+
const firstDataSource = dataSourcesAndTemplates[0];
31+
return new Protocol(firstDataSource);
3232
}
3333

3434
name: ProtocolName;
3535

3636
// TODO: should assert non null? see the constructor switch default case comment
3737
config!: ProtocolConfig;
3838

39-
constructor(name: ProtocolName) {
39+
constructor(datasource: any) {
40+
/**
41+
* TODO: we should improve this `any` type, because some places
42+
* we can initiate a Protocol with just a string (the name) and
43+
* some other places use datasource object
44+
*/
45+
const name = typeof datasource === 'string' ? datasource : datasource.kind;
4046
this.name = Protocol.normalizeName(name)!;
47+
protocolDebug('Initializing protocol %s', this.name);
4148

4249
switch (this.name) {
4350
case 'arweave':
@@ -54,6 +61,14 @@ export default class Protocol {
5461
break;
5562
case 'substreams':
5663
this.config = substreamsProtocol;
64+
65+
/**
66+
* Substreams triggers are a special case of substreams data sources
67+
* which have a mapping file and a handler.
68+
*/
69+
if (datasource?.mapping?.file && datasource?.mapping.handler) {
70+
this.name = 'substreams/triggers';
71+
}
5772
break;
5873
default:
5974
// Do not throw when undefined, a better error message is printed after the constructor
@@ -126,7 +141,13 @@ export default class Protocol {
126141
],
127142
substreams: ['mainnet'],
128143
}) as immutable.Map<
129-
'arweave' | 'ethereum' | 'near' | 'cosmos' | 'substreams',
144+
| 'arweave'
145+
| 'ethereum'
146+
| 'near'
147+
| 'cosmos'
148+
| 'substreams'
149+
// this is temporary, until we have a better way to handle substreams triggers
150+
| 'substreams/triggers',
130151
immutable.List<string>
131152
>;
132153
}
@@ -207,7 +228,13 @@ export default class Protocol {
207228
}
208229
}
209230

210-
export type ProtocolName = 'arweave' | 'ethereum' | 'near' | 'cosmos' | 'substreams';
231+
export type ProtocolName =
232+
| 'arweave'
233+
| 'ethereum'
234+
| 'near'
235+
| 'cosmos'
236+
| 'substreams'
237+
| 'substreams/triggers';
211238

212239
export interface ProtocolConfig {
213240
displayName: string;

packages/cli/src/protocols/substreams/manifest.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ type DataSource {
3232

3333
type SubstreamsSource {
3434
package: SubstreamsPackage!
35+
startBlock: BigInt
3536
}
3637

3738
type SubstreamsPackage {
3839
moduleName: String!
3940
file: String!
41+
params: String
4042
}
4143

4244
type SubstreamMapping {
4345
apiVersion: String!
4446
kind: String!
47+
file: File
48+
handler: String
4549
}

packages/cli/src/subgraph.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ export default class Subgraph {
5353
// Parse the default subgraph schema
5454
const schema = graphql.parse(
5555
await fs.readFile(
56-
path.join(__dirname, 'protocols', protocol.name, `manifest.graphql`),
56+
path.join(
57+
__dirname,
58+
'protocols',
59+
// TODO: substreams/triggers is a special case, should be handled better
60+
protocol.name === 'substreams/triggers' ? 'substreams' : protocol.name,
61+
`manifest.graphql`,
62+
),
5763
'utf-8',
5864
),
5965
);

0 commit comments

Comments
 (0)