@alephium/web3 1.11.1 → 1.11.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.
@@ -918,17 +918,42 @@ function getTestExistingContracts(selfContract, selfContractId, group, params, g
918
918
  });
919
919
  return existingContracts.concat(selfMapEntries, existingMapEntries);
920
920
  }
921
+ function getNewCreatedContractExceptMaps(result, getContractByCodeHash) {
922
+ const isMapContract = (codeHash) => {
923
+ try {
924
+ getContractByCodeHash(codeHash);
925
+ return false;
926
+ }
927
+ catch (error) {
928
+ if (error instanceof Error && error.message.includes('Unknown code with code hash')) {
929
+ // the contract does not exist, because it is a map item contract
930
+ return true;
931
+ }
932
+ throw error;
933
+ }
934
+ };
935
+ const states = [];
936
+ result.events.forEach((event) => {
937
+ if (event.eventIndex === Contract.ContractCreatedEventIndex) {
938
+ const contractAddress = event.fields[0].value;
939
+ const contractState = result.contracts.find((c) => c.address === contractAddress);
940
+ if (contractState !== undefined && !isMapContract(contractState.codeHash)) {
941
+ states.push(contractState);
942
+ }
943
+ }
944
+ });
945
+ return states;
946
+ }
921
947
  function extractMapsFromApiResult(selfAddress, params, group, apiResult, getContractByCodeHash) {
922
948
  const selfMaps = params.initialMaps ?? {};
923
949
  const existingContracts = params.existingContracts ?? [];
924
- const filtered = apiResult.contracts.filter((c) => c.address === selfAddress || existingContracts.find((s) => s.address === c.address) !== undefined);
950
+ const updatedExistingContracts = apiResult.contracts.filter((c) => c.address === selfAddress || existingContracts.find((s) => s.address === c.address) !== undefined);
951
+ const newCreateContracts = getNewCreatedContractExceptMaps(apiResult, getContractByCodeHash);
925
952
  const allMaps = [];
926
- filtered.forEach((state) => {
953
+ updatedExistingContracts.concat(newCreateContracts).forEach((state) => {
927
954
  const artifact = getContractByCodeHash(state.codeHash);
928
955
  if (artifact.mapsSig !== undefined) {
929
- const originMaps = state.address === selfAddress
930
- ? selfMaps
931
- : existingContracts.find((s) => s.address === state.address).maps;
956
+ const originMaps = state.address === selfAddress ? selfMaps : existingContracts.find((s) => s.address === state.address)?.maps;
932
957
  const maps = existingContractsToMaps(artifact, state.address, group, apiResult, originMaps ?? {});
933
958
  allMaps.push({ address: state.address, maps });
934
959
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "1.11.1",
3
+ "version": "1.11.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",
@@ -1452,6 +1452,36 @@ function getTestExistingContracts(
1452
1452
  return existingContracts.concat(selfMapEntries, existingMapEntries)
1453
1453
  }
1454
1454
 
1455
+ function getNewCreatedContractExceptMaps(
1456
+ result: node.TestContractResult,
1457
+ getContractByCodeHash: (codeHash: string) => Contract
1458
+ ) {
1459
+ const isMapContract = (codeHash: string): boolean => {
1460
+ try {
1461
+ getContractByCodeHash(codeHash)
1462
+ return false
1463
+ } catch (error) {
1464
+ if (error instanceof Error && error.message.includes('Unknown code with code hash')) {
1465
+ // the contract does not exist, because it is a map item contract
1466
+ return true
1467
+ }
1468
+ throw error
1469
+ }
1470
+ }
1471
+
1472
+ const states: node.ContractState[] = []
1473
+ result.events.forEach((event) => {
1474
+ if (event.eventIndex === Contract.ContractCreatedEventIndex) {
1475
+ const contractAddress = event.fields[0].value as string
1476
+ const contractState = result.contracts.find((c) => c.address === contractAddress)
1477
+ if (contractState !== undefined && !isMapContract(contractState.codeHash)) {
1478
+ states.push(contractState)
1479
+ }
1480
+ }
1481
+ })
1482
+ return states
1483
+ }
1484
+
1455
1485
  export function extractMapsFromApiResult(
1456
1486
  selfAddress: string,
1457
1487
  params: Optional<TestContractParams, 'testArgs' | 'initialFields'>,
@@ -1461,17 +1491,16 @@ export function extractMapsFromApiResult(
1461
1491
  ): { address: string; maps: Record<string, Map<Val, Val>> }[] {
1462
1492
  const selfMaps = params.initialMaps ?? {}
1463
1493
  const existingContracts = params.existingContracts ?? []
1464
- const filtered = apiResult.contracts.filter(
1494
+ const updatedExistingContracts = apiResult.contracts.filter(
1465
1495
  (c) => c.address === selfAddress || existingContracts.find((s) => s.address === c.address) !== undefined
1466
1496
  )
1497
+ const newCreateContracts = getNewCreatedContractExceptMaps(apiResult, getContractByCodeHash)
1467
1498
  const allMaps: { address: string; maps: Record<string, Map<Val, Val>> }[] = []
1468
- filtered.forEach((state) => {
1499
+ updatedExistingContracts.concat(newCreateContracts).forEach((state) => {
1469
1500
  const artifact = getContractByCodeHash(state.codeHash)
1470
1501
  if (artifact.mapsSig !== undefined) {
1471
1502
  const originMaps =
1472
- state.address === selfAddress
1473
- ? selfMaps
1474
- : (existingContracts.find((s) => s.address === state.address) as ContractStateWithMaps).maps
1503
+ state.address === selfAddress ? selfMaps : existingContracts.find((s) => s.address === state.address)?.maps
1475
1504
  const maps = existingContractsToMaps(artifact, state.address, group, apiResult, originMaps ?? {})
1476
1505
  allMaps.push({ address: state.address, maps })
1477
1506
  }