@alephium/web3 0.4.0 → 0.5.0-rc.0
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +5 -3
- package/dist/src/api/api-alephium.js +1 -1
- package/dist/src/api/types.d.ts +1 -0
- package/dist/src/api/types.js +9 -1
- package/dist/src/constants.d.ts +1 -0
- package/dist/src/constants.js +2 -1
- package/dist/src/contract/contract.d.ts +72 -46
- package/dist/src/contract/contract.js +165 -76
- package/dist/src/contract/ralph.js +12 -3
- package/dist/src/signer/tx-builder.js +2 -2
- package/dist/src/signer/types.d.ts +2 -4
- package/dist/src/utils/utils.js +3 -1
- package/package.json +3 -3
- package/src/api/api-alephium.ts +5 -3
- package/src/api/types.ts +8 -0
- package/src/constants.ts +1 -0
- package/src/contract/contract.ts +249 -141
- package/src/contract/ralph.ts +13 -3
- package/src/signer/tx-builder.ts +2 -2
- package/src/signer/types.ts +14 -6
- package/src/utils/utils.ts +3 -1
|
@@ -43,7 +43,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
43
43
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
44
44
|
};
|
|
45
45
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
-
exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = void 0;
|
|
46
|
+
exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.ContractFactory = exports.randomTxId = exports.toApiVals = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = void 0;
|
|
47
47
|
const buffer_1 = require("buffer/");
|
|
48
48
|
const crypto_1 = require("crypto");
|
|
49
49
|
const fs_1 = __importDefault(require("fs"));
|
|
@@ -52,8 +52,8 @@ const api_1 = require("../api");
|
|
|
52
52
|
const ralph = __importStar(require("./ralph"));
|
|
53
53
|
const utils_1 = require("../utils");
|
|
54
54
|
const global_1 = require("../global");
|
|
55
|
-
const __1 = require("..");
|
|
56
55
|
const path = __importStar(require("path"));
|
|
56
|
+
const events_1 = require("./events");
|
|
57
57
|
var SourceKind;
|
|
58
58
|
(function (SourceKind) {
|
|
59
59
|
SourceKind[SourceKind["Contract"] = 0] = "Contract";
|
|
@@ -249,7 +249,7 @@ class Project {
|
|
|
249
249
|
}
|
|
250
250
|
return script.artifact;
|
|
251
251
|
}
|
|
252
|
-
async saveArtifactsToFile() {
|
|
252
|
+
async saveArtifactsToFile(projectRootDir) {
|
|
253
253
|
const artifactsRootDir = this.artifactsRootDir;
|
|
254
254
|
const saveToFile = async function (compiled) {
|
|
255
255
|
const artifactPath = compiled.sourceInfo.getArtifactPath(artifactsRootDir);
|
|
@@ -261,7 +261,7 @@ class Project {
|
|
|
261
261
|
};
|
|
262
262
|
this.contracts.forEach((contract) => saveToFile(contract));
|
|
263
263
|
this.scripts.forEach((script) => saveToFile(script));
|
|
264
|
-
await this.projectArtifact.saveToFile(
|
|
264
|
+
await this.projectArtifact.saveToFile(projectRootDir);
|
|
265
265
|
}
|
|
266
266
|
contractByCodeHash(codeHash) {
|
|
267
267
|
const contract = [...this.contracts.values()].find((c) => c.artifact.codeHash === codeHash || c.artifact.codeHashDebug == codeHash);
|
|
@@ -270,7 +270,7 @@ class Project {
|
|
|
270
270
|
}
|
|
271
271
|
return contract.artifact;
|
|
272
272
|
}
|
|
273
|
-
static async compile(provider, sourceInfos, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions) {
|
|
273
|
+
static async compile(provider, sourceInfos, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions) {
|
|
274
274
|
const sourceStr = sourceInfos.map((f) => f.sourceCode).join('\n');
|
|
275
275
|
const result = await provider.contracts.postContractsCompileProject({
|
|
276
276
|
code: sourceStr,
|
|
@@ -290,10 +290,10 @@ class Project {
|
|
|
290
290
|
});
|
|
291
291
|
const projectArtifact = Project.buildProjectArtifact(sourceInfos, contracts, scripts, compilerOptions);
|
|
292
292
|
const project = new Project(contractsRootDir, artifactsRootDir, sourceInfos, contracts, scripts, errorOnWarnings, projectArtifact);
|
|
293
|
-
await project.saveArtifactsToFile();
|
|
293
|
+
await project.saveArtifactsToFile(projectRootDir);
|
|
294
294
|
return project;
|
|
295
295
|
}
|
|
296
|
-
static async loadArtifacts(provider, sourceInfos, projectArtifact, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions) {
|
|
296
|
+
static async loadArtifacts(provider, sourceInfos, projectArtifact, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions) {
|
|
297
297
|
try {
|
|
298
298
|
const contracts = new Map();
|
|
299
299
|
const scripts = new Map();
|
|
@@ -317,7 +317,7 @@ class Project {
|
|
|
317
317
|
}
|
|
318
318
|
catch (error) {
|
|
319
319
|
console.log(`Failed to load artifacts, error: ${error}, try to re-compile contracts...`);
|
|
320
|
-
return Project.compile(provider, sourceInfos, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions);
|
|
320
|
+
return Project.compile(provider, sourceInfos, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions);
|
|
321
321
|
}
|
|
322
322
|
}
|
|
323
323
|
static getImportSourcePath(projectRootDir, importPath) {
|
|
@@ -406,14 +406,14 @@ class Project {
|
|
|
406
406
|
const provider = (0, global_1.getCurrentNodeProvider)();
|
|
407
407
|
const sourceFiles = await Project.loadSourceFiles(projectRootDir, contractsRootDir);
|
|
408
408
|
const { errorOnWarnings, ...nodeCompilerOptions } = { ...exports.DEFAULT_COMPILER_OPTIONS, ...compilerOptionsPartial };
|
|
409
|
-
const projectArtifact = await ProjectArtifact.from(
|
|
409
|
+
const projectArtifact = await ProjectArtifact.from(projectRootDir);
|
|
410
410
|
if (typeof projectArtifact === 'undefined' || projectArtifact.needToReCompile(nodeCompilerOptions, sourceFiles)) {
|
|
411
411
|
console.log(`Compiling contracts in folder "${contractsRootDir}"`);
|
|
412
|
-
Project.currentProject = await Project.compile(provider, sourceFiles, contractsRootDir, artifactsRootDir, errorOnWarnings, nodeCompilerOptions);
|
|
412
|
+
Project.currentProject = await Project.compile(provider, sourceFiles, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, nodeCompilerOptions);
|
|
413
413
|
}
|
|
414
414
|
else {
|
|
415
415
|
console.log(`Contracts are compiled already. Loading them from folder "${artifactsRootDir}"`);
|
|
416
|
-
Project.currentProject = await Project.loadArtifacts(provider, sourceFiles, projectArtifact, contractsRootDir, artifactsRootDir, errorOnWarnings, nodeCompilerOptions);
|
|
416
|
+
Project.currentProject = await Project.loadArtifacts(provider, sourceFiles, projectArtifact, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, nodeCompilerOptions);
|
|
417
417
|
}
|
|
418
418
|
}
|
|
419
419
|
}
|
|
@@ -482,12 +482,6 @@ class Contract extends Artifact {
|
|
|
482
482
|
const artifact = JSON.parse(content.toString());
|
|
483
483
|
return Contract.fromJson(artifact, bytecodeDebugPatch, codeHashDebug);
|
|
484
484
|
}
|
|
485
|
-
async fetchState(address, group) {
|
|
486
|
-
const state = await __1.web3.getCurrentNodeProvider().contracts.getContractsAddressState(address, {
|
|
487
|
-
group: group
|
|
488
|
-
});
|
|
489
|
-
return this.fromApiContractState(state);
|
|
490
|
-
}
|
|
491
485
|
toString() {
|
|
492
486
|
const object = {
|
|
493
487
|
version: this.version,
|
|
@@ -519,34 +513,12 @@ class Contract extends Artifact {
|
|
|
519
513
|
bytes[0] = 3;
|
|
520
514
|
return utils_1.bs58.encode(bytes);
|
|
521
515
|
}
|
|
522
|
-
|
|
516
|
+
printDebugMessages(funcName, messages) {
|
|
523
517
|
if (messages.length != 0) {
|
|
524
518
|
console.log(`Testing ${this.name}.${funcName}:`);
|
|
525
519
|
messages.forEach((m) => console.log(`Debug - ${m.contractAddress} - ${m.message}`));
|
|
526
520
|
}
|
|
527
521
|
}
|
|
528
|
-
async _test(funcName, params, expectPublic, accessType, printDebugMessages) {
|
|
529
|
-
const apiParams = this.toTestContract(funcName, params);
|
|
530
|
-
const apiResult = await __1.web3.getCurrentNodeProvider().contracts.postContractsTestContract(apiParams);
|
|
531
|
-
const methodIndex = typeof params.testMethodIndex !== 'undefined' ? params.testMethodIndex : this.getMethodIndex(funcName);
|
|
532
|
-
const isPublic = this.functions[`${methodIndex}`].isPublic;
|
|
533
|
-
if (printDebugMessages) {
|
|
534
|
-
this._printDebugMessages(funcName, apiResult.debugMessages);
|
|
535
|
-
}
|
|
536
|
-
if (isPublic === expectPublic) {
|
|
537
|
-
const result = await this.fromTestContractResult(methodIndex, apiResult);
|
|
538
|
-
return result;
|
|
539
|
-
}
|
|
540
|
-
else {
|
|
541
|
-
throw new Error(`The test method ${funcName} is not ${accessType}`);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
async testPublicMethod(funcName, params, printDebugMessages = true) {
|
|
545
|
-
return this._test(funcName, params, true, 'public', printDebugMessages);
|
|
546
|
-
}
|
|
547
|
-
async testPrivateMethod(funcName, params, printDebugMessages = true) {
|
|
548
|
-
return this._test(funcName, params, false, 'private', printDebugMessages);
|
|
549
|
-
}
|
|
550
522
|
toApiFields(fields) {
|
|
551
523
|
if (typeof fields === 'undefined') {
|
|
552
524
|
return [];
|
|
@@ -573,12 +545,15 @@ class Contract extends Artifact {
|
|
|
573
545
|
toApiContractStates(states) {
|
|
574
546
|
return typeof states != 'undefined' ? states.map((state) => toApiContractState(state)) : undefined;
|
|
575
547
|
}
|
|
576
|
-
|
|
548
|
+
toApiTestContractParams(funcName, params) {
|
|
549
|
+
const immFields = params.initialFields === undefined ? [] : extractFields(params.initialFields, this.fieldsSig, false);
|
|
550
|
+
const mutFields = params.initialFields === undefined ? [] : extractFields(params.initialFields, this.fieldsSig, true);
|
|
577
551
|
return {
|
|
578
552
|
group: params.group,
|
|
579
553
|
address: params.address,
|
|
580
554
|
bytecode: this.bytecodeDebug,
|
|
581
|
-
|
|
555
|
+
initialImmFields: immFields,
|
|
556
|
+
initialMutFields: mutFields,
|
|
582
557
|
initialAsset: typeof params.initialAsset !== 'undefined' ? toApiAsset(params.initialAsset) : undefined,
|
|
583
558
|
methodIndex: this.getMethodIndex(funcName),
|
|
584
559
|
args: this.toApiArgs(funcName, params.testArgs),
|
|
@@ -593,7 +568,7 @@ class Contract extends Artifact {
|
|
|
593
568
|
bytecode: state.bytecode,
|
|
594
569
|
initialStateHash: state.initialStateHash,
|
|
595
570
|
codeHash: state.codeHash,
|
|
596
|
-
fields: fromApiFields(state.
|
|
571
|
+
fields: fromApiFields(state.immFields, state.mutFields, this.fieldsSig),
|
|
597
572
|
fieldsSig: this.fieldsSig,
|
|
598
573
|
asset: fromApiAsset(state.asset)
|
|
599
574
|
};
|
|
@@ -602,12 +577,12 @@ class Contract extends Artifact {
|
|
|
602
577
|
const contract = Project.currentProject.contractByCodeHash(state.codeHash);
|
|
603
578
|
return contract.fromApiContractState(state);
|
|
604
579
|
}
|
|
605
|
-
static fromApiEvent(event, codeHash) {
|
|
580
|
+
static fromApiEvent(event, codeHash, txId) {
|
|
606
581
|
let eventSig;
|
|
607
|
-
if (event.eventIndex ==
|
|
582
|
+
if (event.eventIndex == Contract.ContractCreatedEventIndex) {
|
|
608
583
|
eventSig = this.ContractCreatedEvent;
|
|
609
584
|
}
|
|
610
|
-
else if (event.eventIndex ==
|
|
585
|
+
else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
|
|
611
586
|
eventSig = this.ContractDestroyedEvent;
|
|
612
587
|
}
|
|
613
588
|
else {
|
|
@@ -615,13 +590,15 @@ class Contract extends Artifact {
|
|
|
615
590
|
eventSig = contract.eventsSig[event.eventIndex];
|
|
616
591
|
}
|
|
617
592
|
return {
|
|
593
|
+
txId: txId,
|
|
618
594
|
blockHash: event.blockHash,
|
|
619
595
|
contractAddress: event.contractAddress,
|
|
620
596
|
name: eventSig.name,
|
|
597
|
+
eventIndex: event.eventIndex,
|
|
621
598
|
fields: fromApiEventFields(event.fields, eventSig)
|
|
622
599
|
};
|
|
623
600
|
}
|
|
624
|
-
|
|
601
|
+
fromApiTestContractResult(methodIndex, result, txId) {
|
|
625
602
|
const addressToCodeHash = new Map();
|
|
626
603
|
addressToCodeHash.set(result.address, result.codeHash);
|
|
627
604
|
result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
@@ -632,46 +609,72 @@ class Contract extends Artifact {
|
|
|
632
609
|
gasUsed: result.gasUsed,
|
|
633
610
|
contracts: result.contracts.map((contract) => Contract.fromApiContractState(contract)),
|
|
634
611
|
txOutputs: result.txOutputs.map(fromApiOutput),
|
|
635
|
-
events: result.events
|
|
636
|
-
const contractAddress = event.contractAddress;
|
|
637
|
-
const codeHash = addressToCodeHash.get(contractAddress);
|
|
638
|
-
if (typeof codeHash !== 'undefined' || event.eventIndex < 0) {
|
|
639
|
-
return Contract.fromApiEvent(event, codeHash);
|
|
640
|
-
}
|
|
641
|
-
else {
|
|
642
|
-
throw Error(`Cannot find codeHash for the contract address: ${contractAddress}`);
|
|
643
|
-
}
|
|
644
|
-
}),
|
|
612
|
+
events: Contract.fromApiEvents(result.events, addressToCodeHash, txId),
|
|
645
613
|
debugMessages: result.debugMessages
|
|
646
614
|
};
|
|
647
615
|
}
|
|
648
616
|
async txParamsForDeployment(signer, params) {
|
|
649
|
-
const bytecode = this.buildByteCodeToDeploy(params.initialFields
|
|
617
|
+
const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {});
|
|
650
618
|
const signerParams = {
|
|
651
619
|
signerAddress: await signer.getSelectedAddress(),
|
|
652
620
|
bytecode: bytecode,
|
|
653
|
-
initialAttoAlphAmount: params
|
|
654
|
-
issueTokenAmount: params
|
|
655
|
-
initialTokenAmounts: params
|
|
656
|
-
gasAmount: params
|
|
657
|
-
gasPrice: params
|
|
621
|
+
initialAttoAlphAmount: params?.initialAttoAlphAmount,
|
|
622
|
+
issueTokenAmount: params?.issueTokenAmount,
|
|
623
|
+
initialTokenAmounts: params?.initialTokenAmounts,
|
|
624
|
+
gasAmount: params?.gasAmount,
|
|
625
|
+
gasPrice: params?.gasPrice
|
|
658
626
|
};
|
|
659
627
|
return signerParams;
|
|
660
628
|
}
|
|
661
|
-
async deploy(signer, params) {
|
|
662
|
-
const signerParams = await this.txParamsForDeployment(signer, params);
|
|
663
|
-
return signer.signAndSubmitDeployContractTx(signerParams);
|
|
664
|
-
}
|
|
665
629
|
buildByteCodeToDeploy(initialFields) {
|
|
666
630
|
return ralph.buildContractByteCode(this.bytecode, initialFields, this.fieldsSig);
|
|
667
631
|
}
|
|
632
|
+
static fromApiEvents(events, addressToCodeHash, txId) {
|
|
633
|
+
return events.map((event) => {
|
|
634
|
+
const contractAddress = event.contractAddress;
|
|
635
|
+
const codeHash = addressToCodeHash.get(contractAddress);
|
|
636
|
+
if (typeof codeHash !== 'undefined' || event.eventIndex < 0) {
|
|
637
|
+
return Contract.fromApiEvent(event, codeHash, txId);
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
throw Error(`Cannot find codeHash for the contract address: ${contractAddress}`);
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
toApiCallContract(params, groupIndex, contractAddress, methodIndex) {
|
|
645
|
+
const functionSig = this.functions[`${methodIndex}`];
|
|
646
|
+
const args = toApiVals(params.args ?? {}, functionSig.paramNames, functionSig.paramTypes);
|
|
647
|
+
return {
|
|
648
|
+
...params,
|
|
649
|
+
group: groupIndex,
|
|
650
|
+
address: contractAddress,
|
|
651
|
+
methodIndex: methodIndex,
|
|
652
|
+
args: args
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
fromApiCallContractResult(result, txId, methodIndex) {
|
|
656
|
+
const addressToCodeHash = new Map();
|
|
657
|
+
result.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
658
|
+
const functionSig = this.functions[`${methodIndex}`];
|
|
659
|
+
const returns = (0, api_1.fromApiArray)(result.returns, functionSig.returnTypes);
|
|
660
|
+
return {
|
|
661
|
+
returns: returns,
|
|
662
|
+
gasUsed: result.gasUsed,
|
|
663
|
+
contracts: result.contracts.map((state) => Contract.fromApiContractState(state)),
|
|
664
|
+
txInputs: result.txInputs,
|
|
665
|
+
txOutputs: result.txOutputs.map((output) => fromApiOutput(output)),
|
|
666
|
+
events: Contract.fromApiEvents(result.events, addressToCodeHash, txId)
|
|
667
|
+
};
|
|
668
|
+
}
|
|
668
669
|
}
|
|
669
670
|
exports.Contract = Contract;
|
|
671
|
+
Contract.ContractCreatedEventIndex = -1;
|
|
670
672
|
Contract.ContractCreatedEvent = {
|
|
671
673
|
name: 'ContractCreated',
|
|
672
674
|
fieldNames: ['address'],
|
|
673
675
|
fieldTypes: ['Address']
|
|
674
676
|
};
|
|
677
|
+
Contract.ContractDestroyedEventIndex = -2;
|
|
675
678
|
Contract.ContractDestroyedEvent = {
|
|
676
679
|
name: 'ContractDestroyed',
|
|
677
680
|
fieldNames: ['address'],
|
|
@@ -716,7 +719,7 @@ class Script extends Artifact {
|
|
|
716
719
|
async txParamsForExecution(signer, params) {
|
|
717
720
|
const signerParams = {
|
|
718
721
|
signerAddress: await signer.getSelectedAddress(),
|
|
719
|
-
bytecode: this.buildByteCodeToDeploy(params.initialFields
|
|
722
|
+
bytecode: this.buildByteCodeToDeploy(params.initialFields ?? {}),
|
|
720
723
|
attoAlphAmount: params.attoAlphAmount,
|
|
721
724
|
tokens: params.tokens,
|
|
722
725
|
gasAmount: params.gasAmount,
|
|
@@ -724,16 +727,26 @@ class Script extends Artifact {
|
|
|
724
727
|
};
|
|
725
728
|
return signerParams;
|
|
726
729
|
}
|
|
727
|
-
async execute(signer, params) {
|
|
728
|
-
const signerParams = await this.txParamsForExecution(signer, params);
|
|
729
|
-
return await signer.signAndSubmitExecuteScriptTx(signerParams);
|
|
730
|
-
}
|
|
731
730
|
buildByteCodeToDeploy(initialFields) {
|
|
732
731
|
return ralph.buildScriptByteCode(this.bytecodeTemplate, initialFields, this.fieldsSig);
|
|
733
732
|
}
|
|
734
733
|
}
|
|
735
734
|
exports.Script = Script;
|
|
736
|
-
function fromApiFields(
|
|
735
|
+
function fromApiFields(immFields, mutFields, fieldsSig) {
|
|
736
|
+
const vals = [];
|
|
737
|
+
let immIndex = 0;
|
|
738
|
+
let mutIndex = 0;
|
|
739
|
+
const isMutable = fieldsSig.types.flatMap((tpe, index) => Array((0, api_1.typeLength)(tpe)).fill(fieldsSig.isMutable[`${index}`]));
|
|
740
|
+
isMutable.forEach((mutable) => {
|
|
741
|
+
if (mutable) {
|
|
742
|
+
vals.push(mutFields[`${mutIndex}`]);
|
|
743
|
+
mutIndex += 1;
|
|
744
|
+
}
|
|
745
|
+
else {
|
|
746
|
+
vals.push(immFields[`${immIndex}`]);
|
|
747
|
+
immIndex += 1;
|
|
748
|
+
}
|
|
749
|
+
});
|
|
737
750
|
return (0, api_1.fromApiVals)(vals, fieldsSig.names, fieldsSig.types);
|
|
738
751
|
}
|
|
739
752
|
function fromApiEventFields(vals, eventSig) {
|
|
@@ -759,13 +772,23 @@ function getVal(vals, name) {
|
|
|
759
772
|
throw Error(`No Val exists for ${name}`);
|
|
760
773
|
}
|
|
761
774
|
}
|
|
775
|
+
function extractFields(fields, fieldsSig, mutable) {
|
|
776
|
+
const fieldIndexes = fieldsSig.names
|
|
777
|
+
.map((_, index) => index)
|
|
778
|
+
.filter((index) => fieldsSig.isMutable[`${index}`] === mutable);
|
|
779
|
+
const fieldNames = fieldIndexes.map((index) => fieldsSig.names[`${index}`]);
|
|
780
|
+
const fieldTypes = fieldIndexes.map((index) => fieldsSig.types[`${index}`]);
|
|
781
|
+
return toApiVals(fields, fieldNames, fieldTypes);
|
|
782
|
+
}
|
|
762
783
|
function toApiContractState(state) {
|
|
784
|
+
const stateFields = state.fields ?? {};
|
|
763
785
|
return {
|
|
764
786
|
address: state.address,
|
|
765
787
|
bytecode: state.bytecode,
|
|
766
788
|
codeHash: state.codeHash,
|
|
767
789
|
initialStateHash: state.initialStateHash,
|
|
768
|
-
|
|
790
|
+
immFields: extractFields(stateFields, state.fieldsSig, false),
|
|
791
|
+
mutFields: extractFields(stateFields, state.fieldsSig, true),
|
|
769
792
|
asset: toApiAsset(state.asset)
|
|
770
793
|
};
|
|
771
794
|
}
|
|
@@ -782,6 +805,7 @@ function toApiVals(fields, names, types) {
|
|
|
782
805
|
return (0, api_1.toApiVal)(val, tpe);
|
|
783
806
|
});
|
|
784
807
|
}
|
|
808
|
+
exports.toApiVals = toApiVals;
|
|
785
809
|
function toApiInputAsset(inputAsset) {
|
|
786
810
|
return { address: inputAsset.address, asset: toApiAsset(inputAsset.asset) };
|
|
787
811
|
}
|
|
@@ -813,5 +837,70 @@ function fromApiOutput(output) {
|
|
|
813
837
|
throw new Error(`Unknown output type: ${output}`);
|
|
814
838
|
}
|
|
815
839
|
}
|
|
816
|
-
|
|
817
|
-
|
|
840
|
+
function randomTxId() {
|
|
841
|
+
const bytes = new Uint8Array(32);
|
|
842
|
+
crypto_1.webcrypto.getRandomValues(bytes);
|
|
843
|
+
return (0, utils_1.binToHex)(bytes);
|
|
844
|
+
}
|
|
845
|
+
exports.randomTxId = randomTxId;
|
|
846
|
+
utils_1.assertType;
|
|
847
|
+
class ContractFactory {
|
|
848
|
+
constructor(contract) {
|
|
849
|
+
this.contract = contract;
|
|
850
|
+
}
|
|
851
|
+
async deploy(signer, deployParams) {
|
|
852
|
+
const signerParams = await this.contract.txParamsForDeployment(signer, deployParams);
|
|
853
|
+
const result = await signer.signAndSubmitDeployContractTx(signerParams);
|
|
854
|
+
return {
|
|
855
|
+
...result,
|
|
856
|
+
instance: this.at(result.contractAddress)
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
exports.ContractFactory = ContractFactory;
|
|
861
|
+
function decodeFields(event, eventSig, eventIndex) {
|
|
862
|
+
if (event.eventIndex !== eventIndex) {
|
|
863
|
+
throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
|
|
864
|
+
}
|
|
865
|
+
return (0, api_1.fromApiVals)(event.fields, eventSig.fieldNames, eventSig.fieldTypes);
|
|
866
|
+
}
|
|
867
|
+
function decodeContractCreatedEvent(event) {
|
|
868
|
+
const fields = decodeFields(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
|
|
869
|
+
return {
|
|
870
|
+
blockHash: event.blockHash,
|
|
871
|
+
txId: event.txId,
|
|
872
|
+
eventIndex: event.eventIndex,
|
|
873
|
+
name: Contract.ContractCreatedEvent.name,
|
|
874
|
+
fields: { address: fields['address'] }
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
exports.decodeContractCreatedEvent = decodeContractCreatedEvent;
|
|
878
|
+
function decodeContractDestroyedEvent(event) {
|
|
879
|
+
const fields = decodeFields(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
|
|
880
|
+
return {
|
|
881
|
+
blockHash: event.blockHash,
|
|
882
|
+
txId: event.txId,
|
|
883
|
+
eventIndex: event.eventIndex,
|
|
884
|
+
name: Contract.ContractDestroyedEvent.name,
|
|
885
|
+
fields: { address: fields['address'] }
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
exports.decodeContractDestroyedEvent = decodeContractDestroyedEvent;
|
|
889
|
+
function subscribeEventsFromContract(options, address, eventIndex, decodeFunc, fromCount) {
|
|
890
|
+
const messageCallback = (event) => {
|
|
891
|
+
if (event.eventIndex !== eventIndex) {
|
|
892
|
+
return Promise.resolve();
|
|
893
|
+
}
|
|
894
|
+
return options.messageCallback(decodeFunc(event));
|
|
895
|
+
};
|
|
896
|
+
const errorCallback = (err, subscription) => {
|
|
897
|
+
return options.errorCallback(err, subscription);
|
|
898
|
+
};
|
|
899
|
+
const opt = {
|
|
900
|
+
pollingInterval: options.pollingInterval,
|
|
901
|
+
messageCallback: messageCallback,
|
|
902
|
+
errorCallback: errorCallback
|
|
903
|
+
};
|
|
904
|
+
return (0, events_1.subscribeToEvents)(opt, address, fromCount);
|
|
905
|
+
}
|
|
906
|
+
exports.subscribeEventsFromContract = subscribeEventsFromContract;
|
|
@@ -253,8 +253,12 @@ function buildScriptByteCode(bytecodeTemplate, fields, fieldsSig) {
|
|
|
253
253
|
});
|
|
254
254
|
}
|
|
255
255
|
exports.buildScriptByteCode = buildScriptByteCode;
|
|
256
|
-
function
|
|
257
|
-
const
|
|
256
|
+
function encodeFields(fields, fieldsSig, mutable) {
|
|
257
|
+
const fieldIndexes = fieldsSig.isMutable
|
|
258
|
+
.map((_, index) => index)
|
|
259
|
+
.filter((index) => fieldsSig.isMutable[`${index}`] === mutable);
|
|
260
|
+
const fieldsEncoded = fieldIndexes.flatMap((fieldIndex) => {
|
|
261
|
+
const fieldName = fieldsSig.names[`${fieldIndex}`];
|
|
258
262
|
const fieldType = fieldsSig.types[`${fieldIndex}`];
|
|
259
263
|
if (fieldName in fields) {
|
|
260
264
|
const fieldValue = fields[`${fieldName}`];
|
|
@@ -265,7 +269,12 @@ function buildContractByteCode(bytecode, fields, fieldsSig) {
|
|
|
265
269
|
}
|
|
266
270
|
});
|
|
267
271
|
const fieldsLength = buffer_1.Buffer.from(encodeI256(BigInt(fieldsEncoded.length))).toString('hex');
|
|
268
|
-
return
|
|
272
|
+
return fieldsLength + fieldsEncoded.map((f) => buffer_1.Buffer.from(f).toString('hex')).join('');
|
|
273
|
+
}
|
|
274
|
+
function buildContractByteCode(bytecode, fields, fieldsSig) {
|
|
275
|
+
const encodedImmFields = encodeFields(fields, fieldsSig, false);
|
|
276
|
+
const encodedMutFields = encodeFields(fields, fieldsSig, true);
|
|
277
|
+
return bytecode + encodedImmFields + encodedMutFields;
|
|
269
278
|
}
|
|
270
279
|
exports.buildContractByteCode = buildContractByteCode;
|
|
271
280
|
var ApiValType;
|
|
@@ -62,7 +62,7 @@ class TransactionBuilder {
|
|
|
62
62
|
};
|
|
63
63
|
const response = await this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data);
|
|
64
64
|
const contractId = __1.utils.binToHex(__1.utils.contractIdFromAddress(response.contractAddress));
|
|
65
|
-
return { ...response, contractId, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
65
|
+
return { ...response, groupIndex: response.fromGroup, contractId, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
66
66
|
}
|
|
67
67
|
async buildExecuteScriptTx(params, publicKey) {
|
|
68
68
|
TransactionBuilder.validatePublicKey(params, publicKey);
|
|
@@ -75,7 +75,7 @@ class TransactionBuilder {
|
|
|
75
75
|
...rest
|
|
76
76
|
};
|
|
77
77
|
const response = await this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data);
|
|
78
|
-
return { ...response, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
78
|
+
return { ...response, groupIndex: response.fromGroup, gasPrice: (0, api_1.fromApiNumber256)(response.gasPrice) };
|
|
79
79
|
}
|
|
80
80
|
async buildUnsignedTx(params) {
|
|
81
81
|
const data = { unsignedTx: params.unsignedTx };
|
|
@@ -43,8 +43,7 @@ export interface SignDeployContractTxParams {
|
|
|
43
43
|
gasPrice?: Number256;
|
|
44
44
|
}
|
|
45
45
|
export interface SignDeployContractTxResult {
|
|
46
|
-
|
|
47
|
-
toGroup: number;
|
|
46
|
+
groupIndex: number;
|
|
48
47
|
unsignedTx: string;
|
|
49
48
|
txId: string;
|
|
50
49
|
signature: string;
|
|
@@ -62,8 +61,7 @@ export interface SignExecuteScriptTxParams {
|
|
|
62
61
|
gasPrice?: Number256;
|
|
63
62
|
}
|
|
64
63
|
export interface SignExecuteScriptTxResult {
|
|
65
|
-
|
|
66
|
-
toGroup: number;
|
|
64
|
+
groupIndex: number;
|
|
67
65
|
unsignedTx: string;
|
|
68
66
|
txId: string;
|
|
69
67
|
signature: string;
|
package/dist/src/utils/utils.js
CHANGED
|
@@ -94,7 +94,9 @@ function groupOfAddress(address) {
|
|
|
94
94
|
return groupOfP2shAddress(addressBody);
|
|
95
95
|
}
|
|
96
96
|
else {
|
|
97
|
-
|
|
97
|
+
// Contract Address
|
|
98
|
+
const id = contractIdFromAddress(address);
|
|
99
|
+
return id[`${id.length - 1}`];
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
exports.groupOfAddress = groupOfAddress;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0-rc.0",
|
|
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,8 +27,8 @@
|
|
|
27
27
|
},
|
|
28
28
|
"author": "Alephium dev <dev@alephium.org>",
|
|
29
29
|
"config": {
|
|
30
|
-
"alephium_version": "1.
|
|
31
|
-
"explorer_backend_version": "1.12.0
|
|
30
|
+
"alephium_version": "1.7.0-rc1",
|
|
31
|
+
"explorer_backend_version": "1.12.0"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "rm -rf dist/* && npx tsc --build . && webpack",
|
package/src/api/api-alephium.ts
CHANGED
|
@@ -577,7 +577,8 @@ export interface ContractState {
|
|
|
577
577
|
|
|
578
578
|
/** @format 32-byte-hash */
|
|
579
579
|
initialStateHash?: string
|
|
580
|
-
|
|
580
|
+
immFields: Val[]
|
|
581
|
+
mutFields: Val[]
|
|
581
582
|
asset: AssetState
|
|
582
583
|
}
|
|
583
584
|
|
|
@@ -884,7 +885,8 @@ export interface TestContract {
|
|
|
884
885
|
|
|
885
886
|
/** @format contract */
|
|
886
887
|
bytecode: string
|
|
887
|
-
|
|
888
|
+
initialImmFields?: Val[]
|
|
889
|
+
initialMutFields?: Val[]
|
|
888
890
|
initialAsset?: AssetState
|
|
889
891
|
|
|
890
892
|
/** @format int32 */
|
|
@@ -1332,7 +1334,7 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
1332
1334
|
|
|
1333
1335
|
/**
|
|
1334
1336
|
* @title Alephium API
|
|
1335
|
-
* @version 1.
|
|
1337
|
+
* @version 1.7.0
|
|
1336
1338
|
* @baseUrl ../
|
|
1337
1339
|
*/
|
|
1338
1340
|
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
package/src/api/types.ts
CHANGED
|
@@ -227,3 +227,11 @@ function foldVals(vals: Val[], dims: number[]): Val {
|
|
|
227
227
|
return result
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
|
+
|
|
231
|
+
export function typeLength(tpe: string): number {
|
|
232
|
+
if (tpe === 'U256' || tpe === 'I256' || tpe === 'Bool' || tpe === 'ByteVec' || tpe === 'Address') {
|
|
233
|
+
return 1
|
|
234
|
+
}
|
|
235
|
+
const [, dims] = decodeArrayType(tpe)
|
|
236
|
+
return dims.reduce((a, b) => a * b)
|
|
237
|
+
}
|
package/src/constants.ts
CHANGED