-
Notifications
You must be signed in to change notification settings - Fork 206
/
index.ts
319 lines (285 loc) · 8.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import immutable from 'immutable';
import debug from '../debug';
import Subgraph from '../subgraph';
import * as ArweaveManifestScaffold from './arweave/scaffold/manifest';
import * as ArweaveMappingScaffold from './arweave/scaffold/mapping';
import ArweaveSubgraph from './arweave/subgraph';
import { ContractCtor } from './contract';
import * as CosmosManifestScaffold from './cosmos/scaffold/manifest';
import * as CosmosMappingScaffold from './cosmos/scaffold/mapping';
import CosmosSubgraph from './cosmos/subgraph';
import EthereumABI from './ethereum/abi';
import EthereumTemplateCodeGen from './ethereum/codegen/template';
import EthereumContract from './ethereum/contract';
import * as EthereumManifestScaffold from './ethereum/scaffold/manifest';
import * as EthereumMappingScaffold from './ethereum/scaffold/mapping';
import EthereumSubgraph from './ethereum/subgraph';
import EthereumTypeGenerator from './ethereum/type-generator';
import NearContract from './near/contract';
import * as NearManifestScaffold from './near/scaffold/manifest';
import * as NearMappingScaffold from './near/scaffold/mapping';
import NearSubgraph from './near/subgraph';
import { SubgraphOptions } from './subgraph';
import * as SubstreamsManifestScaffold from './substreams/scaffold/manifest';
import SubstreamsSubgraph from './substreams/subgraph';
const protocolDebug = debug('graph-cli:protocol');
export default class Protocol {
static fromDataSources(dataSourcesAndTemplates: any) {
const firstDataSource = dataSourcesAndTemplates[0];
return new Protocol(firstDataSource);
}
name: ProtocolName;
// TODO: should assert non null? see the constructor switch default case comment
config!: ProtocolConfig;
constructor(datasource: any) {
/**
* TODO: we should improve this `any` type, because some places
* we can initiate a Protocol with just a string (the name) and
* some other places use datasource object
*/
const name = typeof datasource === 'string' ? datasource : datasource.kind;
this.name = Protocol.normalizeName(name)!;
protocolDebug('Initializing protocol %s', this.name);
switch (this.name) {
case 'arweave':
this.config = arweaveProtocol;
break;
case 'cosmos':
this.config = cosmosProtocol;
break;
case 'ethereum':
this.config = ethereumProtocol;
break;
case 'near':
this.config = nearProtocol;
break;
case 'substreams':
this.config = substreamsProtocol;
/**
* Substreams triggers are a special case of substreams data sources
* which have a mapping file and a handler.
*/
if (datasource?.mapping?.file && datasource?.mapping.handler) {
this.name = 'substreams/triggers';
}
break;
default:
// Do not throw when undefined, a better error message is printed after the constructor
// when validating the Subgraph itself
}
}
static availableProtocols() {
return immutable.fromJS({
// `ethereum/contract` is kept for backwards compatibility.
// New networks (or protocol perhaps) shouldn't have the `/contract` anymore (unless a new case makes use of it).
arweave: ['arweave'],
ethereum: ['ethereum', 'ethereum/contract'],
near: ['near'],
cosmos: ['cosmos'],
substreams: ['substreams'],
}) as immutable.Collection<ProtocolName, string[]>;
}
static availableNetworks() {
return immutable.fromJS({
arweave: ['arweave-mainnet'],
ethereum: [
'mainnet',
'rinkeby',
'goerli',
'poa-core',
'poa-sokol',
'gnosis',
'matic',
'mumbai',
'fantom',
'fantom-testnet',
'bsc',
'chapel',
'clover',
'avalanche',
'fuji',
'celo',
'celo-alfajores',
'fuse',
'moonbeam',
'moonriver',
'mbase',
'arbitrum-one',
'arbitrum-goerli',
'arbitrum-sepolia',
'optimism',
'optimism-goerli',
'aurora',
'aurora-testnet',
'base-testnet',
'base',
'zksync-era',
'zksync-era-testnet',
'sepolia',
'polygon-zkevm-testnet',
'polygon-zkevm',
'scroll-sepolia',
'scroll',
],
near: ['near-mainnet', 'near-testnet'],
cosmos: [
'cosmoshub-4',
'theta-testnet-001', // CosmosHub testnet
'osmosis-1',
'osmo-test-4', // Osmosis testnet
'juno-1',
'uni-3', // Juno testnet
],
substreams: ['mainnet'],
}) as immutable.Map<
| 'arweave'
| 'ethereum'
| 'near'
| 'cosmos'
| 'substreams'
// this is temporary, until we have a better way to handle substreams triggers
| 'substreams/triggers',
immutable.List<string>
>;
}
static normalizeName(name: ProtocolName) {
return Protocol.availableProtocols().findKey(possibleNames => {
return possibleNames.includes(name);
})!;
}
displayName() {
return this.config?.displayName;
}
// Receives a data source kind, and checks if it's valid
// for the given protocol instance (this).
isValidKindName(kind: string) {
return Protocol.availableProtocols().get(this.name, immutable.List()).includes(kind);
}
hasABIs() {
return this.config.abi != null;
}
hasContract() {
return this.config.contract != null;
}
hasEvents() {
// A problem with hasEvents usage in the codebase is that it's almost every where
// where used, the ABI data is actually use after the conditional, so it seems
// both concept are related. So internally, we map to this condition.
return this.hasABIs();
}
hasTemplates() {
return this.config.getTemplateCodeGen != null;
}
hasDataSourceMappingFile() {
return this.getMappingScaffold() != null;
}
getTypeGenerator(options: any) {
if (this.config == null || this.config.getTypeGenerator == null) {
return null;
}
return this.config.getTypeGenerator(options);
}
getTemplateCodeGen(template: any) {
if (!this.hasTemplates()) {
throw new Error(`Template data sources with kind '${this.name}' are not supported yet`);
}
return this.config.getTemplateCodeGen?.(template);
}
getABI() {
return this.config.abi;
}
getSubgraph(options: SubgraphOptions) {
return this.config.getSubgraph({ ...options, protocol: this });
}
getContract() {
return this.config.contract;
}
getManifestScaffold() {
return this.config.manifestScaffold;
}
getMappingScaffold() {
return this.config.mappingScaffold;
}
}
export type ProtocolName =
| 'arweave'
| 'ethereum'
| 'near'
| 'cosmos'
| 'substreams'
| 'substreams/triggers';
export interface ProtocolConfig {
displayName: string;
abi?: any;
contract?: ContractCtor;
getTemplateCodeGen?: (template: any) => any;
getTypeGenerator?: (options: any) => any;
getSubgraph(options: SubgraphOptions): Subgraph;
manifestScaffold: any;
mappingScaffold: any;
}
const arweaveProtocol: ProtocolConfig = {
displayName: 'Arweave',
abi: undefined,
contract: undefined,
getTemplateCodeGen: undefined,
getTypeGenerator: undefined,
getSubgraph(options) {
return new ArweaveSubgraph(options);
},
manifestScaffold: ArweaveManifestScaffold,
mappingScaffold: ArweaveMappingScaffold,
};
const cosmosProtocol: ProtocolConfig = {
displayName: 'Cosmos',
abi: undefined,
contract: undefined,
getTemplateCodeGen: undefined,
getTypeGenerator: undefined,
getSubgraph(options) {
return new CosmosSubgraph(options);
},
manifestScaffold: CosmosManifestScaffold,
mappingScaffold: CosmosMappingScaffold,
};
const ethereumProtocol: ProtocolConfig = {
displayName: 'Ethereum',
abi: EthereumABI,
contract: EthereumContract,
getTemplateCodeGen(template) {
return new EthereumTemplateCodeGen(template);
},
getTypeGenerator(options) {
return new EthereumTypeGenerator(options);
},
getSubgraph(options) {
return new EthereumSubgraph(options);
},
manifestScaffold: EthereumManifestScaffold,
mappingScaffold: EthereumMappingScaffold,
};
const nearProtocol: ProtocolConfig = {
displayName: 'NEAR',
abi: undefined,
contract: NearContract,
getTypeGenerator: undefined,
getTemplateCodeGen: undefined,
getSubgraph(options) {
return new NearSubgraph(options);
},
manifestScaffold: NearManifestScaffold,
mappingScaffold: NearMappingScaffold,
};
const substreamsProtocol: ProtocolConfig = {
displayName: 'Substreams',
abi: undefined,
contract: undefined,
getTypeGenerator: undefined,
getTemplateCodeGen: undefined,
getSubgraph(options) {
return new SubstreamsSubgraph(options);
},
manifestScaffold: SubstreamsManifestScaffold,
mappingScaffold: undefined,
};
protocolDebug('Available networks %M', Protocol.availableNetworks());