@alephium/web3 0.36.0 → 0.37.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.
@@ -1002,7 +1002,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
1002
1002
  }
1003
1003
  /**
1004
1004
  * @title Alephium API
1005
- * @version 2.10.0
1005
+ * @version 2.11.0
1006
1006
  * @baseUrl ../
1007
1007
  */
1008
1008
  export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -151,7 +151,7 @@ class HttpClient {
151
151
  exports.HttpClient = HttpClient;
152
152
  /**
153
153
  * @title Alephium API
154
- * @version 2.10.0
154
+ * @version 2.11.0
155
155
  * @baseUrl ../
156
156
  */
157
157
  class Api extends HttpClient {
@@ -297,8 +297,8 @@ export interface CallContractResult<R> {
297
297
  events: ContractEvent[];
298
298
  debugMessages: DebugMessage[];
299
299
  }
300
- export declare const CreateContractEventAddress: string;
301
- export declare const DestroyContractEventAddress: string;
300
+ export declare const CreateContractEventAddresses: string[];
301
+ export declare const DestroyContractEventAddresses: string[];
302
302
  export type ContractCreatedEventFields = {
303
303
  address: Address;
304
304
  parentAddress?: Address;
@@ -323,8 +323,8 @@ export declare abstract class ContractInstance {
323
323
  constructor(address: Address);
324
324
  }
325
325
  export declare function fetchContractState<F extends Fields, I extends ContractInstance>(contract: ContractFactory<I, F>, instance: ContractInstance): Promise<ContractState<F>>;
326
- export declare function subscribeContractCreatedEvent(options: EventSubscribeOptions<ContractCreatedEvent>, fromCount?: number): EventSubscription;
327
- export declare function subscribeContractDestroyedEvent(options: EventSubscribeOptions<ContractDestroyedEvent>, fromCount?: number): EventSubscription;
326
+ export declare function subscribeContractCreatedEvent(options: EventSubscribeOptions<ContractCreatedEvent>, fromGroup: number, fromCount?: number): EventSubscription;
327
+ export declare function subscribeContractDestroyedEvent(options: EventSubscribeOptions<ContractDestroyedEvent>, fromGroup: number, fromCount?: number): EventSubscription;
328
328
  export declare function decodeEvent<F extends Fields, M extends ContractEvent<F>>(contract: Contract, instance: ContractInstance, event: node.ContractEvent, targetEventIndex: number): M;
329
329
  export declare function subscribeContractEvent<F extends Fields, M extends ContractEvent<F>>(contract: Contract, instance: ContractInstance, options: EventSubscribeOptions<M>, eventName: string, fromCount?: number): EventSubscription;
330
330
  export declare function subscribeContractEvents(contract: Contract, instance: ContractInstance, options: EventSubscribeOptions<ContractEvent<any>>, fromCount?: number): EventSubscription;
@@ -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.tryGetCallResult = exports.getTokenIdFromUnsignedTx = exports.getContractIdFromUnsignedTx = exports.getContractEventsCurrentCount = exports.multicallMethods = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.addStdIdToFields = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddress = exports.CreateContractEventAddress = exports.ExecutableScript = exports.ContractFactory = exports.randomTxId = exports.fromApiEventFields = exports.fromApiArray = exports.getDefaultValue = exports.fromApiFields = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.Struct = exports.ProjectArtifact = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = exports.StdIdFieldName = void 0;
46
+ exports.tryGetCallResult = exports.getTokenIdFromUnsignedTx = exports.getContractIdFromUnsignedTx = exports.getContractEventsCurrentCount = exports.multicallMethods = exports.callMethod = exports.subscribeContractEvents = exports.subscribeContractEvent = exports.decodeEvent = exports.subscribeContractDestroyedEvent = exports.subscribeContractCreatedEvent = exports.fetchContractState = exports.ContractInstance = exports.testMethod = exports.addStdIdToFields = exports.subscribeEventsFromContract = exports.decodeContractDestroyedEvent = exports.decodeContractCreatedEvent = exports.DestroyContractEventAddresses = exports.CreateContractEventAddresses = exports.ExecutableScript = exports.ContractFactory = exports.randomTxId = exports.fromApiEventFields = exports.fromApiArray = exports.getDefaultValue = exports.fromApiFields = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.Struct = exports.ProjectArtifact = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = exports.StdIdFieldName = void 0;
47
47
  const buffer_1 = require("buffer/");
48
48
  const fs_1 = __importDefault(require("fs"));
49
49
  const fs_2 = require("fs");
@@ -1129,13 +1129,14 @@ class ExecutableScript {
1129
1129
  }
1130
1130
  }
1131
1131
  exports.ExecutableScript = ExecutableScript;
1132
- function specialContractAddress(n) {
1132
+ function specialContractAddress(eventIndex, groupIndex) {
1133
1133
  const bytes = new Uint8Array(32).fill(0);
1134
- bytes[31] = n;
1134
+ bytes[30] = eventIndex;
1135
+ bytes[31] = groupIndex;
1135
1136
  return (0, utils_1.addressFromContractId)((0, utils_1.binToHex)(bytes));
1136
1137
  }
1137
- exports.CreateContractEventAddress = specialContractAddress(-1);
1138
- exports.DestroyContractEventAddress = specialContractAddress(-2);
1138
+ exports.CreateContractEventAddresses = Array.from(Array(constants_1.TOTAL_NUMBER_OF_GROUPS).keys()).map((groupIndex) => specialContractAddress(Contract.ContractCreatedEventIndex, groupIndex));
1139
+ exports.DestroyContractEventAddresses = Array.from(Array(constants_1.TOTAL_NUMBER_OF_GROUPS).keys()).map((groupIndex) => specialContractAddress(Contract.ContractDestroyedEventIndex, groupIndex));
1139
1140
  function decodeSystemEvent(event, eventSig, eventIndex) {
1140
1141
  if (event.eventIndex !== eventIndex) {
1141
1142
  throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
@@ -1231,20 +1232,29 @@ async function fetchContractState(contract, instance) {
1231
1232
  };
1232
1233
  }
1233
1234
  exports.fetchContractState = fetchContractState;
1234
- function subscribeContractCreatedEvent(options, fromCount) {
1235
- return subscribeEventsFromContract(options, exports.CreateContractEventAddress, Contract.ContractCreatedEventIndex, (event) => {
1235
+ function checkGroupIndex(groupIndex) {
1236
+ if (groupIndex < 0 || groupIndex >= constants_1.TOTAL_NUMBER_OF_GROUPS) {
1237
+ throw new Error(`Invalid group index ${groupIndex}, expected a value within the range [0, ${constants_1.TOTAL_NUMBER_OF_GROUPS})`);
1238
+ }
1239
+ }
1240
+ function subscribeContractCreatedEvent(options, fromGroup, fromCount) {
1241
+ checkGroupIndex(fromGroup);
1242
+ const contractAddress = exports.CreateContractEventAddresses[`${fromGroup}`];
1243
+ return subscribeEventsFromContract(options, contractAddress, Contract.ContractCreatedEventIndex, (event) => {
1236
1244
  return {
1237
1245
  ...decodeContractCreatedEvent(event),
1238
- contractAddress: exports.CreateContractEventAddress
1246
+ contractAddress: contractAddress
1239
1247
  };
1240
1248
  }, fromCount);
1241
1249
  }
1242
1250
  exports.subscribeContractCreatedEvent = subscribeContractCreatedEvent;
1243
- function subscribeContractDestroyedEvent(options, fromCount) {
1244
- return subscribeEventsFromContract(options, exports.DestroyContractEventAddress, Contract.ContractDestroyedEventIndex, (event) => {
1251
+ function subscribeContractDestroyedEvent(options, fromGroup, fromCount) {
1252
+ checkGroupIndex(fromGroup);
1253
+ const contractAddress = exports.DestroyContractEventAddresses[`${fromGroup}`];
1254
+ return subscribeEventsFromContract(options, contractAddress, Contract.ContractDestroyedEventIndex, (event) => {
1245
1255
  return {
1246
1256
  ...decodeContractDestroyedEvent(event),
1247
- contractAddress: exports.DestroyContractEventAddress
1257
+ contractAddress: contractAddress
1248
1258
  };
1249
1259
  }, fromCount);
1250
1260
  }
@@ -240,12 +240,12 @@ function encodeScriptField(tpe, value) {
240
240
  case 'U256':
241
241
  const u256 = (0, api_1.toApiNumber256)(value);
242
242
  return encodeScriptFieldU256(BigInt(u256));
243
- case 'ByteVec':
244
- const hexStr = (0, api_1.toApiByteVec)(value);
245
- return new Uint8Array([Instruction.bytesConst, ...encodeByteVec(hexStr)]);
246
243
  case 'Address':
247
244
  const address = (0, api_1.toApiAddress)(value);
248
245
  return new Uint8Array([Instruction.addressConst, ...encodeAddress(address)]);
246
+ default: // ByteVec or Contract
247
+ const hexStr = (0, api_1.toApiByteVec)(value);
248
+ return new Uint8Array([Instruction.bytesConst, ...encodeByteVec(hexStr)]);
249
249
  }
250
250
  throw invalidScriptField(tpe, value);
251
251
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.36.0",
3
+ "version": "0.37.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,7 +27,7 @@
27
27
  },
28
28
  "author": "Alephium dev <dev@alephium.org>",
29
29
  "config": {
30
- "alephium_version": "2.10.0",
30
+ "alephium_version": "2.11.0",
31
31
  "explorer_backend_version": "1.17.0"
32
32
  },
33
33
  "type": "commonjs",
@@ -1300,7 +1300,7 @@ export class HttpClient<SecurityDataType = unknown> {
1300
1300
 
1301
1301
  /**
1302
1302
  * @title Alephium API
1303
- * @version 2.10.0
1303
+ * @version 2.11.0
1304
1304
  * @baseUrl ../
1305
1305
  */
1306
1306
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -62,7 +62,7 @@ import {
62
62
  import { getCurrentNodeProvider } from '../global'
63
63
  import * as path from 'path'
64
64
  import { EventSubscribeOptions, EventSubscription, subscribeToEvents } from './events'
65
- import { ONE_ALPH } from '../constants'
65
+ import { ONE_ALPH, TOTAL_NUMBER_OF_GROUPS } from '../constants'
66
66
  import * as blake from 'blakejs'
67
67
  import { parseError } from '../utils/error'
68
68
  import { isContractDebugMessageEnabled } from '../debug'
@@ -1722,14 +1722,19 @@ export interface CallContractResult<R> {
1722
1722
  debugMessages: DebugMessage[]
1723
1723
  }
1724
1724
 
1725
- function specialContractAddress(n: number): string {
1725
+ function specialContractAddress(eventIndex: number, groupIndex: number): string {
1726
1726
  const bytes = new Uint8Array(32).fill(0)
1727
- bytes[31] = n
1727
+ bytes[30] = eventIndex
1728
+ bytes[31] = groupIndex
1728
1729
  return addressFromContractId(binToHex(bytes))
1729
1730
  }
1730
1731
 
1731
- export const CreateContractEventAddress = specialContractAddress(-1)
1732
- export const DestroyContractEventAddress = specialContractAddress(-2)
1732
+ export const CreateContractEventAddresses = Array.from(Array(TOTAL_NUMBER_OF_GROUPS).keys()).map((groupIndex) =>
1733
+ specialContractAddress(Contract.ContractCreatedEventIndex, groupIndex)
1734
+ )
1735
+ export const DestroyContractEventAddresses = Array.from(Array(TOTAL_NUMBER_OF_GROUPS).keys()).map((groupIndex) =>
1736
+ specialContractAddress(Contract.ContractDestroyedEventIndex, groupIndex)
1737
+ )
1733
1738
 
1734
1739
  export type ContractCreatedEventFields = {
1735
1740
  address: Address
@@ -1862,18 +1867,29 @@ export async function fetchContractState<F extends Fields, I extends ContractIns
1862
1867
  }
1863
1868
  }
1864
1869
 
1870
+ function checkGroupIndex(groupIndex: number) {
1871
+ if (groupIndex < 0 || groupIndex >= TOTAL_NUMBER_OF_GROUPS) {
1872
+ throw new Error(
1873
+ `Invalid group index ${groupIndex}, expected a value within the range [0, ${TOTAL_NUMBER_OF_GROUPS})`
1874
+ )
1875
+ }
1876
+ }
1877
+
1865
1878
  export function subscribeContractCreatedEvent(
1866
1879
  options: EventSubscribeOptions<ContractCreatedEvent>,
1880
+ fromGroup: number,
1867
1881
  fromCount?: number
1868
1882
  ): EventSubscription {
1883
+ checkGroupIndex(fromGroup)
1884
+ const contractAddress = CreateContractEventAddresses[`${fromGroup}`]
1869
1885
  return subscribeEventsFromContract(
1870
1886
  options,
1871
- CreateContractEventAddress,
1887
+ contractAddress,
1872
1888
  Contract.ContractCreatedEventIndex,
1873
1889
  (event) => {
1874
1890
  return {
1875
1891
  ...decodeContractCreatedEvent(event),
1876
- contractAddress: CreateContractEventAddress
1892
+ contractAddress: contractAddress
1877
1893
  }
1878
1894
  },
1879
1895
  fromCount
@@ -1882,16 +1898,19 @@ export function subscribeContractCreatedEvent(
1882
1898
 
1883
1899
  export function subscribeContractDestroyedEvent(
1884
1900
  options: EventSubscribeOptions<ContractDestroyedEvent>,
1901
+ fromGroup: number,
1885
1902
  fromCount?: number
1886
1903
  ): EventSubscription {
1904
+ checkGroupIndex(fromGroup)
1905
+ const contractAddress = DestroyContractEventAddresses[`${fromGroup}`]
1887
1906
  return subscribeEventsFromContract(
1888
1907
  options,
1889
- DestroyContractEventAddress,
1908
+ contractAddress,
1890
1909
  Contract.ContractDestroyedEventIndex,
1891
1910
  (event) => {
1892
1911
  return {
1893
1912
  ...decodeContractDestroyedEvent(event),
1894
- contractAddress: DestroyContractEventAddress
1913
+ contractAddress: contractAddress
1895
1914
  }
1896
1915
  },
1897
1916
  fromCount
@@ -236,12 +236,12 @@ export function encodeScriptField(tpe: string, value: Val): Uint8Array {
236
236
  case 'U256':
237
237
  const u256 = toApiNumber256(value)
238
238
  return encodeScriptFieldU256(BigInt(u256))
239
- case 'ByteVec':
240
- const hexStr = toApiByteVec(value)
241
- return new Uint8Array([Instruction.bytesConst, ...encodeByteVec(hexStr)])
242
239
  case 'Address':
243
240
  const address = toApiAddress(value)
244
241
  return new Uint8Array([Instruction.addressConst, ...encodeAddress(address)])
242
+ default: // ByteVec or Contract
243
+ const hexStr = toApiByteVec(value)
244
+ return new Uint8Array([Instruction.bytesConst, ...encodeByteVec(hexStr)])
245
245
  }
246
246
 
247
247
  throw invalidScriptField(tpe, value)