@alephium/web3 0.2.0-rc.5 → 0.2.0-rc.6

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.
@@ -11,6 +11,11 @@ declare enum SourceType {
11
11
  AbstractContract = 2,
12
12
  Interface = 3
13
13
  }
14
+ export declare type CompilerOptions = {
15
+ errorOnWarnings: boolean;
16
+ ignoreUnusedConstantsWarnings: boolean;
17
+ };
18
+ export declare const DEFAULT_COMPILER_OPTIONS: CompilerOptions;
14
19
  declare class TypedMatcher<T extends SourceType> {
15
20
  matcher: RegExp;
16
21
  type: T;
@@ -47,8 +52,8 @@ export declare class Project {
47
52
  private constructor();
48
53
  private getContractPath;
49
54
  private static checkCompilerWarnings;
50
- static contract(path: string, errorOnWarnings?: boolean, ignoreUnusedConstantsWarnings?: boolean): Contract;
51
- static script(path: string, errorOnWarnings?: boolean, ignoreUnusedConstantsWarnings?: boolean): Script;
55
+ static contract(path: string, compilerOptions?: Partial<CompilerOptions>): Contract;
56
+ static script(path: string, compilerOptions?: Partial<CompilerOptions>): Script;
52
57
  private saveArtifactsToFile;
53
58
  contractByCodeHash(codeHash: string): Contract;
54
59
  private saveProjectArtifactToFile;
@@ -39,7 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.toApiVal = exports.extractArray = exports.Script = exports.Contract = exports.Artifact = exports.Project = void 0;
42
+ exports.toApiVal = exports.extractArray = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = void 0;
43
43
  const buffer_1 = require("buffer/");
44
44
  const cryptojs = __importStar(require("crypto-js"));
45
45
  const crypto = __importStar(require("crypto"));
@@ -54,6 +54,10 @@ var SourceType;
54
54
  SourceType[SourceType["AbstractContract"] = 2] = "AbstractContract";
55
55
  SourceType[SourceType["Interface"] = 3] = "Interface";
56
56
  })(SourceType || (SourceType = {}));
57
+ exports.DEFAULT_COMPILER_OPTIONS = {
58
+ errorOnWarnings: true,
59
+ ignoreUnusedConstantsWarnings: true
60
+ };
57
61
  class TypedMatcher {
58
62
  constructor(pattern, type) {
59
63
  this.matcher = new RegExp(pattern, 'mg');
@@ -130,13 +134,15 @@ class Project {
130
134
  ? path
131
135
  : this.contractsRootPath + '/' + path;
132
136
  }
133
- static checkCompilerWarnings(warnings, errorOnWarnings, ignoreUnusedConstantsWarnings) {
134
- const remains = ignoreUnusedConstantsWarnings ? warnings.filter((s) => !s.includes('unused constants')) : warnings;
137
+ static checkCompilerWarnings(warnings, compilerOptions) {
138
+ const remains = compilerOptions.ignoreUnusedConstantsWarnings
139
+ ? warnings.filter((s) => !s.includes('unused constants'))
140
+ : warnings;
135
141
  if (remains.length !== 0) {
136
142
  const prefixPerWarning = ' - ';
137
143
  const warningString = prefixPerWarning + remains.join('\n' + prefixPerWarning);
138
144
  const output = 'Compilation warnings:\n' + warningString + '\n';
139
- if (errorOnWarnings) {
145
+ if (compilerOptions.errorOnWarnings) {
140
146
  throw new Error(output);
141
147
  }
142
148
  else {
@@ -144,22 +150,22 @@ class Project {
144
150
  }
145
151
  }
146
152
  }
147
- static contract(path, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
153
+ static contract(path, compilerOptions) {
148
154
  const contractPath = Project.currentProject.getContractPath(path);
149
155
  const contract = Project.currentProject.contracts.find((c) => c.sourceFile.contractPath === contractPath);
150
156
  if (typeof contract === 'undefined') {
151
157
  throw new Error(`Contract ${contractPath} does not exist`);
152
158
  }
153
- Project.checkCompilerWarnings(contract.warnings, errorOnWarnings, ignoreUnusedConstantsWarnings);
159
+ Project.checkCompilerWarnings(contract.warnings, { ...exports.DEFAULT_COMPILER_OPTIONS, ...compilerOptions });
154
160
  return contract.artifact;
155
161
  }
156
- static script(path, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true) {
162
+ static script(path, compilerOptions) {
157
163
  const contractPath = Project.currentProject.getContractPath(path);
158
164
  const script = Project.currentProject.scripts.find((c) => c.sourceFile.contractPath === contractPath);
159
165
  if (typeof script === 'undefined') {
160
166
  throw new Error(`Script ${contractPath} does not exist`);
161
167
  }
162
- Project.checkCompilerWarnings(script.warnings, errorOnWarnings, ignoreUnusedConstantsWarnings);
168
+ Project.checkCompilerWarnings(script.warnings, { ...exports.DEFAULT_COMPILER_OPTIONS, ...compilerOptions });
163
169
  return script.artifact;
164
170
  }
165
171
  async saveArtifactsToFile() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.2.0-rc.5",
3
+ "version": "0.2.0-rc.6",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -39,6 +39,16 @@ enum SourceType {
39
39
  Interface = 3
40
40
  }
41
41
 
42
+ export type CompilerOptions = {
43
+ errorOnWarnings: boolean
44
+ ignoreUnusedConstantsWarnings: boolean
45
+ }
46
+
47
+ export const DEFAULT_COMPILER_OPTIONS: CompilerOptions = {
48
+ errorOnWarnings: true,
49
+ ignoreUnusedConstantsWarnings: true
50
+ }
51
+
42
52
  class TypedMatcher<T extends SourceType> {
43
53
  matcher: RegExp
44
54
  type: T
@@ -174,17 +184,15 @@ export class Project {
174
184
  : this.contractsRootPath + '/' + path
175
185
  }
176
186
 
177
- private static checkCompilerWarnings(
178
- warnings: string[],
179
- errorOnWarnings: boolean,
180
- ignoreUnusedConstantsWarnings: boolean
181
- ): void {
182
- const remains = ignoreUnusedConstantsWarnings ? warnings.filter((s) => !s.includes('unused constants')) : warnings
187
+ private static checkCompilerWarnings(warnings: string[], compilerOptions: CompilerOptions): void {
188
+ const remains = compilerOptions.ignoreUnusedConstantsWarnings
189
+ ? warnings.filter((s) => !s.includes('unused constants'))
190
+ : warnings
183
191
  if (remains.length !== 0) {
184
192
  const prefixPerWarning = ' - '
185
193
  const warningString = prefixPerWarning + remains.join('\n' + prefixPerWarning)
186
194
  const output = 'Compilation warnings:\n' + warningString + '\n'
187
- if (errorOnWarnings) {
195
+ if (compilerOptions.errorOnWarnings) {
188
196
  throw new Error(output)
189
197
  } else {
190
198
  console.log(output)
@@ -192,23 +200,23 @@ export class Project {
192
200
  }
193
201
  }
194
202
 
195
- static contract(path: string, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true): Contract {
203
+ static contract(path: string, compilerOptions?: Partial<CompilerOptions>): Contract {
196
204
  const contractPath = Project.currentProject.getContractPath(path)
197
205
  const contract = Project.currentProject.contracts.find((c) => c.sourceFile.contractPath === contractPath)
198
206
  if (typeof contract === 'undefined') {
199
207
  throw new Error(`Contract ${contractPath} does not exist`)
200
208
  }
201
- Project.checkCompilerWarnings(contract.warnings, errorOnWarnings, ignoreUnusedConstantsWarnings)
209
+ Project.checkCompilerWarnings(contract.warnings, { ...DEFAULT_COMPILER_OPTIONS, ...compilerOptions })
202
210
  return contract.artifact
203
211
  }
204
212
 
205
- static script(path: string, errorOnWarnings = true, ignoreUnusedConstantsWarnings = true): Script {
213
+ static script(path: string, compilerOptions?: Partial<CompilerOptions>): Script {
206
214
  const contractPath = Project.currentProject.getContractPath(path)
207
215
  const script = Project.currentProject.scripts.find((c) => c.sourceFile.contractPath === contractPath)
208
216
  if (typeof script === 'undefined') {
209
217
  throw new Error(`Script ${contractPath} does not exist`)
210
218
  }
211
- Project.checkCompilerWarnings(script.warnings, errorOnWarnings, ignoreUnusedConstantsWarnings)
219
+ Project.checkCompilerWarnings(script.warnings, { ...DEFAULT_COMPILER_OPTIONS, ...compilerOptions })
212
220
  return script.artifact
213
221
  }
214
222
 
@@ -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.5"
15
+ "@alephium/web3": "0.2.0-rc.6"
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.5",
15
+ "@alephium/web3": "0.2.0-rc.6",
16
16
  "react": "^18.0.0",
17
17
  "react-dom": "^18.0.0",
18
18
  "react-scripts": "5.0.1",
@@ -30,12 +30,12 @@ describe('contract', function () {
30
30
  await Project.build(provider)
31
31
 
32
32
  // ignore unused private function warnings
33
- const add = Project.contract('add/add.ral', false)
33
+ const add = Project.contract('add/add.ral', { errorOnWarnings: false })
34
34
  const sub = Project.contract('sub/sub.ral')
35
35
 
36
36
  const subState = sub.toState({ result: 0 }, { alphAmount: BigInt('1000000000000000000') })
37
37
  const testParams: TestContractParams = {
38
- initialFields: { subContractId: subState.contractId, result: 0 },
38
+ initialFields: { sub: subState.contractId, result: 0 },
39
39
  testArgs: { array: [2, 1] },
40
40
  existingContracts: [subState]
41
41
  }
@@ -44,7 +44,7 @@ describe('contract', function () {
44
44
  expect(testResult.contracts[0].codeHash).toEqual(sub.codeHash)
45
45
  expect(testResult.contracts[0].fields.result).toEqual(1)
46
46
  expect(testResult.contracts[1].codeHash).toEqual(add.codeHash)
47
- expect(testResult.contracts[1].fields.subContractId).toEqual(subState.contractId)
47
+ expect(testResult.contracts[1].fields.sub).toEqual(subState.contractId)
48
48
  expect(testResult.contracts[1].fields.result).toEqual(3)
49
49
  const events = testResult.events.sort((a, b) => a.name.localeCompare(b.name))
50
50
  expect(events[0].name).toEqual('Add')
@@ -73,7 +73,7 @@ describe('contract', function () {
73
73
  expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
74
74
 
75
75
  const addDeployTx = await add.transactionForDeployment(signer, {
76
- initialFields: { subContractId: subContractId, result: 0 },
76
+ initialFields: { sub: subContractId, result: 0 },
77
77
  initialTokenAmounts: []
78
78
  })
79
79
  expect(addDeployTx.fromGroup).toEqual(0)
@@ -90,7 +90,7 @@ describe('contract', function () {
90
90
  let fetchedSubState = await sub.fetchState(subContractAddress, 0)
91
91
  expect(fetchedSubState.fields.result).toEqual(0)
92
92
  let fetchedAddState = await add.fetchState(addContractAddress, 0)
93
- expect(fetchedAddState.fields.subContractId).toEqual(subContractId)
93
+ expect(fetchedAddState.fields.sub).toEqual(subContractId)
94
94
  expect(fetchedAddState.fields.result).toEqual(0)
95
95
 
96
96
  const main = Project.script('main.ral')
@@ -107,7 +107,7 @@ describe('contract', function () {
107
107
  fetchedSubState = await sub.fetchState(subContractAddress, 0)
108
108
  expect(fetchedSubState.fields.result).toEqual(1)
109
109
  fetchedAddState = await add.fetchState(addContractAddress, 0)
110
- expect(fetchedAddState.fields.subContractId).toEqual(subContractId)
110
+ expect(fetchedAddState.fields.sub).toEqual(subContractId)
111
111
  expect(fetchedAddState.fields.result).toEqual(3)
112
112
  }
113
113
 
@@ -190,7 +190,7 @@ describe('contract', function () {
190
190
  it('should extract metadata of contracts', async () => {
191
191
  const provider = new NodeProvider('http://127.0.0.1:22973')
192
192
  await Project.build(provider)
193
- const contract = Project.contract('test/metadata.ral', false)
193
+ const contract = Project.contract('test/metadata.ral', { errorOnWarnings: false })
194
194
  expect(contract.functions.map((func) => func.name)).toEqual(['foo', 'bar', 'baz'])
195
195
  expect(contract.publicFunctions()).toEqual(['foo'])
196
196
  expect(contract.usingPreapprovedAssetsFunctions()).toEqual(['foo'])
@@ -200,13 +200,13 @@ describe('contract', function () {
200
200
  it('should handle compiler warnings', async () => {
201
201
  const provider = new NodeProvider('http://127.0.0.1:22973')
202
202
  await Project.build(provider)
203
- const contract = Project.contract('test/warnings.ral', false)
203
+ const contract = Project.contract('test/warnings.ral', { errorOnWarnings: false })
204
204
  expect(contract.publicFunctions()).toEqual(['foo'])
205
205
 
206
206
  expect(() => Project.contract('test/warnings.ral')).toThrowError(
207
207
  'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused fields in Warnings: b'
208
208
  )
209
- expect(() => Project.contract('test/warnings.ral', true, false)).toThrowError(
209
+ expect(() => Project.contract('test/warnings.ral', { ignoreUnusedConstantsWarnings: false })).toThrowError(
210
210
  'Compilation warnings:\n - Found unused variables in Warnings: foo.y\n - Found unused constants in Warnings: C\n - Found unused fields in Warnings: b'
211
211
  )
212
212
  })
@@ -38,9 +38,9 @@ describe('events', function () {
38
38
  expect(subSubmitResult.txId).toEqual(subDeployTx.txId)
39
39
 
40
40
  // ignore unused private function warnings
41
- const add = Project.contract('add/add.ral', false)
41
+ const add = Project.contract('add/add.ral', { errorOnWarnings: false })
42
42
  const addDeployTx = await add.transactionForDeployment(signer, {
43
- initialFields: { subContractId: subContractId, result: 0 },
43
+ initialFields: { sub: subContractId, result: 0 },
44
44
  initialTokenAmounts: []
45
45
  })
46
46
  const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)