@alephium/web3 0.2.0-rc.1 → 0.2.0-rc.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.
@@ -86,7 +86,8 @@ function prepareShared(packageRoot, projectRoot) {
86
86
  }
87
87
  function prepareBase(packageRoot, projectRoot) {
88
88
  prepareShared(packageRoot, projectRoot);
89
- copy('contracts', ['greeter.ral', 'greeter_interface.ral', 'greeter_main.ral']);
89
+ copy('contracts', ['greeter_main.ral']);
90
+ copy('contracts/greeter', ['greeter.ral', 'greeter_interface.ral']);
90
91
  fs_extra_1.default.copySync(path_1.default.join(packageRoot, 'templates/base'), projectRoot);
91
92
  }
92
93
  function prepareReact(packageRoot, projectRoot, projectName) {
@@ -311,11 +311,13 @@ export interface CompileContractResult {
311
311
  fields: FieldsSig;
312
312
  functions: FunctionSig[];
313
313
  events: EventSig[];
314
+ warnings: string[];
314
315
  }
315
316
  export interface CompileScriptResult {
316
317
  bytecodeTemplate: string;
317
318
  fields: FieldsSig;
318
319
  functions: FunctionSig[];
320
+ warnings: string[];
319
321
  }
320
322
  export interface Confirmed {
321
323
  /** @format block-hash */
@@ -409,7 +411,6 @@ export interface Destination {
409
411
  export declare type DiscoveryAction = Reachable | Unreachable;
410
412
  export interface EventSig {
411
413
  name: string;
412
- signature: string;
413
414
  fieldNames: string[];
414
415
  fieldTypes: string[];
415
416
  }
@@ -417,9 +418,9 @@ export interface FetchResponse {
417
418
  blocks: BlockEntry[][];
418
419
  }
419
420
  export interface FieldsSig {
420
- signature: string;
421
421
  names: string[];
422
422
  types: string[];
423
+ isMutable: boolean[];
423
424
  }
424
425
  export interface FixedAssetOutput {
425
426
  /** @format int32 */
@@ -438,9 +439,12 @@ export interface FixedAssetOutput {
438
439
  }
439
440
  export interface FunctionSig {
440
441
  name: string;
441
- signature: string;
442
- argNames: string[];
443
- argTypes: string[];
442
+ usePreapprovedAssets: boolean;
443
+ useAssetsInContract: boolean;
444
+ isPublic: boolean;
445
+ paramNames: string[];
446
+ paramTypes: string[];
447
+ paramIsMutable: boolean[];
444
448
  returnTypes: string[];
445
449
  }
446
450
  export interface Group {
@@ -8,9 +8,12 @@ declare class SourceFile {
8
8
  readonly artifactPath: string;
9
9
  constructor(dirs: string[], fileName: string);
10
10
  }
11
+ declare type FieldsSig = node.FieldsSig;
12
+ declare type EventSig = node.EventSig;
13
+ declare type FunctionSig = node.FunctionSig;
11
14
  export declare abstract class Common {
12
15
  readonly sourceCodeSha256: string;
13
- readonly functions: node.FunctionSig[];
16
+ readonly functions: FunctionSig[];
14
17
  static readonly importRegex: RegExp;
15
18
  static readonly contractRegex: RegExp;
16
19
  static readonly interfaceRegex: RegExp;
@@ -19,7 +22,7 @@ export declare abstract class Common {
19
22
  static artifactCacheCapacity: number;
20
23
  protected static _getArtifactFromCache(codeHash: string): Contract | Script | undefined;
21
24
  protected static _putArtifactToCache(contract: Contract): void;
22
- constructor(sourceCodeSha256: string, functions: node.FunctionSig[]);
25
+ constructor(sourceCodeSha256: string, functions: FunctionSig[]);
23
26
  protected static _artifactsFolder(): string;
24
27
  static getSourceFile(path: string, _dirs: string[]): SourceFile;
25
28
  protected static _handleImports(pathes: string[], contractStr: string, importsCache: string[]): Promise<string>;
@@ -27,19 +30,25 @@ export declare abstract class Common {
27
30
  static checkFileNameExtension(fileName: string): void;
28
31
  protected static _from<T extends {
29
32
  sourceCodeSha256: string;
30
- }>(provider: NodeProvider, sourceFile: SourceFile, loadContractStr: (sourceFile: SourceFile, importsCache: string[]) => Promise<string>, compile: (provider: NodeProvider, sourceFile: SourceFile, contractStr: string, contractHash: string) => Promise<T>): Promise<T>;
33
+ }>(provider: NodeProvider, sourceFile: SourceFile, loadContractStr: (sourceFile: SourceFile, importsCache: string[]) => Promise<string>, compile: (provider: NodeProvider, sourceFile: SourceFile, contractStr: string, contractHash: string, errorOnWarnings: boolean, ignoreUnusedConstantsWarnings: boolean) => Promise<T>, errorOnWarnings: boolean, ignoreUnusedConstantsWarnings: boolean): Promise<T>;
31
34
  protected _saveToFile(sourceFile: SourceFile): Promise<void>;
32
35
  abstract buildByteCodeToDeploy(initialFields?: Fields): string;
36
+ publicFunctions(): string[];
37
+ usingPreapprovedAssetsFunctions(): string[];
38
+ usingAssetsInContractFunctions(): string[];
39
+ protected static checkCompilerWarnings(compiled: {
40
+ warnings: string[];
41
+ }, errorOnWarnings: boolean, ignoreUnusedConstantsWarnings: boolean): void;
33
42
  }
34
43
  export declare class Contract extends Common {
35
44
  readonly bytecode: string;
36
45
  readonly codeHash: string;
37
- readonly fieldsSig: node.FieldsSig;
38
- readonly eventsSig: node.EventSig[];
39
- constructor(sourceCodeSha256: string, bytecode: string, codeHash: string, fieldsSig: node.FieldsSig, eventsSig: node.EventSig[], functions: node.FunctionSig[]);
46
+ readonly fieldsSig: FieldsSig;
47
+ readonly eventsSig: EventSig[];
48
+ constructor(sourceCodeSha256: string, bytecode: string, codeHash: string, fieldsSig: FieldsSig, eventsSig: EventSig[], functions: FunctionSig[]);
40
49
  static checkCodeType(fileName: string, contractStr: string): void;
41
50
  private static loadContractStr;
42
- static fromSource(provider: NodeProvider, path: string): Promise<Contract>;
51
+ static fromSource(provider: NodeProvider, path: string, errorOnWarnings?: boolean, ignoreUnusedConstantsWarnings?: boolean): Promise<Contract>;
43
52
  private static compile;
44
53
  static fromJson(artifact: any): Contract;
45
54
  static fromArtifactFile(path: string): Promise<Contract>;
@@ -56,10 +65,10 @@ export declare class Contract extends Common {
56
65
  toApiContractStates(states?: ContractState[]): node.ContractState[] | undefined;
57
66
  toTestContract(funcName: string, params: TestContractParams): node.TestContract;
58
67
  static fromCodeHash(codeHash: string): Promise<Contract>;
59
- static getFieldsSig(state: node.ContractState): Promise<node.FieldsSig>;
68
+ static getFieldsSig(state: node.ContractState): Promise<FieldsSig>;
60
69
  fromApiContractState(state: node.ContractState): Promise<ContractState>;
61
- static ContractCreatedEvent: node.EventSig;
62
- static ContractDestroyedEvent: node.EventSig;
70
+ static ContractCreatedEvent: EventSig;
71
+ static ContractDestroyedEvent: EventSig;
63
72
  static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined): Promise<ContractEventByTxId>;
64
73
  fromTestContractResult(methodIndex: number, result: node.TestContractResult): Promise<TestContractResult>;
65
74
  paramsForDeployment(params: BuildDeployContractTx): Promise<SignDeployContractTxParams>;
@@ -68,11 +77,11 @@ export declare class Contract extends Common {
68
77
  }
69
78
  export declare class Script extends Common {
70
79
  readonly bytecodeTemplate: string;
71
- readonly fieldsSig: node.FieldsSig;
72
- constructor(sourceCodeSha256: string, bytecodeTemplate: string, fieldsSig: node.FieldsSig, functions: node.FunctionSig[]);
80
+ readonly fieldsSig: FieldsSig;
81
+ constructor(sourceCodeSha256: string, bytecodeTemplate: string, fieldsSig: FieldsSig, functions: FunctionSig[]);
73
82
  static checkCodeType(fileName: string, contractStr: string): void;
74
83
  private static loadContractStr;
75
- static fromSource(provider: NodeProvider, path: string): Promise<Script>;
84
+ static fromSource(provider: NodeProvider, path: string, errorOnWarnings?: boolean, ignoreUnusedConstantsWarnings?: boolean): Promise<Script>;
76
85
  private static compile;
77
86
  static fromJson(artifact: any): Script;
78
87
  static fromArtifactFile(path: string): Promise<Script>;
@@ -107,7 +116,7 @@ export interface ContractState {
107
116
  initialStateHash?: string;
108
117
  codeHash: string;
109
118
  fields: Fields;
110
- fieldsSig: node.FieldsSig;
119
+ fieldsSig: FieldsSig;
111
120
  asset: Asset;
112
121
  }
113
122
  export interface TestContractParams {
@@ -135,7 +135,7 @@ class Common {
135
135
  throw new Error('Smart contract file name should end with ".ral"');
136
136
  }
137
137
  }
138
- static async _from(provider, sourceFile, loadContractStr, compile) {
138
+ static async _from(provider, sourceFile, loadContractStr, compile, errorOnWarnings, ignoreUnusedConstantsWarnings) {
139
139
  Common.checkFileNameExtension(sourceFile.contractPath);
140
140
  const contractStr = await loadContractStr(sourceFile, []);
141
141
  const contractHash = cryptojs.SHA256(contractStr).toString();
@@ -144,7 +144,7 @@ class Common {
144
144
  return existingContract;
145
145
  }
146
146
  else {
147
- return compile(provider, sourceFile, contractStr, contractHash);
147
+ return compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings, ignoreUnusedConstantsWarnings);
148
148
  }
149
149
  }
150
150
  _saveToFile(sourceFile) {
@@ -154,6 +154,31 @@ class Common {
154
154
  }
155
155
  return fs_2.promises.writeFile(sourceFile.artifactPath, this.toString());
156
156
  }
157
+ publicFunctions() {
158
+ return this.functions.filter((func) => func.isPublic).map((func) => func.name);
159
+ }
160
+ usingPreapprovedAssetsFunctions() {
161
+ return this.functions.filter((func) => func.usePreapprovedAssets).map((func) => func.name);
162
+ }
163
+ usingAssetsInContractFunctions() {
164
+ return this.functions.filter((func) => func.useAssetsInContract).map((func) => func.name);
165
+ }
166
+ static checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings) {
167
+ const warnings = ignoreUnusedConstantsWarnings
168
+ ? compiled.warnings.filter((s) => !s.includes('unused constants'))
169
+ : compiled.warnings;
170
+ if (warnings.length !== 0) {
171
+ const prefixPerWarning = ' - ';
172
+ const warningString = prefixPerWarning + warnings.join('\n' + prefixPerWarning);
173
+ const output = 'Compilation warnings:\n' + warningString + '\n';
174
+ if (errorOnWarnings) {
175
+ throw new Error(output);
176
+ }
177
+ else {
178
+ console.log(output);
179
+ }
180
+ }
181
+ }
157
182
  }
158
183
  exports.Common = Common;
159
184
  Common.importRegex = new RegExp('^import "([^"/]+/(([^"]+)/)?)?[a-z][a-z_0-9]*.ral"', 'mg');
@@ -193,17 +218,18 @@ class Contract extends Common {
193
218
  static async loadContractStr(sourceFile) {
194
219
  return Common._loadContractStr(sourceFile, [], (code) => Contract.checkCodeType(sourceFile.contractPath, code));
195
220
  }
196
- static async fromSource(provider, path) {
221
+ static async fromSource(provider, path, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
197
222
  if (!fs_1.default.existsSync(Common._artifactsFolder())) {
198
223
  fs_1.default.mkdirSync(Common._artifactsFolder(), { recursive: true });
199
224
  }
200
225
  const sourceFile = this.getSourceFile(path, []);
201
- const contract = await Common._from(provider, sourceFile, (sourceFile) => Contract.loadContractStr(sourceFile), Contract.compile);
226
+ const contract = await Common._from(provider, sourceFile, Contract.loadContractStr, Contract.compile, errorOnWarnings, ignoreUnusedConstantsWarnings);
202
227
  this._putArtifactToCache(contract);
203
228
  return contract;
204
229
  }
205
- static async compile(provider, sourceFile, contractStr, contractHash) {
230
+ static async compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings, ignoreUnusedConstantsWarnings) {
206
231
  const compiled = await provider.contracts.postContractsCompileContract({ code: contractStr });
232
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings);
207
233
  const artifact = new Contract(contractHash, compiled.bytecode, compiled.codeHash, compiled.fields, compiled.events, compiled.functions);
208
234
  await artifact._saveToFile(sourceFile);
209
235
  return artifact;
@@ -265,7 +291,7 @@ class Contract extends Common {
265
291
  const apiParams = this.toTestContract(funcName, params);
266
292
  const apiResult = await provider.contracts.postContractsTestContract(apiParams);
267
293
  const methodIndex = typeof params.testMethodIndex !== 'undefined' ? params.testMethodIndex : this.getMethodIndex(funcName);
268
- const isPublic = this.functions[`${methodIndex}`].signature.indexOf('pub ') !== -1;
294
+ const isPublic = this.functions[`${methodIndex}`].isPublic;
269
295
  if (isPublic === expectPublic) {
270
296
  const result = await this.fromTestContractResult(methodIndex, apiResult);
271
297
  return result;
@@ -424,13 +450,11 @@ class Contract extends Common {
424
450
  exports.Contract = Contract;
425
451
  Contract.ContractCreatedEvent = {
426
452
  name: 'ContractCreated',
427
- signature: 'event ContractCreated(address:Address)',
428
453
  fieldNames: ['address'],
429
454
  fieldTypes: ['Address']
430
455
  };
431
456
  Contract.ContractDestroyedEvent = {
432
457
  name: 'ContractDestroyed',
433
- signature: 'event ContractDestroyed(address:Address)',
434
458
  fieldNames: ['address'],
435
459
  fieldTypes: ['Address']
436
460
  };
@@ -455,12 +479,13 @@ class Script extends Common {
455
479
  static async loadContractStr(sourceFile) {
456
480
  return Common._loadContractStr(sourceFile, [], (code) => Script.checkCodeType(sourceFile.contractPath, code));
457
481
  }
458
- static async fromSource(provider, path) {
482
+ static async fromSource(provider, path, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
459
483
  const sourceFile = this.getSourceFile(path, []);
460
- return Common._from(provider, sourceFile, (sourceFile) => Script.loadContractStr(sourceFile), Script.compile);
484
+ return Common._from(provider, sourceFile, (sourceFile) => Script.loadContractStr(sourceFile), Script.compile, errorOnWarnings, ignoreUnusedConstantsWarnings);
461
485
  }
462
- static async compile(provider, sourceFile, scriptStr, contractHash) {
486
+ static async compile(provider, sourceFile, scriptStr, contractHash, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
463
487
  const compiled = await provider.contracts.postContractsCompileScript({ code: scriptStr });
488
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings);
464
489
  const artifact = new Script(contractHash, compiled.bytecodeTemplate, compiled.fields, compiled.functions);
465
490
  await artifact._saveToFile(sourceFile);
466
491
  return artifact;
@@ -752,7 +777,7 @@ function toApiFields(fields, fieldsSig) {
752
777
  return toApiVals(fields, fieldsSig.names, fieldsSig.types);
753
778
  }
754
779
  function toApiArgs(args, funcSig) {
755
- return toApiVals(args, funcSig.argNames, funcSig.argTypes);
780
+ return toApiVals(args, funcSig.paramNames, funcSig.paramTypes);
756
781
  }
757
782
  function toApiVals(fields, names, types) {
758
783
  return names.map((name, index) => {
@@ -20,8 +20,8 @@ export declare function addressFromContractId(contractId: string): string;
20
20
  export declare function contractIdFromTx(txId: string, outputIndex: number): string;
21
21
  export declare function subContractId(parentContractId: string, pathInHex: string): string;
22
22
  export declare function stringToHex(str: string): string;
23
- export declare function hexToString(str: any): string;
24
- export declare function timeout(ms: number): Promise<unknown>;
23
+ export declare function hexToString(str: string): string;
24
+ export declare function timeout(ms: number): Promise<void>;
25
25
  declare type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
26
26
  export declare type Eq<X, Y> = _Eq<{
27
27
  [P in keyof X]: X[P];
@@ -195,7 +195,7 @@ function stringToHex(str) {
195
195
  }
196
196
  exports.stringToHex = stringToHex;
197
197
  function hexToString(str) {
198
- return buffer_1.Buffer.from(str.toString(), 'hex').toString();
198
+ return buffer_1.Buffer.from(str, 'hex').toString();
199
199
  }
200
200
  exports.hexToString = hexToString;
201
201
  function timeout(ms) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.2.0-rc.1",
3
+ "version": "0.2.0-rc.4",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "author": "Alephium dev <dev@alephium.org>",
29
29
  "config": {
30
- "alephium_version": "1.5.0-rc2",
30
+ "alephium_version": "1.5.0-rc4",
31
31
  "explorer_backend_version": "1.7.1"
32
32
  },
33
33
  "scripts": {
@@ -87,7 +87,8 @@ function prepareShared(packageRoot: string, projectRoot: string) {
87
87
 
88
88
  function prepareBase(packageRoot: string, projectRoot: string) {
89
89
  prepareShared(packageRoot, projectRoot)
90
- copy('contracts', ['greeter.ral', 'greeter_interface.ral', 'greeter_main.ral'])
90
+ copy('contracts', ['greeter_main.ral'])
91
+ copy('contracts/greeter', ['greeter.ral', 'greeter_interface.ral'])
91
92
  fsExtra.copySync(path.join(packageRoot, 'templates/base'), projectRoot)
92
93
  }
93
94
 
@@ -429,12 +429,14 @@ export interface CompileContractResult {
429
429
  fields: FieldsSig
430
430
  functions: FunctionSig[]
431
431
  events: EventSig[]
432
+ warnings: string[]
432
433
  }
433
434
 
434
435
  export interface CompileScriptResult {
435
436
  bytecodeTemplate: string
436
437
  fields: FieldsSig
437
438
  functions: FunctionSig[]
439
+ warnings: string[]
438
440
  }
439
441
 
440
442
  export interface Confirmed {
@@ -561,7 +563,6 @@ export type DiscoveryAction = Reachable | Unreachable
561
563
 
562
564
  export interface EventSig {
563
565
  name: string
564
- signature: string
565
566
  fieldNames: string[]
566
567
  fieldTypes: string[]
567
568
  }
@@ -571,9 +572,9 @@ export interface FetchResponse {
571
572
  }
572
573
 
573
574
  export interface FieldsSig {
574
- signature: string
575
575
  names: string[]
576
576
  types: string[]
577
+ isMutable: boolean[]
577
578
  }
578
579
 
579
580
  export interface FixedAssetOutput {
@@ -599,9 +600,12 @@ export interface FixedAssetOutput {
599
600
 
600
601
  export interface FunctionSig {
601
602
  name: string
602
- signature: string
603
- argNames: string[]
604
- argTypes: string[]
603
+ usePreapprovedAssets: boolean
604
+ useAssetsInContract: boolean
605
+ isPublic: boolean
606
+ paramNames: string[]
607
+ paramTypes: string[]
608
+ paramIsMutable: boolean[]
605
609
  returnTypes: string[]
606
610
  }
607
611
 
@@ -46,9 +46,13 @@ class SourceFile {
46
46
  }
47
47
  }
48
48
 
49
+ type FieldsSig = node.FieldsSig
50
+ type EventSig = node.EventSig
51
+ type FunctionSig = node.FunctionSig
52
+
49
53
  export abstract class Common {
50
54
  readonly sourceCodeSha256: string
51
- readonly functions: node.FunctionSig[]
55
+ readonly functions: FunctionSig[]
52
56
 
53
57
  static readonly importRegex = new RegExp('^import "([^"/]+/(([^"]+)/)?)?[a-z][a-z_0-9]*.ral"', 'mg')
54
58
  static readonly contractRegex = new RegExp('^(Abstract[ ]+)?Contract [A-Z][a-zA-Z0-9]*', 'mg')
@@ -70,7 +74,7 @@ export abstract class Common {
70
74
  }
71
75
  }
72
76
 
73
- constructor(sourceCodeSha256: string, functions: node.FunctionSig[]) {
77
+ constructor(sourceCodeSha256: string, functions: FunctionSig[]) {
74
78
  this.sourceCodeSha256 = sourceCodeSha256
75
79
  this.functions = functions
76
80
  }
@@ -152,7 +156,16 @@ export abstract class Common {
152
156
  provider: NodeProvider,
153
157
  sourceFile: SourceFile,
154
158
  loadContractStr: (sourceFile: SourceFile, importsCache: string[]) => Promise<string>,
155
- compile: (provider: NodeProvider, sourceFile: SourceFile, contractStr: string, contractHash: string) => Promise<T>
159
+ compile: (
160
+ provider: NodeProvider,
161
+ sourceFile: SourceFile,
162
+ contractStr: string,
163
+ contractHash: string,
164
+ errorOnWarnings: boolean,
165
+ ignoreUnusedConstantsWarnings: boolean
166
+ ) => Promise<T>,
167
+ errorOnWarnings: boolean,
168
+ ignoreUnusedConstantsWarnings: boolean
156
169
  ): Promise<T> {
157
170
  Common.checkFileNameExtension(sourceFile.contractPath)
158
171
 
@@ -162,7 +175,7 @@ export abstract class Common {
162
175
  if (typeof existingContract !== 'undefined') {
163
176
  return existingContract as unknown as T
164
177
  } else {
165
- return compile(provider, sourceFile, contractStr, contractHash)
178
+ return compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings, ignoreUnusedConstantsWarnings)
166
179
  }
167
180
  }
168
181
 
@@ -175,21 +188,53 @@ export abstract class Common {
175
188
  }
176
189
 
177
190
  abstract buildByteCodeToDeploy(initialFields?: Fields): string
191
+
192
+ publicFunctions(): string[] {
193
+ return this.functions.filter((func) => func.isPublic).map((func) => func.name)
194
+ }
195
+
196
+ usingPreapprovedAssetsFunctions(): string[] {
197
+ return this.functions.filter((func) => func.usePreapprovedAssets).map((func) => func.name)
198
+ }
199
+
200
+ usingAssetsInContractFunctions(): string[] {
201
+ return this.functions.filter((func) => func.useAssetsInContract).map((func) => func.name)
202
+ }
203
+
204
+ protected static checkCompilerWarnings(
205
+ compiled: { warnings: string[] },
206
+ errorOnWarnings: boolean,
207
+ ignoreUnusedConstantsWarnings: boolean
208
+ ): void {
209
+ const warnings = ignoreUnusedConstantsWarnings
210
+ ? compiled.warnings.filter((s) => !s.includes('unused constants'))
211
+ : compiled.warnings
212
+ if (warnings.length !== 0) {
213
+ const prefixPerWarning = ' - '
214
+ const warningString = prefixPerWarning + warnings.join('\n' + prefixPerWarning)
215
+ const output = 'Compilation warnings:\n' + warningString + '\n'
216
+ if (errorOnWarnings) {
217
+ throw new Error(output)
218
+ } else {
219
+ console.log(output)
220
+ }
221
+ }
222
+ }
178
223
  }
179
224
 
180
225
  export class Contract extends Common {
181
226
  readonly bytecode: string
182
227
  readonly codeHash: string
183
- readonly fieldsSig: node.FieldsSig
184
- readonly eventsSig: node.EventSig[]
228
+ readonly fieldsSig: FieldsSig
229
+ readonly eventsSig: EventSig[]
185
230
 
186
231
  constructor(
187
232
  sourceCodeSha256: string,
188
233
  bytecode: string,
189
234
  codeHash: string,
190
- fieldsSig: node.FieldsSig,
191
- eventsSig: node.EventSig[],
192
- functions: node.FunctionSig[]
235
+ fieldsSig: FieldsSig,
236
+ eventsSig: EventSig[],
237
+ functions: FunctionSig[]
193
238
  ) {
194
239
  super(sourceCodeSha256, functions)
195
240
  this.bytecode = bytecode
@@ -223,7 +268,12 @@ export class Contract extends Common {
223
268
  return Common._loadContractStr(sourceFile, [], (code) => Contract.checkCodeType(sourceFile.contractPath, code))
224
269
  }
225
270
 
226
- static async fromSource(provider: NodeProvider, path: string): Promise<Contract> {
271
+ static async fromSource(
272
+ provider: NodeProvider,
273
+ path: string,
274
+ errorOnWarnings = true,
275
+ ignoreUnusedConstantsWarnings = true
276
+ ): Promise<Contract> {
227
277
  if (!fs.existsSync(Common._artifactsFolder())) {
228
278
  fs.mkdirSync(Common._artifactsFolder(), { recursive: true })
229
279
  }
@@ -231,8 +281,10 @@ export class Contract extends Common {
231
281
  const contract = await Common._from(
232
282
  provider,
233
283
  sourceFile,
234
- (sourceFile) => Contract.loadContractStr(sourceFile),
235
- Contract.compile
284
+ Contract.loadContractStr,
285
+ Contract.compile,
286
+ errorOnWarnings,
287
+ ignoreUnusedConstantsWarnings
236
288
  )
237
289
  this._putArtifactToCache(contract)
238
290
  return contract
@@ -242,9 +294,13 @@ export class Contract extends Common {
242
294
  provider: NodeProvider,
243
295
  sourceFile: SourceFile,
244
296
  contractStr: string,
245
- contractHash: string
297
+ contractHash: string,
298
+ errorOnWarnings: boolean,
299
+ ignoreUnusedConstantsWarnings: boolean
246
300
  ): Promise<Contract> {
247
301
  const compiled = await provider.contracts.postContractsCompileContract({ code: contractStr })
302
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings)
303
+
248
304
  const artifact = new Contract(
249
305
  contractHash,
250
306
  compiled.bytecode,
@@ -341,7 +397,7 @@ export class Contract extends Common {
341
397
 
342
398
  const methodIndex =
343
399
  typeof params.testMethodIndex !== 'undefined' ? params.testMethodIndex : this.getMethodIndex(funcName)
344
- const isPublic = this.functions[`${methodIndex}`].signature.indexOf('pub ') !== -1
400
+ const isPublic = this.functions[`${methodIndex}`].isPublic
345
401
  if (isPublic === expectPublic) {
346
402
  const result = await this.fromTestContractResult(methodIndex, apiResult)
347
403
  return result
@@ -430,7 +486,7 @@ export class Contract extends Common {
430
486
  throw new Error(`Unknown code with code hash: ${codeHash}`)
431
487
  }
432
488
 
433
- static async getFieldsSig(state: node.ContractState): Promise<node.FieldsSig> {
489
+ static async getFieldsSig(state: node.ContractState): Promise<FieldsSig> {
434
490
  return Contract.fromCodeHash(state.codeHash).then((contract) => contract.fieldsSig)
435
491
  }
436
492
 
@@ -448,16 +504,14 @@ export class Contract extends Common {
448
504
  }
449
505
  }
450
506
 
451
- static ContractCreatedEvent: node.EventSig = {
507
+ static ContractCreatedEvent: EventSig = {
452
508
  name: 'ContractCreated',
453
- signature: 'event ContractCreated(address:Address)',
454
509
  fieldNames: ['address'],
455
510
  fieldTypes: ['Address']
456
511
  }
457
512
 
458
- static ContractDestroyedEvent: node.EventSig = {
513
+ static ContractDestroyedEvent: EventSig = {
459
514
  name: 'ContractDestroyed',
460
- signature: 'event ContractDestroyed(address:Address)',
461
515
  fieldNames: ['address'],
462
516
  fieldTypes: ['Address']
463
517
  }
@@ -466,7 +520,7 @@ export class Contract extends Common {
466
520
  event: node.ContractEventByTxId,
467
521
  codeHash: string | undefined
468
522
  ): Promise<ContractEventByTxId> {
469
- let eventSig: node.EventSig
523
+ let eventSig: EventSig
470
524
 
471
525
  if (event.eventIndex == -1) {
472
526
  eventSig = this.ContractCreatedEvent
@@ -543,14 +597,9 @@ export class Contract extends Common {
543
597
 
544
598
  export class Script extends Common {
545
599
  readonly bytecodeTemplate: string
546
- readonly fieldsSig: node.FieldsSig
600
+ readonly fieldsSig: FieldsSig
547
601
 
548
- constructor(
549
- sourceCodeSha256: string,
550
- bytecodeTemplate: string,
551
- fieldsSig: node.FieldsSig,
552
- functions: node.FunctionSig[]
553
- ) {
602
+ constructor(sourceCodeSha256: string, bytecodeTemplate: string, fieldsSig: FieldsSig, functions: FunctionSig[]) {
554
603
  super(sourceCodeSha256, functions)
555
604
  this.bytecodeTemplate = bytecodeTemplate
556
605
  this.fieldsSig = fieldsSig
@@ -571,18 +620,33 @@ export class Script extends Common {
571
620
  return Common._loadContractStr(sourceFile, [], (code) => Script.checkCodeType(sourceFile.contractPath, code))
572
621
  }
573
622
 
574
- static async fromSource(provider: NodeProvider, path: string): Promise<Script> {
623
+ static async fromSource(
624
+ provider: NodeProvider,
625
+ path: string,
626
+ errorOnWarnings = true,
627
+ ignoreUnusedConstantsWarnings = true
628
+ ): Promise<Script> {
575
629
  const sourceFile = this.getSourceFile(path, [])
576
- return Common._from(provider, sourceFile, (sourceFile) => Script.loadContractStr(sourceFile), Script.compile)
630
+ return Common._from(
631
+ provider,
632
+ sourceFile,
633
+ (sourceFile) => Script.loadContractStr(sourceFile),
634
+ Script.compile,
635
+ errorOnWarnings,
636
+ ignoreUnusedConstantsWarnings
637
+ )
577
638
  }
578
639
 
579
640
  private static async compile(
580
641
  provider: NodeProvider,
581
642
  sourceFile: SourceFile,
582
643
  scriptStr: string,
583
- contractHash: string
644
+ contractHash: string,
645
+ errorOnWarnings = true,
646
+ ignoreUnusedConstantsWarnings = true
584
647
  ): Promise<Script> {
585
648
  const compiled = await provider.contracts.postContractsCompileScript({ code: scriptStr })
649
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings)
586
650
  const artifact = new Script(contractHash, compiled.bytecodeTemplate, compiled.fields, compiled.functions)
587
651
  await artifact._saveToFile(sourceFile)
588
652
  return artifact
@@ -895,7 +959,7 @@ export interface ContractState {
895
959
  initialStateHash?: string
896
960
  codeHash: string
897
961
  fields: Fields
898
- fieldsSig: node.FieldsSig
962
+ fieldsSig: FieldsSig
899
963
  asset: Asset
900
964
  }
901
965
 
@@ -918,12 +982,12 @@ function toApiContractState(state: ContractState): node.ContractState {
918
982
  }
919
983
  }
920
984
 
921
- function toApiFields(fields: Fields, fieldsSig: node.FieldsSig): node.Val[] {
985
+ function toApiFields(fields: Fields, fieldsSig: FieldsSig): node.Val[] {
922
986
  return toApiVals(fields, fieldsSig.names, fieldsSig.types)
923
987
  }
924
988
 
925
- function toApiArgs(args: Arguments, funcSig: node.FunctionSig): node.Val[] {
926
- return toApiVals(args, funcSig.argNames, funcSig.argTypes)
989
+ function toApiArgs(args: Arguments, funcSig: FunctionSig): node.Val[] {
990
+ return toApiVals(args, funcSig.paramNames, funcSig.paramTypes)
927
991
  }
928
992
 
929
993
  function toApiVals(fields: Fields, names: string[], types: string[]): node.Val[] {
@@ -31,7 +31,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
31
31
  this.startPolling()
32
32
  }
33
33
 
34
- override startPolling() {
34
+ override startPolling(): void {
35
35
  this.eventEmitter.on('tick', async () => {
36
36
  await this.polling()
37
37
  })
@@ -42,7 +42,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
42
42
  return this.fromCount
43
43
  }
44
44
 
45
- override async polling() {
45
+ override async polling(): Promise<void> {
46
46
  try {
47
47
  const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
48
48
  start: this.fromCount