@graphprotocol/graph-cli 0.37.4 → 0.37.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/bin.js +6 -0
  3. package/dist/cli.js +26 -0
  4. package/dist/codegen/schema.js +244 -0
  5. package/dist/codegen/schema.test.js +455 -0
  6. package/dist/codegen/template.js +66 -0
  7. package/dist/codegen/types/conversions.js +335 -0
  8. package/dist/codegen/types/index.js +71 -0
  9. package/dist/codegen/types/index.test.js +563 -0
  10. package/dist/codegen/typescript.js +200 -0
  11. package/dist/codegen/util.js +40 -0
  12. package/dist/codegen/util.test.js +93 -0
  13. package/dist/command-helpers/abi.js +76 -0
  14. package/dist/command-helpers/auth.js +76 -0
  15. package/dist/command-helpers/compiler.js +66 -0
  16. package/dist/command-helpers/data-sources.js +29 -0
  17. package/dist/command-helpers/fs.js +9 -0
  18. package/dist/command-helpers/gluegun.js +48 -0
  19. package/dist/command-helpers/ipfs.js +5 -0
  20. package/dist/command-helpers/jsonrpc.js +27 -0
  21. package/dist/command-helpers/network.js +90 -0
  22. package/dist/command-helpers/network.test.js +86 -0
  23. package/dist/command-helpers/node.js +40 -0
  24. package/dist/command-helpers/scaffold.js +110 -0
  25. package/dist/command-helpers/spinner.js +80 -0
  26. package/dist/command-helpers/studio.js +14 -0
  27. package/dist/command-helpers/studio.test.js +41 -0
  28. package/dist/command-helpers/subgraph.js +22 -0
  29. package/dist/command-helpers/version.js +66 -0
  30. package/dist/command-helpers/version.test.js +186 -0
  31. package/dist/commands/add.js +187 -0
  32. package/dist/commands/auth.js +122 -0
  33. package/dist/commands/build.js +136 -0
  34. package/dist/commands/codegen.js +128 -0
  35. package/dist/commands/create.js +94 -0
  36. package/dist/commands/deploy.js +335 -0
  37. package/dist/commands/init.js +800 -0
  38. package/dist/commands/local.js +380 -0
  39. package/dist/commands/remove.js +95 -0
  40. package/dist/commands/test.js +293 -0
  41. package/dist/compiler/asc.js +83 -0
  42. package/dist/compiler/index.js +466 -0
  43. package/dist/debug.js +16 -0
  44. package/dist/migrations/mapping_api_version_0_0_1.js +83 -0
  45. package/dist/migrations/mapping_api_version_0_0_2.js +83 -0
  46. package/dist/migrations/mapping_api_version_0_0_3.js +83 -0
  47. package/dist/migrations/mapping_api_version_0_0_4.js +85 -0
  48. package/dist/migrations/mapping_api_version_0_0_5.js +85 -0
  49. package/dist/migrations/spec_version_0_0_2.js +45 -0
  50. package/dist/migrations/spec_version_0_0_3.js +50 -0
  51. package/dist/migrations/util/load-manifest.js +16 -0
  52. package/dist/migrations/util/versions.js +28 -0
  53. package/dist/migrations.js +54 -0
  54. package/dist/protocols/arweave/manifest.graphql +57 -0
  55. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  56. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  57. package/dist/protocols/arweave/subgraph.js +23 -0
  58. package/dist/protocols/contract.js +2 -0
  59. package/dist/protocols/cosmos/manifest.graphql +76 -0
  60. package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
  61. package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
  62. package/dist/protocols/cosmos/subgraph.js +28 -0
  63. package/dist/protocols/ethereum/abi.js +138 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +474 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +325 -0
  66. package/dist/protocols/ethereum/codegen/template.js +52 -0
  67. package/dist/protocols/ethereum/contract.js +22 -0
  68. package/dist/protocols/ethereum/manifest.graphql +94 -0
  69. package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
  70. package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
  71. package/dist/protocols/ethereum/subgraph.js +172 -0
  72. package/dist/protocols/ethereum/type-generator.js +123 -0
  73. package/dist/protocols/index.js +266 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +53 -0
  75. package/dist/protocols/near/contract.js +41 -0
  76. package/dist/protocols/near/manifest.graphql +64 -0
  77. package/dist/protocols/near/scaffold/manifest.js +16 -0
  78. package/dist/protocols/near/scaffold/mapping.js +38 -0
  79. package/dist/protocols/near/subgraph.js +23 -0
  80. package/dist/protocols/subgraph.js +2 -0
  81. package/dist/protocols/substreams/manifest.graphql +45 -0
  82. package/dist/protocols/substreams/scaffold/manifest.js +12 -0
  83. package/dist/protocols/substreams/subgraph.js +23 -0
  84. package/dist/scaffold/cosmos.test.js +82 -0
  85. package/dist/scaffold/ethereum.test.js +496 -0
  86. package/dist/scaffold/index.js +133 -0
  87. package/dist/scaffold/mapping.js +57 -0
  88. package/dist/scaffold/near.test.js +85 -0
  89. package/dist/scaffold/schema.js +86 -0
  90. package/dist/scaffold/tests.js +179 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +243 -0
  93. package/dist/type-generator.js +225 -0
  94. package/dist/validation/contract.js +44 -0
  95. package/dist/validation/index.js +10 -0
  96. package/dist/validation/manifest.js +266 -0
  97. package/dist/validation/schema.js +768 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/package.json +1 -1
@@ -0,0 +1,172 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const immutable_1 = __importDefault(require("immutable"));
30
+ const DataSourcesExtractor = __importStar(require("../../command-helpers/data-sources"));
31
+ const abi_1 = __importDefault(require("./abi"));
32
+ class EthereumSubgraph {
33
+ manifest;
34
+ resolveFile;
35
+ protocol;
36
+ constructor(options) {
37
+ this.manifest = options.manifest;
38
+ this.resolveFile = options.resolveFile;
39
+ this.protocol = options.protocol;
40
+ }
41
+ validateManifest() {
42
+ return this.validateAbis().concat(this.validateEvents()).concat(this.validateCallFunctions());
43
+ }
44
+ validateAbis() {
45
+ const dataSourcesAndTemplates = DataSourcesExtractor.fromManifest(this.manifest, this.protocol);
46
+ return dataSourcesAndTemplates.reduce((errors, dataSourceOrTemplate) => errors.concat(this.validateDataSourceAbis(dataSourceOrTemplate.get('dataSource'), dataSourceOrTemplate.get('path'))), immutable_1.default.List());
47
+ }
48
+ validateDataSourceAbis(dataSource, path) {
49
+ // Validate that the the "source > abi" reference of all data sources
50
+ // points to an existing ABI in the data source ABIs
51
+ const abiName = dataSource.getIn(['source', 'abi']);
52
+ const abiNames = dataSource.getIn(['mapping', 'abis']).map((abi) => abi.get('name'));
53
+ const nameErrors = abiNames.includes(abiName)
54
+ ? immutable_1.default.List()
55
+ : immutable_1.default.fromJS([
56
+ {
57
+ path: [...path, 'source', 'abi'],
58
+ message: `\
59
+ ABI name '${abiName}' not found in mapping > abis.
60
+ Available ABIs:
61
+ ${abiNames
62
+ .sort()
63
+ .map((name) => `- ${name}`)
64
+ .join('\n')}`,
65
+ },
66
+ ]);
67
+ // Validate that all ABI files are valid
68
+ const fileErrors = dataSource
69
+ .getIn(['mapping', 'abis'])
70
+ .reduce((errors, abi, abiIndex) => {
71
+ try {
72
+ abi_1.default.load(abi.get('name'), this.resolveFile(abi.get('file')));
73
+ return errors;
74
+ }
75
+ catch (e) {
76
+ return errors.push(immutable_1.default.fromJS({
77
+ path: [...path, 'mapping', 'abis', abiIndex, 'file'],
78
+ message: e.message,
79
+ }));
80
+ }
81
+ }, immutable_1.default.List());
82
+ return nameErrors.concat(fileErrors);
83
+ }
84
+ validateEvents() {
85
+ const dataSourcesAndTemplates = DataSourcesExtractor.fromManifest(this.manifest, this.protocol);
86
+ return dataSourcesAndTemplates.reduce((errors, dataSourceOrTemplate) => {
87
+ return errors.concat(this.validateDataSourceEvents(dataSourceOrTemplate.get('dataSource'), dataSourceOrTemplate.get('path')));
88
+ }, immutable_1.default.List());
89
+ }
90
+ validateDataSourceEvents(dataSource, path) {
91
+ let abi;
92
+ try {
93
+ // Resolve the source ABI name into a real ABI object
94
+ const abiName = dataSource.getIn(['source', 'abi']);
95
+ const abiEntry = dataSource
96
+ .getIn(['mapping', 'abis'])
97
+ .find((abi) => abi.get('name') === abiName);
98
+ abi = abi_1.default.load(abiEntry.get('name'), this.resolveFile(abiEntry.get('file')));
99
+ }
100
+ catch (_) {
101
+ // Ignore errors silently; we can't really say anything about
102
+ // the events if the ABI can't even be loaded
103
+ return immutable_1.default.List();
104
+ }
105
+ // Obtain event signatures from the mapping
106
+ const manifestEvents = dataSource
107
+ .getIn(['mapping', 'eventHandlers'], immutable_1.default.List())
108
+ .map((handler) => handler.get('event'));
109
+ // Obtain event signatures from the ABI
110
+ const abiEvents = abi.eventSignatures();
111
+ // Add errors for every manifest event signature that is not
112
+ // present in the ABI
113
+ return manifestEvents.reduce((errors, manifestEvent, index) => abiEvents.includes(manifestEvent)
114
+ ? errors
115
+ : errors.push(immutable_1.default.fromJS({
116
+ path: [...path, 'eventHandlers', index],
117
+ message: `\
118
+ Event with signature '${manifestEvent}' not present in ABI '${abi.name}'.
119
+ Available events:
120
+ ${abiEvents
121
+ .sort()
122
+ .map(event => `- ${event}`)
123
+ .join('\n')}`,
124
+ })), immutable_1.default.List());
125
+ }
126
+ validateCallFunctions() {
127
+ return this.manifest
128
+ .get('dataSources')
129
+ .filter((dataSource) => this.protocol.isValidKindName(dataSource.get('kind')))
130
+ .reduce((errors, dataSource, dataSourceIndex) => {
131
+ const path = ['dataSources', dataSourceIndex, 'callHandlers'];
132
+ let abi;
133
+ try {
134
+ // Resolve the source ABI name into a real ABI object
135
+ const abiName = dataSource.getIn(['source', 'abi']);
136
+ const abiEntry = dataSource
137
+ .getIn(['mapping', 'abis'])
138
+ .find((abi) => abi.get('name') === abiName);
139
+ abi = abi_1.default.load(abiEntry.get('name'), this.resolveFile(abiEntry.get('file')));
140
+ }
141
+ catch (e) {
142
+ // Ignore errors silently; we can't really say anything about
143
+ // the call functions if the ABI can't even be loaded
144
+ return errors;
145
+ }
146
+ // Obtain event signatures from the mapping
147
+ const manifestFunctions = dataSource
148
+ .getIn(['mapping', 'callHandlers'], immutable_1.default.List())
149
+ .map((handler) => handler.get('function'));
150
+ // Obtain event signatures from the ABI
151
+ const abiFunctions = abi.callFunctionSignatures();
152
+ // Add errors for every manifest event signature that is not
153
+ // present in the ABI
154
+ return manifestFunctions.reduce((errors, manifestFunction, index) => abiFunctions.includes(manifestFunction)
155
+ ? errors
156
+ : errors.push(immutable_1.default.fromJS({
157
+ path: [...path, index],
158
+ message: `\
159
+ Call function with signature '${manifestFunction}' not present in ABI '${abi.name}'.
160
+ Available call functions:
161
+ ${abiFunctions
162
+ .sort()
163
+ .map(tx => `- ${tx}`)
164
+ .join('\n')}`,
165
+ })), errors);
166
+ }, immutable_1.default.List());
167
+ }
168
+ handlerTypes() {
169
+ return immutable_1.default.List(['blockHandlers', 'callHandlers', 'eventHandlers']);
170
+ }
171
+ }
172
+ exports.default = EthereumSubgraph;
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ const fs_extra_1 = __importDefault(require("fs-extra"));
8
+ const immutable_1 = __importDefault(require("immutable"));
9
+ const prettier_1 = __importDefault(require("prettier"));
10
+ const typescript_1 = require("../../codegen/typescript");
11
+ const fs_1 = require("../../command-helpers/fs");
12
+ const spinner_1 = require("../../command-helpers/spinner");
13
+ const abi_1 = __importDefault(require("./abi"));
14
+ class EthereumTypeGenerator {
15
+ sourceDir;
16
+ outputDir;
17
+ constructor(options) {
18
+ this.sourceDir = options.sourceDir;
19
+ this.outputDir = options.outputDir;
20
+ }
21
+ async loadABIs(subgraph) {
22
+ return await (0, spinner_1.withSpinner)('Load contract ABIs', 'Failed to load contract ABIs', `Warnings while loading contract ABIs`, async (spinner) => {
23
+ try {
24
+ return subgraph
25
+ .get('dataSources')
26
+ .reduce((abis, dataSource) => dataSource
27
+ .getIn(['mapping', 'abis'])
28
+ .reduce((abis, abi) => abis.push(this._loadABI(dataSource, abi.get('name'), abi.get('file'), spinner)), abis), immutable_1.default.List());
29
+ }
30
+ catch (e) {
31
+ throw Error(`Failed to load contract ABIs: ${e.message}`);
32
+ }
33
+ });
34
+ }
35
+ _loadABI(dataSource, name, maybeRelativePath, spinner) {
36
+ try {
37
+ if (this.sourceDir) {
38
+ const absolutePath = path_1.default.resolve(this.sourceDir, maybeRelativePath);
39
+ (0, spinner_1.step)(spinner, `Load contract ABI from`, (0, fs_1.displayPath)(absolutePath));
40
+ return { dataSource, abi: abi_1.default.load(name, absolutePath) };
41
+ }
42
+ return { dataSource, abi: abi_1.default.load(name, maybeRelativePath) };
43
+ }
44
+ catch (e) {
45
+ throw Error(`Failed to load contract ABI: ${e.message}`);
46
+ }
47
+ }
48
+ async loadDataSourceTemplateABIs(subgraph) {
49
+ return await (0, spinner_1.withSpinner)(`Load data source template ABIs`, `Failed to load data source template ABIs`, `Warnings while loading data source template ABIs`, async (spinner) => {
50
+ const abis = [];
51
+ for (const template of subgraph.get('templates', immutable_1.default.List())) {
52
+ for (const abi of template.getIn(['mapping', 'abis'])) {
53
+ abis.push(this._loadDataSourceTemplateABI(template, abi.get('name'), abi.get('file'), spinner));
54
+ }
55
+ }
56
+ return abis;
57
+ });
58
+ }
59
+ _loadDataSourceTemplateABI(template, name, maybeRelativePath, spinner) {
60
+ try {
61
+ if (this.sourceDir) {
62
+ const absolutePath = path_1.default.resolve(this.sourceDir, maybeRelativePath);
63
+ (0, spinner_1.step)(spinner, `Load data source template ABI from`, (0, fs_1.displayPath)(absolutePath));
64
+ return { template, abi: abi_1.default.load(name, absolutePath) };
65
+ }
66
+ return { template, abi: abi_1.default.load(name, maybeRelativePath) };
67
+ }
68
+ catch (e) {
69
+ throw Error(`Failed to load data source template ABI: ${e.message}`);
70
+ }
71
+ }
72
+ generateTypesForABIs(abis) {
73
+ return (0, spinner_1.withSpinner)(`Generate types for contract ABIs`, `Failed to generate types for contract ABIs`, `Warnings while generating types for contract ABIs`, async (spinner) => {
74
+ return await Promise.all(abis.map(async (abi) => await this._generateTypesForABI(abi, spinner)));
75
+ });
76
+ }
77
+ async _generateTypesForABI(abi, spinner) {
78
+ try {
79
+ (0, spinner_1.step)(spinner, `Generate types for contract ABI:`, `${abi.abi.name} (${(0, fs_1.displayPath)(abi.abi.file)})`);
80
+ const codeGenerator = abi.abi.codeGenerator();
81
+ const code = prettier_1.default.format([
82
+ typescript_1.GENERATED_FILE_NOTE,
83
+ ...codeGenerator.generateModuleImports(),
84
+ ...codeGenerator.generateTypes(),
85
+ ].join('\n'), {
86
+ parser: 'typescript',
87
+ });
88
+ const outputFile = path_1.default.join(this.outputDir, abi.dataSource.get('name'), `${abi.abi.name}.ts`);
89
+ (0, spinner_1.step)(spinner, `Write types to`, (0, fs_1.displayPath)(outputFile));
90
+ await fs_extra_1.default.mkdirs(path_1.default.dirname(outputFile));
91
+ await fs_extra_1.default.writeFile(outputFile, code);
92
+ }
93
+ catch (e) {
94
+ throw Error(`Failed to generate types for contract ABI: ${e.message}`);
95
+ }
96
+ }
97
+ async generateTypesForDataSourceTemplateABIs(abis) {
98
+ return await (0, spinner_1.withSpinner)(`Generate types for data source template ABIs`, `Failed to generate types for data source template ABIs`, `Warnings while generating types for data source template ABIs`, async (spinner) => {
99
+ return await Promise.all(abis.map(async (abi) => await this._generateTypesForDataSourceTemplateABI(abi, spinner)));
100
+ });
101
+ }
102
+ async _generateTypesForDataSourceTemplateABI(abi, spinner) {
103
+ try {
104
+ (0, spinner_1.step)(spinner, `Generate types for data source template ABI:`, `${abi.template.get('name')} > ${abi.abi.name} (${(0, fs_1.displayPath)(abi.abi.file)})`);
105
+ const codeGenerator = abi.abi.codeGenerator();
106
+ const code = prettier_1.default.format([
107
+ typescript_1.GENERATED_FILE_NOTE,
108
+ ...codeGenerator.generateModuleImports(),
109
+ ...codeGenerator.generateTypes(),
110
+ ].join('\n'), {
111
+ parser: 'typescript',
112
+ });
113
+ const outputFile = path_1.default.join(this.outputDir, 'templates', abi.template.get('name'), `${abi.abi.name}.ts`);
114
+ (0, spinner_1.step)(spinner, `Write types to`, (0, fs_1.displayPath)(outputFile));
115
+ await fs_extra_1.default.mkdirs(path_1.default.dirname(outputFile));
116
+ await fs_extra_1.default.writeFile(outputFile, code);
117
+ }
118
+ catch (e) {
119
+ throw Error(`Failed to generate types for data source template ABI: ${e.message}`);
120
+ }
121
+ }
122
+ }
123
+ exports.default = EthereumTypeGenerator;
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const immutable_1 = __importDefault(require("immutable"));
30
+ const debug_1 = __importDefault(require("../debug"));
31
+ const ArweaveManifestScaffold = __importStar(require("./arweave/scaffold/manifest"));
32
+ const ArweaveMappingScaffold = __importStar(require("./arweave/scaffold/mapping"));
33
+ const subgraph_1 = __importDefault(require("./arweave/subgraph"));
34
+ const CosmosManifestScaffold = __importStar(require("./cosmos/scaffold/manifest"));
35
+ const CosmosMappingScaffold = __importStar(require("./cosmos/scaffold/mapping"));
36
+ const subgraph_2 = __importDefault(require("./cosmos/subgraph"));
37
+ const abi_1 = __importDefault(require("./ethereum/abi"));
38
+ const template_1 = __importDefault(require("./ethereum/codegen/template"));
39
+ const contract_1 = __importDefault(require("./ethereum/contract"));
40
+ const EthereumManifestScaffold = __importStar(require("./ethereum/scaffold/manifest"));
41
+ const EthereumMappingScaffold = __importStar(require("./ethereum/scaffold/mapping"));
42
+ const subgraph_3 = __importDefault(require("./ethereum/subgraph"));
43
+ const type_generator_1 = __importDefault(require("./ethereum/type-generator"));
44
+ const contract_2 = __importDefault(require("./near/contract"));
45
+ const NearManifestScaffold = __importStar(require("./near/scaffold/manifest"));
46
+ const NearMappingScaffold = __importStar(require("./near/scaffold/mapping"));
47
+ const subgraph_4 = __importDefault(require("./near/subgraph"));
48
+ const SubstreamsManifestScaffold = __importStar(require("./substreams/scaffold/manifest"));
49
+ const subgraph_5 = __importDefault(require("./substreams/subgraph"));
50
+ const protocolDebug = (0, debug_1.default)('graph-cli:protocol');
51
+ class Protocol {
52
+ static fromDataSources(dataSourcesAndTemplates) {
53
+ const firstDataSourceKind = dataSourcesAndTemplates[0].kind;
54
+ return new Protocol(firstDataSourceKind);
55
+ }
56
+ name;
57
+ // TODO: should assert non null? see the constructor switch default case comment
58
+ config;
59
+ constructor(name) {
60
+ this.name = Protocol.normalizeName(name);
61
+ switch (this.name) {
62
+ case 'arweave':
63
+ this.config = arweaveProtocol;
64
+ break;
65
+ case 'cosmos':
66
+ this.config = cosmosProtocol;
67
+ break;
68
+ case 'ethereum':
69
+ this.config = ethereumProtocol;
70
+ break;
71
+ case 'near':
72
+ this.config = nearProtocol;
73
+ break;
74
+ case 'substreams':
75
+ this.config = substreamsProtocol;
76
+ break;
77
+ default:
78
+ // Do not throw when undefined, a better error message is printed after the constructor
79
+ // when validating the Subgraph itself
80
+ }
81
+ }
82
+ static availableProtocols() {
83
+ return immutable_1.default.fromJS({
84
+ // `ethereum/contract` is kept for backwards compatibility.
85
+ // New networks (or protocol perhaps) shouldn't have the `/contract` anymore (unless a new case makes use of it).
86
+ arweave: ['arweave'],
87
+ ethereum: ['ethereum', 'ethereum/contract'],
88
+ near: ['near'],
89
+ cosmos: ['cosmos'],
90
+ substreams: ['substreams'],
91
+ });
92
+ }
93
+ static availableNetworks() {
94
+ let networks = immutable_1.default.fromJS({
95
+ arweave: ['arweave-mainnet'],
96
+ ethereum: [
97
+ 'mainnet',
98
+ 'rinkeby',
99
+ 'goerli',
100
+ 'poa-core',
101
+ 'poa-sokol',
102
+ 'gnosis',
103
+ 'matic',
104
+ 'mumbai',
105
+ 'fantom',
106
+ 'fantom-testnet',
107
+ 'bsc',
108
+ 'chapel',
109
+ 'clover',
110
+ 'avalanche',
111
+ 'fuji',
112
+ 'celo',
113
+ 'celo-alfajores',
114
+ 'fuse',
115
+ 'moonbeam',
116
+ 'moonriver',
117
+ 'mbase',
118
+ 'arbitrum-one',
119
+ 'arbitrum-rinkeby',
120
+ 'optimism',
121
+ 'optimism-kovan',
122
+ 'aurora',
123
+ 'aurora-testnet',
124
+ ],
125
+ near: ['near-mainnet', 'near-testnet'],
126
+ cosmos: [
127
+ 'cosmoshub-4',
128
+ 'theta-testnet-001',
129
+ 'osmosis-1',
130
+ 'osmo-test-4',
131
+ 'juno-1',
132
+ 'uni-3', // Juno testnet
133
+ ],
134
+ });
135
+ const allNetworks = [];
136
+ networks.forEach(value => {
137
+ allNetworks.push(...value);
138
+ });
139
+ networks = networks.set('substreams', allNetworks);
140
+ return networks;
141
+ }
142
+ static normalizeName(name) {
143
+ return Protocol.availableProtocols().findKey(possibleNames => {
144
+ return possibleNames.includes(name);
145
+ });
146
+ }
147
+ displayName() {
148
+ return this.config?.displayName;
149
+ }
150
+ // Receives a data source kind, and checks if it's valid
151
+ // for the given protocol instance (this).
152
+ isValidKindName(kind) {
153
+ return Protocol.availableProtocols().get(this.name, immutable_1.default.List()).includes(kind);
154
+ }
155
+ hasABIs() {
156
+ return this.config.abi != null;
157
+ }
158
+ hasContract() {
159
+ return this.config.contract != null;
160
+ }
161
+ hasEvents() {
162
+ // A problem with hasEvents usage in the codebase is that it's almost every where
163
+ // where used, the ABI data is actually use after the conditional, so it seems
164
+ // both concept are related. So internally, we map to this condition.
165
+ return this.hasABIs();
166
+ }
167
+ hasTemplates() {
168
+ return this.config.getTemplateCodeGen != null;
169
+ }
170
+ hasDataSourceMappingFile() {
171
+ return this.getMappingScaffold() != null;
172
+ }
173
+ getTypeGenerator(options) {
174
+ if (this.config == null || this.config.getTypeGenerator == null) {
175
+ return null;
176
+ }
177
+ return this.config.getTypeGenerator(options);
178
+ }
179
+ getTemplateCodeGen(template) {
180
+ if (!this.hasTemplates()) {
181
+ throw new Error(`Template data sources with kind '${this.name}' are not supported yet`);
182
+ }
183
+ return this.config.getTemplateCodeGen?.(template);
184
+ }
185
+ getABI() {
186
+ return this.config.abi;
187
+ }
188
+ getSubgraph(options) {
189
+ return this.config.getSubgraph({ ...options, protocol: this });
190
+ }
191
+ getContract() {
192
+ return this.config.contract;
193
+ }
194
+ getManifestScaffold() {
195
+ return this.config.manifestScaffold;
196
+ }
197
+ getMappingScaffold() {
198
+ return this.config.mappingScaffold;
199
+ }
200
+ }
201
+ exports.default = Protocol;
202
+ const arweaveProtocol = {
203
+ displayName: 'Arweave',
204
+ abi: undefined,
205
+ contract: undefined,
206
+ getTemplateCodeGen: undefined,
207
+ getTypeGenerator: undefined,
208
+ getSubgraph(options) {
209
+ return new subgraph_1.default(options);
210
+ },
211
+ manifestScaffold: ArweaveManifestScaffold,
212
+ mappingScaffold: ArweaveMappingScaffold,
213
+ };
214
+ const cosmosProtocol = {
215
+ displayName: 'Cosmos',
216
+ abi: undefined,
217
+ contract: undefined,
218
+ getTemplateCodeGen: undefined,
219
+ getTypeGenerator: undefined,
220
+ getSubgraph(options) {
221
+ return new subgraph_2.default(options);
222
+ },
223
+ manifestScaffold: CosmosManifestScaffold,
224
+ mappingScaffold: CosmosMappingScaffold,
225
+ };
226
+ const ethereumProtocol = {
227
+ displayName: 'Ethereum',
228
+ abi: abi_1.default,
229
+ contract: contract_1.default,
230
+ getTemplateCodeGen(template) {
231
+ return new template_1.default(template);
232
+ },
233
+ getTypeGenerator(options) {
234
+ return new type_generator_1.default(options);
235
+ },
236
+ getSubgraph(options) {
237
+ return new subgraph_3.default(options);
238
+ },
239
+ manifestScaffold: EthereumManifestScaffold,
240
+ mappingScaffold: EthereumMappingScaffold,
241
+ };
242
+ const nearProtocol = {
243
+ displayName: 'NEAR',
244
+ abi: undefined,
245
+ contract: contract_2.default,
246
+ getTypeGenerator: undefined,
247
+ getTemplateCodeGen: undefined,
248
+ getSubgraph(options) {
249
+ return new subgraph_4.default(options);
250
+ },
251
+ manifestScaffold: NearManifestScaffold,
252
+ mappingScaffold: NearMappingScaffold,
253
+ };
254
+ const substreamsProtocol = {
255
+ displayName: 'Substreams',
256
+ abi: undefined,
257
+ contract: undefined,
258
+ getTypeGenerator: undefined,
259
+ getTemplateCodeGen: undefined,
260
+ getSubgraph(options) {
261
+ return new subgraph_5.default(options);
262
+ },
263
+ manifestScaffold: SubstreamsManifestScaffold,
264
+ mappingScaffold: undefined,
265
+ };
266
+ protocolDebug('Available networks %M', Protocol.availableNetworks());
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ /* eslint-disable */
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || function (mod) {
20
+ if (mod && mod.__esModule) return mod;
21
+ var result = {};
22
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
23
+ __setModuleDefault(result, mod);
24
+ return result;
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ const tsCodegen = __importStar(require("../../../codegen/typescript"));
28
+ class IpfsFileTemplateCodeGen {
29
+ template;
30
+ constructor(template) {
31
+ this.template = template;
32
+ this.template = template;
33
+ }
34
+ generateModuleImports() {
35
+ return [];
36
+ }
37
+ generateCreateMethod() {
38
+ const name = this.template.get('name');
39
+ return tsCodegen.staticMethod('create', [tsCodegen.param('cid', tsCodegen.namedType('string'))], tsCodegen.namedType('void'), `
40
+ DataSourceTemplate.create('${name}', [cid])
41
+ `);
42
+ }
43
+ generateCreateWithContextMethod() {
44
+ const name = this.template.get('name');
45
+ return tsCodegen.staticMethod('createWithContext', [
46
+ tsCodegen.param('cid', tsCodegen.namedType('string')),
47
+ tsCodegen.param('context', tsCodegen.namedType('DataSourceContext')),
48
+ ], tsCodegen.namedType('void'), `
49
+ DataSourceTemplate.createWithContext('${name}', [cid], context)
50
+ `);
51
+ }
52
+ }
53
+ exports.default = IpfsFileTemplateCodeGen;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MINIMUM_ACCOUNT_ID_LENGTH = 2;
4
+ const MAXIMUM_ACCOUNT_ID_LENGTH = 64;
5
+ const RULES_URL = 'https://docs.near.org/docs/concepts/account#account-id-rules';
6
+ class NearContract {
7
+ account;
8
+ static identifierName() {
9
+ return 'account';
10
+ }
11
+ constructor(account) {
12
+ this.account = account;
13
+ this.account = account;
14
+ }
15
+ validateLength(value) {
16
+ return value.length >= MINIMUM_ACCOUNT_ID_LENGTH && value.length <= MAXIMUM_ACCOUNT_ID_LENGTH;
17
+ }
18
+ validateFormat(value) {
19
+ const pattern = /^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$/;
20
+ return pattern.test(value);
21
+ }
22
+ validate() {
23
+ if (!this.validateLength(this.account)) {
24
+ return {
25
+ valid: false,
26
+ error: `Account must be between '${MINIMUM_ACCOUNT_ID_LENGTH}' and '${MAXIMUM_ACCOUNT_ID_LENGTH}' characters, see ${RULES_URL}`,
27
+ };
28
+ }
29
+ if (!this.validateFormat(this.account)) {
30
+ return {
31
+ valid: false,
32
+ error: `Account must conform to the rules on ${RULES_URL}`,
33
+ };
34
+ }
35
+ return {
36
+ valid: true,
37
+ error: null,
38
+ };
39
+ }
40
+ }
41
+ exports.default = NearContract;