@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,29 @@
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
+ exports.createJsonRpcClient = void 0;
7
+ const jayson_1 = __importDefault(require("jayson"));
8
+ const gluegun_1 = require("gluegun");
9
+ const createJsonRpcClient = (url) => {
10
+ const params = {
11
+ host: url.hostname,
12
+ port: url.port,
13
+ path: url.pathname,
14
+ // username may be empty
15
+ auth: url.password ? `${url.username}:${url.password}` : undefined,
16
+ };
17
+ if (url.protocol === 'https:') {
18
+ return jayson_1.default.Client.https(params);
19
+ }
20
+ else if (url.protocol === 'http:') {
21
+ return jayson_1.default.Client.http(params);
22
+ }
23
+ else {
24
+ gluegun_1.print.error(`Unsupported protocol: ${url.protocol.substring(0, url.protocol.length - 1)}`);
25
+ gluegun_1.print.error('The Graph Node URL must be of the following format: http(s)://host[:port]/[path]');
26
+ return null;
27
+ }
28
+ };
29
+ exports.createJsonRpcClient = createJsonRpcClient;
@@ -0,0 +1,93 @@
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
+ exports.updateNetworksFile = exports.initNetworksConfig = exports.updateSubgraphNetwork = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const yaml_1 = __importDefault(require("yaml"));
9
+ const spinner_1 = require("../command-helpers/spinner");
10
+ const updateSubgraphNetwork = async (toolbox, manifest, network, networksFile, identifierName) => await (0, spinner_1.withSpinner)(`Update sources network`, `Failed to update sources network`, `Warnings while updating sources network`, async (spinner) => {
11
+ let allNetworks;
12
+ (0, spinner_1.step)(spinner, `Reading networks config`);
13
+ allNetworks = await toolbox.filesystem.read(networksFile, 'json');
14
+ let networkConfig = allNetworks[network];
15
+ // Exit if the network passed with --network does not exits in networks.json
16
+ if (!networkConfig) {
17
+ throw new Error(`Network '${network}' was not found in '${networksFile}'`);
18
+ }
19
+ await toolbox.patching.update(manifest, content => {
20
+ let subgraph = yaml_1.default.parse(content);
21
+ let networkSources = Object.keys(networkConfig);
22
+ let subgraphSources = subgraph.dataSources.map(value => value.name);
23
+ // Update the dataSources network config
24
+ subgraph.dataSources = subgraph.dataSources.map(source => {
25
+ if (!networkSources.includes(source.name)) {
26
+ throw new Error(`'${source.name}' was not found in the '${network}' configuration, please update!`);
27
+ }
28
+ if (hasChanges(identifierName, network, networkConfig[source.name], source)) {
29
+ (0, spinner_1.step)(spinner, `Update '${source.name}' network configuration`);
30
+ source.network = network;
31
+ source.source = source.source.abi ? { abi: source.source.abi } : {};
32
+ Object.assign(source.source, networkConfig[source.name]);
33
+ }
34
+ else {
35
+ (0, spinner_1.step)(spinner, `Skip '${source.name}': No changes to network configuration`);
36
+ }
37
+ return source;
38
+ });
39
+ // All data sources shoud be on the same network,
40
+ // so we have to update the network of all templates too.
41
+ if (subgraph.templates) {
42
+ subgraph.templates = subgraph.templates.map(template => ({
43
+ ...template,
44
+ network,
45
+ }));
46
+ }
47
+ let unsusedSources = networkSources.filter(x => !subgraphSources.includes(x));
48
+ unsusedSources.forEach(source => {
49
+ (0, spinner_1.step)(spinner, `dataSource '${source}' from '${networksFile}' not found in ${manifest}`);
50
+ });
51
+ let yaml_doc = new yaml_1.default.Document();
52
+ yaml_doc.contents = subgraph;
53
+ return yaml_doc.toString();
54
+ });
55
+ });
56
+ exports.updateSubgraphNetwork = updateSubgraphNetwork;
57
+ const initNetworksConfig = async (toolbox, directory, identifierName) => await (0, spinner_1.withSpinner)(`Initialize networks config`, `Failed to initialize networks config`, `Warnings while initializing networks config`, async (spinner) => {
58
+ let subgraphStr = await toolbox.filesystem.read(path_1.default.join(directory, 'subgraph.yaml'));
59
+ let subgraph = yaml_1.default.parse(subgraphStr);
60
+ const networks = subgraph.dataSources.reduce((acc, source) => Object.assign(acc, {
61
+ [source.network]: {
62
+ [source.name]: {
63
+ [identifierName]: source.source.address,
64
+ startBlock: source.source.startBlock,
65
+ },
66
+ },
67
+ }), {});
68
+ await toolbox.filesystem.write(`${directory}/networks.json`, networks);
69
+ return true;
70
+ });
71
+ exports.initNetworksConfig = initNetworksConfig;
72
+ // Checks if any network attribute has been changed
73
+ function hasChanges(identifierName, network, networkConfig, dataSource) {
74
+ let networkChanged = dataSource.network !== network;
75
+ // Return directly if the network is different
76
+ if (networkChanged)
77
+ return networkChanged;
78
+ let addressChanged = networkConfig[identifierName] !== dataSource.source[identifierName];
79
+ let startBlockChanged = networkConfig.startBlock !== dataSource.source.startBlock;
80
+ return networkChanged || addressChanged || startBlockChanged;
81
+ }
82
+ const updateNetworksFile = async (toolbox, network, dataSource, address, networksFile) => {
83
+ await toolbox.patching.update(networksFile, config => {
84
+ if (Object.keys(config).includes(network)) {
85
+ Object.assign(config[network], { [dataSource]: { address } });
86
+ }
87
+ else {
88
+ Object.assign(config, { [network]: { [dataSource]: { address } } });
89
+ }
90
+ return config;
91
+ });
92
+ };
93
+ exports.updateNetworksFile = updateNetworksFile;
@@ -0,0 +1,86 @@
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 network_1 = require("../command-helpers/network");
30
+ const toolbox = __importStar(require("gluegun/toolbox"));
31
+ const yaml_1 = __importDefault(require("yaml"));
32
+ const path_1 = __importDefault(require("path"));
33
+ const SUBGRAPH_PATH_BASE = path_1.default.join(__dirname, '..', '..', '..', '..', 'examples', 'example-subgraph');
34
+ describe('initNetworksConfig', () => {
35
+ beforeAll(async () => {
36
+ await (0, network_1.initNetworksConfig)(toolbox, SUBGRAPH_PATH_BASE, 'address');
37
+ });
38
+ afterAll(async () => {
39
+ await toolbox.filesystem.remove(`${SUBGRAPH_PATH_BASE}/networks.json`);
40
+ });
41
+ test('generates networks.json from subgraph.yaml', () => {
42
+ expect(toolbox.filesystem.exists(`${SUBGRAPH_PATH_BASE}/networks.json`)).toBe('file');
43
+ });
44
+ test('Populates the networks.json file with the data from subgraph.yaml', async () => {
45
+ let networksStr = await toolbox.filesystem.read(`${SUBGRAPH_PATH_BASE}/networks.json`);
46
+ let networks = JSON.parse(networksStr);
47
+ let expected = {
48
+ mainnet: {
49
+ ExampleSubgraph: { address: '0x22843e74c59580b3eaf6c233fa67d8b7c561a835' },
50
+ },
51
+ };
52
+ expect(networks).toStrictEqual(expected);
53
+ });
54
+ });
55
+ describe('updateSubgraphNetwork', () => {
56
+ beforeAll(async () => {
57
+ let content = {
58
+ optimism: {
59
+ ExampleSubgraph: { address: '0x12345...' },
60
+ },
61
+ };
62
+ await toolbox.filesystem.write(`${SUBGRAPH_PATH_BASE}/networks.json`, content);
63
+ await toolbox.filesystem.copy(`${SUBGRAPH_PATH_BASE}/subgraph.yaml`, `${SUBGRAPH_PATH_BASE}/subgraph_copy.yaml`);
64
+ });
65
+ afterAll(async () => {
66
+ await toolbox.filesystem.remove(`${SUBGRAPH_PATH_BASE}/networks.json`);
67
+ await toolbox.filesystem.remove(`${SUBGRAPH_PATH_BASE}/subgraph_copy.yaml`);
68
+ });
69
+ test('Updates subgraph.yaml', async () => {
70
+ let manifest = `${SUBGRAPH_PATH_BASE}/subgraph_copy.yaml`;
71
+ let networksFie = `${SUBGRAPH_PATH_BASE}/networks.json`;
72
+ let subgraph = await toolbox.filesystem.read(manifest);
73
+ let subgraphObj = yaml_1.default.parse(subgraph);
74
+ let network = subgraphObj.dataSources[0].network;
75
+ let address = subgraphObj.dataSources[0].source.address;
76
+ expect(network).toBe('mainnet');
77
+ expect(address).toBe('0x22843e74c59580b3eaf6c233fa67d8b7c561a835');
78
+ await (0, network_1.updateSubgraphNetwork)(toolbox, manifest, 'optimism', networksFie, 'address');
79
+ subgraph = await toolbox.filesystem.read(manifest);
80
+ subgraphObj = yaml_1.default.parse(subgraph);
81
+ network = subgraphObj.dataSources[0].network;
82
+ address = subgraphObj.dataSources[0].source.address;
83
+ expect(network).toBe('optimism');
84
+ expect(address).toBe('0x12345...');
85
+ });
86
+ });
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.chooseNodeUrl = exports.normalizeNodeUrl = exports.validateNodeUrl = void 0;
4
+ const URL = require('url').URL;
5
+ const toolbox_1 = require("gluegun/toolbox");
6
+ const SUBGRAPH_STUDIO_URL = 'https://api.studio.thegraph.com/deploy/';
7
+ const HOSTED_SERVICE_URL = 'https://api.thegraph.com/deploy/';
8
+ const validateNodeUrl = node => new URL(node);
9
+ exports.validateNodeUrl = validateNodeUrl;
10
+ const normalizeNodeUrl = node => new URL(node).toString();
11
+ exports.normalizeNodeUrl = normalizeNodeUrl;
12
+ function chooseNodeUrl({ product, studio, node, allowSimpleName }) {
13
+ if (node) {
14
+ try {
15
+ validateNodeUrl(node);
16
+ }
17
+ catch (e) {
18
+ toolbox_1.print.error(`Graph node "${node}" is invalid: ${e.message}`);
19
+ process.exit(1);
20
+ }
21
+ }
22
+ else {
23
+ if (studio) {
24
+ product = 'subgraph-studio';
25
+ }
26
+ switch (product) {
27
+ case 'subgraph-studio':
28
+ node = SUBGRAPH_STUDIO_URL;
29
+ break;
30
+ case 'hosted-service':
31
+ node = HOSTED_SERVICE_URL;
32
+ break;
33
+ }
34
+ }
35
+ if ((node && node.match(/studio/)) || product === 'subgraph-studio') {
36
+ allowSimpleName = true;
37
+ }
38
+ return { node, allowSimpleName };
39
+ }
40
+ exports.chooseNodeUrl = chooseNodeUrl;
@@ -0,0 +1,112 @@
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
+ exports.writeTestsFiles = exports.writeMapping = exports.writeSchema = exports.writeABI = exports.generateDataSource = exports.writeScaffold = exports.generateScaffold = void 0;
7
+ const fs_extra_1 = __importDefault(require("fs-extra"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const prettier_1 = __importDefault(require("prettier"));
10
+ const yaml_1 = __importDefault(require("yaml"));
11
+ const spinner_1 = require("./spinner");
12
+ const scaffold_1 = __importDefault(require("../scaffold"));
13
+ const mapping_1 = require("../scaffold/mapping");
14
+ const schema_1 = require("../scaffold/schema");
15
+ const tests_1 = require("../scaffold/tests");
16
+ const gluegun_1 = require("gluegun");
17
+ const immutable_1 = require("immutable");
18
+ const generateDataSource = async (protocol, contractName, network, contractAddress, abi) => {
19
+ const protocolManifest = protocol.getManifestScaffold();
20
+ return immutable_1.Map.of('kind', protocol.name, 'name', contractName, 'network', network, 'source', yaml_1.default.parse(prettier_1.default.format(protocolManifest.source({ contract: contractAddress, contractName }), { parser: 'yaml' })), 'mapping', yaml_1.default.parse(prettier_1.default.format(protocolManifest.mapping({ abi, contractName }), {
21
+ parser: 'yaml',
22
+ }))).asMutable();
23
+ };
24
+ exports.generateDataSource = generateDataSource;
25
+ const generateScaffold = async ({ protocolInstance, abi, contract, network, subgraphName, indexEvents, contractName = 'Contract', node, }, spinner) => {
26
+ (0, spinner_1.step)(spinner, 'Generate subgraph');
27
+ const scaffold = new scaffold_1.default({
28
+ protocol: protocolInstance,
29
+ abi,
30
+ indexEvents,
31
+ contract,
32
+ network,
33
+ contractName,
34
+ subgraphName,
35
+ node,
36
+ });
37
+ return scaffold.generate();
38
+ };
39
+ exports.generateScaffold = generateScaffold;
40
+ const writeScaffoldDirectory = async (scaffold, directory, spinner) => {
41
+ // Create directory itself
42
+ await fs_extra_1.default.mkdirs(directory);
43
+ let promises = Object.keys(scaffold).map(async (basename) => {
44
+ let content = scaffold[basename];
45
+ let filename = path_1.default.join(directory, basename);
46
+ // Write file or recurse into subdirectory
47
+ if (typeof content === 'string') {
48
+ await fs_extra_1.default.writeFile(filename, content, { encoding: 'utf-8' });
49
+ }
50
+ else if (content == null) {
51
+ return; // continue loop
52
+ }
53
+ else {
54
+ writeScaffoldDirectory(content, path_1.default.join(directory, basename), spinner);
55
+ }
56
+ });
57
+ await Promise.all(promises);
58
+ };
59
+ const writeScaffold = async (scaffold, directory, spinner) => {
60
+ (0, spinner_1.step)(spinner, `Write subgraph to directory`);
61
+ await writeScaffoldDirectory(scaffold, directory, spinner);
62
+ };
63
+ exports.writeScaffold = writeScaffold;
64
+ const writeABI = async (abi, contractName) => {
65
+ let data = prettier_1.default.format(JSON.stringify(abi.data), {
66
+ parser: 'json',
67
+ });
68
+ await fs_extra_1.default.writeFile(`./abis/${contractName}.json`, data, { encoding: 'utf-8' });
69
+ };
70
+ exports.writeABI = writeABI;
71
+ const writeSchema = async (abi, protocol, schemaPath, entities) => {
72
+ const events = protocol.hasEvents()
73
+ ? (0, schema_1.abiEvents)(abi)
74
+ .filter(event => entities.indexOf(event.get('name')) === -1)
75
+ .toJS()
76
+ : [];
77
+ let data = prettier_1.default.format(events.map(event => (0, schema_1.generateEventType)(event, protocol.name)).join('\n\n'), {
78
+ parser: 'graphql',
79
+ });
80
+ await fs_extra_1.default.appendFile(schemaPath, data, { encoding: 'utf-8' });
81
+ };
82
+ exports.writeSchema = writeSchema;
83
+ const writeMapping = async (abi, protocol, contractName, entities) => {
84
+ const events = protocol.hasEvents()
85
+ ? (0, schema_1.abiEvents)(abi)
86
+ .filter(event => entities.indexOf(event.get('name')) === -1)
87
+ .toJS()
88
+ : [];
89
+ let mapping = prettier_1.default.format((0, mapping_1.generateEventIndexingHandlers)(events, contractName), {
90
+ parser: 'typescript',
91
+ semi: false,
92
+ });
93
+ await fs_extra_1.default.writeFile(`./src/${gluegun_1.strings.kebabCase(contractName)}.ts`, mapping, {
94
+ encoding: 'utf-8',
95
+ });
96
+ };
97
+ exports.writeMapping = writeMapping;
98
+ const writeTestsFiles = async (abi, protocol, contractName) => {
99
+ const hasEvents = protocol.hasEvents();
100
+ const events = hasEvents ? (0, schema_1.abiEvents)(abi).toJS() : [];
101
+ if (events.length > 0) {
102
+ // If a contract is added to a subgraph that has no tests folder
103
+ await fs_extra_1.default.ensureDir('./tests/');
104
+ const testsFiles = (0, tests_1.generateTestsFiles)(contractName, events, true);
105
+ for (const [fileName, content] of Object.entries(testsFiles)) {
106
+ await fs_extra_1.default.writeFile(`./tests/${fileName}`, content, {
107
+ encoding: 'utf-8',
108
+ });
109
+ }
110
+ }
111
+ };
112
+ exports.writeTestsFiles = writeTestsFiles;
@@ -0,0 +1,84 @@
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
+ exports.withSpinner = exports.step = void 0;
27
+ const toolbox = __importStar(require("gluegun"));
28
+ const step = (spinner, subject, text) => {
29
+ if (text) {
30
+ spinner.stopAndPersist({
31
+ text: toolbox.print.colors.muted(`${subject} ${text}`),
32
+ });
33
+ }
34
+ else {
35
+ spinner.stopAndPersist({ text: toolbox.print.colors.muted(subject) });
36
+ }
37
+ spinner.start();
38
+ return spinner;
39
+ };
40
+ exports.step = step;
41
+ // Executes the function `f` in a command-line spinner, using the
42
+ // provided captions for in-progress, error and failed messages.
43
+ //
44
+ // If `f` throws an error, the spinner stops with the failure message
45
+ // and rethrows the error.
46
+ // If `f` returns an object with a `warning` and a `result` key, the
47
+ // spinner stops with the warning message and returns the `result` value.
48
+ // Otherwise the spinner prints the in-progress message with a check mark
49
+ // and simply returns the value returned by `f`.
50
+ const withSpinner = async (text, errorText, warningText, f) => {
51
+ let spinner = toolbox.print.spin(text);
52
+ try {
53
+ let result = await f(spinner);
54
+ if (typeof result === 'object') {
55
+ let hasError = Object.keys(result).indexOf('error') >= 0;
56
+ let hasWarning = Object.keys(result).indexOf('warning') >= 0;
57
+ let hasResult = Object.keys(result).indexOf('result') >= 0;
58
+ if (hasError) {
59
+ spinner.fail(`${errorText}: ${result.error}`);
60
+ return hasResult ? result.result : result;
61
+ }
62
+ if (hasWarning && hasResult) {
63
+ if (result.warning !== null) {
64
+ spinner.warn(`${warningText}: ${result.warning}`);
65
+ }
66
+ spinner.succeed(text);
67
+ return result.result;
68
+ }
69
+ else {
70
+ spinner.succeed(text);
71
+ return result;
72
+ }
73
+ }
74
+ else {
75
+ spinner.succeed(text);
76
+ return result;
77
+ }
78
+ }
79
+ catch (e) {
80
+ spinner.fail(`${errorText}: ${e.message}`);
81
+ throw e;
82
+ }
83
+ };
84
+ exports.withSpinner = withSpinner;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateStudioNetwork = exports.allowedStudioNetworks = void 0;
4
+ const allowedStudioNetworks = ['mainnet', 'rinkeby', 'goerli', 'gnosis'];
5
+ exports.allowedStudioNetworks = allowedStudioNetworks;
6
+ const validateStudioNetwork = ({ studio, product, network }) => {
7
+ let isStudio = studio || product === 'subgraph-studio';
8
+ let isAllowedNetwork = allowedStudioNetworks.includes(network);
9
+ if (isStudio && !isAllowedNetwork) {
10
+ throw new Error(`The Subgraph Studio only allows subgraphs for these networks: ${allowedStudioNetworks.join(', ')}`);
11
+ }
12
+ };
13
+ exports.validateStudioNetwork = validateStudioNetwork;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const studio_1 = require("./studio");
4
+ describe('Version Command Helpers', () => {
5
+ describe('validateStudioNetwork', () => {
6
+ describe("When it's studio", () => {
7
+ test("And it's Ethereum mainnet", () => {
8
+ expect(() => (0, studio_1.validateStudioNetwork)({
9
+ studio: true,
10
+ network: 'mainnet',
11
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
12
+ });
13
+ test("And it's Gnosis chain", () => {
14
+ expect(() => (0, studio_1.validateStudioNetwork)({
15
+ studio: true,
16
+ network: 'gnosis',
17
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
18
+ });
19
+ test("And it's NOT an allowed network", () => {
20
+ expect(() => (0, studio_1.validateStudioNetwork)({
21
+ product: 'subgraph-studio',
22
+ network: 'celo',
23
+ })).toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
24
+ });
25
+ });
26
+ describe("When it's NOT studio", () => {
27
+ test("And it's Rinkeby", () => {
28
+ expect(() => (0, studio_1.validateStudioNetwork)({
29
+ studio: false,
30
+ network: 'rinkeby',
31
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
32
+ });
33
+ test("And it's NOT an allowed network", () => {
34
+ expect(() => (0, studio_1.validateStudioNetwork)({
35
+ product: 'hosted-service',
36
+ network: 'celo',
37
+ })).not.toThrow(new Error(`The Subgraph Studio only allows subgraphs for these networks: ${studio_1.allowedStudioNetworks.join(', ')}`));
38
+ });
39
+ });
40
+ });
41
+ });
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSubgraphBasename = exports.validateSubgraphName = void 0;
4
+ const validateSubgraphName = (name, { allowSimpleName }) => {
5
+ if (allowSimpleName) {
6
+ return name;
7
+ }
8
+ else {
9
+ if (name.split('/').length !== 2) {
10
+ throw new Error(`Subgraph name "${name}" needs to have the format "<PREFIX>/${name}".
11
+
12
+ When using the Hosted Service at https://thegraph.com, <PREFIX> is the
13
+ name of your GitHub user or organization.
14
+
15
+ You can bypass this check with --allow-simple-name.`);
16
+ }
17
+ }
18
+ };
19
+ exports.validateSubgraphName = validateSubgraphName;
20
+ const getSubgraphBasename = (name) => {
21
+ let segments = name.split('/', 2);
22
+ return segments[segments.length - 1];
23
+ };
24
+ exports.getSubgraphBasename = getSubgraphBasename;
@@ -0,0 +1,74 @@
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
+ exports.assertGraphTsVersion = exports.assertManifestApiVersion = void 0;
30
+ const semver_1 = __importDefault(require("semver"));
31
+ const graphTsUtil = __importStar(require("../migrations/util/versions"));
32
+ const manifestUtil = __importStar(require("../migrations/util/load-manifest"));
33
+ const assertManifestApiVersion = async (manifestPath, minimumApiVersion) => {
34
+ let manifest = await manifestUtil.loadManifest(manifestPath);
35
+ let lessThanMinimumVersion = manifestApiVersion => semver_1.default.lt(manifestApiVersion, minimumApiVersion);
36
+ let isLessThanMinimumVersion = false;
37
+ if (manifest) {
38
+ if (manifest.dataSources && Array.isArray(manifest.dataSources)) {
39
+ isLessThanMinimumVersion = manifest.dataSources.some(dataSource => dataSource &&
40
+ dataSource.mapping &&
41
+ dataSource.mapping.apiVersion &&
42
+ lessThanMinimumVersion(dataSource.mapping.apiVersion));
43
+ }
44
+ if (manifest.templates && Array.isArray(manifest.templates)) {
45
+ isLessThanMinimumVersion =
46
+ isLessThanMinimumVersion ||
47
+ manifest.templates.some(template => template &&
48
+ template.mapping &&
49
+ template.mapping.apiVersion &&
50
+ lessThanMinimumVersion(template.mapping.apiVersion));
51
+ }
52
+ }
53
+ if (isLessThanMinimumVersion) {
54
+ throw new Error(`The current version of graph-cli can't be used with mappings on apiVersion less than '${minimumApiVersion}'`);
55
+ }
56
+ };
57
+ exports.assertManifestApiVersion = assertManifestApiVersion;
58
+ const assertGraphTsVersion = async (sourceDir, minimumGraphTsVersion) => {
59
+ let graphTsVersion;
60
+ try {
61
+ graphTsVersion = await graphTsUtil.getGraphTsVersion(sourceDir);
62
+ }
63
+ catch (_) {
64
+ // We only do the assertion if `graph-ts` is installed.
65
+ }
66
+ // Coerce needed because we may be dealing with an alpha version
67
+ // and in the `semver` library, this would return true when comparing
68
+ // the same version.
69
+ if (graphTsVersion && semver_1.default.lt(semver_1.default.coerce(graphTsVersion), minimumGraphTsVersion)) {
70
+ throw new Error(`To use this version of the graph-cli you must upgrade the graph-ts dependency to a version greater than or equal to ${minimumGraphTsVersion}
71
+ Also, you'll probably need to take a look at our AssemblyScript migration guide because of language breaking changes: https://thegraph.com/docs/developer/assemblyscript-migration-guide`);
72
+ }
73
+ };
74
+ exports.assertGraphTsVersion = assertGraphTsVersion;