@graphprotocol/graph-cli 0.37.2 → 0.37.3-alpha-20230110163550-d64bef6

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 (206) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/babel.config.js +3 -0
  3. package/dist/bin.js +6 -0
  4. package/dist/cli.js +32 -0
  5. package/dist/codegen/schema.js +250 -0
  6. package/dist/codegen/schema.test.js +458 -0
  7. package/dist/codegen/template.js +65 -0
  8. package/dist/codegen/types/conversions.js +359 -0
  9. package/dist/codegen/types/index.js +71 -0
  10. package/dist/codegen/types/index.test.js +563 -0
  11. package/dist/codegen/typescript.js +200 -0
  12. package/dist/codegen/util.js +46 -0
  13. package/dist/codegen/util.test.js +93 -0
  14. package/dist/command-helpers/abi.js +78 -0
  15. package/dist/command-helpers/auth.js +77 -0
  16. package/dist/command-helpers/compiler.js +66 -0
  17. package/dist/command-helpers/data-sources.js +29 -0
  18. package/dist/command-helpers/fs.js +9 -0
  19. package/dist/command-helpers/gluegun.js +49 -0
  20. package/dist/command-helpers/ipfs.js +5 -0
  21. package/dist/command-helpers/jsonrpc.js +29 -0
  22. package/dist/command-helpers/network.js +93 -0
  23. package/dist/command-helpers/network.test.js +86 -0
  24. package/dist/command-helpers/node.js +40 -0
  25. package/dist/command-helpers/scaffold.js +112 -0
  26. package/dist/command-helpers/spinner.js +84 -0
  27. package/dist/command-helpers/studio.js +13 -0
  28. package/dist/command-helpers/studio.test.js +41 -0
  29. package/dist/command-helpers/subgraph.js +24 -0
  30. package/dist/command-helpers/version.js +74 -0
  31. package/dist/command-helpers/version.test.js +179 -0
  32. package/dist/commands/add.js +190 -0
  33. package/dist/commands/auth.js +127 -0
  34. package/dist/commands/build.js +140 -0
  35. package/dist/commands/codegen.js +135 -0
  36. package/dist/commands/create.js +86 -0
  37. package/dist/commands/deploy.js +334 -0
  38. package/dist/commands/init.js +788 -0
  39. package/dist/commands/local.js +387 -0
  40. package/dist/commands/remove.js +86 -0
  41. package/dist/commands/test.js +295 -0
  42. package/dist/compiler/asc.js +83 -0
  43. package/dist/compiler/index.js +474 -0
  44. package/dist/debug.js +16 -0
  45. package/dist/migrations/mapping_api_version_0_0_1.js +78 -0
  46. package/dist/migrations/mapping_api_version_0_0_2.js +76 -0
  47. package/dist/migrations/mapping_api_version_0_0_3.js +78 -0
  48. package/dist/migrations/mapping_api_version_0_0_4.js +80 -0
  49. package/dist/migrations/mapping_api_version_0_0_5.js +80 -0
  50. package/dist/migrations/spec_version_0_0_2.js +49 -0
  51. package/dist/migrations/spec_version_0_0_3.js +54 -0
  52. package/dist/migrations/util/load-manifest.js +18 -0
  53. package/dist/migrations/util/versions.js +29 -0
  54. package/dist/migrations.js +56 -0
  55. package/dist/protocols/arweave/manifest.graphql +57 -0
  56. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  57. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  58. package/dist/protocols/arweave/subgraph.js +23 -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 +25 -0
  63. package/dist/protocols/ethereum/abi.js +140 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +451 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +328 -0
  66. package/dist/protocols/ethereum/codegen/template.js +50 -0
  67. package/dist/protocols/ethereum/contract.js +20 -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 +171 -0
  72. package/dist/protocols/ethereum/type-generator.js +125 -0
  73. package/dist/protocols/index.js +268 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +50 -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 +20 -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 +83 -0
  85. package/dist/scaffold/ethereum.test.js +505 -0
  86. package/dist/scaffold/index.js +128 -0
  87. package/dist/scaffold/mapping.js +61 -0
  88. package/dist/scaffold/near.test.js +86 -0
  89. package/dist/scaffold/schema.js +83 -0
  90. package/dist/scaffold/tests.js +175 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +249 -0
  93. package/dist/type-generator.js +224 -0
  94. package/dist/validation/contract.js +46 -0
  95. package/dist/validation/index.js +8 -0
  96. package/dist/validation/manifest.js +271 -0
  97. package/dist/validation/schema.js +796 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/jest.config.js +1 -1
  101. package/package.json +16 -9
  102. package/{bin/graph → src/bin.ts} +2 -1
  103. package/src/cli.ts +36 -0
  104. package/src/codegen/schema.js +23 -22
  105. package/src/codegen/schema.test.js +33 -27
  106. package/src/codegen/{template.js → template.ts} +8 -6
  107. package/src/codegen/types/conversions.ts +382 -0
  108. package/src/codegen/types/index.js +4 -7
  109. package/src/codegen/types/index.test.js +10 -4
  110. package/src/codegen/{typescript.js → typescript.ts} +44 -26
  111. package/src/codegen/util.js +1 -1
  112. package/src/codegen/util.test.js +5 -2
  113. package/src/command-helpers/abi.js +46 -30
  114. package/src/command-helpers/auth.js +6 -9
  115. package/src/command-helpers/compiler.js +4 -6
  116. package/src/command-helpers/data-sources.js +8 -11
  117. package/src/command-helpers/fs.ts +5 -0
  118. package/src/command-helpers/gluegun.js +2 -4
  119. package/src/command-helpers/ipfs.ts +3 -0
  120. package/src/command-helpers/{jsonrpc.js → jsonrpc.ts} +11 -16
  121. package/src/command-helpers/network.js +58 -44
  122. package/src/command-helpers/network.test.js +23 -19
  123. package/src/command-helpers/node.js +3 -7
  124. package/src/command-helpers/scaffold.js +34 -25
  125. package/src/command-helpers/{spinner.js → spinner.ts} +10 -10
  126. package/src/command-helpers/studio.test.js +61 -30
  127. package/src/command-helpers/studio.ts +20 -0
  128. package/src/command-helpers/{subgraph.js → subgraph.ts} +6 -6
  129. package/src/command-helpers/version.js +11 -16
  130. package/src/command-helpers/version.test.js +111 -82
  131. package/src/commands/add.js +49 -35
  132. package/src/commands/auth.js +13 -25
  133. package/src/commands/build.js +9 -9
  134. package/src/commands/codegen.js +12 -12
  135. package/src/commands/create.js +22 -22
  136. package/src/commands/deploy.js +39 -34
  137. package/src/commands/init.js +21 -24
  138. package/src/commands/local.js +15 -14
  139. package/src/commands/remove.js +22 -22
  140. package/src/commands/test.js +86 -70
  141. package/src/compiler/{asc.js → asc.ts} +29 -16
  142. package/src/compiler/index.js +18 -18
  143. package/src/{debug.js → debug.ts} +2 -2
  144. package/src/migrations/mapping_api_version_0_0_1.js +7 -7
  145. package/src/migrations/mapping_api_version_0_0_2.js +5 -5
  146. package/src/migrations/mapping_api_version_0_0_3.js +7 -7
  147. package/src/migrations/mapping_api_version_0_0_4.js +7 -7
  148. package/src/migrations/mapping_api_version_0_0_5.js +7 -7
  149. package/src/migrations/spec_version_0_0_2.js +5 -5
  150. package/src/migrations/spec_version_0_0_3.js +5 -5
  151. package/src/migrations/util/load-manifest.js +6 -9
  152. package/src/migrations/util/versions.js +5 -10
  153. package/src/{migrations.js → migrations.ts} +14 -15
  154. package/src/protocols/arweave/scaffold/manifest.js +1 -4
  155. package/src/protocols/arweave/scaffold/mapping.js +1 -3
  156. package/src/protocols/arweave/subgraph.ts +22 -0
  157. package/src/protocols/cosmos/scaffold/manifest.js +1 -4
  158. package/src/protocols/cosmos/scaffold/mapping.js +1 -3
  159. package/src/protocols/cosmos/subgraph.js +2 -2
  160. package/src/protocols/ethereum/abi.js +7 -13
  161. package/src/protocols/ethereum/codegen/abi.js +219 -222
  162. package/src/protocols/ethereum/codegen/abi.test.js +6 -6
  163. package/src/protocols/ethereum/codegen/template.js +3 -5
  164. package/src/protocols/ethereum/contract.js +3 -2
  165. package/src/protocols/ethereum/scaffold/manifest.js +4 -7
  166. package/src/protocols/ethereum/scaffold/mapping.js +4 -11
  167. package/src/protocols/ethereum/subgraph.js +21 -20
  168. package/src/protocols/ethereum/type-generator.js +24 -30
  169. package/src/protocols/{index.js → index.ts} +65 -46
  170. package/src/protocols/ipfs/codegen/file_template.js +2 -2
  171. package/src/protocols/near/{contract.js → contract.ts} +16 -12
  172. package/src/protocols/near/scaffold/manifest.js +2 -5
  173. package/src/protocols/near/scaffold/mapping.js +1 -3
  174. package/src/protocols/near/subgraph.js +3 -6
  175. package/src/protocols/subgraph.ts +12 -0
  176. package/src/protocols/substreams/scaffold/{manifest.js → manifest.ts} +2 -7
  177. package/src/protocols/substreams/subgraph.ts +22 -0
  178. package/src/scaffold/cosmos.test.js +3 -3
  179. package/src/scaffold/ethereum.test.js +4 -4
  180. package/src/scaffold/index.js +7 -7
  181. package/src/scaffold/mapping.js +3 -3
  182. package/src/scaffold/near.test.js +3 -3
  183. package/src/scaffold/schema.js +4 -4
  184. package/src/scaffold/tests.js +72 -58
  185. package/src/schema.ts +26 -0
  186. package/src/{subgraph.js → subgraph.ts} +80 -60
  187. package/src/type-generator.js +20 -20
  188. package/src/validation/contract.js +2 -6
  189. package/src/validation/index.js +1 -1
  190. package/src/validation/manifest.js +24 -22
  191. package/src/validation/schema.test.js +1 -1
  192. package/src/validation/{schema.js → schema.ts} +268 -221
  193. package/src/{watcher.js → watcher.ts} +23 -12
  194. package/tests/cli/globalSetup.js +2 -2
  195. package/tests/cli/globalTeardown.js +2 -2
  196. package/tests/cli/init.test.js +2 -2
  197. package/tests/cli/util.js +7 -13
  198. package/tsconfig.json +18 -0
  199. package/src/cli.js +0 -38
  200. package/src/codegen/types/conversions.js +0 -329
  201. package/src/command-helpers/fs.js +0 -8
  202. package/src/command-helpers/ipfs.js +0 -6
  203. package/src/command-helpers/studio.js +0 -15
  204. package/src/protocols/arweave/subgraph.js +0 -20
  205. package/src/protocols/substreams/subgraph.js +0 -17
  206. package/src/schema.js +0 -23
@@ -0,0 +1,171 @@
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 abi_1 = __importDefault(require("./abi"));
31
+ const DataSourcesExtractor = __importStar(require("../../command-helpers/data-sources"));
32
+ class EthereumSubgraph {
33
+ constructor(options = {}) {
34
+ this.manifest = options.manifest;
35
+ this.resolveFile = options.resolveFile;
36
+ this.protocol = options.protocol;
37
+ }
38
+ validateManifest() {
39
+ return this.validateAbis()
40
+ .concat(this.validateEvents())
41
+ .concat(this.validateCallFunctions());
42
+ }
43
+ validateAbis() {
44
+ const dataSourcesAndTemplates = DataSourcesExtractor.fromManifest(this.manifest, this.protocol);
45
+ return dataSourcesAndTemplates.reduce((errors, dataSourceOrTemplate) => errors.concat(this.validateDataSourceAbis(dataSourceOrTemplate.get('dataSource'), dataSourceOrTemplate.get('path'))), immutable_1.default.List());
46
+ }
47
+ validateDataSourceAbis(dataSource, path) {
48
+ // Validate that the the "source > abi" reference of all data sources
49
+ // points to an existing ABI in the data source ABIs
50
+ let abiName = dataSource.getIn(['source', 'abi']);
51
+ let abiNames = dataSource.getIn(['mapping', 'abis']).map(abi => abi.get('name'));
52
+ let nameErrors = abiNames.includes(abiName)
53
+ ? immutable_1.default.List()
54
+ : immutable_1.default.fromJS([
55
+ {
56
+ path: [...path, 'source', 'abi'],
57
+ message: `\
58
+ ABI name '${abiName}' not found in mapping > abis.
59
+ Available ABIs:
60
+ ${abiNames
61
+ .sort()
62
+ .map(name => `- ${name}`)
63
+ .join('\n')}`,
64
+ },
65
+ ]);
66
+ // Validate that all ABI files are valid
67
+ let fileErrors = dataSource
68
+ .getIn(['mapping', 'abis'])
69
+ .reduce((errors, abi, abiIndex) => {
70
+ try {
71
+ abi_1.default.load(abi.get('name'), this.resolveFile(abi.get('file')));
72
+ return errors;
73
+ }
74
+ catch (e) {
75
+ return errors.push(immutable_1.default.fromJS({
76
+ path: [...path, 'mapping', 'abis', abiIndex, 'file'],
77
+ message: e.message,
78
+ }));
79
+ }
80
+ }, immutable_1.default.List());
81
+ return nameErrors.concat(fileErrors);
82
+ }
83
+ validateEvents() {
84
+ const dataSourcesAndTemplates = DataSourcesExtractor.fromManifest(this.manifest, this.protocol);
85
+ return dataSourcesAndTemplates.reduce((errors, dataSourceOrTemplate) => {
86
+ return errors.concat(this.validateDataSourceEvents(dataSourceOrTemplate.get('dataSource'), dataSourceOrTemplate.get('path')));
87
+ }, immutable_1.default.List());
88
+ }
89
+ validateDataSourceEvents(dataSource, path) {
90
+ let abi;
91
+ try {
92
+ // Resolve the source ABI name into a real ABI object
93
+ let abiName = dataSource.getIn(['source', 'abi']);
94
+ let abiEntry = dataSource
95
+ .getIn(['mapping', 'abis'])
96
+ .find(abi => abi.get('name') === abiName);
97
+ abi = abi_1.default.load(abiEntry.get('name'), this.resolveFile(abiEntry.get('file')));
98
+ }
99
+ catch (_) {
100
+ // Ignore errors silently; we can't really say anything about
101
+ // the events if the ABI can't even be loaded
102
+ return immutable_1.default.List();
103
+ }
104
+ // Obtain event signatures from the mapping
105
+ let manifestEvents = dataSource
106
+ .getIn(['mapping', 'eventHandlers'], immutable_1.default.List())
107
+ .map(handler => handler.get('event'));
108
+ // Obtain event signatures from the ABI
109
+ let abiEvents = abi.eventSignatures();
110
+ // Add errors for every manifest event signature that is not
111
+ // present in the ABI
112
+ return manifestEvents.reduce((errors, manifestEvent, index) => abiEvents.includes(manifestEvent)
113
+ ? errors
114
+ : errors.push(immutable_1.default.fromJS({
115
+ path: [...path, 'eventHandlers', index],
116
+ message: `\
117
+ Event with signature '${manifestEvent}' not present in ABI '${abi.name}'.
118
+ Available events:
119
+ ${abiEvents
120
+ .sort()
121
+ .map(event => `- ${event}`)
122
+ .join('\n')}`,
123
+ })), immutable_1.default.List());
124
+ }
125
+ validateCallFunctions() {
126
+ return this.manifest
127
+ .get('dataSources')
128
+ .filter(dataSource => this.protocol.isValidKindName(dataSource.get('kind')))
129
+ .reduce((errors, dataSource, dataSourceIndex) => {
130
+ let path = ['dataSources', dataSourceIndex, 'callHandlers'];
131
+ let abi;
132
+ try {
133
+ // Resolve the source ABI name into a real ABI object
134
+ let abiName = dataSource.getIn(['source', 'abi']);
135
+ let abiEntry = dataSource
136
+ .getIn(['mapping', 'abis'])
137
+ .find(abi => abi.get('name') === abiName);
138
+ abi = abi_1.default.load(abiEntry.get('name'), this.resolveFile(abiEntry.get('file')));
139
+ }
140
+ catch (e) {
141
+ // Ignore errors silently; we can't really say anything about
142
+ // the call functions if the ABI can't even be loaded
143
+ return errors;
144
+ }
145
+ // Obtain event signatures from the mapping
146
+ let manifestFunctions = dataSource
147
+ .getIn(['mapping', 'callHandlers'], immutable_1.default.List())
148
+ .map(handler => handler.get('function'));
149
+ // Obtain event signatures from the ABI
150
+ let abiFunctions = abi.callFunctionSignatures();
151
+ // Add errors for every manifest event signature that is not
152
+ // present in the ABI
153
+ return manifestFunctions.reduce((errors, manifestFunction, index) => abiFunctions.includes(manifestFunction)
154
+ ? errors
155
+ : errors.push(immutable_1.default.fromJS({
156
+ path: [...path, index],
157
+ message: `\
158
+ Call function with signature '${manifestFunction}' not present in ABI '${abi.name}'.
159
+ Available call functions:
160
+ ${abiFunctions
161
+ .sort()
162
+ .map(tx => `- ${tx}`)
163
+ .join('\n')}`,
164
+ })), errors);
165
+ }, immutable_1.default.List());
166
+ }
167
+ handlerTypes() {
168
+ return immutable_1.default.List(['blockHandlers', 'callHandlers', 'eventHandlers']);
169
+ }
170
+ }
171
+ exports.default = EthereumSubgraph;
@@ -0,0 +1,125 @@
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 fs_extra_1 = __importDefault(require("fs-extra"));
7
+ const path_1 = __importDefault(require("path"));
8
+ const immutable_1 = __importDefault(require("immutable"));
9
+ const prettier_1 = __importDefault(require("prettier"));
10
+ const abi_1 = __importDefault(require("./abi"));
11
+ const spinner_1 = require("../../command-helpers/spinner");
12
+ const typescript_1 = require("../../codegen/typescript");
13
+ const fs_1 = require("../../command-helpers/fs");
14
+ class EthereumTypeGenerator {
15
+ constructor(options = {}) {
16
+ this.sourceDir = options.sourceDir;
17
+ this.outputDir = options.outputDir;
18
+ }
19
+ async loadABIs(subgraph) {
20
+ return await (0, spinner_1.withSpinner)('Load contract ABIs', 'Failed to load contract ABIs', `Warnings while loading contract ABIs`, async (spinner) => {
21
+ try {
22
+ return subgraph
23
+ .get('dataSources')
24
+ .reduce((abis, dataSource) => dataSource
25
+ .getIn(['mapping', 'abis'])
26
+ .reduce((abis, abi) => abis.push(this._loadABI(dataSource, abi.get('name'), abi.get('file'), spinner)), abis), immutable_1.default.List());
27
+ }
28
+ catch (e) {
29
+ throw Error(`Failed to load contract ABIs: ${e.message}`);
30
+ }
31
+ });
32
+ }
33
+ _loadABI(dataSource, name, maybeRelativePath, spinner) {
34
+ try {
35
+ if (this.sourceDir) {
36
+ let absolutePath = path_1.default.resolve(this.sourceDir, maybeRelativePath);
37
+ (0, spinner_1.step)(spinner, `Load contract ABI from`, (0, fs_1.displayPath)(absolutePath));
38
+ return { dataSource: dataSource, abi: abi_1.default.load(name, absolutePath) };
39
+ }
40
+ else {
41
+ return { dataSource: dataSource, abi: abi_1.default.load(name, maybeRelativePath) };
42
+ }
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
+ let abis = [];
51
+ for (let template of subgraph.get('templates', immutable_1.default.List())) {
52
+ for (let 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
+ let 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
+ else {
67
+ return { template, abi: abi_1.default.load(name, maybeRelativePath) };
68
+ }
69
+ }
70
+ catch (e) {
71
+ throw Error(`Failed to load data source template ABI: ${e.message}`);
72
+ }
73
+ }
74
+ generateTypesForABIs(abis) {
75
+ 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) => {
76
+ return await Promise.all(abis.map(async (abi, name) => await this._generateTypesForABI(abi, spinner)));
77
+ });
78
+ }
79
+ async _generateTypesForABI(abi, spinner) {
80
+ try {
81
+ (0, spinner_1.step)(spinner, `Generate types for contract ABI:`, `${abi.abi.name} (${(0, fs_1.displayPath)(abi.abi.file)})`);
82
+ let codeGenerator = abi.abi.codeGenerator();
83
+ let code = prettier_1.default.format([
84
+ typescript_1.GENERATED_FILE_NOTE,
85
+ ...codeGenerator.generateModuleImports(),
86
+ ...codeGenerator.generateTypes(),
87
+ ].join('\n'), {
88
+ parser: 'typescript',
89
+ });
90
+ let outputFile = path_1.default.join(this.outputDir, abi.dataSource.get('name'), `${abi.abi.name}.ts`);
91
+ (0, spinner_1.step)(spinner, `Write types to`, (0, fs_1.displayPath)(outputFile));
92
+ await fs_extra_1.default.mkdirs(path_1.default.dirname(outputFile));
93
+ await fs_extra_1.default.writeFile(outputFile, code);
94
+ }
95
+ catch (e) {
96
+ throw Error(`Failed to generate types for contract ABI: ${e.message}`);
97
+ }
98
+ }
99
+ async generateTypesForDataSourceTemplateABIs(abis) {
100
+ 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) => {
101
+ return await Promise.all(abis.map(async (abi, name) => await this._generateTypesForDataSourceTemplateABI(abi, spinner)));
102
+ });
103
+ }
104
+ async _generateTypesForDataSourceTemplateABI(abi, spinner) {
105
+ try {
106
+ (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)})`);
107
+ let codeGenerator = abi.abi.codeGenerator();
108
+ let code = prettier_1.default.format([
109
+ typescript_1.GENERATED_FILE_NOTE,
110
+ ...codeGenerator.generateModuleImports(),
111
+ ...codeGenerator.generateTypes(),
112
+ ].join('\n'), {
113
+ parser: 'typescript',
114
+ });
115
+ let outputFile = path_1.default.join(this.outputDir, 'templates', abi.template.get('name'), `${abi.abi.name}.ts`);
116
+ (0, spinner_1.step)(spinner, `Write types to`, (0, fs_1.displayPath)(outputFile));
117
+ await fs_extra_1.default.mkdirs(path_1.default.dirname(outputFile));
118
+ await fs_extra_1.default.writeFile(outputFile, code);
119
+ }
120
+ catch (e) {
121
+ throw Error(`Failed to generate types for data source template ABI: ${e.message}`);
122
+ }
123
+ }
124
+ }
125
+ exports.default = EthereumTypeGenerator;
@@ -0,0 +1,268 @@
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 subgraph_1 = __importDefault(require("./arweave/subgraph"));
31
+ const type_generator_1 = __importDefault(require("./ethereum/type-generator"));
32
+ const template_1 = __importDefault(require("./ethereum/codegen/template"));
33
+ const abi_1 = __importDefault(require("./ethereum/abi"));
34
+ const subgraph_2 = __importDefault(require("./ethereum/subgraph"));
35
+ const subgraph_3 = __importDefault(require("./near/subgraph"));
36
+ const subgraph_4 = __importDefault(require("./cosmos/subgraph"));
37
+ const subgraph_5 = __importDefault(require("./substreams/subgraph"));
38
+ const contract_1 = __importDefault(require("./ethereum/contract"));
39
+ const contract_2 = __importDefault(require("./near/contract"));
40
+ const ArweaveManifestScaffold = __importStar(require("./arweave/scaffold/manifest"));
41
+ const EthereumManifestScaffold = __importStar(require("./ethereum/scaffold/manifest"));
42
+ const NearManifestScaffold = __importStar(require("./near/scaffold/manifest"));
43
+ const CosmosManifestScaffold = __importStar(require("./cosmos/scaffold/manifest"));
44
+ const SubstreamsManifestScaffold = __importStar(require("./substreams/scaffold/manifest"));
45
+ const ArweaveMappingScaffold = __importStar(require("./arweave/scaffold/mapping"));
46
+ const EthereumMappingScaffold = __importStar(require("./ethereum/scaffold/mapping"));
47
+ const NearMappingScaffold = __importStar(require("./near/scaffold/mapping"));
48
+ const CosmosMappingScaffold = __importStar(require("./cosmos/scaffold/mapping"));
49
+ const debug_1 = __importDefault(require("../debug"));
50
+ let 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
+ let 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()
154
+ .get(this.name, immutable_1.default.List())
155
+ .includes(kind);
156
+ }
157
+ hasABIs() {
158
+ return this.config.abi != null;
159
+ }
160
+ hasContract() {
161
+ return this.config.contract != null;
162
+ }
163
+ hasEvents() {
164
+ // A problem with hasEvents usage in the codebase is that it's almost every where
165
+ // where used, the ABI data is actually use after the conditional, so it seems
166
+ // both concept are related. So internally, we map to this condition.
167
+ return this.hasABIs();
168
+ }
169
+ hasTemplates() {
170
+ return this.config.getTemplateCodeGen != null;
171
+ }
172
+ hasDataSourceMappingFile() {
173
+ return this.getMappingScaffold() != null;
174
+ }
175
+ getTypeGenerator(options) {
176
+ if (this.config == null || this.config.getTypeGenerator == null) {
177
+ return null;
178
+ }
179
+ return this.config.getTypeGenerator(options);
180
+ }
181
+ getTemplateCodeGen(template) {
182
+ if (!this.hasTemplates()) {
183
+ throw new Error(`Template data sources with kind '${this.name}' are not supported yet`);
184
+ }
185
+ return this.config.getTemplateCodeGen?.(template);
186
+ }
187
+ getABI() {
188
+ return this.config.abi;
189
+ }
190
+ getSubgraph(options = {}) {
191
+ return this.config.getSubgraph({ ...options, protocol: this });
192
+ }
193
+ getContract() {
194
+ return this.config.contract;
195
+ }
196
+ getManifestScaffold() {
197
+ return this.config.manifestScaffold;
198
+ }
199
+ getMappingScaffold() {
200
+ return this.config.mappingScaffold;
201
+ }
202
+ }
203
+ exports.default = Protocol;
204
+ const arweaveProtocol = {
205
+ displayName: 'Arweave',
206
+ abi: undefined,
207
+ contract: undefined,
208
+ getTemplateCodeGen: undefined,
209
+ getTypeGenerator: undefined,
210
+ getSubgraph(options) {
211
+ return new subgraph_1.default(options);
212
+ },
213
+ manifestScaffold: ArweaveManifestScaffold,
214
+ mappingScaffold: ArweaveMappingScaffold,
215
+ };
216
+ const cosmosProtocol = {
217
+ displayName: 'Cosmos',
218
+ abi: undefined,
219
+ contract: undefined,
220
+ getTemplateCodeGen: undefined,
221
+ getTypeGenerator: undefined,
222
+ getSubgraph(options) {
223
+ return new subgraph_4.default(options);
224
+ },
225
+ manifestScaffold: CosmosManifestScaffold,
226
+ mappingScaffold: CosmosMappingScaffold,
227
+ };
228
+ const ethereumProtocol = {
229
+ displayName: 'Ethereum',
230
+ abi: abi_1.default,
231
+ contract: contract_1.default,
232
+ getTemplateCodeGen(template) {
233
+ return new template_1.default(template);
234
+ },
235
+ getTypeGenerator(options) {
236
+ return new type_generator_1.default(options);
237
+ },
238
+ getSubgraph(options) {
239
+ return new subgraph_2.default(options);
240
+ },
241
+ manifestScaffold: EthereumManifestScaffold,
242
+ mappingScaffold: EthereumMappingScaffold,
243
+ };
244
+ const nearProtocol = {
245
+ displayName: 'NEAR',
246
+ abi: undefined,
247
+ contract: contract_2.default,
248
+ getTypeGenerator: undefined,
249
+ getTemplateCodeGen: undefined,
250
+ getSubgraph(options) {
251
+ return new subgraph_3.default(options);
252
+ },
253
+ manifestScaffold: NearManifestScaffold,
254
+ mappingScaffold: NearMappingScaffold,
255
+ };
256
+ const substreamsProtocol = {
257
+ displayName: 'Substreams',
258
+ abi: undefined,
259
+ contract: undefined,
260
+ getTypeGenerator: undefined,
261
+ getTemplateCodeGen: undefined,
262
+ getSubgraph(options) {
263
+ return new subgraph_5.default(options);
264
+ },
265
+ manifestScaffold: SubstreamsManifestScaffold,
266
+ mappingScaffold: undefined,
267
+ };
268
+ protocolDebug('Available networks %M', Protocol.availableNetworks());
@@ -0,0 +1,50 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const tsCodegen = __importStar(require("../../../codegen/typescript"));
27
+ class IpfsFileTemplateCodeGen {
28
+ constructor(template) {
29
+ this.template = template;
30
+ }
31
+ generateModuleImports() {
32
+ return [];
33
+ }
34
+ generateCreateMethod() {
35
+ const name = this.template.get('name');
36
+ return tsCodegen.staticMethod('create', [tsCodegen.param('cid', tsCodegen.namedType('string'))], tsCodegen.namedType('void'), `
37
+ DataSourceTemplate.create('${name}', [cid])
38
+ `);
39
+ }
40
+ generateCreateWithContextMethod() {
41
+ const name = this.template.get('name');
42
+ return tsCodegen.staticMethod('createWithContext', [
43
+ tsCodegen.param('cid', tsCodegen.namedType('string')),
44
+ tsCodegen.param('context', tsCodegen.namedType('DataSourceContext')),
45
+ ], tsCodegen.namedType('void'), `
46
+ DataSourceTemplate.createWithContext('${name}', [cid], context)
47
+ `);
48
+ }
49
+ }
50
+ 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
+ }
14
+ validateLength(value) {
15
+ return (value.length >= MINIMUM_ACCOUNT_ID_LENGTH &&
16
+ 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;