@alephium/web3 0.2.0-rc.2 → 0.2.0-rc.3
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/test/metadata.ral +17 -0
- package/contracts/test/warnings.ral +5 -0
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +9 -5
- package/dist/src/contract/contract.d.ts +23 -14
- package/dist/src/contract/contract.js +34 -12
- package/dist/src/utils/utils.d.ts +2 -2
- package/dist/src/utils/utils.js +1 -1
- package/package.json +2 -2
- package/src/api/api-alephium.ts +9 -5
- package/src/contract/contract.ts +74 -33
- package/src/contract/events.ts +2 -2
- package/src/contract/ralph.test.ts +4 -4
- package/src/transaction/status.ts +1 -1
- package/src/utils/subscription.ts +1 -1
- package/src/utils/utils.ts +3 -3
- package/templates/base/package.json +2 -2
- package/templates/react/package.json +2 -2
- package/test/contract.test.ts +19 -0
- package/test/transaction.test.ts +1 -1
|
@@ -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
|
-
|
|
442
|
-
|
|
443
|
-
|
|
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:
|
|
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:
|
|
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
|
|
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>;
|
|
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): void;
|
|
33
42
|
}
|
|
34
43
|
export declare class Contract extends Common {
|
|
35
44
|
readonly bytecode: string;
|
|
36
45
|
readonly codeHash: string;
|
|
37
|
-
readonly fieldsSig:
|
|
38
|
-
readonly eventsSig:
|
|
39
|
-
constructor(sourceCodeSha256: string, bytecode: string, codeHash: string, fieldsSig:
|
|
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): 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<
|
|
68
|
+
static getFieldsSig(state: node.ContractState): Promise<FieldsSig>;
|
|
60
69
|
fromApiContractState(state: node.ContractState): Promise<ContractState>;
|
|
61
|
-
static ContractCreatedEvent:
|
|
62
|
-
static ContractDestroyedEvent:
|
|
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:
|
|
72
|
-
constructor(sourceCodeSha256: string, bytecodeTemplate: string, fieldsSig:
|
|
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): 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:
|
|
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) {
|
|
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);
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
_saveToFile(sourceFile) {
|
|
@@ -154,6 +154,28 @@ 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) {
|
|
167
|
+
if (compiled.warnings.length !== 0) {
|
|
168
|
+
const prefixPerWarning = ' - ';
|
|
169
|
+
const warningString = prefixPerWarning + compiled.warnings.join('\n' + prefixPerWarning);
|
|
170
|
+
const output = 'Compilation warnings:\n' + warningString + '\n';
|
|
171
|
+
if (errorOnWarnings) {
|
|
172
|
+
throw new Error(output);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
console.log(output);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
157
179
|
}
|
|
158
180
|
exports.Common = Common;
|
|
159
181
|
Common.importRegex = new RegExp('^import "([^"/]+/(([^"]+)/)?)?[a-z][a-z_0-9]*.ral"', 'mg');
|
|
@@ -193,17 +215,18 @@ class Contract extends Common {
|
|
|
193
215
|
static async loadContractStr(sourceFile) {
|
|
194
216
|
return Common._loadContractStr(sourceFile, [], (code) => Contract.checkCodeType(sourceFile.contractPath, code));
|
|
195
217
|
}
|
|
196
|
-
static async fromSource(provider, path) {
|
|
218
|
+
static async fromSource(provider, path, errorOnWarnings = true) {
|
|
197
219
|
if (!fs_1.default.existsSync(Common._artifactsFolder())) {
|
|
198
220
|
fs_1.default.mkdirSync(Common._artifactsFolder(), { recursive: true });
|
|
199
221
|
}
|
|
200
222
|
const sourceFile = this.getSourceFile(path, []);
|
|
201
|
-
const contract = await Common._from(provider, sourceFile, (sourceFile) => Contract.loadContractStr(sourceFile), Contract.compile);
|
|
223
|
+
const contract = await Common._from(provider, sourceFile, (sourceFile) => Contract.loadContractStr(sourceFile), Contract.compile, errorOnWarnings);
|
|
202
224
|
this._putArtifactToCache(contract);
|
|
203
225
|
return contract;
|
|
204
226
|
}
|
|
205
|
-
static async compile(provider, sourceFile, contractStr, contractHash) {
|
|
227
|
+
static async compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings) {
|
|
206
228
|
const compiled = await provider.contracts.postContractsCompileContract({ code: contractStr });
|
|
229
|
+
Common.checkCompilerWarnings(compiled, errorOnWarnings);
|
|
207
230
|
const artifact = new Contract(contractHash, compiled.bytecode, compiled.codeHash, compiled.fields, compiled.events, compiled.functions);
|
|
208
231
|
await artifact._saveToFile(sourceFile);
|
|
209
232
|
return artifact;
|
|
@@ -265,7 +288,7 @@ class Contract extends Common {
|
|
|
265
288
|
const apiParams = this.toTestContract(funcName, params);
|
|
266
289
|
const apiResult = await provider.contracts.postContractsTestContract(apiParams);
|
|
267
290
|
const methodIndex = typeof params.testMethodIndex !== 'undefined' ? params.testMethodIndex : this.getMethodIndex(funcName);
|
|
268
|
-
const isPublic = this.functions[`${methodIndex}`].
|
|
291
|
+
const isPublic = this.functions[`${methodIndex}`].isPublic;
|
|
269
292
|
if (isPublic === expectPublic) {
|
|
270
293
|
const result = await this.fromTestContractResult(methodIndex, apiResult);
|
|
271
294
|
return result;
|
|
@@ -424,13 +447,11 @@ class Contract extends Common {
|
|
|
424
447
|
exports.Contract = Contract;
|
|
425
448
|
Contract.ContractCreatedEvent = {
|
|
426
449
|
name: 'ContractCreated',
|
|
427
|
-
signature: 'event ContractCreated(address:Address)',
|
|
428
450
|
fieldNames: ['address'],
|
|
429
451
|
fieldTypes: ['Address']
|
|
430
452
|
};
|
|
431
453
|
Contract.ContractDestroyedEvent = {
|
|
432
454
|
name: 'ContractDestroyed',
|
|
433
|
-
signature: 'event ContractDestroyed(address:Address)',
|
|
434
455
|
fieldNames: ['address'],
|
|
435
456
|
fieldTypes: ['Address']
|
|
436
457
|
};
|
|
@@ -455,12 +476,13 @@ class Script extends Common {
|
|
|
455
476
|
static async loadContractStr(sourceFile) {
|
|
456
477
|
return Common._loadContractStr(sourceFile, [], (code) => Script.checkCodeType(sourceFile.contractPath, code));
|
|
457
478
|
}
|
|
458
|
-
static async fromSource(provider, path) {
|
|
479
|
+
static async fromSource(provider, path, errorOnWarnings = true) {
|
|
459
480
|
const sourceFile = this.getSourceFile(path, []);
|
|
460
|
-
return Common._from(provider, sourceFile, (sourceFile) => Script.loadContractStr(sourceFile), Script.compile);
|
|
481
|
+
return Common._from(provider, sourceFile, (sourceFile) => Script.loadContractStr(sourceFile), Script.compile, errorOnWarnings);
|
|
461
482
|
}
|
|
462
|
-
static async compile(provider, sourceFile, scriptStr, contractHash) {
|
|
483
|
+
static async compile(provider, sourceFile, scriptStr, contractHash, errorOnWarnings = true) {
|
|
463
484
|
const compiled = await provider.contracts.postContractsCompileScript({ code: scriptStr });
|
|
485
|
+
Common.checkCompilerWarnings(compiled, errorOnWarnings);
|
|
464
486
|
const artifact = new Script(contractHash, compiled.bytecodeTemplate, compiled.fields, compiled.functions);
|
|
465
487
|
await artifact._saveToFile(sourceFile);
|
|
466
488
|
return artifact;
|
|
@@ -752,7 +774,7 @@ function toApiFields(fields, fieldsSig) {
|
|
|
752
774
|
return toApiVals(fields, fieldsSig.names, fieldsSig.types);
|
|
753
775
|
}
|
|
754
776
|
function toApiArgs(args, funcSig) {
|
|
755
|
-
return toApiVals(args, funcSig.
|
|
777
|
+
return toApiVals(args, funcSig.paramNames, funcSig.paramTypes);
|
|
756
778
|
}
|
|
757
779
|
function toApiVals(fields, names, types) {
|
|
758
780
|
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:
|
|
24
|
-
export declare function timeout(ms: number): Promise<
|
|
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];
|
package/dist/src/utils/utils.js
CHANGED
|
@@ -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
|
|
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.
|
|
3
|
+
"version": "0.2.0-rc.3",
|
|
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-
|
|
30
|
+
"alephium_version": "1.5.0-rc4",
|
|
31
31
|
"explorer_backend_version": "1.7.1"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
package/src/api/api-alephium.ts
CHANGED
|
@@ -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
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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
|
|
package/src/contract/contract.ts
CHANGED
|
@@ -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:
|
|
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:
|
|
77
|
+
constructor(sourceCodeSha256: string, functions: FunctionSig[]) {
|
|
74
78
|
this.sourceCodeSha256 = sourceCodeSha256
|
|
75
79
|
this.functions = functions
|
|
76
80
|
}
|
|
@@ -152,7 +156,14 @@ export abstract class Common {
|
|
|
152
156
|
provider: NodeProvider,
|
|
153
157
|
sourceFile: SourceFile,
|
|
154
158
|
loadContractStr: (sourceFile: SourceFile, importsCache: string[]) => Promise<string>,
|
|
155
|
-
compile: (
|
|
159
|
+
compile: (
|
|
160
|
+
provider: NodeProvider,
|
|
161
|
+
sourceFile: SourceFile,
|
|
162
|
+
contractStr: string,
|
|
163
|
+
contractHash: string,
|
|
164
|
+
errorOnWarnings: boolean
|
|
165
|
+
) => Promise<T>,
|
|
166
|
+
errorOnWarnings: boolean
|
|
156
167
|
): Promise<T> {
|
|
157
168
|
Common.checkFileNameExtension(sourceFile.contractPath)
|
|
158
169
|
|
|
@@ -162,7 +173,7 @@ export abstract class Common {
|
|
|
162
173
|
if (typeof existingContract !== 'undefined') {
|
|
163
174
|
return existingContract as unknown as T
|
|
164
175
|
} else {
|
|
165
|
-
return compile(provider, sourceFile, contractStr, contractHash)
|
|
176
|
+
return compile(provider, sourceFile, contractStr, contractHash, errorOnWarnings)
|
|
166
177
|
}
|
|
167
178
|
}
|
|
168
179
|
|
|
@@ -175,21 +186,46 @@ export abstract class Common {
|
|
|
175
186
|
}
|
|
176
187
|
|
|
177
188
|
abstract buildByteCodeToDeploy(initialFields?: Fields): string
|
|
189
|
+
|
|
190
|
+
publicFunctions(): string[] {
|
|
191
|
+
return this.functions.filter((func) => func.isPublic).map((func) => func.name)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
usingPreapprovedAssetsFunctions(): string[] {
|
|
195
|
+
return this.functions.filter((func) => func.usePreapprovedAssets).map((func) => func.name)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
usingAssetsInContractFunctions(): string[] {
|
|
199
|
+
return this.functions.filter((func) => func.useAssetsInContract).map((func) => func.name)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
protected static checkCompilerWarnings(compiled: { warnings: string[] }, errorOnWarnings: boolean): void {
|
|
203
|
+
if (compiled.warnings.length !== 0) {
|
|
204
|
+
const prefixPerWarning = ' - '
|
|
205
|
+
const warningString = prefixPerWarning + compiled.warnings.join('\n' + prefixPerWarning)
|
|
206
|
+
const output = 'Compilation warnings:\n' + warningString + '\n'
|
|
207
|
+
if (errorOnWarnings) {
|
|
208
|
+
throw new Error(output)
|
|
209
|
+
} else {
|
|
210
|
+
console.log(output)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
178
214
|
}
|
|
179
215
|
|
|
180
216
|
export class Contract extends Common {
|
|
181
217
|
readonly bytecode: string
|
|
182
218
|
readonly codeHash: string
|
|
183
|
-
readonly fieldsSig:
|
|
184
|
-
readonly eventsSig:
|
|
219
|
+
readonly fieldsSig: FieldsSig
|
|
220
|
+
readonly eventsSig: EventSig[]
|
|
185
221
|
|
|
186
222
|
constructor(
|
|
187
223
|
sourceCodeSha256: string,
|
|
188
224
|
bytecode: string,
|
|
189
225
|
codeHash: string,
|
|
190
|
-
fieldsSig:
|
|
191
|
-
eventsSig:
|
|
192
|
-
functions:
|
|
226
|
+
fieldsSig: FieldsSig,
|
|
227
|
+
eventsSig: EventSig[],
|
|
228
|
+
functions: FunctionSig[]
|
|
193
229
|
) {
|
|
194
230
|
super(sourceCodeSha256, functions)
|
|
195
231
|
this.bytecode = bytecode
|
|
@@ -223,7 +259,7 @@ export class Contract extends Common {
|
|
|
223
259
|
return Common._loadContractStr(sourceFile, [], (code) => Contract.checkCodeType(sourceFile.contractPath, code))
|
|
224
260
|
}
|
|
225
261
|
|
|
226
|
-
static async fromSource(provider: NodeProvider, path: string): Promise<Contract> {
|
|
262
|
+
static async fromSource(provider: NodeProvider, path: string, errorOnWarnings = true): Promise<Contract> {
|
|
227
263
|
if (!fs.existsSync(Common._artifactsFolder())) {
|
|
228
264
|
fs.mkdirSync(Common._artifactsFolder(), { recursive: true })
|
|
229
265
|
}
|
|
@@ -232,7 +268,8 @@ export class Contract extends Common {
|
|
|
232
268
|
provider,
|
|
233
269
|
sourceFile,
|
|
234
270
|
(sourceFile) => Contract.loadContractStr(sourceFile),
|
|
235
|
-
Contract.compile
|
|
271
|
+
Contract.compile,
|
|
272
|
+
errorOnWarnings
|
|
236
273
|
)
|
|
237
274
|
this._putArtifactToCache(contract)
|
|
238
275
|
return contract
|
|
@@ -242,9 +279,12 @@ export class Contract extends Common {
|
|
|
242
279
|
provider: NodeProvider,
|
|
243
280
|
sourceFile: SourceFile,
|
|
244
281
|
contractStr: string,
|
|
245
|
-
contractHash: string
|
|
282
|
+
contractHash: string,
|
|
283
|
+
errorOnWarnings: boolean
|
|
246
284
|
): Promise<Contract> {
|
|
247
285
|
const compiled = await provider.contracts.postContractsCompileContract({ code: contractStr })
|
|
286
|
+
Common.checkCompilerWarnings(compiled, errorOnWarnings)
|
|
287
|
+
|
|
248
288
|
const artifact = new Contract(
|
|
249
289
|
contractHash,
|
|
250
290
|
compiled.bytecode,
|
|
@@ -341,7 +381,7 @@ export class Contract extends Common {
|
|
|
341
381
|
|
|
342
382
|
const methodIndex =
|
|
343
383
|
typeof params.testMethodIndex !== 'undefined' ? params.testMethodIndex : this.getMethodIndex(funcName)
|
|
344
|
-
const isPublic = this.functions[`${methodIndex}`].
|
|
384
|
+
const isPublic = this.functions[`${methodIndex}`].isPublic
|
|
345
385
|
if (isPublic === expectPublic) {
|
|
346
386
|
const result = await this.fromTestContractResult(methodIndex, apiResult)
|
|
347
387
|
return result
|
|
@@ -430,7 +470,7 @@ export class Contract extends Common {
|
|
|
430
470
|
throw new Error(`Unknown code with code hash: ${codeHash}`)
|
|
431
471
|
}
|
|
432
472
|
|
|
433
|
-
static async getFieldsSig(state: node.ContractState): Promise<
|
|
473
|
+
static async getFieldsSig(state: node.ContractState): Promise<FieldsSig> {
|
|
434
474
|
return Contract.fromCodeHash(state.codeHash).then((contract) => contract.fieldsSig)
|
|
435
475
|
}
|
|
436
476
|
|
|
@@ -448,16 +488,14 @@ export class Contract extends Common {
|
|
|
448
488
|
}
|
|
449
489
|
}
|
|
450
490
|
|
|
451
|
-
static ContractCreatedEvent:
|
|
491
|
+
static ContractCreatedEvent: EventSig = {
|
|
452
492
|
name: 'ContractCreated',
|
|
453
|
-
signature: 'event ContractCreated(address:Address)',
|
|
454
493
|
fieldNames: ['address'],
|
|
455
494
|
fieldTypes: ['Address']
|
|
456
495
|
}
|
|
457
496
|
|
|
458
|
-
static ContractDestroyedEvent:
|
|
497
|
+
static ContractDestroyedEvent: EventSig = {
|
|
459
498
|
name: 'ContractDestroyed',
|
|
460
|
-
signature: 'event ContractDestroyed(address:Address)',
|
|
461
499
|
fieldNames: ['address'],
|
|
462
500
|
fieldTypes: ['Address']
|
|
463
501
|
}
|
|
@@ -466,7 +504,7 @@ export class Contract extends Common {
|
|
|
466
504
|
event: node.ContractEventByTxId,
|
|
467
505
|
codeHash: string | undefined
|
|
468
506
|
): Promise<ContractEventByTxId> {
|
|
469
|
-
let eventSig:
|
|
507
|
+
let eventSig: EventSig
|
|
470
508
|
|
|
471
509
|
if (event.eventIndex == -1) {
|
|
472
510
|
eventSig = this.ContractCreatedEvent
|
|
@@ -543,14 +581,9 @@ export class Contract extends Common {
|
|
|
543
581
|
|
|
544
582
|
export class Script extends Common {
|
|
545
583
|
readonly bytecodeTemplate: string
|
|
546
|
-
readonly fieldsSig:
|
|
584
|
+
readonly fieldsSig: FieldsSig
|
|
547
585
|
|
|
548
|
-
constructor(
|
|
549
|
-
sourceCodeSha256: string,
|
|
550
|
-
bytecodeTemplate: string,
|
|
551
|
-
fieldsSig: node.FieldsSig,
|
|
552
|
-
functions: node.FunctionSig[]
|
|
553
|
-
) {
|
|
586
|
+
constructor(sourceCodeSha256: string, bytecodeTemplate: string, fieldsSig: FieldsSig, functions: FunctionSig[]) {
|
|
554
587
|
super(sourceCodeSha256, functions)
|
|
555
588
|
this.bytecodeTemplate = bytecodeTemplate
|
|
556
589
|
this.fieldsSig = fieldsSig
|
|
@@ -571,18 +604,26 @@ export class Script extends Common {
|
|
|
571
604
|
return Common._loadContractStr(sourceFile, [], (code) => Script.checkCodeType(sourceFile.contractPath, code))
|
|
572
605
|
}
|
|
573
606
|
|
|
574
|
-
static async fromSource(provider: NodeProvider, path: string): Promise<Script> {
|
|
607
|
+
static async fromSource(provider: NodeProvider, path: string, errorOnWarnings = true): Promise<Script> {
|
|
575
608
|
const sourceFile = this.getSourceFile(path, [])
|
|
576
|
-
return Common._from(
|
|
609
|
+
return Common._from(
|
|
610
|
+
provider,
|
|
611
|
+
sourceFile,
|
|
612
|
+
(sourceFile) => Script.loadContractStr(sourceFile),
|
|
613
|
+
Script.compile,
|
|
614
|
+
errorOnWarnings
|
|
615
|
+
)
|
|
577
616
|
}
|
|
578
617
|
|
|
579
618
|
private static async compile(
|
|
580
619
|
provider: NodeProvider,
|
|
581
620
|
sourceFile: SourceFile,
|
|
582
621
|
scriptStr: string,
|
|
583
|
-
contractHash: string
|
|
622
|
+
contractHash: string,
|
|
623
|
+
errorOnWarnings = true
|
|
584
624
|
): Promise<Script> {
|
|
585
625
|
const compiled = await provider.contracts.postContractsCompileScript({ code: scriptStr })
|
|
626
|
+
Common.checkCompilerWarnings(compiled, errorOnWarnings)
|
|
586
627
|
const artifact = new Script(contractHash, compiled.bytecodeTemplate, compiled.fields, compiled.functions)
|
|
587
628
|
await artifact._saveToFile(sourceFile)
|
|
588
629
|
return artifact
|
|
@@ -895,7 +936,7 @@ export interface ContractState {
|
|
|
895
936
|
initialStateHash?: string
|
|
896
937
|
codeHash: string
|
|
897
938
|
fields: Fields
|
|
898
|
-
fieldsSig:
|
|
939
|
+
fieldsSig: FieldsSig
|
|
899
940
|
asset: Asset
|
|
900
941
|
}
|
|
901
942
|
|
|
@@ -918,12 +959,12 @@ function toApiContractState(state: ContractState): node.ContractState {
|
|
|
918
959
|
}
|
|
919
960
|
}
|
|
920
961
|
|
|
921
|
-
function toApiFields(fields: Fields, fieldsSig:
|
|
962
|
+
function toApiFields(fields: Fields, fieldsSig: FieldsSig): node.Val[] {
|
|
922
963
|
return toApiVals(fields, fieldsSig.names, fieldsSig.types)
|
|
923
964
|
}
|
|
924
965
|
|
|
925
|
-
function toApiArgs(args: Arguments, funcSig:
|
|
926
|
-
return toApiVals(args, funcSig.
|
|
966
|
+
function toApiArgs(args: Arguments, funcSig: FunctionSig): node.Val[] {
|
|
967
|
+
return toApiVals(args, funcSig.paramNames, funcSig.paramTypes)
|
|
927
968
|
}
|
|
928
969
|
|
|
929
970
|
function toApiVals(fields: Fields, names: string[], types: string[]): node.Val[] {
|
package/src/contract/events.ts
CHANGED
|
@@ -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
|
|
@@ -133,9 +133,9 @@ describe('contract', function () {
|
|
|
133
133
|
it('should test buildScriptByteCode', async () => {
|
|
134
134
|
const variables = { x: true, y: 0x05, z: 'ff', a: '1C2RAVWSuaXw8xtUxqVERR7ChKBE1XgscNFw73NSHE1v3' }
|
|
135
135
|
const fieldsSig: node.FieldsSig = {
|
|
136
|
-
signature: 'Not really',
|
|
137
136
|
names: ['x', 'y', 'z', 'a'],
|
|
138
|
-
types: ['Bool', 'U256', 'ByteVec', 'Address']
|
|
137
|
+
types: ['Bool', 'U256', 'ByteVec', 'Address'],
|
|
138
|
+
isMutable: [false, false, false, false]
|
|
139
139
|
}
|
|
140
140
|
const bytecode = ralph.buildScriptByteCode('-{0}-{1}-{2}-{3}-', variables, fieldsSig)
|
|
141
141
|
expect(bytecode).toEqual('-03-1305-1401ff-1500a3cd757be03c7dac8d48bf79e2a7d6e735e018a9c054b99138c7b29738c437ec-')
|
|
@@ -150,9 +150,9 @@ describe('contract', function () {
|
|
|
150
150
|
e: [false, true]
|
|
151
151
|
}
|
|
152
152
|
const fieldsSig: node.FieldsSig = {
|
|
153
|
-
signature: 'Not really',
|
|
154
153
|
names: ['a', 'b', 'c', 'd', 'e'],
|
|
155
|
-
types: ['I256', 'U256', 'ByteVec', 'Address', '[Bool;2]']
|
|
154
|
+
types: ['I256', 'U256', 'ByteVec', 'Address', '[Bool;2]'],
|
|
155
|
+
isMutable: [false, false, false, false]
|
|
156
156
|
}
|
|
157
157
|
const encoded = ralph.buildContractByteCode('ff', fields, fieldsSig)
|
|
158
158
|
expect(encoded).toEqual(
|
|
@@ -33,7 +33,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
|
33
33
|
this.startPolling()
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
override async polling() {
|
|
36
|
+
override async polling(): Promise<void> {
|
|
37
37
|
try {
|
|
38
38
|
const txStatus = await this.provider.transactions.getTransactionsStatus({
|
|
39
39
|
txId: this.txId,
|
package/src/utils/utils.ts
CHANGED
|
@@ -203,11 +203,11 @@ export function stringToHex(str: string): string {
|
|
|
203
203
|
return hex
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
export function hexToString(str:
|
|
207
|
-
return Buffer.from(str
|
|
206
|
+
export function hexToString(str: string): string {
|
|
207
|
+
return Buffer.from(str, 'hex').toString()
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
export function timeout(ms: number) {
|
|
210
|
+
export function timeout(ms: number): Promise<void> {
|
|
211
211
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
212
212
|
}
|
|
213
213
|
|