@graphprotocol/graph-cli 0.37.3-alpha-20230111115045-e3bcf23 → 0.37.3-alpha-20230111132934-b4190fb

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.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @graphprotocol/graph-cli
2
2
 
3
- ## 0.37.3-alpha-20230111115045-e3bcf23
3
+ ## 0.37.3-alpha-20230111132934-b4190fb
4
4
 
5
5
  ### Patch Changes
6
6
 
@@ -9,7 +9,7 @@ const conversions_1 = __importDefault(require("./conversions"));
9
9
  // Conversion utilities
10
10
  const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
11
11
  let conversions = conversions_1.default.getIn([fromTypeSystem, toTypeSystem]);
12
- if (conversions === undefined) {
12
+ if (conversions == undefined) {
13
13
  throw new Error(`Conversions from '${fromTypeSystem}' to '${toTypeSystem}' are not supported`);
14
14
  }
15
15
  return conversions;
@@ -31,7 +31,7 @@ const findConversionFromType = (fromTypeSystem, toTypeSystem, fromType) => {
31
31
  let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem);
32
32
  let conversion = conversions.find(conversion => typeof conversion.get(0) === 'string'
33
33
  ? conversion.get(0) === fromType
34
- : fromType.match(conversion.get(0)));
34
+ : Boolean(fromType.match(conversion.get(0))));
35
35
  if (conversion === undefined) {
36
36
  throw new Error(`Conversion from '${fromTypeSystem}' to '${toTypeSystem}' for ` +
37
37
  `source type '${fromType}' is not supported`);
@@ -42,7 +42,7 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
42
42
  let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem);
43
43
  let conversion = conversions.find(conversion => typeof conversion.get(1) === 'string'
44
44
  ? conversion.get(1) === toType
45
- : toType.match(conversion.get(1)));
45
+ : Boolean(toType.match(conversion.get(1))));
46
46
  if (conversion === undefined) {
47
47
  throw new Error(`Conversion from '${fromTypeSystem}' to '${toTypeSystem}' for ` +
48
48
  `target type '${toType}' is not supported`);
@@ -50,20 +50,26 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
50
50
  return objectifyConversion(fromTypeSystem, toTypeSystem, conversion);
51
51
  };
52
52
  // High-level type system API
53
- const ascTypeForProtocol = (protocol, protocolType) => findConversionFromType(protocol, 'AssemblyScript', protocolType).getIn(['to', 'type']);
53
+ const ascTypeForProtocol = (protocol, protocolType) => findConversionFromType(protocol, 'AssemblyScript', protocolType).getIn([
54
+ 'to',
55
+ 'type',
56
+ ]);
54
57
  exports.ascTypeForProtocol = ascTypeForProtocol;
55
58
  // TODO: this can be removed/replaced by the function above
56
- const ascTypeForEthereum = ethereumType => ascTypeForProtocol('ethereum', ethereumType);
59
+ const ascTypeForEthereum = (ethereumType) => (0, exports.ascTypeForProtocol)('ethereum', ethereumType);
57
60
  exports.ascTypeForEthereum = ascTypeForEthereum;
58
- const ethereumTypeForAsc = ascType => findConversionFromType('AssemblyScript', 'ethereum', ascType).getIn(['to', 'type']);
61
+ const ethereumTypeForAsc = (ascType) => findConversionFromType('AssemblyScript', 'ethereum', ascType).getIn([
62
+ 'to',
63
+ 'type',
64
+ ]);
59
65
  exports.ethereumTypeForAsc = ethereumTypeForAsc;
60
66
  const ethereumToAsc = (code, ethereumType, internalType) => findConversionFromType('ethereum', 'AssemblyScript', ethereumType).get('convert')(code, internalType);
61
67
  exports.ethereumToAsc = ethereumToAsc;
62
68
  const ethereumFromAsc = (code, ethereumType) => findConversionToType('AssemblyScript', 'ethereum', ethereumType).get('convert')(code);
63
69
  exports.ethereumFromAsc = ethereumFromAsc;
64
- const ascTypeForValue = valueType => findConversionFromType('Value', 'AssemblyScript', valueType).getIn(['to', 'type']);
70
+ const ascTypeForValue = (valueType) => findConversionFromType('Value', 'AssemblyScript', valueType).getIn(['to', 'type']);
65
71
  exports.ascTypeForValue = ascTypeForValue;
66
- const valueTypeForAsc = ascType => findConversionFromType('AssemblyScript', 'Value', ascType).getIn(['to', 'type']);
72
+ const valueTypeForAsc = (ascType) => findConversionFromType('AssemblyScript', 'Value', ascType).getIn(['to', 'type']);
67
73
  exports.valueTypeForAsc = valueTypeForAsc;
68
74
  const valueToAsc = (code, valueType) => findConversionFromType('Value', 'AssemblyScript', valueType).get('convert')(code);
69
75
  exports.valueToAsc = valueToAsc;
@@ -3,8 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const immutable_1 = __importDefault(require("immutable"));
7
- const _1 = __importDefault(require("./"));
6
+ const index_1 = __importDefault(require("./index"));
8
7
  const protocols_1 = __importDefault(require("../protocols"));
9
8
  const protocol = new protocols_1.default('cosmos');
10
9
  const scaffoldOptions = {
@@ -12,7 +11,7 @@ const scaffoldOptions = {
12
11
  network: 'cosmoshub-4',
13
12
  contractName: 'CosmosHub',
14
13
  };
15
- const scaffold = new _1.default(scaffoldOptions);
14
+ const scaffold = new index_1.default(scaffoldOptions);
16
15
  describe('Cosmos subgraph scaffolding', () => {
17
16
  test('Manifest', () => {
18
17
  expect(scaffold.generateManifest()).toEqual(`\
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const abi_1 = __importDefault(require("../protocols/ethereum/abi"));
7
7
  const immutable_1 = __importDefault(require("immutable"));
8
- const _1 = __importDefault(require("./"));
8
+ const index_1 = __importDefault(require("./index"));
9
9
  const protocols_1 = __importDefault(require("../protocols"));
10
10
  const TEST_EVENT = {
11
11
  name: 'ExampleEvent',
@@ -72,8 +72,8 @@ const scaffoldOptions = {
72
72
  network: 'kovan',
73
73
  contractName: 'Contract',
74
74
  };
75
- const scaffold = new _1.default(scaffoldOptions);
76
- const scaffoldWithIndexEvents = new _1.default({
75
+ const scaffold = new index_1.default(scaffoldOptions);
76
+ const scaffoldWithIndexEvents = new index_1.default({
77
77
  ...scaffoldOptions,
78
78
  indexEvents: true,
79
79
  });
@@ -248,8 +248,8 @@ export function handleExampleEvent1(event: ExampleEvent1Event): void {
248
248
  });
249
249
  test('Test Files (default)', () => {
250
250
  const files = scaffoldWithIndexEvents.generateTests();
251
- const testFile = files['contract.test.ts'];
252
- const utilsFile = files['contract-utils.ts'];
251
+ const testFile = files?.['contract.test.ts'];
252
+ const utilsFile = files?.['contract-utils.ts'];
253
253
  expect(testFile).toEqual(`\
254
254
  import {
255
255
  assert,
@@ -376,8 +376,8 @@ export function createExampleEvent1Event(a: Bytes): ExampleEvent1 {
376
376
  });
377
377
  test('Test Files (for indexing events)', () => {
378
378
  const files = scaffoldWithIndexEvents.generateTests();
379
- const testFile = files['contract.test.ts'];
380
- const utilsFile = files['contract-utils.ts'];
379
+ const testFile = files?.['contract.test.ts'];
380
+ const utilsFile = files?.['contract-utils.ts'];
381
381
  expect(testFile).toEqual(`\
382
382
  import {
383
383
  assert,
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const prettier_1 = __importDefault(require("prettier"));
7
- const pkginfo = require('pkginfo')(module);
8
7
  const gluegun_1 = require("gluegun");
9
8
  const GRAPH_CLI_VERSION = process.env.GRAPH_CLI_TESTS
10
9
  ? // JSON.stringify should remove this key, we will install the local
@@ -17,7 +16,15 @@ const mapping_1 = require("./mapping");
17
16
  const tests_1 = require("./tests");
18
17
  const subgraph_1 = require("../command-helpers/subgraph");
19
18
  class Scaffold {
20
- constructor(options = {}) {
19
+ protocol;
20
+ abi;
21
+ indexEvents;
22
+ contract;
23
+ network;
24
+ contractName;
25
+ subgraphName;
26
+ node;
27
+ constructor(options) {
21
28
  this.protocol = options.protocol;
22
29
  this.abi = options.abi;
23
30
  this.indexEvents = options.indexEvents;
@@ -29,7 +36,7 @@ class Scaffold {
29
36
  }
30
37
  generatePackageJson() {
31
38
  return prettier_1.default.format(JSON.stringify({
32
- name: (0, subgraph_1.getSubgraphBasename)(this.subgraphName),
39
+ name: (0, subgraph_1.getSubgraphBasename)(String(this.subgraphName)),
33
40
  license: 'UNLICENSED',
34
41
  scripts: {
35
42
  codegen: 'graph codegen',
@@ -70,7 +77,9 @@ dataSources:
70
77
  const hasEvents = this.protocol.hasEvents();
71
78
  const events = hasEvents ? (0, schema_1.abiEvents)(this.abi).toJS() : [];
72
79
  return prettier_1.default.format(hasEvents && this.indexEvents
73
- ? events.map(event => (0, schema_1.generateEventType)(event, this.protocol.name)).join('\n\n')
80
+ ? events
81
+ .map((event) => (0, schema_1.generateEventType)(event, this.protocol.name))
82
+ .join('\n\n')
74
83
  : (0, schema_1.generateExampleEntityType)(this.protocol, events), {
75
84
  parser: 'graphql',
76
85
  });
@@ -100,7 +109,7 @@ dataSources:
100
109
  generateABIs() {
101
110
  return this.protocol.hasABIs()
102
111
  ? {
103
- [`${this.contractName}.json`]: prettier_1.default.format(JSON.stringify(this.abi.data), {
112
+ [`${this.contractName}.json`]: prettier_1.default.format(JSON.stringify(this.abi?.data), {
104
113
  parser: 'json',
105
114
  }),
106
115
  }
@@ -22,22 +22,18 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
25
  Object.defineProperty(exports, "__esModule", { value: true });
29
26
  exports.generateEventIndexingHandlers = exports.generateEventFieldAssignments = exports.generateFieldAssignments = exports.generateFieldAssignment = void 0;
30
- const path_1 = __importDefault(require("path"));
31
27
  const util = __importStar(require("../codegen/util"));
32
- const generateFieldAssignment = path => `entity.${path.join('_')} = event.params.${path.join('.')}`;
28
+ const generateFieldAssignment = (path) => `entity.${path.join('_')} = event.params.${path.join('.')}`;
33
29
  exports.generateFieldAssignment = generateFieldAssignment;
34
- const generateFieldAssignments = ({ index, input }) => input.type === 'tuple'
30
+ const generateFieldAssignments = ({ index, input, }) => input.type === 'tuple'
35
31
  ? util
36
32
  .unrollTuple({ value: input, index, path: [input.name || `param${index}`] })
37
- .map(({ path }) => generateFieldAssignment(path))
38
- : generateFieldAssignment([input.name || `param${index}`]);
33
+ .map(({ path }) => (0, exports.generateFieldAssignment)(path))
34
+ : (0, exports.generateFieldAssignment)([input.name || `param${index}`]);
39
35
  exports.generateFieldAssignments = generateFieldAssignments;
40
- const generateEventFieldAssignments = event => event.inputs.reduce((acc, input, index) => acc.concat(generateFieldAssignments({ input, index })), []);
36
+ const generateEventFieldAssignments = (event) => event.inputs.reduce((acc, input, index) => acc.concat((0, exports.generateFieldAssignments)({ input, index })), []);
41
37
  exports.generateEventFieldAssignments = generateEventFieldAssignments;
42
38
  const generateEventIndexingHandlers = (events, contractName) => `
43
39
  import { ${events.map(event => `${event._alias} as ${event._alias}Event`)}} from '../generated/${contractName}/${contractName}'
@@ -47,7 +43,7 @@ const generateEventIndexingHandlers = (events, contractName) => `
47
43
  .map(event => `
48
44
  export function handle${event._alias}(event: ${event._alias}Event): void {
49
45
  let entity = new ${event._alias}(event.transaction.hash.concatI32(event.logIndex.toI32()))
50
- ${generateEventFieldAssignments(event).join('\n')}
46
+ ${(0, exports.generateEventFieldAssignments)(event).join('\n')}
51
47
 
52
48
  entity.blockNumber = event.block.number
53
49
  entity.blockTimestamp = event.block.timestamp
@@ -3,8 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const immutable_1 = __importDefault(require("immutable"));
7
- const _1 = __importDefault(require("./"));
6
+ const index_1 = __importDefault(require("./index"));
8
7
  const protocols_1 = __importDefault(require("../protocols"));
9
8
  const protocol = new protocols_1.default('near');
10
9
  const scaffoldOptions = {
@@ -13,7 +12,7 @@ const scaffoldOptions = {
13
12
  network: 'near-mainnet',
14
13
  contractName: 'Contract',
15
14
  };
16
- const scaffold = new _1.default(scaffoldOptions);
15
+ const scaffold = new index_1.default(scaffoldOptions);
17
16
  describe('NEAR subgraph scaffolding', () => {
18
17
  test('Manifest', () => {
19
18
  expect(scaffold.generateManifest()).toEqual(`\
@@ -26,9 +26,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.generateExampleEntityType = exports.generateEventType = exports.generateEventFields = exports.generateField = exports.protocolTypeToGraphQL = exports.abiEvents = void 0;
27
27
  const types_1 = require("../codegen/types");
28
28
  const util = __importStar(require("../codegen/util"));
29
- const abiEvents = abi => util.disambiguateNames({
30
- values: abi.data.filter(item => item.get('type') === 'event'),
31
- getName: event => event.get('name'),
29
+ const abiEvents = (abi) => util.disambiguateNames({
30
+ values: abi.data.filter((item) => item.get('type') === 'event'),
31
+ getName: (event) => event.get('name'),
32
32
  setName: (event, name) => event.set('_alias', name),
33
33
  });
34
34
  exports.abiEvents = abiEvents;
@@ -37,14 +37,14 @@ const protocolTypeToGraphQL = (protocol, name) => {
37
37
  return (0, types_1.valueTypeForAsc)(ascType);
38
38
  };
39
39
  exports.protocolTypeToGraphQL = protocolTypeToGraphQL;
40
- const generateField = ({ name, type, protocolName }) => `${name}: ${protocolTypeToGraphQL(protocolName, type)}! # ${type}`;
40
+ const generateField = ({ name, type, protocolName, }) => `${name}: ${(0, exports.protocolTypeToGraphQL)(protocolName, type)}! # ${type}`;
41
41
  exports.generateField = generateField;
42
- const generateEventFields = ({ index, input, protocolName }) => input.type == 'tuple'
42
+ const generateEventFields = ({ index, input, protocolName, }) => input.type == 'tuple'
43
43
  ? util
44
44
  .unrollTuple({ value: input, path: [input.name || `param${index}`], index })
45
- .map(({ path, type }) => generateField({ name: path.join('_'), type, protocolName }))
45
+ .map(({ path, type }) => (0, exports.generateField)({ name: path.join('_'), type, protocolName }))
46
46
  : [
47
- generateField({
47
+ (0, exports.generateField)({
48
48
  name: input.name || `param${index}`,
49
49
  type: input.type,
50
50
  protocolName,
@@ -54,7 +54,7 @@ exports.generateEventFields = generateEventFields;
54
54
  const generateEventType = (event, protocolName) => `type ${event._alias} @entity(immutable: true) {
55
55
  id: Bytes!
56
56
  ${event.inputs
57
- .reduce((acc, input, index) => acc.concat(generateEventFields({ input, index, protocolName })), [])
57
+ .reduce((acc, input, index) => acc.concat((0, exports.generateEventFields)({ input, index, protocolName })), [])
58
58
  .join('\n')}
59
59
  blockNumber: BigInt!
60
60
  blockTimestamp: BigInt!
@@ -67,7 +67,7 @@ const generateExampleEntityType = (protocol, events) => {
67
67
  id: Bytes!
68
68
  count: BigInt!
69
69
  ${events[0].inputs
70
- .reduce((acc, input, index) => acc.concat(generateEventFields({ input, index, protocolName: protocol.name })), [])
70
+ .reduce((acc, input, index) => acc.concat((0, exports.generateEventFields)({ input, index, protocolName: protocol.name })), [])
71
71
  .slice(0, 2)
72
72
  .join('\n')}
73
73
  }`;
@@ -17,11 +17,11 @@ const VARIABLES_VALUES = {
17
17
  };
18
18
  const generateTestsFiles = (contract, events, indexEvents) => {
19
19
  const eventsTypes = events
20
- .flatMap(event => event.inputs.map(input => {
20
+ .flatMap(event => event.inputs.map((input) => {
21
21
  // If the asc type is Array<T> we need to check if T is a native type or a custom graph-ts type
22
22
  // If we don't do that we may miss a type that should be imported from graph-ts
23
23
  const ascType = (0, types_1.ascTypeForEthereum)(input.type);
24
- const inner = fetchArrayInnerType(ascType);
24
+ const inner = fetchArrayInnerType(String(ascType));
25
25
  return inner ? inner[1] : ascType;
26
26
  }))
27
27
  .filter(type => !type.startsWith('ethereum.') && !isNativeType(type));
@@ -39,16 +39,16 @@ exports.generateTestsFiles = generateTestsFiles;
39
39
  let displayName = "Example string value"
40
40
  let imageUrl = "Example string value"
41
41
  */
42
- const generateArguments = eventInputs => {
42
+ const generateArguments = (eventInputs) => {
43
43
  return eventInputs
44
44
  .map((input, index) => {
45
45
  let ascType = (0, types_1.ascTypeForEthereum)(input.type);
46
- return `let ${input.name || `param${index}`} = ${assignValue(ascType, input.name)}`;
46
+ return `let ${input.name || `param${index}`} = ${assignValue(ascType)}`;
47
47
  })
48
48
  .join('\n');
49
49
  };
50
50
  // Generates the value that will be assigned to a variable in generateArguments()
51
- const assignValue = type => {
51
+ const assignValue = (type) => {
52
52
  switch (type) {
53
53
  case 'string':
54
54
  return `"${VARIABLES_VALUES[type]}"`;
@@ -85,7 +85,7 @@ const generateFieldsAssertions = (entity, eventInputs, indexEvents) => eventInpu
85
85
  )`)
86
86
  .join('\n');
87
87
  // Returns the expected value for a given type in generateFieldsAssertions()
88
- const expectedValue = type => {
88
+ const expectedValue = (type) => {
89
89
  switch (type) {
90
90
  case fetchArrayInnerType(type)?.input:
91
91
  const innerType = fetchArrayInnerType(type)[1];
@@ -96,11 +96,11 @@ const expectedValue = type => {
96
96
  }
97
97
  };
98
98
  // Checks if the type is a native AS type or should be imported from graph-ts
99
- const isNativeType = type => {
99
+ const isNativeType = (type) => {
100
100
  let natives = [/i32/, /string/, /boolean/];
101
101
  return natives.some(rx => rx.test(type));
102
102
  };
103
- const fetchArrayInnerType = type => type.match(/Array<(.*?)>/);
103
+ const fetchArrayInnerType = (type) => type.match(/Array<(.*?)>/);
104
104
  // Generates the example test.ts file
105
105
  const generateExampleTest = (contract, event, indexEvents, importTypes) => {
106
106
  const entity = indexEvents ? `${event._alias}` : 'ExampleEntity';
@@ -156,11 +156,12 @@ const generateTestHelper = (contract, events, importTypes) => {
156
156
 
157
157
  ${generateMockedEvents(events).join('\n')}`;
158
158
  };
159
- const generateMockedEvents = events => events.reduce((acc, event) => acc.concat(generateMockedEvent(event)), []);
160
- const generateMockedEvent = event => {
159
+ const generateMockedEvents = (events) => events.reduce((acc, event) => acc.concat(generateMockedEvent(event)), []);
160
+ const generateMockedEvent = (event) => {
161
161
  const varName = `${gluegun_1.strings.camelCase(event._alias)}Event`;
162
162
  const fnArgs = event.inputs.map((input, index) => `${input.name || `param${index}`}: ${(0, types_1.ascTypeForEthereum)(input.type)}`);
163
- const ascToEth = event.inputs.map((input, index) => `${varName}.parameters.push(new ethereum.EventParam("${input.name || `param${index}`}", ${(0, types_1.ethereumFromAsc)(input.name || `param${index}`, input.type)}))`);
163
+ const ascToEth = event.inputs.map((input, index) => `${varName}.parameters.push(new ethereum.EventParam("${input.name ||
164
+ `param${index}`}", ${(0, types_1.ethereumFromAsc)(input.name || `param${index}`, input.type)}))`);
164
165
  return `
165
166
  export function create${event._alias}Event(${fnArgs.join(', ')}): ${event._alias} {
166
167
  let ${varName} = changetype<${event._alias}>(newMockEvent());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphprotocol/graph-cli",
3
- "version": "0.37.3-alpha-20230111115045-e3bcf23",
3
+ "version": "0.37.3-alpha-20230111132934-b4190fb",
4
4
  "license": "(Apache-2.0 OR MIT)",
5
5
  "description": "CLI for building for and deploying to The Graph",
6
6
  "main": "dist/cli.js",
@@ -4,17 +4,21 @@ import TYPE_CONVERSIONS from './conversions'
4
4
 
5
5
  // Conversion utilities
6
6
 
7
- const conversionsForTypeSystems = (fromTypeSystem, toTypeSystem) => {
7
+ const conversionsForTypeSystems = (fromTypeSystem: string, toTypeSystem: string) => {
8
8
  let conversions = TYPE_CONVERSIONS.getIn([fromTypeSystem, toTypeSystem])
9
- if (conversions === undefined) {
9
+ if (conversions == undefined) {
10
10
  throw new Error(
11
11
  `Conversions from '${fromTypeSystem}' to '${toTypeSystem}' are not supported`,
12
12
  )
13
13
  }
14
- return conversions
14
+ return conversions as immutable.Collection<any, any>
15
15
  }
16
16
 
17
- const objectifyConversion = (fromTypeSystem, toTypeSystem, conversion) => {
17
+ const objectifyConversion = (
18
+ fromTypeSystem: string,
19
+ toTypeSystem: string,
20
+ conversion: immutable.Collection<any, any>,
21
+ ) => {
18
22
  return immutable.fromJS({
19
23
  from: {
20
24
  typeSystem: fromTypeSystem,
@@ -28,13 +32,17 @@ const objectifyConversion = (fromTypeSystem, toTypeSystem, conversion) => {
28
32
  })
29
33
  }
30
34
 
31
- const findConversionFromType = (fromTypeSystem, toTypeSystem, fromType) => {
35
+ const findConversionFromType = (
36
+ fromTypeSystem: string,
37
+ toTypeSystem: string,
38
+ fromType: string,
39
+ ): immutable.Collection<any, any> => {
32
40
  let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
33
41
 
34
42
  let conversion = conversions.find(conversion =>
35
43
  typeof conversion.get(0) === 'string'
36
44
  ? conversion.get(0) === fromType
37
- : fromType.match(conversion.get(0)),
45
+ : Boolean(fromType.match(conversion.get(0))),
38
46
  )
39
47
 
40
48
  if (conversion === undefined) {
@@ -47,13 +55,17 @@ const findConversionFromType = (fromTypeSystem, toTypeSystem, fromType) => {
47
55
  return objectifyConversion(fromTypeSystem, toTypeSystem, conversion)
48
56
  }
49
57
 
50
- const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
58
+ const findConversionToType = (
59
+ fromTypeSystem: string,
60
+ toTypeSystem: string,
61
+ toType: string,
62
+ ): immutable.Collection<any, any> => {
51
63
  let conversions = conversionsForTypeSystems(fromTypeSystem, toTypeSystem)
52
64
 
53
65
  let conversion = conversions.find(conversion =>
54
66
  typeof conversion.get(1) === 'string'
55
67
  ? conversion.get(1) === toType
56
- : toType.match(conversion.get(1)),
68
+ : Boolean(toType.match(conversion.get(1))),
57
69
  )
58
70
 
59
71
  if (conversion === undefined) {
@@ -68,47 +80,43 @@ const findConversionToType = (fromTypeSystem, toTypeSystem, toType) => {
68
80
 
69
81
  // High-level type system API
70
82
 
71
- const ascTypeForProtocol = (protocol, protocolType) =>
72
- findConversionFromType(protocol, 'AssemblyScript', protocolType).getIn(['to', 'type'])
83
+ export const ascTypeForProtocol = (protocol: string, protocolType: string) =>
84
+ findConversionFromType(protocol, 'AssemblyScript', protocolType).getIn([
85
+ 'to',
86
+ 'type',
87
+ ]) as string
73
88
 
74
89
  // TODO: this can be removed/replaced by the function above
75
- const ascTypeForEthereum = ethereumType => ascTypeForProtocol('ethereum', ethereumType)
76
-
77
- const ethereumTypeForAsc = ascType =>
78
- findConversionFromType('AssemblyScript', 'ethereum', ascType).getIn(['to', 'type'])
79
-
80
- const ethereumToAsc = (code, ethereumType, internalType) =>
90
+ export const ascTypeForEthereum = (ethereumType: string) =>
91
+ ascTypeForProtocol('ethereum', ethereumType)
92
+
93
+ export const ethereumTypeForAsc = (ascType: string) =>
94
+ findConversionFromType('AssemblyScript', 'ethereum', ascType).getIn([
95
+ 'to',
96
+ 'type',
97
+ ]) as string
98
+
99
+ export const ethereumToAsc = (
100
+ code: string,
101
+ ethereumType: string,
102
+ internalType?: string,
103
+ ) =>
81
104
  findConversionFromType('ethereum', 'AssemblyScript', ethereumType).get('convert')(
82
105
  code,
83
106
  internalType,
84
107
  )
85
108
 
86
- const ethereumFromAsc = (code, ethereumType) =>
109
+ export const ethereumFromAsc = (code: string, ethereumType: string) =>
87
110
  findConversionToType('AssemblyScript', 'ethereum', ethereumType).get('convert')(code)
88
111
 
89
- const ascTypeForValue = valueType =>
112
+ export const ascTypeForValue = (valueType: string) =>
90
113
  findConversionFromType('Value', 'AssemblyScript', valueType).getIn(['to', 'type'])
91
114
 
92
- const valueTypeForAsc = ascType =>
115
+ export const valueTypeForAsc = (ascType: string) =>
93
116
  findConversionFromType('AssemblyScript', 'Value', ascType).getIn(['to', 'type'])
94
117
 
95
- const valueToAsc = (code, valueType) =>
118
+ export const valueToAsc = (code: string, valueType: string) =>
96
119
  findConversionFromType('Value', 'AssemblyScript', valueType).get('convert')(code)
97
120
 
98
- const valueFromAsc = (code, valueType) =>
121
+ export const valueFromAsc = (code: string, valueType: string) =>
99
122
  findConversionToType('AssemblyScript', 'Value', valueType).get('convert')(code)
100
-
101
- export {
102
- // protocol <-> AssemblyScript
103
- ascTypeForProtocol,
104
- // ethereum <-> AssemblyScript
105
- ascTypeForEthereum,
106
- ethereumTypeForAsc,
107
- ethereumToAsc,
108
- ethereumFromAsc,
109
- // Value <-> AssemblyScript
110
- ascTypeForValue,
111
- valueTypeForAsc,
112
- valueToAsc,
113
- valueFromAsc,
114
- }
@@ -1,5 +1,4 @@
1
- import immutable from 'immutable'
2
- import Scaffold from './'
1
+ import Scaffold from './index'
3
2
  import Protocol from '../protocols'
4
3
 
5
4
  const protocol = new Protocol('cosmos')
@@ -1,6 +1,6 @@
1
1
  import ABI from '../protocols/ethereum/abi'
2
2
  import immutable from 'immutable'
3
- import Scaffold from './'
3
+ import Scaffold from './index'
4
4
  import Protocol from '../protocols'
5
5
 
6
6
  const TEST_EVENT = {
@@ -262,8 +262,8 @@ export function handleExampleEvent1(event: ExampleEvent1Event): void {
262
262
 
263
263
  test('Test Files (default)', () => {
264
264
  const files = scaffoldWithIndexEvents.generateTests()
265
- const testFile = files['contract.test.ts']
266
- const utilsFile = files['contract-utils.ts']
265
+ const testFile = files?.['contract.test.ts']
266
+ const utilsFile = files?.['contract-utils.ts']
267
267
  expect(testFile).toEqual(`\
268
268
  import {
269
269
  assert,
@@ -391,8 +391,8 @@ export function createExampleEvent1Event(a: Bytes): ExampleEvent1 {
391
391
 
392
392
  test('Test Files (for indexing events)', () => {
393
393
  const files = scaffoldWithIndexEvents.generateTests()
394
- const testFile = files['contract.test.ts']
395
- const utilsFile = files['contract-utils.ts']
394
+ const testFile = files?.['contract.test.ts']
395
+ const utilsFile = files?.['contract-utils.ts']
396
396
 
397
397
  expect(testFile).toEqual(`\
398
398
  import {
@@ -1,5 +1,4 @@
1
1
  import prettier from 'prettier'
2
- const pkginfo = require('pkginfo')(module)
3
2
  import { strings } from 'gluegun'
4
3
 
5
4
  const GRAPH_CLI_VERSION = process.env.GRAPH_CLI_TESTS
@@ -12,10 +11,32 @@ const GRAPH_CLI_VERSION = process.env.GRAPH_CLI_TESTS
12
11
  import { abiEvents, generateEventType, generateExampleEntityType } from './schema'
13
12
  import { generateEventIndexingHandlers } from './mapping'
14
13
  import { generateTestsFiles } from './tests'
14
+ import Protocol from '../protocols'
15
15
  import { getSubgraphBasename } from '../command-helpers/subgraph'
16
+ import ABI from '../protocols/ethereum/abi'
17
+
18
+ export interface ScaffoldOptions {
19
+ protocol: Protocol
20
+ abi?: ABI
21
+ indexEvents?: boolean
22
+ contract?: string
23
+ network: string
24
+ contractName: string
25
+ subgraphName?: string
26
+ node?: string
27
+ }
16
28
 
17
29
  export default class Scaffold {
18
- constructor(options = {}) {
30
+ protocol: Protocol
31
+ abi?: ABI
32
+ indexEvents?: boolean
33
+ contract?: string
34
+ network: string
35
+ contractName: string
36
+ subgraphName?: string
37
+ node?: string
38
+
39
+ constructor(options: ScaffoldOptions) {
19
40
  this.protocol = options.protocol
20
41
  this.abi = options.abi
21
42
  this.indexEvents = options.indexEvents
@@ -29,7 +50,7 @@ export default class Scaffold {
29
50
  generatePackageJson() {
30
51
  return prettier.format(
31
52
  JSON.stringify({
32
- name: getSubgraphBasename(this.subgraphName),
53
+ name: getSubgraphBasename(String(this.subgraphName)),
33
54
  license: 'UNLICENSED',
34
55
  scripts: {
35
56
  codegen: 'graph codegen',
@@ -81,7 +102,9 @@ dataSources:
81
102
 
82
103
  return prettier.format(
83
104
  hasEvents && this.indexEvents
84
- ? events.map(event => generateEventType(event, this.protocol.name)).join('\n\n')
105
+ ? events
106
+ .map((event: any) => generateEventType(event, this.protocol.name))
107
+ .join('\n\n')
85
108
  : generateExampleEntityType(this.protocol, events),
86
109
  {
87
110
  parser: 'graphql',
@@ -124,7 +147,7 @@ dataSources:
124
147
  generateABIs() {
125
148
  return this.protocol.hasABIs()
126
149
  ? {
127
- [`${this.contractName}.json`]: prettier.format(JSON.stringify(this.abi.data), {
150
+ [`${this.contractName}.json`]: prettier.format(JSON.stringify(this.abi?.data), {
128
151
  parser: 'json',
129
152
  }),
130
153
  }
@@ -1,24 +1,29 @@
1
- import path from 'path'
2
-
3
1
  import * as util from '../codegen/util'
4
2
 
5
- const generateFieldAssignment = path =>
3
+ export const generateFieldAssignment = (path: string[]) =>
6
4
  `entity.${path.join('_')} = event.params.${path.join('.')}`
7
5
 
8
- const generateFieldAssignments = ({ index, input }) =>
6
+ export const generateFieldAssignments = ({
7
+ index,
8
+ input,
9
+ }: {
10
+ index: number
11
+ input: any
12
+ }) =>
9
13
  input.type === 'tuple'
10
14
  ? util
11
15
  .unrollTuple({ value: input, index, path: [input.name || `param${index}`] })
12
- .map(({ path }) => generateFieldAssignment(path))
16
+ .map(({ path }: any) => generateFieldAssignment(path))
13
17
  : generateFieldAssignment([input.name || `param${index}`])
14
18
 
15
- const generateEventFieldAssignments = event =>
19
+ export const generateEventFieldAssignments = (event: any) =>
16
20
  event.inputs.reduce(
17
- (acc, input, index) => acc.concat(generateFieldAssignments({ input, index })),
21
+ (acc: any[], input: any, index: number) =>
22
+ acc.concat(generateFieldAssignments({ input, index })),
18
23
  [],
19
24
  )
20
25
 
21
- const generateEventIndexingHandlers = (events, contractName) =>
26
+ export const generateEventIndexingHandlers = (events: any[], contractName: string) =>
22
27
  `
23
28
  import { ${events.map(
24
29
  event => `${event._alias} as ${event._alias}Event`,
@@ -45,10 +50,3 @@ const generateEventIndexingHandlers = (events, contractName) =>
45
50
  )
46
51
  .join('\n')}
47
52
  `
48
-
49
- export {
50
- generateFieldAssignment,
51
- generateFieldAssignments,
52
- generateEventFieldAssignments,
53
- generateEventIndexingHandlers,
54
- }
@@ -1,5 +1,4 @@
1
- import immutable from 'immutable'
2
- import Scaffold from './'
1
+ import Scaffold from './index'
3
2
  import Protocol from '../protocols'
4
3
 
5
4
  const protocol = new Protocol('near')
@@ -1,26 +1,42 @@
1
1
  import { ascTypeForProtocol, valueTypeForAsc } from '../codegen/types'
2
2
  import * as util from '../codegen/util'
3
+ import Protocol from '../protocols'
3
4
 
4
- const abiEvents = abi =>
5
+ export const abiEvents = (abi: any) =>
5
6
  util.disambiguateNames({
6
- values: abi.data.filter(item => item.get('type') === 'event'),
7
- getName: event => event.get('name'),
8
- setName: (event, name) => event.set('_alias', name),
7
+ values: abi.data.filter((item: any) => item.get('type') === 'event'),
8
+ getName: (event: any) => event.get('name'),
9
+ setName: (event: any, name: string) => event.set('_alias', name),
9
10
  })
10
11
 
11
- const protocolTypeToGraphQL = (protocol, name) => {
12
+ export const protocolTypeToGraphQL = (protocol: string, name: string) => {
12
13
  let ascType = ascTypeForProtocol(protocol, name)
13
14
  return valueTypeForAsc(ascType)
14
15
  }
15
16
 
16
- const generateField = ({ name, type, protocolName }) =>
17
- `${name}: ${protocolTypeToGraphQL(protocolName, type)}! # ${type}`
17
+ export const generateField = ({
18
+ name,
19
+ type,
20
+ protocolName,
21
+ }: {
22
+ name: string
23
+ type: string
24
+ protocolName: string
25
+ }) => `${name}: ${protocolTypeToGraphQL(protocolName, type)}! # ${type}`
18
26
 
19
- const generateEventFields = ({ index, input, protocolName }) =>
27
+ export const generateEventFields = ({
28
+ index,
29
+ input,
30
+ protocolName,
31
+ }: {
32
+ index: number
33
+ input: any
34
+ protocolName: string
35
+ }) =>
20
36
  input.type == 'tuple'
21
37
  ? util
22
38
  .unrollTuple({ value: input, path: [input.name || `param${index}`], index })
23
- .map(({ path, type }) =>
39
+ .map(({ path, type }: any) =>
24
40
  generateField({ name: path.join('_'), type, protocolName }),
25
41
  )
26
42
  : [
@@ -31,13 +47,13 @@ const generateEventFields = ({ index, input, protocolName }) =>
31
47
  }),
32
48
  ]
33
49
 
34
- const generateEventType = (event, protocolName) => `type ${
50
+ export const generateEventType = (event: any, protocolName: string) => `type ${
35
51
  event._alias
36
52
  } @entity(immutable: true) {
37
53
  id: Bytes!
38
54
  ${event.inputs
39
55
  .reduce(
40
- (acc, input, index) =>
56
+ (acc: any[], input: any, index: number) =>
41
57
  acc.concat(generateEventFields({ input, index, protocolName })),
42
58
  [],
43
59
  )
@@ -47,14 +63,14 @@ const generateEventType = (event, protocolName) => `type ${
47
63
  transactionHash: Bytes!
48
64
  }`
49
65
 
50
- const generateExampleEntityType = (protocol, events) => {
66
+ export const generateExampleEntityType = (protocol: Protocol, events: any[]) => {
51
67
  if (protocol.hasABIs() && events.length > 0) {
52
68
  return `type ExampleEntity @entity {
53
69
  id: Bytes!
54
70
  count: BigInt!
55
71
  ${events[0].inputs
56
72
  .reduce(
57
- (acc, input, index) =>
73
+ (acc: any[], input: any, index: number) =>
58
74
  acc.concat(generateEventFields({ input, index, protocolName: protocol.name })),
59
75
  [],
60
76
  )
@@ -69,12 +85,3 @@ const generateExampleEntityType = (protocol, events) => {
69
85
  }`
70
86
  }
71
87
  }
72
-
73
- export {
74
- abiEvents,
75
- protocolTypeToGraphQL,
76
- generateField,
77
- generateEventFields,
78
- generateEventType,
79
- generateExampleEntityType,
80
- }
@@ -11,14 +11,18 @@ const VARIABLES_VALUES = {
11
11
  bool: true,
12
12
  }
13
13
 
14
- const generateTestsFiles = (contract, events, indexEvents) => {
14
+ export const generateTestsFiles = (
15
+ contract: any,
16
+ events: any[],
17
+ indexEvents?: boolean,
18
+ ) => {
15
19
  const eventsTypes = events
16
20
  .flatMap(event =>
17
- event.inputs.map(input => {
21
+ event.inputs.map((input: any) => {
18
22
  // If the asc type is Array<T> we need to check if T is a native type or a custom graph-ts type
19
23
  // If we don't do that we may miss a type that should be imported from graph-ts
20
24
  const ascType = ascTypeForEthereum(input.type)
21
- const inner = fetchArrayInnerType(ascType)
25
+ const inner = fetchArrayInnerType(String(ascType))
22
26
  return inner ? inner[1] : ascType
23
27
  }),
24
28
  )
@@ -44,17 +48,17 @@ const generateTestsFiles = (contract, events, indexEvents) => {
44
48
  let displayName = "Example string value"
45
49
  let imageUrl = "Example string value"
46
50
  */
47
- const generateArguments = eventInputs => {
51
+ const generateArguments = (eventInputs: any[]) => {
48
52
  return eventInputs
49
53
  .map((input, index) => {
50
54
  let ascType = ascTypeForEthereum(input.type)
51
- return `let ${input.name || `param${index}`} = ${assignValue(ascType, input.name)}`
55
+ return `let ${input.name || `param${index}`} = ${assignValue(ascType)}`
52
56
  })
53
57
  .join('\n')
54
58
  }
55
59
 
56
60
  // Generates the value that will be assigned to a variable in generateArguments()
57
- const assignValue = type => {
61
+ const assignValue = (type: string): string | number | boolean => {
58
62
  switch (type) {
59
63
  case 'string':
60
64
  return `"${VARIABLES_VALUES[type]}"`
@@ -65,10 +69,10 @@ const assignValue = type => {
65
69
  case 'Bytes':
66
70
  return `Bytes.fromI32(${VARIABLES_VALUES[type]})`
67
71
  case fetchArrayInnerType(type)?.input:
68
- const innerType = fetchArrayInnerType(type)[1]
72
+ const innerType = fetchArrayInnerType(type)![1]
69
73
  return `[${assignValue(innerType)}]`
70
74
  default:
71
- let value = VARIABLES_VALUES[type]
75
+ let value = VARIABLES_VALUES[type as keyof typeof VARIABLES_VALUES]
72
76
  return value ? value : `"${type} Not implemented"`
73
77
  }
74
78
  }
@@ -82,7 +86,11 @@ const assignValue = type => {
82
86
  "0x0000000000000000000000000000000000000001"
83
87
  )
84
88
  */
85
- const generateFieldsAssertions = (entity, eventInputs, indexEvents) =>
89
+ const generateFieldsAssertions = (
90
+ entity: string,
91
+ eventInputs: any[],
92
+ indexEvents?: boolean,
93
+ ) =>
86
94
  eventInputs
87
95
  .filter(input => input.name != 'id')
88
96
  .map(
@@ -97,28 +105,33 @@ const generateFieldsAssertions = (entity, eventInputs, indexEvents) =>
97
105
  .join('\n')
98
106
 
99
107
  // Returns the expected value for a given type in generateFieldsAssertions()
100
- const expectedValue = type => {
108
+ const expectedValue = (type: string): string | number | boolean => {
101
109
  switch (type) {
102
110
  case fetchArrayInnerType(type)?.input:
103
- const innerType = fetchArrayInnerType(type)[1]
111
+ const innerType = fetchArrayInnerType(type)![1]
104
112
  return `[${expectedValue(innerType)}]`
105
113
  default:
106
- let value = VARIABLES_VALUES[type]
114
+ let value = VARIABLES_VALUES[type as keyof typeof VARIABLES_VALUES]
107
115
  return value ? value : `${type} Not implemented`
108
116
  }
109
117
  }
110
118
 
111
119
  // Checks if the type is a native AS type or should be imported from graph-ts
112
- const isNativeType = type => {
120
+ const isNativeType = (type: string) => {
113
121
  let natives = [/i32/, /string/, /boolean/]
114
122
 
115
123
  return natives.some(rx => rx.test(type))
116
124
  }
117
125
 
118
- const fetchArrayInnerType = type => type.match(/Array<(.*?)>/)
126
+ const fetchArrayInnerType = (type: string) => type.match(/Array<(.*?)>/)
119
127
 
120
128
  // Generates the example test.ts file
121
- const generateExampleTest = (contract, event, indexEvents, importTypes) => {
129
+ const generateExampleTest = (
130
+ contract: string,
131
+ event: any,
132
+ indexEvents?: boolean,
133
+ importTypes?: string,
134
+ ) => {
122
135
  const entity = indexEvents ? `${event._alias}` : 'ExampleEntity'
123
136
  const eventInputs = event.inputs
124
137
  const eventName = event._alias
@@ -141,7 +154,7 @@ const generateExampleTest = (contract, event, indexEvents, importTypes) => {
141
154
  beforeAll(() => {
142
155
  ${generateArguments(eventInputs)}
143
156
  let new${eventName}Event = create${eventName}Event(${eventInputs
144
- .map((input, index) => input.name || `param${index}`)
157
+ .map((input: any, index: number) => input.name || `param${index}`)
145
158
  .join(', ')});
146
159
  handle${eventName}(new${eventName}Event)
147
160
  })
@@ -167,7 +180,7 @@ const generateExampleTest = (contract, event, indexEvents, importTypes) => {
167
180
  }
168
181
 
169
182
  // Generates the utils helper file
170
- const generateTestHelper = (contract, events, importTypes) => {
183
+ const generateTestHelper = (contract: string, events: any[], importTypes: string) => {
171
184
  const eventsNames = events.map(event => event._alias)
172
185
 
173
186
  return `
@@ -178,20 +191,22 @@ const generateTestHelper = (contract, events, importTypes) => {
178
191
  ${generateMockedEvents(events).join('\n')}`
179
192
  }
180
193
 
181
- const generateMockedEvents = events =>
194
+ const generateMockedEvents = (events: any[]) =>
182
195
  events.reduce((acc, event) => acc.concat(generateMockedEvent(event)), [])
183
196
 
184
- const generateMockedEvent = event => {
197
+ const generateMockedEvent = (event: any) => {
185
198
  const varName = `${strings.camelCase(event._alias)}Event`
186
199
  const fnArgs = event.inputs.map(
187
- (input, index) =>
200
+ (input: any, index: number) =>
188
201
  `${input.name || `param${index}`}: ${ascTypeForEthereum(input.type)}`,
189
202
  )
190
203
  const ascToEth = event.inputs.map(
191
- (input, index) =>
192
- `${varName}.parameters.push(new ethereum.EventParam("${
193
- input.name || `param${index}`
194
- }", ${ethereumFromAsc(input.name || `param${index}`, input.type)}))`,
204
+ (input: any, index: number) =>
205
+ `${varName}.parameters.push(new ethereum.EventParam("${input.name ||
206
+ `param${index}`}", ${ethereumFromAsc(
207
+ input.name || `param${index}`,
208
+ input.type,
209
+ )}))`,
195
210
  )
196
211
 
197
212
  return `
@@ -206,5 +221,3 @@ const generateMockedEvent = event => {
206
221
  }
207
222
  `
208
223
  }
209
-
210
- export { generateTestsFiles }