@guardian-network/policy-intermediate-representation 0.3.2

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 (137) hide show
  1. package/README.md +36 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +8 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/ir-notation/errors/ErrorFactory.d.ts +7 -0
  7. package/dist/ir-notation/errors/ErrorFactory.d.ts.map +1 -0
  8. package/dist/ir-notation/errors/ErrorFactory.js +17 -0
  9. package/dist/ir-notation/errors/ErrorFactory.js.map +1 -0
  10. package/dist/ir-notation/errors/index.d.ts +2 -0
  11. package/dist/ir-notation/errors/index.d.ts.map +1 -0
  12. package/dist/ir-notation/errors/index.js +18 -0
  13. package/dist/ir-notation/errors/index.js.map +1 -0
  14. package/dist/ir-notation/errors/validation-errors.d.ts +11 -0
  15. package/dist/ir-notation/errors/validation-errors.d.ts.map +1 -0
  16. package/dist/ir-notation/errors/validation-errors.js +26 -0
  17. package/dist/ir-notation/errors/validation-errors.js.map +1 -0
  18. package/dist/ir-notation/helpers/formatters.d.ts +18 -0
  19. package/dist/ir-notation/helpers/formatters.d.ts.map +1 -0
  20. package/dist/ir-notation/helpers/formatters.js +45 -0
  21. package/dist/ir-notation/helpers/formatters.js.map +1 -0
  22. package/dist/ir-notation/helpers/index.d.ts +5 -0
  23. package/dist/ir-notation/helpers/index.d.ts.map +1 -0
  24. package/dist/ir-notation/helpers/index.js +21 -0
  25. package/dist/ir-notation/helpers/index.js.map +1 -0
  26. package/dist/ir-notation/helpers/matchers.d.ts +4 -0
  27. package/dist/ir-notation/helpers/matchers.d.ts.map +1 -0
  28. package/dist/ir-notation/helpers/matchers.js +17 -0
  29. package/dist/ir-notation/helpers/matchers.js.map +1 -0
  30. package/dist/ir-notation/helpers/morphs.d.ts +4 -0
  31. package/dist/ir-notation/helpers/morphs.d.ts.map +1 -0
  32. package/dist/ir-notation/helpers/morphs.js +26 -0
  33. package/dist/ir-notation/helpers/morphs.js.map +1 -0
  34. package/dist/ir-notation/helpers/validation/index.d.ts +2 -0
  35. package/dist/ir-notation/helpers/validation/index.d.ts.map +1 -0
  36. package/dist/ir-notation/helpers/validation/index.js +18 -0
  37. package/dist/ir-notation/helpers/validation/index.js.map +1 -0
  38. package/dist/ir-notation/helpers/validation/typing.validation.d.ts +10 -0
  39. package/dist/ir-notation/helpers/validation/typing.validation.d.ts.map +1 -0
  40. package/dist/ir-notation/helpers/validation/typing.validation.js +41 -0
  41. package/dist/ir-notation/helpers/validation/typing.validation.js.map +1 -0
  42. package/dist/ir-notation/parser/index.d.ts +4 -0
  43. package/dist/ir-notation/parser/index.d.ts.map +1 -0
  44. package/dist/ir-notation/parser/index.js +14 -0
  45. package/dist/ir-notation/parser/index.js.map +1 -0
  46. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.d.ts +14 -0
  47. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.d.ts.map +1 -0
  48. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.js +44 -0
  49. package/dist/ir-notation/parser/parser-contracts/ParserWithValidation.js.map +1 -0
  50. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.d.ts +12 -0
  51. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.d.ts.map +1 -0
  52. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.js +27 -0
  53. package/dist/ir-notation/parser/parser-contracts/SimplifiedParser.js.map +1 -0
  54. package/dist/ir-notation/parser/parser-contracts/StaticParser.d.ts +7 -0
  55. package/dist/ir-notation/parser/parser-contracts/StaticParser.d.ts.map +1 -0
  56. package/dist/ir-notation/parser/parser-contracts/StaticParser.js +16 -0
  57. package/dist/ir-notation/parser/parser-contracts/StaticParser.js.map +1 -0
  58. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.d.ts +7 -0
  59. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.d.ts.map +1 -0
  60. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.js +40 -0
  61. package/dist/ir-notation/parser/parser-contracts/base/ParserBase.js.map +1 -0
  62. package/dist/ir-notation/parser/parser-contracts/guards.d.ts +5 -0
  63. package/dist/ir-notation/parser/parser-contracts/guards.d.ts.map +1 -0
  64. package/dist/ir-notation/parser/parser-contracts/guards.js +14 -0
  65. package/dist/ir-notation/parser/parser-contracts/guards.js.map +1 -0
  66. package/dist/ir-notation/parser/parser-contracts/index.d.ts +5 -0
  67. package/dist/ir-notation/parser/parser-contracts/index.d.ts.map +1 -0
  68. package/dist/ir-notation/parser/parser-contracts/index.js +13 -0
  69. package/dist/ir-notation/parser/parser-contracts/index.js.map +1 -0
  70. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.d.ts +19 -0
  71. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.d.ts.map +1 -0
  72. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.js +140 -0
  73. package/dist/ir-notation/parser/parser-contracts/tools/ParamsExtractor.js.map +1 -0
  74. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.d.ts +7 -0
  75. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.d.ts.map +1 -0
  76. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.js +49 -0
  77. package/dist/ir-notation/parser/parser-contracts/tools/TypesNormalizer.js.map +1 -0
  78. package/dist/ir-notation/parser/parser-contracts/tools/index.d.ts +6 -0
  79. package/dist/ir-notation/parser/parser-contracts/tools/index.d.ts.map +1 -0
  80. package/dist/ir-notation/parser/parser-contracts/tools/index.js +25 -0
  81. package/dist/ir-notation/parser/parser-contracts/tools/index.js.map +1 -0
  82. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.d.ts +5 -0
  83. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.d.ts.map +1 -0
  84. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.js +26 -0
  85. package/dist/ir-notation/parser/parser-contracts/tools/tools.helper.js.map +1 -0
  86. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.d.ts +10 -0
  87. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.d.ts.map +1 -0
  88. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.js +36 -0
  89. package/dist/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.js.map +1 -0
  90. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.d.ts +12 -0
  91. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.d.ts.map +1 -0
  92. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.js +39 -0
  93. package/dist/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.js.map +1 -0
  94. package/dist/ir-notation/parser/parser-contracts/types.d.ts +59 -0
  95. package/dist/ir-notation/parser/parser-contracts/types.d.ts.map +1 -0
  96. package/dist/ir-notation/parser/parser-contracts/types.js +12 -0
  97. package/dist/ir-notation/parser/parser-contracts/types.js.map +1 -0
  98. package/dist/ir-notation/parser/parser.unvalidated.d.ts +6 -0
  99. package/dist/ir-notation/parser/parser.unvalidated.d.ts.map +1 -0
  100. package/dist/ir-notation/parser/parser.unvalidated.js +92 -0
  101. package/dist/ir-notation/parser/parser.unvalidated.js.map +1 -0
  102. package/dist/ir-notation/parser/parser.validated.d.ts +17 -0
  103. package/dist/ir-notation/parser/parser.validated.d.ts.map +1 -0
  104. package/dist/ir-notation/parser/parser.validated.js +37 -0
  105. package/dist/ir-notation/parser/parser.validated.js.map +1 -0
  106. package/dist/ir-notation/parser/types.d.ts +9 -0
  107. package/dist/ir-notation/parser/types.d.ts.map +1 -0
  108. package/dist/ir-notation/parser/types.js +11 -0
  109. package/dist/ir-notation/parser/types.js.map +1 -0
  110. package/package.json +71 -0
  111. package/src/index.ts +5 -0
  112. package/src/ir-notation/errors/ErrorFactory.ts +25 -0
  113. package/src/ir-notation/errors/index.ts +1 -0
  114. package/src/ir-notation/errors/validation-errors.ts +38 -0
  115. package/src/ir-notation/helpers/formatters.ts +63 -0
  116. package/src/ir-notation/helpers/index.ts +4 -0
  117. package/src/ir-notation/helpers/matchers.ts +12 -0
  118. package/src/ir-notation/helpers/morphs.ts +26 -0
  119. package/src/ir-notation/helpers/validation/index.ts +1 -0
  120. package/src/ir-notation/helpers/validation/typing.validation.ts +89 -0
  121. package/src/ir-notation/parser/index.ts +10 -0
  122. package/src/ir-notation/parser/parser-contracts/ParserWithValidation.ts +99 -0
  123. package/src/ir-notation/parser/parser-contracts/SimplifiedParser.ts +41 -0
  124. package/src/ir-notation/parser/parser-contracts/StaticParser.ts +25 -0
  125. package/src/ir-notation/parser/parser-contracts/base/ParserBase.ts +79 -0
  126. package/src/ir-notation/parser/parser-contracts/guards.ts +17 -0
  127. package/src/ir-notation/parser/parser-contracts/index.ts +7 -0
  128. package/src/ir-notation/parser/parser-contracts/tools/ParamsExtractor.ts +251 -0
  129. package/src/ir-notation/parser/parser-contracts/tools/TypesNormalizer.ts +78 -0
  130. package/src/ir-notation/parser/parser-contracts/tools/index.ts +5 -0
  131. package/src/ir-notation/parser/parser-contracts/tools/tools.helper.ts +33 -0
  132. package/src/ir-notation/parser/parser-contracts/tools/types-source/DSLConfigArgsTypesSource.ts +52 -0
  133. package/src/ir-notation/parser/parser-contracts/tools/types-source/OnchainDescriptorArgsTypesSource.ts +76 -0
  134. package/src/ir-notation/parser/parser-contracts/types.ts +78 -0
  135. package/src/ir-notation/parser/parser.unvalidated.ts +200 -0
  136. package/src/ir-notation/parser/parser.validated.ts +67 -0
  137. package/src/ir-notation/parser/types.ts +28 -0
@@ -0,0 +1,89 @@
1
+ import {
2
+ MinArbitraryDataArtifactFactory,
3
+ MinTypedValue,
4
+ NodeTreeInitData as ParsingResult,
5
+ } from '@guardian-network/shared';
6
+ import { ContractRunner } from 'ethers';
7
+ import { ErrorFactory } from '../../errors';
8
+ import { DSLTypesToIRTypes } from '../formatters';
9
+
10
+ type InitExecArgumentsConfig = {
11
+ execArguments: Array<MinTypedValue>;
12
+ initArguments: Array<MinTypedValue>;
13
+ };
14
+
15
+ export const dslTypesToOnchainTypesParamsValidation =
16
+ (provider: ContractRunner) =>
17
+ async (
18
+ artifactAddress: string,
19
+ initExecArgumentsConfig: InitExecArgumentsConfig,
20
+ ) => {
21
+ const instance = MinArbitraryDataArtifactFactory.connect(
22
+ artifactAddress,
23
+ provider,
24
+ );
25
+
26
+ const { argsNames, argsTypes } = await instance.getExecDescriptor();
27
+
28
+ const { argsNames: initArgsNames, argsTypes: initArgsTypes } =
29
+ await instance.getInitDescriptor();
30
+
31
+ for (let i = 0; i < initArgsNames.length; i++) {
32
+ const mirroredDslParameter = initExecArgumentsConfig.initArguments[i];
33
+ const parsedType = DSLTypesToIRTypes(mirroredDslParameter.type);
34
+ if (initArgsTypes[i] !== parsedType) {
35
+ throw ErrorFactory.initTypesNotMacth(
36
+ mirroredDslParameter.value,
37
+ initArgsNames[i],
38
+ initArgsTypes[i],
39
+ parsedType,
40
+ );
41
+ }
42
+ }
43
+
44
+ for (let i = 0; i < argsNames.length; i++) {
45
+ const mirroredDslParameter = initExecArgumentsConfig.execArguments[i];
46
+ const parsedType = DSLTypesToIRTypes(mirroredDslParameter.type);
47
+ if (argsTypes[i] !== parsedType) {
48
+ throw ErrorFactory.execTypesNotMacth(
49
+ mirroredDslParameter.value,
50
+ argsNames[i],
51
+ argsTypes[i],
52
+ parsedType,
53
+ );
54
+ }
55
+ }
56
+ };
57
+
58
+ export const onchainSubstitutionToReturnTypesValidation =
59
+ (provider: ContractRunner) => async (output: ParsingResult[]) => {
60
+ for (const instance of output) {
61
+ const currentInstance = MinArbitraryDataArtifactFactory.connect(
62
+ <string>instance.artifactAddress,
63
+ provider,
64
+ );
65
+
66
+ for (const refNode of instance.substitutedExecArgs) {
67
+ const refDeclaration = output.find(
68
+ (el) => el.id == refNode.supplierNodeId,
69
+ )!;
70
+ const refArtifactInstance = MinArbitraryDataArtifactFactory.connect(
71
+ <string>refDeclaration?.artifactAddress,
72
+ provider,
73
+ );
74
+
75
+ const { returnType } = await refArtifactInstance.getExecDescriptor();
76
+ const { argsTypes, argsNames } =
77
+ await currentInstance.getExecDescriptor();
78
+
79
+ if (!(argsTypes[Number(refNode.index)] == returnType))
80
+ throw ErrorFactory.substitutionTypesNotMatch(
81
+ <string>instance.id,
82
+ argsNames[Number(refNode.index)],
83
+ argsTypes[Number(refNode.index)],
84
+ <string>refNode.supplierNodeId,
85
+ returnType,
86
+ );
87
+ }
88
+ }
89
+ };
@@ -0,0 +1,10 @@
1
+ export {
2
+ ParserWithValidation,
3
+ SimplifiedParser,
4
+ StaticParser,
5
+ } from './parser-contracts';
6
+ export {
7
+ parseIRByDSLTypesWithInterceptor,
8
+ parseIRByOnchainTypesWithInterceptor,
9
+ } from './parser.unvalidated';
10
+ export { getIRParser, getIRParserUnvalidate } from './parser.validated';
@@ -0,0 +1,99 @@
1
+ import {
2
+ LacLangCompilerOptions as CompilerConfiguration,
3
+ InstanceConfig,
4
+ TranspilerOutput,
5
+ } from '@guardian-network/shared';
6
+ import { ContractRunner } from 'ethers';
7
+ import { TypingsValidator } from '../parser.validated';
8
+ import { ValidationMiddlware } from '../types';
9
+ import { SimplifiedParser } from './SimplifiedParser';
10
+ import { DSLConfigArgsTypesSource, createOrInferTypesSource } from './tools';
11
+ import { GetTypesValues } from './types';
12
+
13
+ export class ParserWithValidation extends SimplifiedParser {
14
+ static fromOnchainSource = (
15
+ intermediatePresentation: string,
16
+ provider: ContractRunner,
17
+ ): ParserWithValidation => {
18
+ const middleware = TypingsValidator(provider);
19
+
20
+ const getTypesSource = createOrInferTypesSource(provider);
21
+
22
+ return this.create(intermediatePresentation, getTypesSource, middleware);
23
+ };
24
+
25
+ static fromDSLBasedConfig = (
26
+ intermediatePresentation: string,
27
+ artifactConfigsList: Array<InstanceConfig>,
28
+ provider?: ContractRunner,
29
+ ) => {
30
+ let middleware: ValidationMiddlware | undefined;
31
+ if (!!provider) {
32
+ middleware = TypingsValidator(provider);
33
+ }
34
+
35
+ const getTypesSource = DSLConfigArgsTypesSource.build(artifactConfigsList);
36
+
37
+ return this.create(
38
+ intermediatePresentation,
39
+ getTypesSource.getTypes,
40
+ middleware,
41
+ );
42
+ };
43
+
44
+ static fromCompilerConfiguration = (
45
+ config: CompilerConfiguration,
46
+ {
47
+ ir: intermediatePresentation,
48
+ typings: artifactConfigsList,
49
+ }: Pick<TranspilerOutput, 'ir' | 'typings'>,
50
+ ): ParserWithValidation => {
51
+ const {
52
+ checkTypesAgainstDslDeclarations,
53
+ checkTypesAgainstOnchainDescriptors,
54
+ provider,
55
+ } = config;
56
+ const isProvider = !!provider;
57
+
58
+ if (!!checkTypesAgainstDslDeclarations) {
59
+ return this.fromDSLBasedConfig(
60
+ intermediatePresentation,
61
+ artifactConfigsList,
62
+ provider,
63
+ );
64
+ }
65
+
66
+ if (!!checkTypesAgainstOnchainDescriptors) {
67
+ if (!isProvider) throw new Error(`Parser: provider not supplied`);
68
+
69
+ return this.fromOnchainSource(intermediatePresentation, provider);
70
+ }
71
+
72
+ return this.fromDSLBasedConfig(
73
+ intermediatePresentation,
74
+ artifactConfigsList,
75
+ );
76
+ };
77
+
78
+ static create = (
79
+ intermediatePresentation: string,
80
+ getTypesSource: GetTypesValues,
81
+ middleware?: ValidationMiddlware,
82
+ ): ParserWithValidation => {
83
+ return new ParserWithValidation(
84
+ getTypesSource,
85
+ intermediatePresentation,
86
+ middleware,
87
+ );
88
+ };
89
+
90
+ // note: This expects String of intermediate-representation of artifacts and their relations.
91
+ // The string value is validated in ParserBase, Extractor
92
+ constructor(
93
+ getTypesSource: GetTypesValues,
94
+ intermediatePresentation: string,
95
+ private middleware?: ValidationMiddlware,
96
+ ) {
97
+ super(getTypesSource, intermediatePresentation);
98
+ }
99
+ }
@@ -0,0 +1,41 @@
1
+ import { ContractRunner } from 'ethers';
2
+ import { ParsingResult } from '../types';
3
+ import { ParserBase } from './base/ParserBase';
4
+ import { createOrInferTypesSource, toUnprocessedArtifactsList } from './tools';
5
+ import { GetTypesValues, IArgsTypesSource } from './types';
6
+
7
+ export class SimplifiedParser {
8
+ static build = (
9
+ intermediatePresentation: string,
10
+ providerOrSource: ContractRunner | IArgsTypesSource,
11
+ ): SimplifiedParser => {
12
+ const getTypesSource = createOrInferTypesSource(providerOrSource);
13
+
14
+ return new SimplifiedParser(getTypesSource, intermediatePresentation);
15
+ };
16
+
17
+ // note: This expects String of intermediate-representation of artifacts and their relations.
18
+ // The string value is validated in ParserBase, Extractor
19
+ constructor(
20
+ protected getTypesSource: GetTypesValues,
21
+ protected intermediatePresentation: string,
22
+ ) {}
23
+
24
+ protected get unprocessedArtifacts(): Array<string> {
25
+ return toUnprocessedArtifactsList(this.intermediatePresentation);
26
+ }
27
+
28
+ // note:
29
+ // a. When processed it basically returns a List of 'NodeInitDataStruct' matching
30
+ // the interface requirements of onchain artifacts declaration
31
+ // b. Each List entry includes the values of [constants-data], [substitutions-data], [init-data], [runtime-supplied-params-indices]
32
+ // and other related data of Artifact.
33
+ process = async (): Promise<Array<ParsingResult>> => {
34
+ const processPromises = this.unprocessedArtifacts.map((v, i) =>
35
+ ParserBase.processSingleWithId(v, i, this.getTypesSource),
36
+ );
37
+
38
+ const processedArtifacts = await Promise.all(processPromises);
39
+ return processedArtifacts;
40
+ };
41
+ }
@@ -0,0 +1,25 @@
1
+ import { ContractRunner } from 'ethers';
2
+ import { ParsingResult } from '../types';
3
+ import { ParserBase } from './base/ParserBase';
4
+ import { createOrInferTypesSource, toUnprocessedArtifactsList } from './tools';
5
+ import { IArgsTypesSource } from './types';
6
+
7
+ export class StaticParser {
8
+ static process = async (
9
+ intermediatePresentation: string,
10
+ providerOrSource: ContractRunner | IArgsTypesSource,
11
+ ): Promise<Array<ParsingResult>> => {
12
+ const unprocessedArtifacts = toUnprocessedArtifactsList(
13
+ intermediatePresentation,
14
+ );
15
+
16
+ const getTypesSource = createOrInferTypesSource(providerOrSource);
17
+
18
+ const processPromises = unprocessedArtifacts.map((v, i) =>
19
+ ParserBase.processSingleWithId(v, i, getTypesSource),
20
+ );
21
+
22
+ const processedArtifacts = await Promise.all(processPromises);
23
+ return processedArtifacts;
24
+ };
25
+ }
@@ -0,0 +1,79 @@
1
+ import { NodeId } from '@guardian-network/shared';
2
+ import { ParsingResult } from '../../types';
3
+ import { ParamsExtractor } from '../tools';
4
+ import {
5
+ GetTypesValues,
6
+ NormalizedExecParameter,
7
+ SubstitutingParameter,
8
+ } from '../types';
9
+
10
+ export class ParserBase {
11
+ static processSingle = async (
12
+ unprocessedArtifact: string,
13
+ getTypesSource: GetTypesValues,
14
+ pos?: number,
15
+ ) => {
16
+ // note: wrapper is kinda temporaty solution;
17
+ // todo: re-design Parser class to support 'IGetArgsTypes' a bit smootherly
18
+ const getTypesSourceAtIndex = (param: string) => getTypesSource(param, pos);
19
+
20
+ const exctractionHandler = ParamsExtractor.build(
21
+ unprocessedArtifact,
22
+ getTypesSourceAtIndex,
23
+ );
24
+
25
+ const extractedData = await exctractionHandler.artifactData();
26
+
27
+ const {
28
+ artifactAddress,
29
+ argsCount,
30
+ execKnownParamsList,
31
+ execRuntimeVariablesIndices,
32
+ execRuntimeVariablesInjectionsWithIndices,
33
+ execSubstitutionParamsList,
34
+ initDataParamsSolidityPacked,
35
+ needsInitialization,
36
+ } = extractedData;
37
+
38
+ const processedArtifact: Omit<ParsingResult, 'id'> = {
39
+ artifactAddress,
40
+ constantExecArgs: execKnownParamsList,
41
+ variableExecArgs: execRuntimeVariablesIndices,
42
+ injections: execRuntimeVariablesInjectionsWithIndices,
43
+ argsCount,
44
+ substitutedExecArgs: execSubstitutionParamsList.map(toRegularParam),
45
+ initData: initDataParamsSolidityPacked,
46
+ needsInitialization,
47
+ };
48
+
49
+ return processedArtifact;
50
+ };
51
+
52
+ static processSingleWithId = async (
53
+ unprocessedArtifact: string,
54
+ salt: number,
55
+ getTypesSource: GetTypesValues,
56
+ ): Promise<ParsingResult> => {
57
+ const nodeId = NodeId.fromNotation(unprocessedArtifact, salt);
58
+ const processed = await this.processSingle(
59
+ unprocessedArtifact,
60
+ getTypesSource,
61
+ salt,
62
+ );
63
+
64
+ return {
65
+ id: nodeId,
66
+ ...processed,
67
+ };
68
+ };
69
+ }
70
+
71
+ const toRegularParam = ({
72
+ from,
73
+ atPos,
74
+ }: SubstitutingParameter): NormalizedExecParameter => {
75
+ return {
76
+ supplierNodeId: from,
77
+ index: atPos,
78
+ };
79
+ };
@@ -0,0 +1,17 @@
1
+ import { ContractRunner } from 'ethers';
2
+ import { SupportedSolidityTypesEnum } from './types';
3
+
4
+ export const isSupportedSolidityType = (
5
+ value: string,
6
+ ): value is SupportedSolidityTypesEnum => {
7
+ const isType = Object.values(SupportedSolidityTypesEnum).includes(
8
+ value as any,
9
+ );
10
+ return isType;
11
+ };
12
+
13
+ export const isContractProviderType = (
14
+ value: Object,
15
+ ): value is ContractRunner => {
16
+ return 'provider' in value;
17
+ };
@@ -0,0 +1,7 @@
1
+ export { ParserWithValidation } from './ParserWithValidation';
2
+ export { SimplifiedParser } from './SimplifiedParser';
3
+ export { StaticParser } from './StaticParser';
4
+ export {
5
+ DSLConfigArgsTypesSource,
6
+ OnchainDescriptorArgsTypesSource,
7
+ } from './tools';
@@ -0,0 +1,251 @@
1
+ import { getAddress } from 'ethers';
2
+ import {
3
+ ArtifactData,
4
+ GetTypesValues,
5
+ IParamsExtractor,
6
+ Parameter,
7
+ RawArtifactComponents,
8
+ SubstitutingParameter,
9
+ } from '../types';
10
+ import { TypesNormalizer } from './TypesNormalizer';
11
+
12
+ // handles the extraction of artifact components (taken from input string taken AS A LINE from intermediate presentation)
13
+ // then it retrieves [known-params], [runtime-supplied], or [substituted] values, etc.
14
+ export class ParamsExtractor implements IParamsExtractor {
15
+ static build = (
16
+ artifactDeclarationUnprocessed: string,
17
+ descriptors: GetTypesValues,
18
+ ): ParamsExtractor => {
19
+ return new ParamsExtractor(artifactDeclarationUnprocessed, descriptors);
20
+ };
21
+
22
+ constructor(
23
+ private artifactDeclarationUnprocessed: string,
24
+ private descriptors: GetTypesValues,
25
+ ) {}
26
+
27
+ private get artifact() {
28
+ return this.artifactDeclarationUnprocessed;
29
+ }
30
+
31
+ private get retrieveInstanceDescriptors() {
32
+ return this.descriptors;
33
+ }
34
+
35
+ artifactData = async (
36
+ artifact: string = this.artifact, // can accept artifact declaration runtime to extract
37
+ ): Promise<ArtifactData> => {
38
+ const {
39
+ addressClause: artifactAddress,
40
+ initClause,
41
+ paramsClause,
42
+ } = this.toComponents(artifact);
43
+
44
+ const { execParamsTypes, initParamsTypes } =
45
+ await this.retrieveInstanceDescriptors(artifactAddress);
46
+
47
+ // note: all exec params; not splitted yet to [known-params], [runtime-supplied], or [substituted]
48
+ const execParamsList = this.getAllArgumentsWithIndices(paramsClause);
49
+
50
+ // note: the ones should be replaced by evaluation-result of linked Artifact
51
+ const execSubstitutionParamsList =
52
+ this.getSubstitutionsWithIndices(execParamsList);
53
+
54
+ // note: this actually is constant-values needed runtime and known when artifact is declared but prior to its deployment
55
+ const execKnownParamsList = this.getKnownParamsWithIndices(
56
+ execParamsList,
57
+ execParamsTypes,
58
+ );
59
+
60
+ // note: when applied onchain, this facilitates what position provided arguments belong in general exec-params list of graph node
61
+ const execRuntimeVariablesIndices: Array<number> =
62
+ this.getRuntimeVariablesIndices(execParamsList);
63
+
64
+ // note: index here is a position of an injection in an array of variables, NOT in an array of execParams
65
+ const execRuntimeVariablesInjectionsWithIndices: Array<Parameter> =
66
+ this.getInjectionsWithIndices(execParamsList);
67
+
68
+ let initDataParams: string[] = [];
69
+ if (initClause) {
70
+ // note: if artifact is statefull, then it may require init-params
71
+ initDataParams = this.getAllArguments(initClause);
72
+ }
73
+
74
+ const initDataParamsSolidityPacked =
75
+ TypesNormalizer.toSolidityEncodedValueFromString(
76
+ initParamsTypes,
77
+ initDataParams,
78
+ );
79
+
80
+ return {
81
+ artifactAddress,
82
+ execKnownParamsList,
83
+ execSubstitutionParamsList,
84
+ execRuntimeVariablesIndices,
85
+ execRuntimeVariablesInjectionsWithIndices,
86
+ needsInitialization: initClause !== null,
87
+ argsCount: execParamsList.length,
88
+ initDataParamsSolidityPacked,
89
+ };
90
+ };
91
+
92
+ // note: raw components from 'this.artifact'
93
+ private toComponents(
94
+ artifact: string = this.artifact,
95
+ ): RawArtifactComponents {
96
+ const artifactMatcher = /\{([^}]+)\}\s*\(([^)]*)\)(?:\s*<([^>]*)>)?/;
97
+ const matched = artifact.match(artifactMatcher);
98
+
99
+ if (!!matched) {
100
+ // todo: const [, instanceAddress, execParamsInfo, initDataParams] = match;
101
+ const [, addressClause, paramsClause, initClause] = matched;
102
+
103
+ // todo: add more validations
104
+ return {
105
+ addressClause: getAddress(addressClause),
106
+ initClause: initClause ?? null,
107
+ paramsClause,
108
+ };
109
+ }
110
+
111
+ // note: throw error if the format of the supplied string is incorrect
112
+ throw new Error(
113
+ 'Artifact intermediate presentation does not match the expected format.',
114
+ );
115
+ }
116
+
117
+ // exec-params and init-params from param clause usually taken first from 'this.rawComponents'
118
+ private getAllArguments(clause: string): string[] {
119
+ // note: return empty array if the input is an empty string
120
+ if (clause.trim() === '') {
121
+ return [];
122
+ }
123
+
124
+ // note: split the input string by commas and trim each value
125
+ const extractedParams = clause.split(',').map((value) => value.trim());
126
+ return extractedParams;
127
+ }
128
+
129
+ private getAllArgumentsWithIndices = (clause: string): Array<Parameter> => {
130
+ const extracted = this.getAllArguments(clause);
131
+
132
+ const extractedWithIndex = extracted.map((v, i) => ({
133
+ value: v,
134
+ index: i,
135
+ }));
136
+ return extractedWithIndex;
137
+ };
138
+
139
+ private getRuntimeVariables = (
140
+ paramsList: Array<Parameter>,
141
+ ): Array<Parameter> => {
142
+ // filter out parameters that are variables
143
+ const isVariableFilter = (v: string): boolean => {
144
+ const regex = /^var/;
145
+ return regex.test(v);
146
+ };
147
+
148
+ const runtimeVariables = paramsList.filter(({ value }) =>
149
+ isVariableFilter(value),
150
+ );
151
+
152
+ return runtimeVariables;
153
+ };
154
+
155
+ private getRuntimeVariablesIndices = (
156
+ paramsList: Array<Parameter>,
157
+ ): Array<number> => {
158
+ // return runtime-variable indices
159
+ const variableIndices = this.getRuntimeVariables(paramsList).map(
160
+ ({ index }) => index,
161
+ );
162
+ return variableIndices;
163
+ };
164
+
165
+ // note: this keeps original indeces from general list of params
166
+ private getInjectionsWithIndices = (
167
+ paramsList: Array<Parameter>,
168
+ ): Array<Parameter> => {
169
+ const extractInjection = (input: string): string => {
170
+ const matched = input.match(/\$"([^"]*)"$/);
171
+ if (!!matched) return matched[1];
172
+
173
+ return '';
174
+ };
175
+
176
+ const injectionMapper = (
177
+ v: Parameter,
178
+ indexOfValueInVariablesList: number,
179
+ ) => {
180
+ const extracted = extractInjection(v.value);
181
+ if (!!extracted && extracted !== '') {
182
+ return {
183
+ value: v.value,
184
+ index: indexOfValueInVariablesList,
185
+ };
186
+ }
187
+
188
+ return undefined;
189
+ };
190
+
191
+ const injectionsListUnsafe =
192
+ this.getRuntimeVariables(paramsList).map(injectionMapper);
193
+
194
+ const injectionsList = injectionsListUnsafe.filter((v) => !!v);
195
+ return injectionsList;
196
+ };
197
+
198
+ private getSubstitutionsWithIndices = (
199
+ paramsList: Array<Parameter>,
200
+ ): Array<SubstitutingParameter> => {
201
+ // filter out parameters that are substitutions (runtime-supplied parameters)
202
+ const isSubstitutionFilter = (v: string): boolean => {
203
+ return v.startsWith('|', 0) && v.endsWith('|');
204
+ };
205
+
206
+ const substitutions = paramsList
207
+ .filter(({ value }) => isSubstitutionFilter(value))
208
+ .map(({ value: filteredValue, index }) => ({
209
+ from: filteredValue.replace(/\|/g, ''), // cleaning up the substitution value
210
+ atPos: index, // return substitutions indices
211
+ }));
212
+
213
+ return substitutions;
214
+ };
215
+
216
+ private getKnownParamsWithIndices = (
217
+ paramsList: Array<Parameter>,
218
+ argsTypes: string[],
219
+ ): Array<Parameter> => {
220
+ // filter out non-constant values (var or substituion), thereby only constant is found
221
+ const isConstantFilter = (value: string): boolean => {
222
+ const regex = /^(?!var)(?!\|.*\|$).*/;
223
+ return regex.test(value);
224
+ };
225
+
226
+ // filters constants and preservs their indeces among other entries in 'parameters-list'
227
+ const knownParamsFiltered: Array<Parameter> = paramsList.filter(
228
+ ({ value }) => isConstantFilter(value),
229
+ );
230
+
231
+ if (knownParamsFiltered.length > argsTypes.length) {
232
+ throw new Error(`Extraction error. Provided onchain types list length (${argsTypes.length})
233
+ is less than extracted constants list length (${knownParamsFiltered.length})`);
234
+ }
235
+
236
+ const knownParamsWithIndices = knownParamsFiltered.map(
237
+ ({ value: filteredValue, index }) => {
238
+ const encodedConstantWithIndex: Parameter = {
239
+ value: TypesNormalizer.toSolidityEncodedValueFromString(
240
+ [argsTypes[index]],
241
+ [filteredValue],
242
+ ),
243
+ index,
244
+ };
245
+ return encodedConstantWithIndex;
246
+ },
247
+ );
248
+
249
+ return knownParamsWithIndices;
250
+ };
251
+ }