@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.
- package/contracts/add/add.ral +1 -3
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/contract/contract.d.ts +7 -2
- package/dist/src/contract/contract.js +14 -8
- package/package.json +1 -1
- package/src/contract/contract.ts +19 -11
- package/templates/base/package.json +1 -1
- package/templates/react/package.json +1 -1
- package/test/contract.test.ts +9 -9
- package/test/events.test.ts +2 -2
|
@@ -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,
|
|
51
|
-
static script(path: string,
|
|
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,
|
|
134
|
-
const remains = ignoreUnusedConstantsWarnings
|
|
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,
|
|
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,
|
|
159
|
+
Project.checkCompilerWarnings(contract.warnings, { ...exports.DEFAULT_COMPILER_OPTIONS, ...compilerOptions });
|
|
154
160
|
return contract.artifact;
|
|
155
161
|
}
|
|
156
|
-
static script(path,
|
|
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,
|
|
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
package/src/contract/contract.ts
CHANGED
|
@@ -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
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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,
|
|
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,
|
|
209
|
+
Project.checkCompilerWarnings(contract.warnings, { ...DEFAULT_COMPILER_OPTIONS, ...compilerOptions })
|
|
202
210
|
return contract.artifact
|
|
203
211
|
}
|
|
204
212
|
|
|
205
|
-
static script(path: string,
|
|
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,
|
|
219
|
+
Project.checkCompilerWarnings(script.warnings, { ...DEFAULT_COMPILER_OPTIONS, ...compilerOptions })
|
|
212
220
|
return script.artifact
|
|
213
221
|
}
|
|
214
222
|
|
package/test/contract.test.ts
CHANGED
|
@@ -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: {
|
|
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.
|
|
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: {
|
|
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.
|
|
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.
|
|
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',
|
|
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
|
})
|
package/test/events.test.ts
CHANGED
|
@@ -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: {
|
|
43
|
+
initialFields: { sub: subContractId, result: 0 },
|
|
44
44
|
initialTokenAmounts: []
|
|
45
45
|
})
|
|
46
46
|
const addSubmitResult = await signer.submitTransaction(addDeployTx.unsignedTx, addDeployTx.txId)
|