@alephium/web3 1.1.1 → 1.2.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 +37 -2
- package/dist/src/api/api-alephium.js +17 -1
- package/dist/src/codec/codec.d.ts +1 -0
- package/dist/src/codec/codec.js +32 -1
- package/dist/src/codec/lockup-script-codec.js +2 -1
- package/dist/src/codec/unlock-script-codec.js +2 -1
- package/dist/src/contract/contract.d.ts +15 -3
- package/dist/src/contract/contract.js +36 -14
- package/package.json +2 -2
- package/src/api/api-alephium.ts +51 -3
- package/src/codec/codec.ts +13 -0
- package/src/codec/contract-codec.ts +1 -1
- package/src/codec/lockup-script-codec.ts +2 -2
- package/src/codec/unlock-script-codec.ts +2 -2
- package/src/contract/contract.ts +59 -17
|
@@ -359,7 +359,7 @@ export interface CallContract {
|
|
|
359
359
|
/** @format int32 */
|
|
360
360
|
methodIndex: number;
|
|
361
361
|
args?: Val[];
|
|
362
|
-
|
|
362
|
+
interestedContracts?: string[];
|
|
363
363
|
inputAssets?: TestInputAsset[];
|
|
364
364
|
}
|
|
365
365
|
/** CallContractFailed */
|
|
@@ -381,6 +381,32 @@ export interface CallContractSucceeded {
|
|
|
381
381
|
debugMessages: DebugMessage[];
|
|
382
382
|
type: string;
|
|
383
383
|
}
|
|
384
|
+
/** CallTxScript */
|
|
385
|
+
export interface CallTxScript {
|
|
386
|
+
/** @format int32 */
|
|
387
|
+
group: number;
|
|
388
|
+
/** @format hex-string */
|
|
389
|
+
bytecode: string;
|
|
390
|
+
/** @format address */
|
|
391
|
+
callerAddress?: string;
|
|
392
|
+
/** @format block-hash */
|
|
393
|
+
worldStateBlockHash?: string;
|
|
394
|
+
/** @format 32-byte-hash */
|
|
395
|
+
txId?: string;
|
|
396
|
+
inputAssets?: TestInputAsset[];
|
|
397
|
+
interestedContracts?: string[];
|
|
398
|
+
}
|
|
399
|
+
/** CallTxScriptResult */
|
|
400
|
+
export interface CallTxScriptResult {
|
|
401
|
+
returns: Val[];
|
|
402
|
+
/** @format int32 */
|
|
403
|
+
gasUsed: number;
|
|
404
|
+
contracts: ContractState[];
|
|
405
|
+
txInputs: string[];
|
|
406
|
+
txOutputs: Output[];
|
|
407
|
+
events: ContractEventByTxId[];
|
|
408
|
+
debugMessages: DebugMessage[];
|
|
409
|
+
}
|
|
384
410
|
/** ChainInfo */
|
|
385
411
|
export interface ChainInfo {
|
|
386
412
|
/** @format int32 */
|
|
@@ -1153,7 +1179,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
|
|
|
1153
1179
|
}
|
|
1154
1180
|
/**
|
|
1155
1181
|
* @title Alephium API
|
|
1156
|
-
* @version 3.
|
|
1182
|
+
* @version 3.3.0
|
|
1157
1183
|
* @baseUrl ../
|
|
1158
1184
|
*/
|
|
1159
1185
|
export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
@@ -1820,6 +1846,15 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
1820
1846
|
* @request POST:/contracts/multicall-contract
|
|
1821
1847
|
*/
|
|
1822
1848
|
postContractsMulticallContract: (data: MultipleCallContract, params?: RequestParams) => Promise<MultipleCallContractResult>;
|
|
1849
|
+
/**
|
|
1850
|
+
* No description
|
|
1851
|
+
*
|
|
1852
|
+
* @tags Contracts
|
|
1853
|
+
* @name PostContractsCallTxScript
|
|
1854
|
+
* @summary Call TxScript
|
|
1855
|
+
* @request POST:/contracts/call-tx-script
|
|
1856
|
+
*/
|
|
1857
|
+
postContractsCallTxScript: (data: CallTxScript, params?: RequestParams) => Promise<CallTxScriptResult>;
|
|
1823
1858
|
};
|
|
1824
1859
|
multisig: {
|
|
1825
1860
|
/**
|
|
@@ -151,7 +151,7 @@ class HttpClient {
|
|
|
151
151
|
exports.HttpClient = HttpClient;
|
|
152
152
|
/**
|
|
153
153
|
* @title Alephium API
|
|
154
|
-
* @version 3.
|
|
154
|
+
* @version 3.3.0
|
|
155
155
|
* @baseUrl ../
|
|
156
156
|
*/
|
|
157
157
|
class Api extends HttpClient {
|
|
@@ -1118,6 +1118,22 @@ class Api extends HttpClient {
|
|
|
1118
1118
|
type: ContentType.Json,
|
|
1119
1119
|
format: 'json',
|
|
1120
1120
|
...params
|
|
1121
|
+
}).then(utils_1.convertHttpResponse),
|
|
1122
|
+
/**
|
|
1123
|
+
* No description
|
|
1124
|
+
*
|
|
1125
|
+
* @tags Contracts
|
|
1126
|
+
* @name PostContractsCallTxScript
|
|
1127
|
+
* @summary Call TxScript
|
|
1128
|
+
* @request POST:/contracts/call-tx-script
|
|
1129
|
+
*/
|
|
1130
|
+
postContractsCallTxScript: (data, params = {}) => this.request({
|
|
1131
|
+
path: `/contracts/call-tx-script`,
|
|
1132
|
+
method: 'POST',
|
|
1133
|
+
body: data,
|
|
1134
|
+
type: ContentType.Json,
|
|
1135
|
+
format: 'json',
|
|
1136
|
+
...params
|
|
1121
1137
|
}).then(utils_1.convertHttpResponse)
|
|
1122
1138
|
};
|
|
1123
1139
|
this.multisig = {
|
package/dist/src/codec/codec.js
CHANGED
|
@@ -1,9 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assert = void 0;
|
|
3
|
+
exports.fixedSizeBytes = exports.assert = void 0;
|
|
4
|
+
/*
|
|
5
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
6
|
+
This file is part of the alephium project.
|
|
7
|
+
|
|
8
|
+
The library is free software: you can redistribute it and/or modify
|
|
9
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
10
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
(at your option) any later version.
|
|
12
|
+
|
|
13
|
+
The library is distributed in the hope that it will be useful,
|
|
14
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
GNU Lesser General Public License for more details.
|
|
17
|
+
|
|
18
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
19
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
*/
|
|
21
|
+
const binary_parser_1 = require("binary-parser");
|
|
4
22
|
function assert(value, message) {
|
|
5
23
|
if (!value) {
|
|
6
24
|
throw new Error(message);
|
|
7
25
|
}
|
|
8
26
|
}
|
|
9
27
|
exports.assert = assert;
|
|
28
|
+
function fixedSizeBytes(name, length) {
|
|
29
|
+
return binary_parser_1.Parser.start().wrapped({
|
|
30
|
+
length,
|
|
31
|
+
type: binary_parser_1.Parser.start().buffer(name, { length }),
|
|
32
|
+
wrapper: function (result) {
|
|
33
|
+
if (result.length === length) {
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
throw new Error(`Too few bytes when parsing ${name}, expected ${length}, got ${result.length}`);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
exports.fixedSizeBytes = fixedSizeBytes;
|
|
@@ -20,10 +20,11 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
20
20
|
*/
|
|
21
21
|
const binary_parser_1 = require("binary-parser");
|
|
22
22
|
const compact_int_codec_1 = require("./compact-int-codec");
|
|
23
|
+
const codec_1 = require("./codec");
|
|
23
24
|
const array_codec_1 = require("./array-codec");
|
|
24
25
|
class PublicKeyHashCodec {
|
|
25
26
|
constructor() {
|
|
26
|
-
this.parser =
|
|
27
|
+
this.parser = (0, codec_1.fixedSizeBytes)('publicKeyHash', 32);
|
|
27
28
|
}
|
|
28
29
|
encode(input) {
|
|
29
30
|
return input.publicKeyHash;
|
|
@@ -21,13 +21,14 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
21
21
|
const binary_parser_1 = require("binary-parser");
|
|
22
22
|
const array_codec_1 = require("./array-codec");
|
|
23
23
|
const compact_int_codec_1 = require("./compact-int-codec");
|
|
24
|
+
const codec_1 = require("./codec");
|
|
24
25
|
const script_codec_1 = require("./script-codec");
|
|
25
26
|
const bytestring_codec_1 = require("./bytestring-codec");
|
|
26
27
|
const lockup_script_codec_1 = require("./lockup-script-codec");
|
|
27
28
|
const utils_1 = require("../utils");
|
|
28
29
|
class P2PKHCodec {
|
|
29
30
|
constructor() {
|
|
30
|
-
this.parser =
|
|
31
|
+
this.parser = (0, codec_1.fixedSizeBytes)('publicKey', 33);
|
|
31
32
|
}
|
|
32
33
|
encode(input) {
|
|
33
34
|
return input.publicKey;
|
|
@@ -184,10 +184,12 @@ export declare abstract class ContractFactory<I extends ContractInstance, F exte
|
|
|
184
184
|
deploy(signer: SignerProvider, deployParams: DeployContractParams<F>): Promise<DeployContractResult<I>>;
|
|
185
185
|
stateForTest(initFields: F, asset?: Asset, address?: string): ContractState<F>;
|
|
186
186
|
}
|
|
187
|
-
export declare class ExecutableScript<P extends Fields = Fields> {
|
|
187
|
+
export declare class ExecutableScript<P extends Fields = Fields, R extends Val | null = null> {
|
|
188
188
|
readonly script: Script;
|
|
189
|
-
|
|
189
|
+
readonly getContractByCodeHash: (codeHash: string) => Contract;
|
|
190
|
+
constructor(script: Script, getContractByCodeHash: (codeHash: string) => Contract);
|
|
190
191
|
execute(signer: SignerProvider, params: ExecuteScriptParams<P>): Promise<ExecuteScriptResult>;
|
|
192
|
+
call(params: CallScriptParams<P>): Promise<CallScriptResult<R>>;
|
|
191
193
|
}
|
|
192
194
|
export interface ExecuteScriptParams<P extends Fields = Fields> {
|
|
193
195
|
initialFields: P;
|
|
@@ -204,11 +206,21 @@ export interface ExecuteScriptResult {
|
|
|
204
206
|
gasAmount: number;
|
|
205
207
|
gasPrice: Number256;
|
|
206
208
|
}
|
|
209
|
+
export interface CallScriptParams<P extends Fields = Fields> {
|
|
210
|
+
initialFields: P;
|
|
211
|
+
groupIndex?: number;
|
|
212
|
+
callerAddress?: string;
|
|
213
|
+
worldStateBlockHash?: string;
|
|
214
|
+
txId?: string;
|
|
215
|
+
interestedContracts?: string[];
|
|
216
|
+
inputAssets?: InputAsset[];
|
|
217
|
+
}
|
|
218
|
+
export type CallScriptResult<R> = CallContractResult<R>;
|
|
207
219
|
export interface CallContractParams<T extends Arguments = Arguments> {
|
|
208
220
|
args: T;
|
|
209
221
|
worldStateBlockHash?: string;
|
|
210
222
|
txId?: string;
|
|
211
|
-
|
|
223
|
+
interestedContracts?: string[];
|
|
212
224
|
inputAssets?: InputAsset[];
|
|
213
225
|
}
|
|
214
226
|
export interface CallContractResult<R> {
|
|
@@ -382,19 +382,7 @@ class Contract extends Artifact {
|
|
|
382
382
|
fromApiCallContractResult(result, txId, methodIndex, getContractByCodeHash) {
|
|
383
383
|
const returnTypes = this.functions[`${methodIndex}`].returnTypes;
|
|
384
384
|
const callResult = (0, api_1.tryGetCallResult)(result);
|
|
385
|
-
|
|
386
|
-
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
|
|
387
|
-
const addressToCodeHash = new Map();
|
|
388
|
-
callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
389
|
-
return {
|
|
390
|
-
returns: returns,
|
|
391
|
-
gasUsed: callResult.gasUsed,
|
|
392
|
-
contracts: callResult.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
|
|
393
|
-
txInputs: callResult.txInputs,
|
|
394
|
-
txOutputs: callResult.txOutputs.map((output) => fromApiOutput(output)),
|
|
395
|
-
events: Contract.fromApiEvents(callResult.events, addressToCodeHash, txId, getContractByCodeHash),
|
|
396
|
-
debugMessages: callResult.debugMessages
|
|
397
|
-
};
|
|
385
|
+
return fromCallResult(callResult, txId, returnTypes, this.structs, getContractByCodeHash);
|
|
398
386
|
}
|
|
399
387
|
}
|
|
400
388
|
exports.Contract = Contract;
|
|
@@ -410,6 +398,21 @@ Contract.ContractDestroyedEvent = {
|
|
|
410
398
|
fieldNames: ['address'],
|
|
411
399
|
fieldTypes: ['Address']
|
|
412
400
|
};
|
|
401
|
+
function fromCallResult(callResult, txId, returnTypes, structs, getContractByCodeHash) {
|
|
402
|
+
const rawReturn = fromApiArray(callResult.returns, returnTypes, structs);
|
|
403
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn;
|
|
404
|
+
const addressToCodeHash = new Map();
|
|
405
|
+
callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash));
|
|
406
|
+
return {
|
|
407
|
+
returns: returns,
|
|
408
|
+
gasUsed: callResult.gasUsed,
|
|
409
|
+
contracts: callResult.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
|
|
410
|
+
txInputs: callResult.txInputs,
|
|
411
|
+
txOutputs: callResult.txOutputs.map((output) => fromApiOutput(output)),
|
|
412
|
+
events: Contract.fromApiEvents(callResult.events, addressToCodeHash, txId, getContractByCodeHash),
|
|
413
|
+
debugMessages: callResult.debugMessages
|
|
414
|
+
};
|
|
415
|
+
}
|
|
413
416
|
class Script extends Artifact {
|
|
414
417
|
constructor(version, name, bytecodeTemplate, bytecodeDebugPatch, fieldsSig, functions, structs) {
|
|
415
418
|
super(version, name, functions);
|
|
@@ -635,13 +638,32 @@ class ContractFactory {
|
|
|
635
638
|
}
|
|
636
639
|
exports.ContractFactory = ContractFactory;
|
|
637
640
|
class ExecutableScript {
|
|
638
|
-
constructor(script) {
|
|
641
|
+
constructor(script, getContractByCodeHash) {
|
|
639
642
|
this.script = script;
|
|
643
|
+
this.getContractByCodeHash = getContractByCodeHash;
|
|
640
644
|
}
|
|
641
645
|
async execute(signer, params) {
|
|
642
646
|
const signerParams = await this.script.txParamsForExecution(signer, params);
|
|
643
647
|
return await signer.signAndSubmitExecuteScriptTx(signerParams);
|
|
644
648
|
}
|
|
649
|
+
async call(params) {
|
|
650
|
+
const mainFunc = this.script.functions.find((f) => f.name === 'main');
|
|
651
|
+
if (mainFunc === undefined) {
|
|
652
|
+
throw new Error(`There is no main function in script ${this.script.name}`);
|
|
653
|
+
}
|
|
654
|
+
const bytecode = this.script.buildByteCodeToDeploy(params.initialFields);
|
|
655
|
+
const txId = params.txId ?? randomTxId();
|
|
656
|
+
const provider = (0, global_1.getCurrentNodeProvider)();
|
|
657
|
+
const callResult = await provider.contracts.postContractsCallTxScript({
|
|
658
|
+
...params,
|
|
659
|
+
group: params.groupIndex ?? 0,
|
|
660
|
+
bytecode: bytecode,
|
|
661
|
+
inputAssets: toApiInputAssets(params.inputAssets)
|
|
662
|
+
});
|
|
663
|
+
const returnTypes = mainFunc.returnTypes;
|
|
664
|
+
const result = fromCallResult(callResult, txId, returnTypes, this.script.structs, this.getContractByCodeHash);
|
|
665
|
+
return result;
|
|
666
|
+
}
|
|
645
667
|
}
|
|
646
668
|
exports.ExecutableScript = ExecutableScript;
|
|
647
669
|
function specialContractAddress(eventIndex, groupIndex) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.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",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
},
|
|
34
34
|
"author": "Alephium dev <dev@alephium.org>",
|
|
35
35
|
"config": {
|
|
36
|
-
"alephium_version": "3.
|
|
36
|
+
"alephium_version": "3.3.0",
|
|
37
37
|
"explorer_backend_version": "1.19.3"
|
|
38
38
|
},
|
|
39
39
|
"type": "commonjs",
|
package/src/api/api-alephium.ts
CHANGED
|
@@ -401,7 +401,7 @@ export interface CallContract {
|
|
|
401
401
|
/** @format int32 */
|
|
402
402
|
methodIndex: number
|
|
403
403
|
args?: Val[]
|
|
404
|
-
|
|
404
|
+
interestedContracts?: string[]
|
|
405
405
|
inputAssets?: TestInputAsset[]
|
|
406
406
|
}
|
|
407
407
|
|
|
@@ -427,6 +427,34 @@ export interface CallContractSucceeded {
|
|
|
427
427
|
type: string
|
|
428
428
|
}
|
|
429
429
|
|
|
430
|
+
/** CallTxScript */
|
|
431
|
+
export interface CallTxScript {
|
|
432
|
+
/** @format int32 */
|
|
433
|
+
group: number
|
|
434
|
+
/** @format hex-string */
|
|
435
|
+
bytecode: string
|
|
436
|
+
/** @format address */
|
|
437
|
+
callerAddress?: string
|
|
438
|
+
/** @format block-hash */
|
|
439
|
+
worldStateBlockHash?: string
|
|
440
|
+
/** @format 32-byte-hash */
|
|
441
|
+
txId?: string
|
|
442
|
+
inputAssets?: TestInputAsset[]
|
|
443
|
+
interestedContracts?: string[]
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** CallTxScriptResult */
|
|
447
|
+
export interface CallTxScriptResult {
|
|
448
|
+
returns: Val[]
|
|
449
|
+
/** @format int32 */
|
|
450
|
+
gasUsed: number
|
|
451
|
+
contracts: ContractState[]
|
|
452
|
+
txInputs: string[]
|
|
453
|
+
txOutputs: Output[]
|
|
454
|
+
events: ContractEventByTxId[]
|
|
455
|
+
debugMessages: DebugMessage[]
|
|
456
|
+
}
|
|
457
|
+
|
|
430
458
|
/** ChainInfo */
|
|
431
459
|
export interface ChainInfo {
|
|
432
460
|
/** @format int32 */
|
|
@@ -1453,7 +1481,7 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
1453
1481
|
|
|
1454
1482
|
/**
|
|
1455
1483
|
* @title Alephium API
|
|
1456
|
-
* @version 3.
|
|
1484
|
+
* @version 3.3.0
|
|
1457
1485
|
* @baseUrl ../
|
|
1458
1486
|
*/
|
|
1459
1487
|
export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
|
|
@@ -2714,7 +2742,27 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
|
|
|
2714
2742
|
type: ContentType.Json,
|
|
2715
2743
|
format: 'json',
|
|
2716
2744
|
...params
|
|
2717
|
-
}).then(convertHttpResponse)
|
|
2745
|
+
}).then(convertHttpResponse),
|
|
2746
|
+
|
|
2747
|
+
/**
|
|
2748
|
+
* No description
|
|
2749
|
+
*
|
|
2750
|
+
* @tags Contracts
|
|
2751
|
+
* @name PostContractsCallTxScript
|
|
2752
|
+
* @summary Call TxScript
|
|
2753
|
+
* @request POST:/contracts/call-tx-script
|
|
2754
|
+
*/
|
|
2755
|
+
postContractsCallTxScript: (data: CallTxScript, params: RequestParams = {}) =>
|
|
2756
|
+
this.request<CallTxScriptResult, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>(
|
|
2757
|
+
{
|
|
2758
|
+
path: `/contracts/call-tx-script`,
|
|
2759
|
+
method: 'POST',
|
|
2760
|
+
body: data,
|
|
2761
|
+
type: ContentType.Json,
|
|
2762
|
+
format: 'json',
|
|
2763
|
+
...params
|
|
2764
|
+
}
|
|
2765
|
+
).then(convertHttpResponse)
|
|
2718
2766
|
}
|
|
2719
2767
|
multisig = {
|
|
2720
2768
|
/**
|
package/src/codec/codec.ts
CHANGED
|
@@ -28,3 +28,16 @@ export function assert(value: boolean, message: string) {
|
|
|
28
28
|
throw new Error(message)
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
+
|
|
32
|
+
export function fixedSizeBytes(name: string, length: number): Parser {
|
|
33
|
+
return Parser.start().wrapped({
|
|
34
|
+
length,
|
|
35
|
+
type: Parser.start().buffer(name, { length }),
|
|
36
|
+
wrapper: function (result) {
|
|
37
|
+
if (result.length === length) {
|
|
38
|
+
return result
|
|
39
|
+
}
|
|
40
|
+
throw new Error(`Too few bytes when parsing ${name}, expected ${length}, got ${result.length}`)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
}
|
|
@@ -19,7 +19,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
19
19
|
import { Parser } from 'binary-parser'
|
|
20
20
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
21
21
|
import { Codec } from './codec'
|
|
22
|
-
import { compactSignedIntCodec,
|
|
22
|
+
import { compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
23
23
|
import { Method, MethodCodec, methodCodec } from './method-codec'
|
|
24
24
|
import { concatBytes } from '../utils'
|
|
25
25
|
|
|
@@ -17,7 +17,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
import { Parser } from 'binary-parser'
|
|
19
19
|
import { DecodedCompactInt, compactSignedIntCodec } from './compact-int-codec'
|
|
20
|
-
import { Codec } from './codec'
|
|
20
|
+
import { Codec, fixedSizeBytes } from './codec'
|
|
21
21
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
22
22
|
|
|
23
23
|
export interface PublicKeyHash {
|
|
@@ -25,7 +25,7 @@ export interface PublicKeyHash {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
class PublicKeyHashCodec implements Codec<PublicKeyHash> {
|
|
28
|
-
parser =
|
|
28
|
+
parser = fixedSizeBytes('publicKeyHash', 32)
|
|
29
29
|
|
|
30
30
|
encode(input: PublicKeyHash): Uint8Array {
|
|
31
31
|
return input.publicKeyHash
|
|
@@ -18,7 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
import { Parser } from 'binary-parser'
|
|
19
19
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
20
20
|
import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
21
|
-
import { Codec } from './codec'
|
|
21
|
+
import { Codec, fixedSizeBytes } from './codec'
|
|
22
22
|
import { DecodedScript, scriptCodec } from './script-codec'
|
|
23
23
|
import { ByteString, byteStringCodec } from './bytestring-codec'
|
|
24
24
|
import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
|
|
@@ -29,7 +29,7 @@ export interface P2PKH {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
class P2PKHCodec implements Codec<P2PKH> {
|
|
32
|
-
parser =
|
|
32
|
+
parser = fixedSizeBytes('publicKey', 33)
|
|
33
33
|
|
|
34
34
|
encode(input: P2PKH): Uint8Array {
|
|
35
35
|
return input.publicKey
|
package/src/contract/contract.ts
CHANGED
|
@@ -77,7 +77,6 @@ import {
|
|
|
77
77
|
StoreMutFieldByIndex,
|
|
78
78
|
DestroySelf,
|
|
79
79
|
Pop,
|
|
80
|
-
byteStringCodec,
|
|
81
80
|
StoreLocal,
|
|
82
81
|
instrCodec,
|
|
83
82
|
U256Const,
|
|
@@ -603,20 +602,30 @@ export class Contract extends Artifact {
|
|
|
603
602
|
): CallContractResult<unknown> {
|
|
604
603
|
const returnTypes = this.functions[`${methodIndex}`].returnTypes
|
|
605
604
|
const callResult = tryGetCallResult(result)
|
|
606
|
-
|
|
607
|
-
|
|
605
|
+
return fromCallResult(callResult, txId, returnTypes, this.structs, getContractByCodeHash)
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
608
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
609
|
+
function fromCallResult(
|
|
610
|
+
callResult: node.CallContractSucceeded | node.CallTxScriptResult,
|
|
611
|
+
txId: string,
|
|
612
|
+
returnTypes: string[],
|
|
613
|
+
structs: Struct[],
|
|
614
|
+
getContractByCodeHash: (codeHash: string) => Contract
|
|
615
|
+
): CallContractResult<unknown> {
|
|
616
|
+
const rawReturn = fromApiArray(callResult.returns, returnTypes, structs)
|
|
617
|
+
const returns = rawReturn.length === 0 ? null : rawReturn.length === 1 ? rawReturn[0] : rawReturn
|
|
618
|
+
|
|
619
|
+
const addressToCodeHash = new Map<string, string>()
|
|
620
|
+
callResult.contracts.forEach((contract) => addressToCodeHash.set(contract.address, contract.codeHash))
|
|
621
|
+
return {
|
|
622
|
+
returns: returns,
|
|
623
|
+
gasUsed: callResult.gasUsed,
|
|
624
|
+
contracts: callResult.contracts.map((state) => Contract.fromApiContractState(state, getContractByCodeHash)),
|
|
625
|
+
txInputs: callResult.txInputs,
|
|
626
|
+
txOutputs: callResult.txOutputs.map((output) => fromApiOutput(output)),
|
|
627
|
+
events: Contract.fromApiEvents(callResult.events, addressToCodeHash, txId, getContractByCodeHash),
|
|
628
|
+
debugMessages: callResult.debugMessages
|
|
620
629
|
}
|
|
621
630
|
}
|
|
622
631
|
|
|
@@ -1011,17 +1020,38 @@ export abstract class ContractFactory<I extends ContractInstance, F extends Fiel
|
|
|
1011
1020
|
}
|
|
1012
1021
|
}
|
|
1013
1022
|
|
|
1014
|
-
export class ExecutableScript<P extends Fields = Fields> {
|
|
1023
|
+
export class ExecutableScript<P extends Fields = Fields, R extends Val | null = null> {
|
|
1015
1024
|
readonly script: Script
|
|
1025
|
+
readonly getContractByCodeHash: (codeHash: string) => Contract
|
|
1016
1026
|
|
|
1017
|
-
constructor(script: Script) {
|
|
1027
|
+
constructor(script: Script, getContractByCodeHash: (codeHash: string) => Contract) {
|
|
1018
1028
|
this.script = script
|
|
1029
|
+
this.getContractByCodeHash = getContractByCodeHash
|
|
1019
1030
|
}
|
|
1020
1031
|
|
|
1021
1032
|
async execute(signer: SignerProvider, params: ExecuteScriptParams<P>): Promise<ExecuteScriptResult> {
|
|
1022
1033
|
const signerParams = await this.script.txParamsForExecution(signer, params)
|
|
1023
1034
|
return await signer.signAndSubmitExecuteScriptTx(signerParams)
|
|
1024
1035
|
}
|
|
1036
|
+
|
|
1037
|
+
async call(params: CallScriptParams<P>): Promise<CallScriptResult<R>> {
|
|
1038
|
+
const mainFunc = this.script.functions.find((f) => f.name === 'main')
|
|
1039
|
+
if (mainFunc === undefined) {
|
|
1040
|
+
throw new Error(`There is no main function in script ${this.script.name}`)
|
|
1041
|
+
}
|
|
1042
|
+
const bytecode = this.script.buildByteCodeToDeploy(params.initialFields)
|
|
1043
|
+
const txId = params.txId ?? randomTxId()
|
|
1044
|
+
const provider = getCurrentNodeProvider()
|
|
1045
|
+
const callResult = await provider.contracts.postContractsCallTxScript({
|
|
1046
|
+
...params,
|
|
1047
|
+
group: params.groupIndex ?? 0,
|
|
1048
|
+
bytecode: bytecode,
|
|
1049
|
+
inputAssets: toApiInputAssets(params.inputAssets)
|
|
1050
|
+
})
|
|
1051
|
+
const returnTypes = mainFunc.returnTypes
|
|
1052
|
+
const result = fromCallResult(callResult, txId, returnTypes, this.script.structs, this.getContractByCodeHash)
|
|
1053
|
+
return result as CallScriptResult<R>
|
|
1054
|
+
}
|
|
1025
1055
|
}
|
|
1026
1056
|
|
|
1027
1057
|
export interface ExecuteScriptParams<P extends Fields = Fields> {
|
|
@@ -1041,11 +1071,23 @@ export interface ExecuteScriptResult {
|
|
|
1041
1071
|
gasPrice: Number256
|
|
1042
1072
|
}
|
|
1043
1073
|
|
|
1074
|
+
export interface CallScriptParams<P extends Fields = Fields> {
|
|
1075
|
+
initialFields: P
|
|
1076
|
+
groupIndex?: number
|
|
1077
|
+
callerAddress?: string
|
|
1078
|
+
worldStateBlockHash?: string
|
|
1079
|
+
txId?: string
|
|
1080
|
+
interestedContracts?: string[]
|
|
1081
|
+
inputAssets?: InputAsset[]
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
export type CallScriptResult<R> = CallContractResult<R>
|
|
1085
|
+
|
|
1044
1086
|
export interface CallContractParams<T extends Arguments = Arguments> {
|
|
1045
1087
|
args: T
|
|
1046
1088
|
worldStateBlockHash?: string
|
|
1047
1089
|
txId?: string
|
|
1048
|
-
|
|
1090
|
+
interestedContracts?: string[]
|
|
1049
1091
|
inputAssets?: InputAsset[]
|
|
1050
1092
|
}
|
|
1051
1093
|
|