@alephium/web3 0.11.8 → 0.12.0

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.
@@ -8,6 +8,8 @@ export type FunctionSig = node.FunctionSig;
8
8
  export type Fields = NamedVals;
9
9
  export type Arguments = NamedVals;
10
10
  export type HexString = string;
11
+ export type Constant = node.Constant;
12
+ export type Enum = node.Enum;
11
13
  export declare const StdIdFieldName = "__stdInterfaceId";
12
14
  declare enum SourceKind {
13
15
  Contract = 0,
@@ -107,10 +109,12 @@ export declare class Contract extends Artifact {
107
109
  readonly codeHash: string;
108
110
  readonly fieldsSig: FieldsSig;
109
111
  readonly eventsSig: EventSig[];
112
+ readonly constants: Constant[];
113
+ readonly enums: Enum[];
110
114
  readonly stdInterfaceId?: HexString;
111
115
  readonly bytecodeDebug: string;
112
116
  readonly codeHashDebug: string;
113
- constructor(version: string, name: string, bytecode: string, bytecodeDebugPatch: string, codeHash: string, codeHashDebug: string, fieldsSig: FieldsSig, eventsSig: EventSig[], functions: FunctionSig[], stdInterfaceId?: HexString);
117
+ constructor(version: string, name: string, bytecode: string, bytecodeDebugPatch: string, codeHash: string, codeHashDebug: string, fieldsSig: FieldsSig, eventsSig: EventSig[], functions: FunctionSig[], constants: Constant[], enums: Enum[], stdInterfaceId?: HexString);
114
118
  static fromJson(artifact: any, bytecodeDebugPatch?: string, codeHashDebug?: string): Contract;
115
119
  static fromCompileResult(result: node.CompileContractResult): Contract;
116
120
  static fromArtifactFile(path: string, bytecodeDebugPatch: string, codeHashDebug: string): Promise<Contract>;
@@ -92,12 +92,16 @@ function removeParentsPrefix(parts) {
92
92
  }
93
93
  class SourceInfo {
94
94
  getArtifactPath(artifactsRootDir) {
95
+ let fullPath;
95
96
  if (this.isExternal) {
96
97
  const relativePath = removeParentsPrefix(this.contractRelativePath.split(path.sep));
97
98
  const externalPath = path.join('.external', relativePath);
98
- return path.join(artifactsRootDir, externalPath) + '.json';
99
+ fullPath = path.join(artifactsRootDir, externalPath);
99
100
  }
100
- return path.join(artifactsRootDir, this.contractRelativePath) + '.json';
101
+ else {
102
+ fullPath = path.join(artifactsRootDir, this.contractRelativePath);
103
+ }
104
+ return path.join(path.dirname(fullPath), `${this.name}.ral.json`);
101
105
  }
102
106
  constructor(type, name, sourceCode, sourceCodeHash, contractRelativePath, isExternal) {
103
107
  this.type = type;
@@ -285,20 +289,34 @@ class Project {
285
289
  return contract.artifact;
286
290
  }
287
291
  static async compile(fullNodeVersion, provider, sourceInfos, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions) {
288
- const sourceStr = sourceInfos.map((f) => f.sourceCode).join('\n');
292
+ const removeDuplicates = sourceInfos.reduce((acc, sourceInfo) => {
293
+ if (acc.find((info) => info.sourceCodeHash === sourceInfo.sourceCodeHash) === undefined) {
294
+ acc.push(sourceInfo);
295
+ }
296
+ return acc;
297
+ }, []);
298
+ const sourceStr = removeDuplicates.map((f) => f.sourceCode).join('\n');
289
299
  const result = await provider.contracts.postContractsCompileProject({
290
300
  code: sourceStr,
291
301
  compilerOptions: compilerOptions
292
302
  });
293
303
  const contracts = new Map();
294
304
  const scripts = new Map();
295
- result.contracts.forEach((contractResult, index) => {
296
- const sourceInfo = sourceInfos[`${index}`];
305
+ result.contracts.forEach((contractResult) => {
306
+ const sourceInfo = sourceInfos.find((sourceInfo) => sourceInfo.type === SourceKind.Contract && sourceInfo.name === contractResult.name);
307
+ if (sourceInfo === undefined) {
308
+ // this should never happen
309
+ throw new Error(`SourceInfo does not exist for contract ${contractResult.name}`);
310
+ }
297
311
  const contract = Contract.fromCompileResult(contractResult);
298
312
  contracts.set(contract.name, new Compiled(sourceInfo, contract, contractResult.warnings));
299
313
  });
300
- result.scripts.forEach((scriptResult, index) => {
301
- const sourceInfo = sourceInfos[index + contracts.size];
314
+ result.scripts.forEach((scriptResult) => {
315
+ const sourceInfo = sourceInfos.find((sourceInfo) => sourceInfo.type === SourceKind.Script && sourceInfo.name === scriptResult.name);
316
+ if (sourceInfo === undefined) {
317
+ // this should never happen
318
+ throw new Error(`SourceInfo does not exist for script ${scriptResult.name}`);
319
+ }
302
320
  const script = Script.fromCompileResult(scriptResult);
303
321
  scripts.set(script.name, new Compiled(sourceInfo, script, scriptResult.warnings));
304
322
  });
@@ -465,13 +483,15 @@ class Artifact {
465
483
  }
466
484
  exports.Artifact = Artifact;
467
485
  class Contract extends Artifact {
468
- constructor(version, name, bytecode, bytecodeDebugPatch, codeHash, codeHashDebug, fieldsSig, eventsSig, functions, stdInterfaceId) {
486
+ constructor(version, name, bytecode, bytecodeDebugPatch, codeHash, codeHashDebug, fieldsSig, eventsSig, functions, constants, enums, stdInterfaceId) {
469
487
  super(version, name, functions);
470
488
  this.bytecode = bytecode;
471
489
  this.bytecodeDebugPatch = bytecodeDebugPatch;
472
490
  this.codeHash = codeHash;
473
491
  this.fieldsSig = fieldsSig;
474
492
  this.eventsSig = eventsSig;
493
+ this.constants = constants;
494
+ this.enums = enums;
475
495
  this.stdInterfaceId = stdInterfaceId;
476
496
  this.bytecodeDebug = ralph.buildDebugBytecode(this.bytecode, this.bytecodeDebugPatch);
477
497
  this.codeHashDebug = codeHashDebug;
@@ -484,14 +504,16 @@ class Contract extends Artifact {
484
504
  artifact.codeHash == null ||
485
505
  artifact.fieldsSig == null ||
486
506
  artifact.eventsSig == null ||
507
+ artifact.constants == null ||
508
+ artifact.enums == null ||
487
509
  artifact.functions == null) {
488
510
  throw Error('The artifact JSON for contract is incomplete');
489
511
  }
490
- const contract = new Contract(artifact.version, artifact.name, artifact.bytecode, bytecodeDebugPatch, artifact.codeHash, codeHashDebug ? codeHashDebug : artifact.codeHash, artifact.fieldsSig, artifact.eventsSig, artifact.functions, artifact.stdInterfaceId === null ? undefined : artifact.stdInterfaceId);
512
+ const contract = new Contract(artifact.version, artifact.name, artifact.bytecode, bytecodeDebugPatch, artifact.codeHash, codeHashDebug ? codeHashDebug : artifact.codeHash, artifact.fieldsSig, artifact.eventsSig, artifact.functions, artifact.constants, artifact.enums, artifact.stdInterfaceId === null ? undefined : artifact.stdInterfaceId);
491
513
  return contract;
492
514
  }
493
515
  static fromCompileResult(result) {
494
- return new Contract(result.version, result.name, result.bytecode, result.bytecodeDebugPatch, result.codeHash, result.codeHashDebug, result.fields, result.events, result.functions, result.stdInterfaceId);
516
+ return new Contract(result.version, result.name, result.bytecode, result.bytecodeDebugPatch, result.codeHash, result.codeHashDebug, result.fields, result.events, result.functions, result.constants, result.enums, result.stdInterfaceId);
495
517
  }
496
518
  // support both 'code.ral' and 'code.ral.json'
497
519
  static async fromArtifactFile(path, bytecodeDebugPatch, codeHashDebug) {
@@ -507,7 +529,9 @@ class Contract extends Artifact {
507
529
  codeHash: this.codeHash,
508
530
  fieldsSig: this.fieldsSig,
509
531
  eventsSig: this.eventsSig,
510
- functions: this.functions
532
+ functions: this.functions,
533
+ constants: this.constants,
534
+ enums: this.enums
511
535
  };
512
536
  if (this.stdInterfaceId !== undefined) {
513
537
  object.stdInterfaceId = this.stdInterfaceId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.11.8",
3
+ "version": "0.12.0",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -67,6 +67,8 @@ export type FunctionSig = node.FunctionSig
67
67
  export type Fields = NamedVals
68
68
  export type Arguments = NamedVals
69
69
  export type HexString = string
70
+ export type Constant = node.Constant
71
+ export type Enum = node.Enum
70
72
 
71
73
  export const StdIdFieldName = '__stdInterfaceId'
72
74
 
@@ -123,12 +125,15 @@ class SourceInfo {
123
125
  isExternal: boolean
124
126
 
125
127
  getArtifactPath(artifactsRootDir: string): string {
128
+ let fullPath: string
126
129
  if (this.isExternal) {
127
130
  const relativePath = removeParentsPrefix(this.contractRelativePath.split(path.sep))
128
131
  const externalPath = path.join('.external', relativePath)
129
- return path.join(artifactsRootDir, externalPath) + '.json'
132
+ fullPath = path.join(artifactsRootDir, externalPath)
133
+ } else {
134
+ fullPath = path.join(artifactsRootDir, this.contractRelativePath)
130
135
  }
131
- return path.join(artifactsRootDir, this.contractRelativePath) + '.json'
136
+ return path.join(path.dirname(fullPath), `${this.name}.ral.json`)
132
137
  }
133
138
 
134
139
  constructor(
@@ -418,20 +423,38 @@ export class Project {
418
423
  errorOnWarnings: boolean,
419
424
  compilerOptions: node.CompilerOptions
420
425
  ): Promise<Project> {
421
- const sourceStr = sourceInfos.map((f) => f.sourceCode).join('\n')
426
+ const removeDuplicates = sourceInfos.reduce((acc: SourceInfo[], sourceInfo: SourceInfo) => {
427
+ if (acc.find((info) => info.sourceCodeHash === sourceInfo.sourceCodeHash) === undefined) {
428
+ acc.push(sourceInfo)
429
+ }
430
+ return acc
431
+ }, [])
432
+ const sourceStr = removeDuplicates.map((f) => f.sourceCode).join('\n')
422
433
  const result = await provider.contracts.postContractsCompileProject({
423
434
  code: sourceStr,
424
435
  compilerOptions: compilerOptions
425
436
  })
426
437
  const contracts = new Map<string, Compiled<Contract>>()
427
438
  const scripts = new Map<string, Compiled<Script>>()
428
- result.contracts.forEach((contractResult, index) => {
429
- const sourceInfo = sourceInfos[`${index}`]
439
+ result.contracts.forEach((contractResult) => {
440
+ const sourceInfo = sourceInfos.find(
441
+ (sourceInfo) => sourceInfo.type === SourceKind.Contract && sourceInfo.name === contractResult.name
442
+ )
443
+ if (sourceInfo === undefined) {
444
+ // this should never happen
445
+ throw new Error(`SourceInfo does not exist for contract ${contractResult.name}`)
446
+ }
430
447
  const contract = Contract.fromCompileResult(contractResult)
431
448
  contracts.set(contract.name, new Compiled(sourceInfo, contract, contractResult.warnings))
432
449
  })
433
- result.scripts.forEach((scriptResult, index) => {
434
- const sourceInfo = sourceInfos[index + contracts.size]
450
+ result.scripts.forEach((scriptResult) => {
451
+ const sourceInfo = sourceInfos.find(
452
+ (sourceInfo) => sourceInfo.type === SourceKind.Script && sourceInfo.name === scriptResult.name
453
+ )
454
+ if (sourceInfo === undefined) {
455
+ // this should never happen
456
+ throw new Error(`SourceInfo does not exist for script ${scriptResult.name}`)
457
+ }
435
458
  const script = Script.fromCompileResult(scriptResult)
436
459
  scripts.set(script.name, new Compiled(sourceInfo, script, scriptResult.warnings))
437
460
  })
@@ -696,6 +719,8 @@ export class Contract extends Artifact {
696
719
  readonly codeHash: string
697
720
  readonly fieldsSig: FieldsSig
698
721
  readonly eventsSig: EventSig[]
722
+ readonly constants: Constant[]
723
+ readonly enums: Enum[]
699
724
  readonly stdInterfaceId?: HexString
700
725
 
701
726
  readonly bytecodeDebug: string
@@ -711,6 +736,8 @@ export class Contract extends Artifact {
711
736
  fieldsSig: FieldsSig,
712
737
  eventsSig: EventSig[],
713
738
  functions: FunctionSig[],
739
+ constants: Constant[],
740
+ enums: Enum[],
714
741
  stdInterfaceId?: HexString
715
742
  ) {
716
743
  super(version, name, functions)
@@ -719,6 +746,8 @@ export class Contract extends Artifact {
719
746
  this.codeHash = codeHash
720
747
  this.fieldsSig = fieldsSig
721
748
  this.eventsSig = eventsSig
749
+ this.constants = constants
750
+ this.enums = enums
722
751
  this.stdInterfaceId = stdInterfaceId
723
752
 
724
753
  this.bytecodeDebug = ralph.buildDebugBytecode(this.bytecode, this.bytecodeDebugPatch)
@@ -734,6 +763,8 @@ export class Contract extends Artifact {
734
763
  artifact.codeHash == null ||
735
764
  artifact.fieldsSig == null ||
736
765
  artifact.eventsSig == null ||
766
+ artifact.constants == null ||
767
+ artifact.enums == null ||
737
768
  artifact.functions == null
738
769
  ) {
739
770
  throw Error('The artifact JSON for contract is incomplete')
@@ -748,6 +779,8 @@ export class Contract extends Artifact {
748
779
  artifact.fieldsSig,
749
780
  artifact.eventsSig,
750
781
  artifact.functions,
782
+ artifact.constants,
783
+ artifact.enums,
751
784
  artifact.stdInterfaceId === null ? undefined : artifact.stdInterfaceId
752
785
  )
753
786
  return contract
@@ -764,6 +797,8 @@ export class Contract extends Artifact {
764
797
  result.fields,
765
798
  result.events,
766
799
  result.functions,
800
+ result.constants,
801
+ result.enums,
767
802
  result.stdInterfaceId
768
803
  )
769
804
  }
@@ -783,7 +818,9 @@ export class Contract extends Artifact {
783
818
  codeHash: this.codeHash,
784
819
  fieldsSig: this.fieldsSig,
785
820
  eventsSig: this.eventsSig,
786
- functions: this.functions
821
+ functions: this.functions,
822
+ constants: this.constants,
823
+ enums: this.enums
787
824
  }
788
825
  if (this.stdInterfaceId !== undefined) {
789
826
  object.stdInterfaceId = this.stdInterfaceId