@alephium/web3 0.2.0-rc.3 → 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.
@@ -30,7 +30,7 @@ export declare abstract class Common {
30
30
  static checkFileNameExtension(fileName: string): void;
31
31
  protected static _from<T extends {
32
32
  sourceCodeSha256: string;
33
- }>(provider: NodeProvider, sourceFile: SourceFile, loadContractStr: (sourceFile: SourceFile, importsCache: string[]) => Promise<string>, compile: (provider: NodeProvider, sourceFile: SourceFile, contractStr: string, contractHash: string, errorOnWarnings: boolean) => Promise<T>, errorOnWarnings: boolean): 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>;
34
34
  protected _saveToFile(sourceFile: SourceFile): Promise<void>;
35
35
  abstract buildByteCodeToDeploy(initialFields?: Fields): string;
36
36
  publicFunctions(): string[];
@@ -38,7 +38,7 @@ export declare abstract class Common {
38
38
  usingAssetsInContractFunctions(): string[];
39
39
  protected static checkCompilerWarnings(compiled: {
40
40
  warnings: string[];
41
- }, errorOnWarnings: boolean): void;
41
+ }, errorOnWarnings: boolean, ignoreUnusedConstantsWarnings: boolean): void;
42
42
  }
43
43
  export declare class Contract extends Common {
44
44
  readonly bytecode: string;
@@ -48,7 +48,7 @@ export declare class Contract extends Common {
48
48
  constructor(sourceCodeSha256: string, bytecode: string, codeHash: string, fieldsSig: FieldsSig, eventsSig: EventSig[], functions: FunctionSig[]);
49
49
  static checkCodeType(fileName: string, contractStr: string): void;
50
50
  private static loadContractStr;
51
- static fromSource(provider: NodeProvider, path: string, errorOnWarnings?: boolean): Promise<Contract>;
51
+ static fromSource(provider: NodeProvider, path: string, errorOnWarnings?: boolean, ignoreUnusedConstantsWarnings?: boolean): Promise<Contract>;
52
52
  private static compile;
53
53
  static fromJson(artifact: any): Contract;
54
54
  static fromArtifactFile(path: string): Promise<Contract>;
@@ -81,7 +81,7 @@ export declare class Script extends Common {
81
81
  constructor(sourceCodeSha256: string, bytecodeTemplate: string, fieldsSig: FieldsSig, functions: FunctionSig[]);
82
82
  static checkCodeType(fileName: string, contractStr: string): void;
83
83
  private static loadContractStr;
84
- static fromSource(provider: NodeProvider, path: string, errorOnWarnings?: boolean): Promise<Script>;
84
+ static fromSource(provider: NodeProvider, path: string, errorOnWarnings?: boolean, ignoreUnusedConstantsWarnings?: boolean): Promise<Script>;
85
85
  private static compile;
86
86
  static fromJson(artifact: any): Script;
87
87
  static fromArtifactFile(path: string): Promise<Script>;
@@ -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, errorOnWarnings) {
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, errorOnWarnings);
147
+ return compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings, ignoreUnusedConstantsWarnings);
148
148
  }
149
149
  }
150
150
  _saveToFile(sourceFile) {
@@ -163,10 +163,13 @@ class Common {
163
163
  usingAssetsInContractFunctions() {
164
164
  return this.functions.filter((func) => func.useAssetsInContract).map((func) => func.name);
165
165
  }
166
- static checkCompilerWarnings(compiled, errorOnWarnings) {
167
- if (compiled.warnings.length !== 0) {
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) {
168
171
  const prefixPerWarning = ' - ';
169
- const warningString = prefixPerWarning + compiled.warnings.join('\n' + prefixPerWarning);
172
+ const warningString = prefixPerWarning + warnings.join('\n' + prefixPerWarning);
170
173
  const output = 'Compilation warnings:\n' + warningString + '\n';
171
174
  if (errorOnWarnings) {
172
175
  throw new Error(output);
@@ -215,18 +218,18 @@ class Contract extends Common {
215
218
  static async loadContractStr(sourceFile) {
216
219
  return Common._loadContractStr(sourceFile, [], (code) => Contract.checkCodeType(sourceFile.contractPath, code));
217
220
  }
218
- static async fromSource(provider, path, errorOnWarnings = true) {
221
+ static async fromSource(provider, path, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
219
222
  if (!fs_1.default.existsSync(Common._artifactsFolder())) {
220
223
  fs_1.default.mkdirSync(Common._artifactsFolder(), { recursive: true });
221
224
  }
222
225
  const sourceFile = this.getSourceFile(path, []);
223
- const contract = await Common._from(provider, sourceFile, (sourceFile) => Contract.loadContractStr(sourceFile), Contract.compile, errorOnWarnings);
226
+ const contract = await Common._from(provider, sourceFile, Contract.loadContractStr, Contract.compile, errorOnWarnings, ignoreUnusedConstantsWarnings);
224
227
  this._putArtifactToCache(contract);
225
228
  return contract;
226
229
  }
227
- static async compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings) {
230
+ static async compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings, ignoreUnusedConstantsWarnings) {
228
231
  const compiled = await provider.contracts.postContractsCompileContract({ code: contractStr });
229
- Common.checkCompilerWarnings(compiled, errorOnWarnings);
232
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings);
230
233
  const artifact = new Contract(contractHash, compiled.bytecode, compiled.codeHash, compiled.fields, compiled.events, compiled.functions);
231
234
  await artifact._saveToFile(sourceFile);
232
235
  return artifact;
@@ -476,13 +479,13 @@ class Script extends Common {
476
479
  static async loadContractStr(sourceFile) {
477
480
  return Common._loadContractStr(sourceFile, [], (code) => Script.checkCodeType(sourceFile.contractPath, code));
478
481
  }
479
- static async fromSource(provider, path, errorOnWarnings = true) {
482
+ static async fromSource(provider, path, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
480
483
  const sourceFile = this.getSourceFile(path, []);
481
- return Common._from(provider, sourceFile, (sourceFile) => Script.loadContractStr(sourceFile), Script.compile, errorOnWarnings);
484
+ return Common._from(provider, sourceFile, (sourceFile) => Script.loadContractStr(sourceFile), Script.compile, errorOnWarnings, ignoreUnusedConstantsWarnings);
482
485
  }
483
- static async compile(provider, sourceFile, scriptStr, contractHash, errorOnWarnings = true) {
486
+ static async compile(provider, sourceFile, scriptStr, contractHash, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
484
487
  const compiled = await provider.contracts.postContractsCompileScript({ code: scriptStr });
485
- Common.checkCompilerWarnings(compiled, errorOnWarnings);
488
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings);
486
489
  const artifact = new Script(contractHash, compiled.bytecodeTemplate, compiled.fields, compiled.functions);
487
490
  await artifact._saveToFile(sourceFile);
488
491
  return artifact;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.2.0-rc.3",
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",
@@ -161,9 +161,11 @@ export abstract class Common {
161
161
  sourceFile: SourceFile,
162
162
  contractStr: string,
163
163
  contractHash: string,
164
- errorOnWarnings: boolean
164
+ errorOnWarnings: boolean,
165
+ ignoreUnusedConstantsWarnings: boolean
165
166
  ) => Promise<T>,
166
- errorOnWarnings: boolean
167
+ errorOnWarnings: boolean,
168
+ ignoreUnusedConstantsWarnings: boolean
167
169
  ): Promise<T> {
168
170
  Common.checkFileNameExtension(sourceFile.contractPath)
169
171
 
@@ -173,7 +175,7 @@ export abstract class Common {
173
175
  if (typeof existingContract !== 'undefined') {
174
176
  return existingContract as unknown as T
175
177
  } else {
176
- return compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings)
178
+ return compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings, ignoreUnusedConstantsWarnings)
177
179
  }
178
180
  }
179
181
 
@@ -199,10 +201,17 @@ export abstract class Common {
199
201
  return this.functions.filter((func) => func.useAssetsInContract).map((func) => func.name)
200
202
  }
201
203
 
202
- protected static checkCompilerWarnings(compiled: { warnings: string[] }, errorOnWarnings: boolean): void {
203
- if (compiled.warnings.length !== 0) {
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) {
204
213
  const prefixPerWarning = ' - '
205
- const warningString = prefixPerWarning + compiled.warnings.join('\n' + prefixPerWarning)
214
+ const warningString = prefixPerWarning + warnings.join('\n' + prefixPerWarning)
206
215
  const output = 'Compilation warnings:\n' + warningString + '\n'
207
216
  if (errorOnWarnings) {
208
217
  throw new Error(output)
@@ -259,7 +268,12 @@ export class Contract extends Common {
259
268
  return Common._loadContractStr(sourceFile, [], (code) => Contract.checkCodeType(sourceFile.contractPath, code))
260
269
  }
261
270
 
262
- static async fromSource(provider: NodeProvider, path: string, errorOnWarnings = true): Promise<Contract> {
271
+ static async fromSource(
272
+ provider: NodeProvider,
273
+ path: string,
274
+ errorOnWarnings = true,
275
+ ignoreUnusedConstantsWarnings = true
276
+ ): Promise<Contract> {
263
277
  if (!fs.existsSync(Common._artifactsFolder())) {
264
278
  fs.mkdirSync(Common._artifactsFolder(), { recursive: true })
265
279
  }
@@ -267,9 +281,10 @@ export class Contract extends Common {
267
281
  const contract = await Common._from(
268
282
  provider,
269
283
  sourceFile,
270
- (sourceFile) => Contract.loadContractStr(sourceFile),
284
+ Contract.loadContractStr,
271
285
  Contract.compile,
272
- errorOnWarnings
286
+ errorOnWarnings,
287
+ ignoreUnusedConstantsWarnings
273
288
  )
274
289
  this._putArtifactToCache(contract)
275
290
  return contract
@@ -280,10 +295,11 @@ export class Contract extends Common {
280
295
  sourceFile: SourceFile,
281
296
  contractStr: string,
282
297
  contractHash: string,
283
- errorOnWarnings: boolean
298
+ errorOnWarnings: boolean,
299
+ ignoreUnusedConstantsWarnings: boolean
284
300
  ): Promise<Contract> {
285
301
  const compiled = await provider.contracts.postContractsCompileContract({ code: contractStr })
286
- Common.checkCompilerWarnings(compiled, errorOnWarnings)
302
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings)
287
303
 
288
304
  const artifact = new Contract(
289
305
  contractHash,
@@ -604,14 +620,20 @@ export class Script extends Common {
604
620
  return Common._loadContractStr(sourceFile, [], (code) => Script.checkCodeType(sourceFile.contractPath, code))
605
621
  }
606
622
 
607
- static async fromSource(provider: NodeProvider, path: string, errorOnWarnings = true): Promise<Script> {
623
+ static async fromSource(
624
+ provider: NodeProvider,
625
+ path: string,
626
+ errorOnWarnings = true,
627
+ ignoreUnusedConstantsWarnings = true
628
+ ): Promise<Script> {
608
629
  const sourceFile = this.getSourceFile(path, [])
609
630
  return Common._from(
610
631
  provider,
611
632
  sourceFile,
612
633
  (sourceFile) => Script.loadContractStr(sourceFile),
613
634
  Script.compile,
614
- errorOnWarnings
635
+ errorOnWarnings,
636
+ ignoreUnusedConstantsWarnings
615
637
  )
616
638
  }
617
639
 
@@ -620,10 +642,11 @@ export class Script extends Common {
620
642
  sourceFile: SourceFile,
621
643
  scriptStr: string,
622
644
  contractHash: string,
623
- errorOnWarnings = true
645
+ errorOnWarnings = true,
646
+ ignoreUnusedConstantsWarnings = true
624
647
  ): Promise<Script> {
625
648
  const compiled = await provider.contracts.postContractsCompileScript({ code: scriptStr })
626
- Common.checkCompilerWarnings(compiled, errorOnWarnings)
649
+ Common.checkCompilerWarnings(compiled, errorOnWarnings, ignoreUnusedConstantsWarnings)
627
650
  const artifact = new Script(contractHash, compiled.bytecodeTemplate, compiled.fields, compiled.functions)
628
651
  await artifact._saveToFile(sourceFile)
629
652
  return artifact
@@ -12,7 +12,7 @@
12
12
  "stop-devnet": "node scripts/stop-devnet.js"
13
13
  },
14
14
  "dependencies": {
15
- "@alephium/web3": "0.2.0-rc.3"
15
+ "@alephium/web3": "0.2.0-rc.4"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/elliptic": "^6.4.13",
@@ -12,7 +12,7 @@
12
12
  "@types/node": "^16.11.26",
13
13
  "@types/react": "^18.0.3",
14
14
  "@types/react-dom": "^18.0.0",
15
- "@alephium/web3": "0.2.0-rc.3",
15
+ "@alephium/web3": "0.2.0-rc.4",
16
16
  "react": "^18.0.0",
17
17
  "react-dom": "^18.0.0",
18
18
  "react-scripts": "5.0.1",
@@ -193,5 +193,8 @@ describe('contract', function () {
193
193
  await expect(Contract.fromSource(provider, 'test/warnings.ral')).rejects.toThrowError(
194
194
  'Compilation warnings:\n - Found unused variables in function foo: foo.y\n - Found unused fields: b'
195
195
  )
196
+ await expect(Contract.fromSource(provider, 'test/warnings.ral', true, false)).rejects.toThrowError(
197
+ 'Compilation warnings:\n - Found unused variables in function foo: foo.y\n - Found unused constants: C\n - Found unused fields: b'
198
+ )
196
199
  })
197
200
  })