-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathdeploy.ts
More file actions
397 lines (356 loc) · 12.6 KB
/
deploy.ts
File metadata and controls
397 lines (356 loc) · 12.6 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import path from 'node:path';
import { URL } from 'node:url';
import { print, prompt } from 'gluegun';
import { Args, Command, Flags } from '@oclif/core';
import { identifyDeployKey } from '../command-helpers/auth.js';
import { appendApiVersionForGraph, createCompiler } from '../command-helpers/compiler.js';
import * as DataSourcesExtractor from '../command-helpers/data-sources.js';
import { DEFAULT_IPFS_URL } from '../command-helpers/ipfs.js';
import { createJsonRpcClient } from '../command-helpers/jsonrpc.js';
import { updateSubgraphNetwork } from '../command-helpers/network.js';
import { chooseNodeUrl } from '../command-helpers/node.js';
import { loadRegistry } from '../command-helpers/registry.js';
import { assertGraphTsVersion, assertManifestApiVersion } from '../command-helpers/version.js';
import { GRAPH_CLI_SHARED_HEADERS } from '../constants.js';
import debugFactory from '../debug.js';
import Protocol from '../protocols/index.js';
import { createIpfsClient } from '../utils.js';
const headersFlag = Flags.custom<Record<string, string>>({
summary: 'Add custom headers that will be used by the IPFS HTTP client.',
aliases: ['hdr'],
parse: val => JSON.parse(val),
default: {},
});
const deployDebugger = debugFactory('graph-cli:deploy');
export default class DeployCommand extends Command {
static description = 'Deploys a subgraph to a Graph node.';
static args = {
'subgraph-name': Args.string({}),
'subgraph-manifest': Args.string({
default: 'subgraph.yaml',
}),
};
static flags = {
help: Flags.help({
char: 'h',
}),
node: Flags.string({
summary: 'Graph node for which to initialize.',
char: 'g',
}),
'deploy-key': Flags.string({
summary: 'User deploy key.',
exclusive: ['access-token'],
}),
'access-token': Flags.string({
exclusive: ['deploy-key'],
deprecated: {
to: 'deploy-key',
message: "In next version, we are removing this flag in favor of '--deploy-key'",
},
}),
'version-label': Flags.string({
summary: 'Version label used for the deployment.',
char: 'l',
}),
ipfs: Flags.string({
summary: 'Upload build results to an IPFS node.',
char: 'i',
default: DEFAULT_IPFS_URL,
}),
'ipfs-hash': Flags.string({
summary: 'IPFS hash of the subgraph manifest to deploy.',
required: false,
}),
headers: headersFlag(),
'debug-fork': Flags.string({
summary: 'ID of a remote subgraph whose store will be GraphQL queried.',
}),
'output-dir': Flags.directory({
summary: 'Output directory for build results.',
char: 'o',
default: 'build/',
}),
'skip-migrations': Flags.boolean({
summary: 'Skip subgraph migrations.',
}),
watch: Flags.boolean({
summary: 'Regenerate types when subgraph files change.',
char: 'w',
}),
network: Flags.string({
summary: 'Network configuration to use from the networks config file.',
}),
'network-file': Flags.file({
summary: 'Networks config file path.',
default: 'networks.json',
}),
};
async run() {
const {
args: { 'subgraph-name': subgraphNameArg, 'subgraph-manifest': manifest },
flags: {
'deploy-key': deployKeyFlag,
'access-token': accessToken,
'version-label': versionLabelFlag,
ipfs,
headers,
node: nodeFlag,
'output-dir': outputDir,
'skip-migrations': skipMigrations,
watch,
'debug-fork': debugFork,
network,
'network-file': networkFile,
'ipfs-hash': ipfsHash,
},
} = await this.parse(DeployCommand);
const { subgraphName } = await prompt
.ask<{ subgraphName: string }>([
{
type: 'input',
name: 'subgraphName',
message: () => 'What is the subgraph name?',
skip: () => !!subgraphNameArg,
initial: subgraphNameArg,
required: true,
},
])
.catch(() => this.exit(1));
const { node } = chooseNodeUrl({
node: nodeFlag,
});
if (!node) {
// shouldn't happen, but we do the check to satisfy TS
this.error('No Graph node provided');
}
const requestUrl = new URL(node);
const client = createJsonRpcClient(requestUrl);
// Exit with an error code if the client couldn't be created
if (!client) {
this.exit(1);
}
// Use the deploy key, if one is set
let deployKey = deployKeyFlag;
if (!deployKey && accessToken) {
deployKey = accessToken; // backwards compatibility
}
deployKey = await identifyDeployKey(node, deployKey);
if (deployKey !== undefined && deployKey !== null) {
// @ts-expect-error options property seems to exist
client.options.headers = {
...GRAPH_CLI_SHARED_HEADERS,
Authorization: 'Bearer ' + deployKey,
};
}
// Ask for label if not on hosted service
const { versionLabel } = await prompt
.ask<{ versionLabel: string }>([
{
type: 'input',
name: 'versionLabel',
message: () => 'Which version label to use? (e.g. "v0.0.1")',
initial: versionLabelFlag,
skip: () => !!versionLabelFlag,
required: true,
},
])
.catch(() => this.exit(1));
const deploySubgraph = async (ipfsHash: string) => {
const spinner = print.spin(`Deploying to Graph node ${requestUrl}`);
client.request(
'subgraph_deploy',
{
name: subgraphName,
ipfs_hash: ipfsHash,
version_label: versionLabel,
debug_fork: debugFork,
},
async (
// @ts-expect-error TODO: why are the arguments not typed?
requestError,
// @ts-expect-error TODO: why are the arguments not typed?
jsonRpcError,
// @ts-expect-error TODO: why are the arguments not typed?
res,
) => {
deployDebugger('requestError: %O', requestError);
deployDebugger('jsonRpcError: %O', jsonRpcError);
if (jsonRpcError) {
const message = jsonRpcError?.message || jsonRpcError?.code?.toString();
deployDebugger('message: %O', message);
let errorMessage = `Failed to deploy to Graph node ${requestUrl}: ${message}`;
if (message?.match(/auth failure/)) {
errorMessage += '\nYou may need to authenticate first.';
}
spinner.fail(errorMessage);
process.exit(1);
} else if (requestError) {
spinner.fail(`HTTP error deploying the subgraph ${requestError.code}`);
process.exit(1);
} else {
spinner.stop();
const base = requestUrl.protocol + '//' + requestUrl.hostname;
let playground = res.playground;
let queries = res.queries;
// Add a base URL if graph-node did not return the full URL
if (playground.charAt(0) === ':') {
playground = base + playground;
}
if (queries.charAt(0) === ':') {
queries = base + queries;
}
print.success(`Deployed to ${playground}`);
print.info('\nSubgraph endpoints:');
print.info(`Queries (HTTP): ${queries}`);
print.info(``);
process.exit(0);
}
},
);
};
// we are provided the IPFS hash, so we deploy directly
if (ipfsHash) {
// Connect to the IPFS node (if a node address was provided)
const ipfsClient = createIpfsClient({
url: appendApiVersionForGraph(ipfs.toString()),
headers: {
...headers,
...GRAPH_CLI_SHARED_HEADERS,
},
});
// Fetch the manifest from IPFS
const manifestBuffer = ipfsClient.cat(ipfsHash);
let manifestFile = '';
for await (const chunk of manifestBuffer) {
manifestFile += chunk.toString();
}
if (!manifestFile) {
this.error(`Could not find subgraph manifest at IPFS hash ${ipfsHash}`, { exit: 1 });
}
await ipfsClient.pin.add(ipfsHash);
await deploySubgraph(ipfsHash);
return;
}
let protocol;
let registry;
try {
// Checks to make sure deploy doesn't run against
// older subgraphs (both apiVersion and graph-ts version).
//
// We don't want the deploy to run without these conditions
// because that would mean the CLI would try to compile code
// using the wrong AssemblyScript compiler.
await assertManifestApiVersion(manifest, '0.0.5');
await assertGraphTsVersion(path.dirname(manifest), '0.25.0');
try {
registry = await loadRegistry();
deployDebugger('Loaded networks registry with %d networks', registry.networks.length);
} catch (e) {
deployDebugger('Failed to load networks registry: %O', e);
print.warning('Could not load networks registry. Skipping contract validation.');
}
const dataSourcesAndTemplates = await DataSourcesExtractor.fromFilePath(manifest);
protocol = Protocol.fromDataSources(dataSourcesAndTemplates);
if (registry) {
for (const ds of dataSourcesAndTemplates) {
const address = ds.source.address;
if (!address) continue;
const network = ds.network;
if (!network) continue;
try {
const networkInfo = registry.getNetworkByGraphId(network);
if (!networkInfo) {
deployDebugger('Network %s not found in registry', network);
continue;
}
const rpcEndpoints = registry.getRpcUrls(networkInfo.id) || [];
if (rpcEndpoints.length === 0) {
deployDebugger('No RPC endpoints found for network %s', network);
continue;
}
const rpcUrl = rpcEndpoints[0];
deployDebugger(
'Checking contract %s on network %s via RPC %s',
address,
network,
rpcUrl,
);
const blockchainClient = createJsonRpcClient(new URL(rpcUrl));
if (!blockchainClient) {
deployDebugger('Could not create blockchain client for %s', rpcUrl);
continue;
}
await new Promise<void>(resolve => {
blockchainClient.request(
'eth_getTransactionCount',
[address, 'latest'],
(requestError, jsonRpcError, res) => {
if (jsonRpcError) {
const message = jsonRpcError?.message;
deployDebugger('JSON-RPC error checking contract %s: %O', address, message);
resolve();
} else if (requestError) {
deployDebugger('HTTP error checking contract %s: %O', address, requestError);
resolve();
} else if (res === '0x0') {
print.warning(
`Warning: Contract ${address} does not appear to exist on network ${network}. ` +
`Subgraph may index no events.`,
);
resolve();
} else {
deployDebugger(
'Contract %s exists on network %s (tx count: %s)',
address,
network,
res,
);
resolve();
}
},
);
});
} catch (e) {
print.warning(`Could not check contract ${address}: ${e}`);
deployDebugger('Exception checking contract %s: %O', address, e);
}
}
}
} catch (e) {
this.error(e, { exit: 1 });
}
if (network) {
const identifierName = protocol.getContract()!.identifierName();
await updateSubgraphNetwork(manifest, network, networkFile, identifierName);
}
const compiler = createCompiler(manifest, {
ipfs,
headers,
outputDir,
outputFormat: 'wasm',
skipMigrations,
blockIpfsMethods: undefined,
protocol,
});
// Exit with an error code if the compiler couldn't be created
if (!compiler) {
this.exit(1);
}
if (watch) {
await compiler.watchAndCompile(async ipfsHash => {
if (ipfsHash !== undefined) {
await deploySubgraph(ipfsHash);
}
});
} else {
const result = await compiler.compile({ validate: true });
if (result === undefined || result === false) {
// Compilation failed, not deploying.
process.exitCode = 1;
return;
}
await deploySubgraph(result);
}
}
}