@graphprotocol/graph-cli 0.37.3 → 0.37.4

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 (267) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/package.json +5 -1
  3. package/babel.config.js +0 -3
  4. package/jest.config.js +0 -175
  5. package/src/bin.ts +0 -6
  6. package/src/cli.ts +0 -30
  7. package/src/codegen/schema.test.ts +0 -460
  8. package/src/codegen/schema.ts +0 -299
  9. package/src/codegen/template.ts +0 -45
  10. package/src/codegen/types/conversions.ts +0 -358
  11. package/src/codegen/types/index.test.ts +0 -715
  12. package/src/codegen/types/index.ts +0 -111
  13. package/src/codegen/typescript.ts +0 -224
  14. package/src/codegen/util.test.ts +0 -112
  15. package/src/codegen/util.ts +0 -54
  16. package/src/command-helpers/abi.ts +0 -99
  17. package/src/command-helpers/auth.ts +0 -46
  18. package/src/command-helpers/compiler.ts +0 -60
  19. package/src/command-helpers/data-sources.ts +0 -36
  20. package/src/command-helpers/fs.ts +0 -5
  21. package/src/command-helpers/gluegun.ts +0 -52
  22. package/src/command-helpers/ipfs-http-client.d.ts +0 -1
  23. package/src/command-helpers/ipfs.ts +0 -3
  24. package/src/command-helpers/jsonrpc.ts +0 -22
  25. package/src/command-helpers/network.test.ts +0 -85
  26. package/src/command-helpers/network.ts +0 -133
  27. package/src/command-helpers/node.ts +0 -46
  28. package/src/command-helpers/scaffold.ts +0 -174
  29. package/src/command-helpers/spinner.ts +0 -60
  30. package/src/command-helpers/studio.test.ts +0 -84
  31. package/src/command-helpers/studio.ts +0 -25
  32. package/src/command-helpers/subgraph.ts +0 -21
  33. package/src/command-helpers/version.test.ts +0 -241
  34. package/src/command-helpers/version.ts +0 -53
  35. package/src/commands/add.ts +0 -220
  36. package/src/commands/auth.ts +0 -138
  37. package/src/commands/binary-install-raw.d.ts +0 -1
  38. package/src/commands/build.ts +0 -147
  39. package/src/commands/codegen.ts +0 -123
  40. package/src/commands/create.ts +0 -109
  41. package/src/commands/deploy.ts +0 -388
  42. package/src/commands/init.ts +0 -1060
  43. package/src/commands/local.ts +0 -539
  44. package/src/commands/remove.ts +0 -110
  45. package/src/commands/test.ts +0 -369
  46. package/src/compiler/asc.ts +0 -77
  47. package/src/compiler/index.ts +0 -677
  48. package/src/debug.ts +0 -15
  49. package/src/migrations/mapping_api_version_0_0_1.ts +0 -75
  50. package/src/migrations/mapping_api_version_0_0_2.ts +0 -75
  51. package/src/migrations/mapping_api_version_0_0_3.ts +0 -75
  52. package/src/migrations/mapping_api_version_0_0_4.ts +0 -77
  53. package/src/migrations/mapping_api_version_0_0_5.ts +0 -77
  54. package/src/migrations/spec_version_0_0_2.ts +0 -24
  55. package/src/migrations/spec_version_0_0_3.ts +0 -29
  56. package/src/migrations/util/load-manifest.ts +0 -10
  57. package/src/migrations/util/versions.ts +0 -27
  58. package/src/migrations.ts +0 -33
  59. package/src/protocols/arweave/manifest.graphql +0 -57
  60. package/src/protocols/arweave/scaffold/manifest.ts +0 -14
  61. package/src/protocols/arweave/scaffold/mapping.ts +0 -49
  62. package/src/protocols/arweave/subgraph.ts +0 -22
  63. package/src/protocols/contract.ts +0 -9
  64. package/src/protocols/cosmos/manifest.graphql +0 -76
  65. package/src/protocols/cosmos/scaffold/manifest.ts +0 -11
  66. package/src/protocols/cosmos/scaffold/mapping.ts +0 -35
  67. package/src/protocols/cosmos/subgraph.ts +0 -27
  68. package/src/protocols/ethereum/abi.ts +0 -153
  69. package/src/protocols/ethereum/codegen/abi.test.ts +0 -330
  70. package/src/protocols/ethereum/codegen/abi.ts +0 -719
  71. package/src/protocols/ethereum/codegen/template.ts +0 -41
  72. package/src/protocols/ethereum/contract.ts +0 -24
  73. package/src/protocols/ethereum/manifest.graphql +0 -94
  74. package/src/protocols/ethereum/scaffold/manifest.ts +0 -28
  75. package/src/protocols/ethereum/scaffold/mapping.ts +0 -76
  76. package/src/protocols/ethereum/subgraph.ts +0 -189
  77. package/src/protocols/ethereum/type-generator.ts +0 -191
  78. package/src/protocols/index.ts +0 -287
  79. package/src/protocols/ipfs/codegen/file_template.ts +0 -43
  80. package/src/protocols/near/contract.ts +0 -47
  81. package/src/protocols/near/manifest.graphql +0 -64
  82. package/src/protocols/near/scaffold/manifest.ts +0 -13
  83. package/src/protocols/near/scaffold/mapping.ts +0 -35
  84. package/src/protocols/near/subgraph.ts +0 -22
  85. package/src/protocols/subgraph.ts +0 -12
  86. package/src/protocols/substreams/manifest.graphql +0 -45
  87. package/src/protocols/substreams/scaffold/manifest.ts +0 -8
  88. package/src/protocols/substreams/subgraph.ts +0 -22
  89. package/src/scaffold/cosmos.test.ts +0 -83
  90. package/src/scaffold/ethereum.test.ts +0 -512
  91. package/src/scaffold/index.ts +0 -172
  92. package/src/scaffold/mapping.ts +0 -44
  93. package/src/scaffold/near.test.ts +0 -86
  94. package/src/scaffold/schema.ts +0 -89
  95. package/src/scaffold/tests.ts +0 -217
  96. package/src/schema.ts +0 -26
  97. package/src/subgraph.ts +0 -302
  98. package/src/type-generator.ts +0 -266
  99. package/src/validation/contract.ts +0 -51
  100. package/src/validation/index.ts +0 -5
  101. package/src/validation/manifest.ts +0 -360
  102. package/src/validation/schema.test.ts +0 -38
  103. package/src/validation/schema.ts +0 -957
  104. package/src/watcher.ts +0 -96
  105. package/tests/cli/globalSetup.ts +0 -8
  106. package/tests/cli/globalTeardown.ts +0 -8
  107. package/tests/cli/init/ethereum/abis/Marketplace.json +0 -408
  108. package/tests/cli/init/ethereum/abis/OverloadedElements.json +0 -91
  109. package/tests/cli/init/ethereum/abis/SoloMargin.json +0 -2729
  110. package/tests/cli/init/ethereum/from-contract-with-abi-and-structs.stderr +0 -14
  111. package/tests/cli/init/ethereum/from-contract-with-abi-and-structs.stdout +0 -12
  112. package/tests/cli/init/ethereum/from-contract-with-abi.stderr +0 -14
  113. package/tests/cli/init/ethereum/from-contract-with-abi.stdout +0 -12
  114. package/tests/cli/init/ethereum/from-contract-with-overloaded-elements.stderr +0 -14
  115. package/tests/cli/init/ethereum/from-contract-with-overloaded-elements.stdout +0 -12
  116. package/tests/cli/init/ethereum/from-contract.stderr +0 -16
  117. package/tests/cli/init/ethereum/from-contract.stdout +0 -12
  118. package/tests/cli/init/ethereum/from-example.stderr +0 -12
  119. package/tests/cli/init/ethereum/from-example.stdout +0 -12
  120. package/tests/cli/init/near/from-contract.stderr +0 -14
  121. package/tests/cli/init/near/from-contract.stdout +0 -12
  122. package/tests/cli/init.test.ts +0 -157
  123. package/tests/cli/spawn-command.d.ts +0 -7
  124. package/tests/cli/util.ts +0 -132
  125. package/tests/cli/validation/abi-not-found/Abi.json +0 -7
  126. package/tests/cli/validation/abi-not-found/mapping.ts +0 -0
  127. package/tests/cli/validation/abi-not-found/schema.graphql +0 -3
  128. package/tests/cli/validation/abi-not-found/subgraph.yaml +0 -42
  129. package/tests/cli/validation/abi-not-found copy.stderr +0 -13
  130. package/tests/cli/validation/big-decimal-is-valid/Abi.json +0 -7
  131. package/tests/cli/validation/big-decimal-is-valid/mapping.ts +0 -0
  132. package/tests/cli/validation/big-decimal-is-valid/schema.graphql +0 -4
  133. package/tests/cli/validation/big-decimal-is-valid/subgraph.yaml +0 -23
  134. package/tests/cli/validation/call-function-not-found/Abi.json +0 -57
  135. package/tests/cli/validation/call-function-not-found/mapping.ts +0 -0
  136. package/tests/cli/validation/call-function-not-found/schema.graphql +0 -3
  137. package/tests/cli/validation/call-function-not-found/subgraph.yaml +0 -22
  138. package/tests/cli/validation/call-function-not-found.stderr +0 -10
  139. package/tests/cli/validation/call-handler-with-tuple/Abi.json +0 -24
  140. package/tests/cli/validation/call-handler-with-tuple/generated/schema.ts +0 -44
  141. package/tests/cli/validation/call-handler-with-tuple/mapping.ts +0 -0
  142. package/tests/cli/validation/call-handler-with-tuple/schema.graphql +0 -3
  143. package/tests/cli/validation/call-handler-with-tuple/subgraph.yaml +0 -22
  144. package/tests/cli/validation/call-handler-with-tuple.stderr +0 -24
  145. package/tests/cli/validation/conflicting-network-names/Abi.json +0 -7
  146. package/tests/cli/validation/conflicting-network-names/mapping.ts +0 -0
  147. package/tests/cli/validation/conflicting-network-names/schema.graphql +0 -3
  148. package/tests/cli/validation/conflicting-network-names/subgraph.yaml +0 -41
  149. package/tests/cli/validation/conflicting-network-names.stderr +0 -10
  150. package/tests/cli/validation/conflicting-protocol-names/Abi.json +0 -7
  151. package/tests/cli/validation/conflicting-protocol-names/mapping.ts +0 -0
  152. package/tests/cli/validation/conflicting-protocol-names/schema.graphql +0 -3
  153. package/tests/cli/validation/conflicting-protocol-names/subgraph.yaml +0 -41
  154. package/tests/cli/validation/conflicting-protocol-names.stderr +0 -10
  155. package/tests/cli/validation/derived-from-target-type-missing/Abi.json +0 -6
  156. package/tests/cli/validation/derived-from-target-type-missing/mapping.ts +0 -0
  157. package/tests/cli/validation/derived-from-target-type-missing/schema.graphql +0 -4
  158. package/tests/cli/validation/derived-from-target-type-missing/subgraph.yaml +0 -22
  159. package/tests/cli/validation/derived-from-target-type-missing.stderr +0 -5
  160. package/tests/cli/validation/derived-from-with-interface/Gravity.json +0 -1
  161. package/tests/cli/validation/derived-from-with-interface/mapping.ts +0 -22
  162. package/tests/cli/validation/derived-from-with-interface/schema.graphql +0 -19
  163. package/tests/cli/validation/derived-from-with-interface/subgraph.yaml +0 -25
  164. package/tests/cli/validation/duplicate-data-source-name/Abi.json +0 -7
  165. package/tests/cli/validation/duplicate-data-source-name/mapping.ts +0 -0
  166. package/tests/cli/validation/duplicate-data-source-name/schema.graphql +0 -4
  167. package/tests/cli/validation/duplicate-data-source-name/subgraph.yaml +0 -40
  168. package/tests/cli/validation/duplicate-data-source-name.stderr +0 -5
  169. package/tests/cli/validation/duplicate-template-name/Abi.json +0 -7
  170. package/tests/cli/validation/duplicate-template-name/mapping.ts +0 -0
  171. package/tests/cli/validation/duplicate-template-name/schema.graphql +0 -4
  172. package/tests/cli/validation/duplicate-template-name/subgraph.yaml +0 -59
  173. package/tests/cli/validation/duplicate-template-name.stderr +0 -5
  174. package/tests/cli/validation/entity-field-args/Abi.json +0 -7
  175. package/tests/cli/validation/entity-field-args/mapping.ts +0 -0
  176. package/tests/cli/validation/entity-field-args/schema.graphql +0 -4
  177. package/tests/cli/validation/entity-field-args/subgraph.yaml +0 -22
  178. package/tests/cli/validation/entity-field-args.stderr +0 -5
  179. package/tests/cli/validation/ethereum-contract-without-handlers/Abi.json +0 -7
  180. package/tests/cli/validation/ethereum-contract-without-handlers/mapping.ts +0 -0
  181. package/tests/cli/validation/ethereum-contract-without-handlers/schema.graphql +0 -3
  182. package/tests/cli/validation/ethereum-contract-without-handlers/subgraph.yaml +0 -19
  183. package/tests/cli/validation/ethereum-contract-without-handlers.stderr +0 -6
  184. package/tests/cli/validation/event-not-found/Abi.json +0 -7
  185. package/tests/cli/validation/event-not-found/mapping.ts +0 -0
  186. package/tests/cli/validation/event-not-found/schema.graphql +0 -3
  187. package/tests/cli/validation/event-not-found/subgraph.yaml +0 -40
  188. package/tests/cli/validation/event-not-found.stderr +0 -12
  189. package/tests/cli/validation/example-values-found/Abi.json +0 -7
  190. package/tests/cli/validation/example-values-found/generated/ExampleSubgraph/ExampleContract.ts +0 -35
  191. package/tests/cli/validation/example-values-found/generated/schema.ts +0 -44
  192. package/tests/cli/validation/example-values-found/mapping.ts +0 -0
  193. package/tests/cli/validation/example-values-found/schema.graphql +0 -3
  194. package/tests/cli/validation/example-values-found/subgraph.yaml +0 -24
  195. package/tests/cli/validation/example-values-found.stderr +0 -34
  196. package/tests/cli/validation/invalid-abis/InvalidAbi.json +0 -1
  197. package/tests/cli/validation/invalid-abis/mapping.ts +0 -0
  198. package/tests/cli/validation/invalid-abis/schema.graphql +0 -3
  199. package/tests/cli/validation/invalid-abis/subgraph.yaml +0 -22
  200. package/tests/cli/validation/invalid-abis.stderr +0 -5
  201. package/tests/cli/validation/invalid-contract-addresses/Abi.json +0 -7
  202. package/tests/cli/validation/invalid-contract-addresses/mapping.ts +0 -0
  203. package/tests/cli/validation/invalid-contract-addresses/schema.graphql +0 -3
  204. package/tests/cli/validation/invalid-contract-addresses/subgraph.yaml +0 -58
  205. package/tests/cli/validation/invalid-contract-addresses.stderr +0 -14
  206. package/tests/cli/validation/invalid-data-source-template/mapping.ts +0 -0
  207. package/tests/cli/validation/invalid-data-source-template/schema.graphql +0 -3
  208. package/tests/cli/validation/invalid-data-source-template/subgraph.yaml +0 -32
  209. package/tests/cli/validation/invalid-data-source-template.stderr +0 -30
  210. package/tests/cli/validation/invalid-entity-field-types/Abi.json +0 -7
  211. package/tests/cli/validation/invalid-entity-field-types/mapping.ts +0 -0
  212. package/tests/cli/validation/invalid-entity-field-types/schema.graphql +0 -157
  213. package/tests/cli/validation/invalid-entity-field-types/subgraph.yaml +0 -22
  214. package/tests/cli/validation/invalid-entity-field-types.stderr +0 -148
  215. package/tests/cli/validation/invalid-fulltext-directive/Abi.json +0 -7
  216. package/tests/cli/validation/invalid-fulltext-directive/mapping.ts +0 -0
  217. package/tests/cli/validation/invalid-fulltext-directive/schema.graphql +0 -140
  218. package/tests/cli/validation/invalid-fulltext-directive/subgraph.yaml +0 -22
  219. package/tests/cli/validation/invalid-fulltext-directive.stderr +0 -32
  220. package/tests/cli/validation/invalid-graphql-schema/Abi.json +0 -7
  221. package/tests/cli/validation/invalid-graphql-schema/mapping.ts +0 -0
  222. package/tests/cli/validation/invalid-graphql-schema/schema.graphql +0 -8
  223. package/tests/cli/validation/invalid-graphql-schema/subgraph.yaml +0 -22
  224. package/tests/cli/validation/invalid-graphql-schema.stderr +0 -25
  225. package/tests/cli/validation/invalid-manifest/subgraph.yaml +0 -13
  226. package/tests/cli/validation/invalid-manifest-cannot-infer-protocol/subgraph.yaml +0 -12
  227. package/tests/cli/validation/invalid-manifest-cannot-infer-protocol.stderr +0 -5
  228. package/tests/cli/validation/invalid-manifest.stderr +0 -28
  229. package/tests/cli/validation/missing-entity-id/Abi.json +0 -7
  230. package/tests/cli/validation/missing-entity-id/mapping.ts +0 -0
  231. package/tests/cli/validation/missing-entity-id/schema.graphql +0 -3
  232. package/tests/cli/validation/missing-entity-id/subgraph.yaml +0 -22
  233. package/tests/cli/validation/missing-entity-id/types/ExampleSubgraph/ExampleContract.ts +0 -35
  234. package/tests/cli/validation/missing-entity-id/types/schema.ts +0 -27
  235. package/tests/cli/validation/missing-entity-id.stderr +0 -5
  236. package/tests/cli/validation/missing-or-invalid-derived-from-fields/Abi.json +0 -7
  237. package/tests/cli/validation/missing-or-invalid-derived-from-fields/mapping.ts +0 -0
  238. package/tests/cli/validation/missing-or-invalid-derived-from-fields/schema.graphql +0 -40
  239. package/tests/cli/validation/missing-or-invalid-derived-from-fields/subgraph.yaml +0 -22
  240. package/tests/cli/validation/missing-or-invalid-derived-from-fields.stderr +0 -10
  241. package/tests/cli/validation/missing-or-invalid-derived-from-fields.stdout +0 -0
  242. package/tests/cli/validation/near-is-valid/mapping.ts +0 -0
  243. package/tests/cli/validation/near-is-valid/schema.graphql +0 -8
  244. package/tests/cli/validation/near-is-valid/subgraph.yaml +0 -30
  245. package/tests/cli/validation/nested-template-nice-error/Abi.json +0 -7
  246. package/tests/cli/validation/nested-template-nice-error/mapping.ts +0 -0
  247. package/tests/cli/validation/nested-template-nice-error/schema.graphql +0 -3
  248. package/tests/cli/validation/nested-template-nice-error/subgraph.yaml +0 -35
  249. package/tests/cli/validation/nested-template-nice-error.stderr +0 -5
  250. package/tests/cli/validation/no-network-names/Abi.json +0 -7
  251. package/tests/cli/validation/no-network-names/mapping.ts +0 -0
  252. package/tests/cli/validation/no-network-names/schema.graphql +0 -3
  253. package/tests/cli/validation/no-network-names/subgraph.yaml +0 -40
  254. package/tests/cli/validation/no-network-names.stderr +0 -34
  255. package/tests/cli/validation/source-without-address-is-valid/Abi.json +0 -7
  256. package/tests/cli/validation/source-without-address-is-valid/generated/ExampleSubgraph/ExampleContract.ts +0 -35
  257. package/tests/cli/validation/source-without-address-is-valid/mapping.ts +0 -0
  258. package/tests/cli/validation/source-without-address-is-valid/schema.graphql +0 -3
  259. package/tests/cli/validation/source-without-address-is-valid/subgraph.yaml +0 -23
  260. package/tests/cli/validation/source-without-address-is-valid.stderr +0 -24
  261. package/tests/cli/validation/topic0-is-valid/Abi.json +0 -7
  262. package/tests/cli/validation/topic0-is-valid/mapping.ts +0 -0
  263. package/tests/cli/validation/topic0-is-valid/schema.graphql +0 -4
  264. package/tests/cli/validation/topic0-is-valid/subgraph.yaml +0 -24
  265. package/tests/cli/validation.test.ts +0 -226
  266. package/tsconfig.build.json +0 -4
  267. package/tsconfig.json +0 -19
@@ -1,719 +0,0 @@
1
- import fs from 'fs';
2
- import immutable from 'immutable';
3
- import request from 'sync-request';
4
- import Web3EthAbi from 'web3-eth-abi';
5
- import yaml from 'yaml';
6
- import * as typesCodegen from '../../../codegen/types';
7
- import * as tsCodegen from '../../../codegen/typescript';
8
- import * as util from '../../../codegen/util';
9
- import ABI from '../abi';
10
-
11
- const doFixtureCodegen = fs.existsSync('./fixtures.yaml');
12
-
13
- export default class AbiCodeGenerator {
14
- constructor(private abi: ABI) {
15
- this.abi = abi;
16
- }
17
-
18
- generateModuleImports() {
19
- const imports = [
20
- tsCodegen.moduleImports(
21
- [
22
- // Ethereum integration
23
- 'ethereum',
24
-
25
- // Base classes
26
- 'JSONValue',
27
- 'TypedMap',
28
- 'Entity',
29
-
30
- // AssemblyScript types
31
- 'Bytes',
32
- 'Address',
33
- 'BigInt',
34
- ],
35
- '@graphprotocol/graph-ts',
36
- ),
37
- ];
38
-
39
- if (doFixtureCodegen) {
40
- imports.push(tsCodegen.moduleImports(['newMockEvent'], 'matchstick-as/assembly/index'));
41
- }
42
-
43
- return imports;
44
- }
45
-
46
- generateTypes() {
47
- return [
48
- ...this._generateEventTypes(),
49
- ...this._generateSmartContractClass(),
50
- ...this._generateCallTypes(),
51
- ];
52
- }
53
-
54
- _generateCallTypes() {
55
- let callFunctions = util.disambiguateNames({
56
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
57
- values: this.abi.callFunctions(),
58
- getName: fn =>
59
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
60
- fn.get('name') || (fn.get('type') === 'constructor' ? 'constructor' : 'default'),
61
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
62
- setName: (fn, name) => fn.set('_alias', name),
63
- }) as any[];
64
-
65
- callFunctions = callFunctions.map(fn => {
66
- const fnAlias = fn.get('_alias');
67
- const fnClassName = `${fnAlias.charAt(0).toUpperCase()}${fnAlias.slice(1)}Call`;
68
- const tupleClasses: any[] = [];
69
-
70
- // First, generate a class with the input getters
71
- const inputsClassName = fnClassName + '__Inputs';
72
- const inputsClass = tsCodegen.klass(inputsClassName, { export: true });
73
- inputsClass.addMember(tsCodegen.klassMember('_call', fnClassName));
74
- inputsClass.addMethod(
75
- tsCodegen.method(
76
- `constructor`,
77
- [tsCodegen.param(`call`, fnClassName)],
78
- null,
79
- `this._call = call`,
80
- ),
81
- );
82
-
83
- // Generate getters and classes for function inputs
84
- util
85
- .disambiguateNames({
86
- values: fn.get('inputs', immutable.List()),
87
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
88
- getName: (input, index) => input.get('name') || `value${index}`,
89
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
90
- setName: (input, name) => input.set('name', name),
91
- })
92
- .forEach((input: any, index) => {
93
- const callInput = this._generateInputOrOutput(
94
- input,
95
- index,
96
- fnClassName,
97
- `call`,
98
- `inputValues`,
99
- );
100
- inputsClass.addMethod(callInput.getter);
101
- tupleClasses.push(...callInput.classes);
102
- });
103
-
104
- // Second, generate a class with the output getters
105
- const outputsClassName = fnClassName + '__Outputs';
106
- const outputsClass = tsCodegen.klass(outputsClassName, { export: true });
107
- outputsClass.addMember(tsCodegen.klassMember('_call', fnClassName));
108
- outputsClass.addMethod(
109
- tsCodegen.method(
110
- `constructor`,
111
- [tsCodegen.param(`call`, fnClassName)],
112
- null,
113
- `this._call = call`,
114
- ),
115
- );
116
-
117
- // Generate getters and classes for function outputs
118
- util
119
- .disambiguateNames({
120
- values: fn.get('outputs', immutable.List()),
121
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
122
- getName: (output, index) => output.get('name') || `value${index}`,
123
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
124
- setName: (output, name) => output.set('name', name),
125
- })
126
- .forEach((output: any, index) => {
127
- const callInput = this._generateInputOrOutput(
128
- output,
129
- index,
130
- fnClassName,
131
- `call`,
132
- `outputValues`,
133
- );
134
- outputsClass.addMethod(callInput.getter);
135
- tupleClasses.push(...callInput.classes);
136
- });
137
-
138
- // Then, generate the event class itself
139
- const klass = tsCodegen.klass(fnClassName, {
140
- export: true,
141
- extends: 'ethereum.Call',
142
- });
143
- klass.addMethod(
144
- tsCodegen.method(
145
- `get inputs`,
146
- [],
147
- tsCodegen.namedType(inputsClassName),
148
- `return new ${inputsClassName}(this)`,
149
- ),
150
- );
151
- klass.addMethod(
152
- tsCodegen.method(
153
- `get outputs`,
154
- [],
155
- tsCodegen.namedType(outputsClassName),
156
- `return new ${outputsClassName}(this)`,
157
- ),
158
- );
159
- return [klass, inputsClass, outputsClass, ...tupleClasses];
160
- });
161
-
162
- return callFunctions.reduce(
163
- // flatten the array
164
- (array, classes) => array.concat(classes),
165
- [],
166
- );
167
- }
168
-
169
- _generateEventTypes() {
170
- // Enumerate events with duplicate names
171
- let events = util.disambiguateNames({
172
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
173
- values: this.abi.data.filter(member => member.get('type') === 'event'),
174
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
175
- getName: event => event.get('name'),
176
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
177
- setName: (event, name) => event.set('_alias', name),
178
- }) as any[];
179
-
180
- events = events.map(event => {
181
- const eventClassName = event.get('_alias');
182
- const tupleClasses: any[] = [];
183
-
184
- // First, generate a class with the param getters
185
- const paramsClassName = eventClassName + '__Params';
186
- const paramsClass = tsCodegen.klass(paramsClassName, { export: true });
187
- paramsClass.addMember(tsCodegen.klassMember('_event', eventClassName));
188
- paramsClass.addMethod(
189
- tsCodegen.method(
190
- `constructor`,
191
- [tsCodegen.param(`event`, eventClassName)],
192
- null,
193
- `this._event = event`,
194
- ),
195
- );
196
-
197
- // Enumerate inputs with duplicate names
198
- const inputs = util.disambiguateNames({
199
- values: event.get('inputs'),
200
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
201
- getName: (input, index) => input.get('name') || `param${index}`,
202
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
203
- setName: (input, name) => input.set('name', name),
204
- }) as any[];
205
-
206
- const namesAndTypes: any[] = [];
207
- inputs.forEach((input, index) => {
208
- // Generate getters and classes for event params
209
- const paramObject = this._generateInputOrOutput(
210
- input,
211
- index,
212
- eventClassName,
213
- `event`,
214
- `parameters`,
215
- );
216
- paramsClass.addMethod(paramObject.getter);
217
-
218
- // Fixture generation
219
- if (doFixtureCodegen) {
220
- let ethType = typesCodegen.ethereumTypeForAsc(String(paramObject.getter.returnType));
221
- if (typeof ethType !== 'string' && (ethType.test('int256') || ethType.test('uint256'))) {
222
- ethType = 'int32';
223
- }
224
- namesAndTypes.push({ name: paramObject.getter.name.slice(4), type: ethType });
225
- }
226
-
227
- tupleClasses.push(...paramObject.classes);
228
- });
229
-
230
- // Then, generate the event class itself
231
- const klass = tsCodegen.klass(eventClassName, {
232
- export: true,
233
- extends: 'ethereum.Event',
234
- });
235
- klass.addMethod(
236
- tsCodegen.method(
237
- `get params`,
238
- [],
239
- tsCodegen.namedType(paramsClassName),
240
- `return new ${paramsClassName}(this)`,
241
- ),
242
- );
243
-
244
- // Fixture generation
245
- if (doFixtureCodegen) {
246
- const args = yaml.parse(fs.readFileSync('./fixtures.yaml', 'utf8'));
247
- const blockNumber = args['blockNumber'];
248
- const contractAddr = args['contractAddr'];
249
- const topic0 = args['topic0'];
250
- const apiKey = args['apiKey'];
251
- const url = `https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=${blockNumber}&toBlock=${blockNumber}&address=${contractAddr}&${topic0}=topic0&apikey=${apiKey}`;
252
-
253
- const resp = request('GET', url);
254
- const body = JSON.parse(resp.getBody('utf8'));
255
- if (body.status === '0') {
256
- throw new Error(body.result);
257
- }
258
-
259
- const res = Web3EthAbi
260
- // @ts-expect-error decodeLog seems to exist on Web3EthAbi
261
- .decodeLog(namesAndTypes, body.result[0].data, []);
262
-
263
- let stmnts = '';
264
- for (let i = 0; i < namesAndTypes.length; i++) {
265
- let code = '"' + res[i] + '"';
266
- if (namesAndTypes[i].type.toString() == 'address') {
267
- code = `Address.fromString(${code})`;
268
- }
269
- stmnts = stmnts.concat(
270
- `event.parameters.push(new ethereum.EventParam("${
271
- namesAndTypes[i].name
272
- }", ${typesCodegen.ethereumFromAsc(code, namesAndTypes[i].type)}));`,
273
- `\n`,
274
- );
275
- }
276
-
277
- klass.addMethod(
278
- tsCodegen.staticMethod(
279
- `mock${eventClassName}`,
280
- [],
281
- tsCodegen.namedType(eventClassName),
282
- `
283
- let event = changetype<${eventClassName}>(newMockEvent());
284
- ${stmnts}
285
- return event;
286
- `,
287
- ),
288
- );
289
- }
290
-
291
- return [klass, paramsClass, ...tupleClasses];
292
- });
293
-
294
- return events.reduce(
295
- // flatten the array
296
- (array, classes) => array.concat(classes),
297
- [],
298
- );
299
- }
300
-
301
- _generateInputOrOutput(
302
- inputOrOutput: immutable.Map<any, any>,
303
- index: number,
304
- parentClass: string,
305
- parentType: string,
306
- parentField?: string,
307
- ) {
308
- // Get name and type of the param, adjusting for indexed params and missing names
309
- let name = inputOrOutput.get('name');
310
- const valueType =
311
- parentType === 'event' && inputOrOutput.get('indexed')
312
- ? this._indexedInputType(inputOrOutput.get('type'))
313
- : inputOrOutput.get('type');
314
-
315
- if (name === undefined || name === null || name === '') {
316
- name = parentType === 'event' ? `param${index}` : `value${index}`;
317
- }
318
-
319
- // Generate getters and classes for the param (classes only created for Ethereum tuple types)
320
- return util.containsTupleType(valueType)
321
- ? this._generateTupleType(inputOrOutput, index, parentClass, parentType, parentField)
322
- : {
323
- name: [],
324
- getter: tsCodegen.method(
325
- `get ${name}`,
326
- [],
327
- typesCodegen.ascTypeForEthereum(valueType),
328
- `
329
- return ${typesCodegen.ethereumToAsc(
330
- util.isTupleType(parentType)
331
- ? `this[${index}]`
332
- : `this._${parentType}.${parentField}[${index}].value`,
333
- valueType,
334
- )}
335
- `,
336
- ),
337
- classes: [],
338
- };
339
- }
340
-
341
- _tupleTypeName(inputOrOutput: any, index: number, parentClass: string, parentType: string) {
342
- return this._generateTupleType(inputOrOutput, index, parentClass, parentType, '').name;
343
- }
344
-
345
- _generateTupleType(
346
- inputOrOutput: any,
347
- index: number,
348
- parentClass: string,
349
- parentType: string,
350
- parentField?: string,
351
- ) {
352
- const type = inputOrOutput.get('type');
353
- let name = inputOrOutput.get('name');
354
- if (name === undefined || name === null || name === '') {
355
- name = parentType === 'event' ? `param${index}` : `value${index}`;
356
- }
357
-
358
- const tupleIdentifier = parentClass + tsCodegen.namedType(name).capitalize();
359
- const tupleClassName = tupleIdentifier + 'Struct';
360
- let tupleClasses: any[] = [];
361
-
362
- const isTupleType = util.isTupleType(type);
363
- const returnValue = typesCodegen.ethereumToAsc(
364
- parentType === 'tuple'
365
- ? `this[${index}]`
366
- : `this._${parentType}.${parentField}[${index}].value`,
367
- type,
368
- tupleClassName,
369
- );
370
-
371
- // Generate getter for parent class
372
- const tupleGetter = tsCodegen.method(
373
- `get ${name}`,
374
- [],
375
- util.isTupleMatrixType(type)
376
- ? `Array<Array<${tupleClassName}>>`
377
- : util.isTupleArrayType(type)
378
- ? `Array<${tupleClassName}>`
379
- : tupleClassName,
380
- `
381
- return ${isTupleType ? `changetype<${tupleClassName}>(${returnValue})` : String(returnValue)}
382
- `,
383
- );
384
-
385
- // Generate tuple class
386
- const baseTupleClass = tsCodegen.klass(tupleClassName, {
387
- export: true,
388
- extends: 'ethereum.Tuple',
389
- });
390
-
391
- // Add param getters to tuple class and generate classes for each tuple parameter
392
- inputOrOutput.get('components').forEach((component: any, index: number) => {
393
- const paramObject = this._generateInputOrOutput(component, index, tupleIdentifier, `tuple`);
394
- baseTupleClass.addMethod(paramObject.getter);
395
- tupleClasses = tupleClasses.concat(paramObject.classes);
396
- });
397
-
398
- // Combine all tuple classes generated
399
- tupleClasses.unshift(baseTupleClass);
400
-
401
- return {
402
- name: tupleClassName,
403
- getter: tupleGetter,
404
- classes: tupleClasses,
405
- };
406
- }
407
-
408
- _generateSmartContractClass() {
409
- const klass = tsCodegen.klass(this.abi.name, {
410
- export: true,
411
- extends: 'ethereum.SmartContract',
412
- });
413
- let types = immutable.List();
414
-
415
- klass.addMethod(
416
- tsCodegen.staticMethod(
417
- 'bind',
418
- // TODO: add support for iterable staticMethod params
419
- immutable.List([
420
- tsCodegen.param('address', typesCodegen.ascTypeForEthereum('address')),
421
- ]) as unknown as tsCodegen.Param[],
422
- tsCodegen.namedType(this.abi.name),
423
- `
424
- return new ${this.abi.name}('${this.abi.name}', address);
425
- `,
426
- ),
427
- );
428
-
429
- // Get view/pure functions from the contract
430
- let functions = this.callableFunctions();
431
-
432
- // Disambiguate functions with duplicate names
433
- functions = util.disambiguateNames({
434
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
435
- values: functions,
436
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
437
- getName: fn => fn.get('name'),
438
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
439
- setName: (fn, name) => fn.set('_alias', name),
440
- }) as any;
441
-
442
- functions.forEach(member => {
443
- const fnName = member.get('name');
444
- const fnAlias = member.get('_alias');
445
- const fnSignature = this.abi.functionSignature(member);
446
-
447
- // Generate a type for the result of calling the function
448
- let returnType: any = undefined;
449
- let simpleReturnType = true;
450
- const tupleResultParentType = this.abi.name + '__' + fnAlias + 'Result';
451
-
452
- // Disambiguate outputs with duplicate names
453
- const outputs = util.disambiguateNames({
454
- values: member.get('outputs', immutable.List()),
455
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
456
- getName: (input, index) => input.get('name') || `value${index}`,
457
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
458
- setName: (input, name) => input.set('name', name),
459
- }) as any;
460
-
461
- if (member.get('outputs', immutable.List()).size > 1) {
462
- simpleReturnType = false;
463
-
464
- // Create a type dedicated to holding the return values
465
- returnType = tsCodegen.klass(this.abi.name + '__' + fnAlias + 'Result', {
466
- export: true,
467
- });
468
-
469
- // Add a constructor to this type
470
- returnType.addMethod(
471
- tsCodegen.method(
472
- 'constructor',
473
- outputs.map((output: any, index: number) =>
474
- tsCodegen.param(
475
- `value${index}`,
476
- this._getTupleParamType(output, index, tupleResultParentType),
477
- ),
478
- ),
479
- null,
480
- outputs
481
- .map((_output: any, index: number) => `this.value${index} = value${index}`)
482
- .join('\n'),
483
- ),
484
- );
485
-
486
- // Add a `toMap(): TypedMap<string,ethereum.Value>` function to the return type
487
- returnType.addMethod(
488
- tsCodegen.method(
489
- 'toMap',
490
- [],
491
- tsCodegen.namedType('TypedMap<string,ethereum.Value>'),
492
- `
493
- let map = new TypedMap<string,ethereum.Value>();
494
- ${outputs
495
- .map(
496
- (output: any, index: number) =>
497
- `map.set('value${index}', ${typesCodegen.ethereumFromAsc(
498
- `this.value${index}`,
499
- output.get('type'),
500
- )})`,
501
- )
502
- .join(';')}
503
- return map;
504
- `,
505
- ),
506
- );
507
-
508
- // Add value0, value1 etc. members to the type
509
- outputs
510
- .map((output: any, index: number) =>
511
- tsCodegen.klassMember(
512
- `value${index}`,
513
- this._getTupleParamType(output, index, tupleResultParentType),
514
- ),
515
- )
516
- .forEach((member: any) => returnType.addMember(member));
517
-
518
- // Add getters to the type
519
- outputs
520
- .map(
521
- (output: any, index: number) =>
522
- !!output.get('name') &&
523
- tsCodegen.method(
524
- `get${output.get('name')[0].toUpperCase()}${output.get('name').slice(1)}`,
525
- [],
526
- this._getTupleParamType(output, index, tupleResultParentType),
527
- `return this.value${index};`,
528
- ),
529
- )
530
- .forEach((method: any) => !!method && returnType.addMethod(method));
531
-
532
- // Create types for Tuple outputs
533
- outputs.forEach((output: any, index: number) => {
534
- if (util.containsTupleType(output.get('type'))) {
535
- types = types.concat(
536
- this._generateTupleType(
537
- output,
538
- index,
539
- tupleResultParentType,
540
- 'function',
541
- this.abi.name,
542
- ).classes,
543
- );
544
- }
545
- });
546
-
547
- // Add the type to the types we'll create
548
- types = types.push(returnType);
549
-
550
- returnType = tsCodegen.namedType(returnType.name);
551
- } else {
552
- const type = outputs.get(0).get('type');
553
- if (util.containsTupleType(type)) {
554
- // Add the Tuple type to the types we'll create
555
- const tuple = this._generateTupleType(
556
- outputs.get(0),
557
- 0,
558
- tupleResultParentType,
559
- 'function',
560
- this.abi.name,
561
- );
562
- types = types.concat(tuple.classes);
563
- returnType = util.isTupleType(type)
564
- ? tsCodegen.namedType(tuple.name)
565
- : `Array<${tsCodegen.namedType(tuple.name)}>`;
566
- } else {
567
- returnType = tsCodegen.namedType(typesCodegen.ascTypeForEthereum(type));
568
- }
569
- }
570
-
571
- // Disambiguate inputs with duplicate names
572
- const inputs = util.disambiguateNames({
573
- values: member.get('inputs', immutable.List()),
574
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
575
- getName: (input, index) => input.get('name') || `param${index}`,
576
- // @ts-expect-error improve typings of disambiguateNames to handle iterables
577
- setName: (input, name) => input.set('name', name),
578
- }) as any;
579
-
580
- // Generate a type prefix to identify the Tuple inputs to a function
581
- const tupleInputParentType = this.abi.name + '__' + fnAlias + 'Input';
582
-
583
- // Create types for Tuple inputs
584
- inputs.forEach((input: any, index: number) => {
585
- if (util.containsTupleType(input.get('type'))) {
586
- types = types.concat(
587
- this._generateTupleType(input, index, tupleInputParentType, 'function', this.abi.name)
588
- .classes,
589
- );
590
- }
591
- });
592
-
593
- // Generate and add a method that implements calling the function on
594
- // the smart contract
595
- const params = inputs.map((input: any, index: number) =>
596
- tsCodegen.param(
597
- input.get('name'),
598
- this._getTupleParamType(input, index, tupleInputParentType),
599
- ),
600
- );
601
-
602
- const superInputs = `
603
- '${fnName}',
604
- '${fnSignature}',
605
- [${
606
- inputs.size > 0
607
- ? inputs
608
- .map((input: any) =>
609
- typesCodegen.ethereumFromAsc(input.get('name'), input.get('type')),
610
- )
611
- .map((coercion: any) => coercion.toString())
612
- .join(', ')
613
- : ''
614
- }]`;
615
-
616
- const methodCallBody = (isTry: boolean) => {
617
- const methodBody = `
618
- ${
619
- isTry
620
- ? `
621
- let result = super.tryCall(${superInputs})
622
- if (result.reverted) {
623
- return new ethereum.CallResult()
624
- }
625
- let value = result.value
626
- return ethereum.CallResult.fromValue(`
627
- : `
628
- let result = super.call(${superInputs})
629
-
630
- return (`
631
- }`;
632
-
633
- const returnVal = simpleReturnType
634
- ? typesCodegen.ethereumToAsc(
635
- isTry ? 'value[0]' : 'result[0]',
636
- outputs.get(0).get('type'),
637
- util.isTupleArrayType(outputs.get(0).get('type'))
638
- ? this._tupleTypeName(outputs.get(0), 0, tupleResultParentType, this.abi.name)
639
- : '',
640
- )
641
- : `new ${returnType.name}(
642
- ${outputs
643
- .map((output: any, index: number) => {
644
- const val = typesCodegen.ethereumToAsc(
645
- isTry ? `value[${index}]` : `result[${index}]`,
646
- output.get('type'),
647
- util.isTupleArrayType(output.get('type'))
648
- ? this._tupleTypeName(output, index, tupleResultParentType, this.abi.name)
649
- : '',
650
- );
651
- return util.isTupleType(output.get('type'))
652
- ? `changetype<${this._tupleTypeName(
653
- output,
654
- index,
655
- tupleResultParentType,
656
- this.abi.name,
657
- )}>(${val})`
658
- : val;
659
- })
660
- .join(', ')}
661
- )`;
662
-
663
- const isTuple = util.isTupleType(outputs.get(0).get('type'));
664
- return `${methodBody} ${isTuple ? `changetype<${returnType}>(${returnVal})` : returnVal})`;
665
- };
666
-
667
- // Generate method with an without `try_`.
668
- klass.addMethod(tsCodegen.method(fnAlias, params, returnType, methodCallBody(false)));
669
- klass.addMethod(
670
- tsCodegen.method(
671
- 'try_' + fnAlias,
672
- params,
673
- 'ethereum.CallResult<' + returnType + '>',
674
- methodCallBody(true),
675
- ),
676
- );
677
- });
678
-
679
- return [...types, klass];
680
- }
681
-
682
- _getTupleParamType(inputOrOutput: any, index: number, tupleParentType: string) {
683
- const type = inputOrOutput.get('type');
684
- return util.isTupleType(type)
685
- ? this._tupleTypeName(inputOrOutput, index, tupleParentType, this.abi.name)
686
- : util.isTupleMatrixType(type)
687
- ? `Array<Array<${this._tupleTypeName(inputOrOutput, index, tupleParentType, this.abi.name)}>>`
688
- : util.isTupleArrayType(type)
689
- ? `Array<${this._tupleTypeName(inputOrOutput, index, tupleParentType, this.abi.name)}>`
690
- : typesCodegen.ascTypeForEthereum(type);
691
- }
692
-
693
- _indexedInputType(inputType: string) {
694
- // strings, bytes and arrays are encoded and hashed to a bytes32 value
695
- if (
696
- inputType === 'string' ||
697
- inputType === 'bytes' ||
698
- inputType === 'tuple' ||
699
- // the following matches arrays of the forms `uint256[]` and `uint256[12356789]`;
700
- // the value type name doesn't matter here, just that the type name ends with
701
- // brackets and, optionally, a number inside the brackets
702
- inputType.match(/\[[0-9]*\]$/g)
703
- ) {
704
- return 'bytes32';
705
- }
706
- return inputType;
707
- }
708
-
709
- callableFunctions() {
710
- const allowedMutability = ['view', 'pure', 'nonpayable', 'constant'];
711
- return this.abi.data.filter(
712
- member =>
713
- member.get('type') === 'function' &&
714
- member.get('outputs', immutable.List()).size !== 0 &&
715
- (allowedMutability.includes(member.get('stateMutability')) ||
716
- (member.get('stateMutability') === undefined && !member.get('payable', false))),
717
- );
718
- }
719
- }