@alephium/web3 1.1.2 → 1.2.1-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.
@@ -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
- constructor(script: Script);
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
- existingContracts?: string[];
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
- const rawReturn = fromApiArray(callResult.returns, returnTypes, this.structs);
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.1.2",
3
+ "version": "1.2.1-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",
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "author": "Alephium dev <dev@alephium.org>",
35
35
  "config": {
36
- "alephium_version": "3.2.0",
37
- "explorer_backend_version": "1.19.3"
36
+ "alephium_version": "3.3.0",
37
+ "explorer_backend_version": "2.0.0"
38
38
  },
39
39
  "type": "commonjs",
40
40
  "dependencies": {
@@ -41,6 +41,15 @@ export function validateAddress(address: string) {
41
41
  decodeAndValidateAddress(address)
42
42
  }
43
43
 
44
+ export function isValidAddress(address: string): boolean {
45
+ try {
46
+ validateAddress(address)
47
+ return true
48
+ } catch {
49
+ return false
50
+ }
51
+ }
52
+
44
53
  function decodeAndValidateAddress(address: string): Uint8Array {
45
54
  let decoded: Uint8Array
46
55
  try {
@@ -401,7 +401,7 @@ export interface CallContract {
401
401
  /** @format int32 */
402
402
  methodIndex: number
403
403
  args?: Val[]
404
- existingContracts?: string[]
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.2.0
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
  /**