@alephium/web3 0.5.0-rc.4 → 0.5.0-rc.6

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.
@@ -18,7 +18,7 @@ export declare function toApiByteVec(v: Val): string;
18
18
  export declare function toApiAddress(v: Val): string;
19
19
  export declare function toApiArray(tpe: string, v: Val): node.Val;
20
20
  export declare function toApiVal(v: Val, tpe: string): node.Val;
21
- export declare function fromApiVals(vals: node.Val[], names: string[], types: string[]): NamedVals;
21
+ export declare function fromApiVals(vals: node.Val[], names: string[], types: string[], optionalNames?: string[], optionalTypes?: string[]): NamedVals;
22
22
  export declare function fromApiArray(vals: node.Val[], types: string[]): Val[];
23
23
  export declare function fromApiVal(v: node.Val, tpe: string): Val;
24
24
  export declare function typeLength(tpe: string): number;
@@ -165,7 +165,7 @@ function _fromApiVal(vals, valIndex, tpe) {
165
165
  }
166
166
  }
167
167
  }
168
- function fromApiVals(vals, names, types) {
168
+ function fromApiVals(vals, names, types, optionalNames = [], optionalTypes = []) {
169
169
  let valIndex = 0;
170
170
  const result = {};
171
171
  types.forEach((currentType, index) => {
@@ -174,7 +174,11 @@ function fromApiVals(vals, names, types) {
174
174
  valIndex = nextIndex;
175
175
  result[`${currentName}`] = val;
176
176
  });
177
- return result;
177
+ if (valIndex === vals.length) {
178
+ return result;
179
+ }
180
+ const optionalFields = fromApiVals(vals.slice(valIndex), optionalNames, optionalTypes);
181
+ return { ...result, ...optionalFields };
178
182
  }
179
183
  exports.fromApiVals = fromApiVals;
180
184
  function fromApiArray(vals, types) {
@@ -123,9 +123,9 @@ export declare class Contract extends Artifact {
123
123
  fromApiContractState(state: node.ContractState): ContractState<Fields>;
124
124
  static fromApiContractState(state: node.ContractState): ContractState;
125
125
  static ContractCreatedEventIndex: number;
126
- static ContractCreatedEvent: EventSig;
126
+ static ContractCreatedEvent: SystemEventSig;
127
127
  static ContractDestroyedEventIndex: number;
128
- static ContractDestroyedEvent: EventSig;
128
+ static ContractDestroyedEvent: SystemEventSig;
129
129
  static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined, txId: string): ContractEvent;
130
130
  fromApiTestContractResult(methodName: string, result: node.TestContractResult, txId: string): TestContractResult<unknown>;
131
131
  txParamsForDeployment<P extends Fields>(signer: SignerProvider, params: DeployContractParams<P>): Promise<SignDeployContractTxParams>;
@@ -257,11 +257,16 @@ export interface CallContractResult<R> {
257
257
  txOutputs: Output[];
258
258
  events: ContractEvent[];
259
259
  }
260
+ export interface SystemEventSig extends EventSig {
261
+ optionalFieldNames?: string[];
262
+ optionalFieldTypes?: string[];
263
+ }
260
264
  export declare type ContractCreatedEvent = ContractEvent<{
261
- address: HexString;
265
+ address: Address;
266
+ parentAddress?: Address;
262
267
  }>;
263
268
  export declare type ContractDestroyedEvent = ContractEvent<{
264
- address: HexString;
269
+ address: Address;
265
270
  }>;
266
271
  export declare function decodeContractCreatedEvent(event: node.ContractEvent): Omit<ContractCreatedEvent, 'contractAddress'>;
267
272
  export declare function decodeContractDestroyedEvent(event: node.ContractEvent): Omit<ContractDestroyedEvent, 'contractAddress'>;
@@ -579,24 +579,29 @@ class Contract extends Artifact {
579
579
  return contract.fromApiContractState(state);
580
580
  }
581
581
  static fromApiEvent(event, codeHash, txId) {
582
- let eventSig;
582
+ let fields;
583
+ let name;
583
584
  if (event.eventIndex == Contract.ContractCreatedEventIndex) {
584
- eventSig = this.ContractCreatedEvent;
585
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent);
586
+ name = Contract.ContractCreatedEvent.name;
585
587
  }
586
588
  else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
587
- eventSig = this.ContractDestroyedEvent;
589
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent);
590
+ name = Contract.ContractDestroyedEvent.name;
588
591
  }
589
592
  else {
590
593
  const contract = Project.currentProject.contractByCodeHash(codeHash);
591
- eventSig = contract.eventsSig[event.eventIndex];
594
+ const eventSig = contract.eventsSig[event.eventIndex];
595
+ fields = fromApiEventFields(event.fields, eventSig);
596
+ name = eventSig.name;
592
597
  }
593
598
  return {
594
599
  txId: txId,
595
600
  blockHash: event.blockHash,
596
601
  contractAddress: event.contractAddress,
597
- name: eventSig.name,
602
+ name: name,
598
603
  eventIndex: event.eventIndex,
599
- fields: fromApiEventFields(event.fields, eventSig)
604
+ fields: fields
600
605
  };
601
606
  }
602
607
  fromApiTestContractResult(methodName, result, txId) {
@@ -678,7 +683,9 @@ Contract.ContractCreatedEventIndex = -1;
678
683
  Contract.ContractCreatedEvent = {
679
684
  name: 'ContractCreated',
680
685
  fieldNames: ['address'],
681
- fieldTypes: ['Address']
686
+ fieldTypes: ['Address'],
687
+ optionalFieldNames: ['parentAddress'],
688
+ optionalFieldTypes: ['Address']
682
689
  };
683
690
  Contract.ContractDestroyedEventIndex = -2;
684
691
  Contract.ContractDestroyedEvent = {
@@ -758,6 +765,9 @@ function fromApiFields(immFields, mutFields, fieldsSig) {
758
765
  function fromApiEventFields(vals, eventSig) {
759
766
  return (0, api_1.fromApiVals)(vals, eventSig.fieldNames, eventSig.fieldTypes);
760
767
  }
768
+ function fromApiSystemEventFields(vals, systemEventSig) {
769
+ return (0, api_1.fromApiVals)(vals, systemEventSig.fieldNames, systemEventSig.fieldTypes, systemEventSig.optionalFieldNames ?? [], systemEventSig.optionalFieldTypes ?? []);
770
+ }
761
771
  function toApiAsset(asset) {
762
772
  return {
763
773
  attoAlphAmount: (0, api_1.toApiNumber256)(asset.alphAmount),
@@ -872,25 +882,28 @@ class ContractFactory {
872
882
  }
873
883
  }
874
884
  exports.ContractFactory = ContractFactory;
875
- function decodeFields(event, eventSig, eventIndex) {
885
+ function decodeSystemEvent(event, systemEventSig, eventIndex) {
876
886
  if (event.eventIndex !== eventIndex) {
877
887
  throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
878
888
  }
879
- return (0, api_1.fromApiVals)(event.fields, eventSig.fieldNames, eventSig.fieldTypes);
889
+ return fromApiSystemEventFields(event.fields, systemEventSig);
880
890
  }
881
891
  function decodeContractCreatedEvent(event) {
882
- const fields = decodeFields(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
892
+ const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
883
893
  return {
884
894
  blockHash: event.blockHash,
885
895
  txId: event.txId,
886
896
  eventIndex: event.eventIndex,
887
897
  name: Contract.ContractCreatedEvent.name,
888
- fields: { address: fields['address'] }
898
+ fields: {
899
+ address: fields['address'],
900
+ parentAddress: fields['parentAddress'] === undefined ? undefined : fields['parentAddress']
901
+ }
889
902
  };
890
903
  }
891
904
  exports.decodeContractCreatedEvent = decodeContractCreatedEvent;
892
905
  function decodeContractDestroyedEvent(event) {
893
- const fields = decodeFields(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
906
+ const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
894
907
  return {
895
908
  blockHash: event.blockHash,
896
909
  txId: event.txId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.5.0-rc.4",
3
+ "version": "0.5.0-rc.6",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
package/src/api/types.ts CHANGED
@@ -162,7 +162,13 @@ function _fromApiVal(vals: node.Val[], valIndex: number, tpe: string): [result:
162
162
  }
163
163
  }
164
164
 
165
- export function fromApiVals(vals: node.Val[], names: string[], types: string[]): NamedVals {
165
+ export function fromApiVals(
166
+ vals: node.Val[],
167
+ names: string[],
168
+ types: string[],
169
+ optionalNames: string[] = [],
170
+ optionalTypes: string[] = []
171
+ ): NamedVals {
166
172
  let valIndex = 0
167
173
  const result: NamedVals = {}
168
174
  types.forEach((currentType, index) => {
@@ -171,7 +177,11 @@ export function fromApiVals(vals: node.Val[], names: string[], types: string[]):
171
177
  valIndex = nextIndex
172
178
  result[`${currentName}`] = val
173
179
  })
174
- return result
180
+ if (valIndex === vals.length) {
181
+ return result
182
+ }
183
+ const optionalFields = fromApiVals(vals.slice(valIndex), optionalNames, optionalTypes)
184
+ return { ...result, ...optionalFields }
175
185
  }
176
186
 
177
187
  export function fromApiArray(vals: node.Val[], types: string[]): Val[] {
@@ -848,38 +848,45 @@ export class Contract extends Artifact {
848
848
  }
849
849
 
850
850
  static ContractCreatedEventIndex = -1
851
- static ContractCreatedEvent: EventSig = {
851
+ static ContractCreatedEvent: SystemEventSig = {
852
852
  name: 'ContractCreated',
853
853
  fieldNames: ['address'],
854
- fieldTypes: ['Address']
854
+ fieldTypes: ['Address'],
855
+ optionalFieldNames: ['parentAddress'],
856
+ optionalFieldTypes: ['Address']
855
857
  }
856
858
 
857
859
  static ContractDestroyedEventIndex = -2
858
- static ContractDestroyedEvent: EventSig = {
860
+ static ContractDestroyedEvent: SystemEventSig = {
859
861
  name: 'ContractDestroyed',
860
862
  fieldNames: ['address'],
861
863
  fieldTypes: ['Address']
862
864
  }
863
865
 
864
866
  static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined, txId: string): ContractEvent {
865
- let eventSig: EventSig
867
+ let fields: Fields
868
+ let name: string
866
869
 
867
870
  if (event.eventIndex == Contract.ContractCreatedEventIndex) {
868
- eventSig = this.ContractCreatedEvent
871
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent)
872
+ name = Contract.ContractCreatedEvent.name
869
873
  } else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
870
- eventSig = this.ContractDestroyedEvent
874
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent)
875
+ name = Contract.ContractDestroyedEvent.name
871
876
  } else {
872
877
  const contract = Project.currentProject.contractByCodeHash(codeHash!)
873
- eventSig = contract.eventsSig[event.eventIndex]
878
+ const eventSig = contract.eventsSig[event.eventIndex]
879
+ fields = fromApiEventFields(event.fields, eventSig)
880
+ name = eventSig.name
874
881
  }
875
882
 
876
883
  return {
877
884
  txId: txId,
878
885
  blockHash: event.blockHash,
879
886
  contractAddress: event.contractAddress,
880
- name: eventSig.name,
887
+ name: name,
881
888
  eventIndex: event.eventIndex,
882
- fields: fromApiEventFields(event.fields, eventSig)
889
+ fields: fields
883
890
  }
884
891
  }
885
892
 
@@ -1095,6 +1102,16 @@ function fromApiEventFields(vals: node.Val[], eventSig: node.EventSig): Fields {
1095
1102
  return fromApiVals(vals, eventSig.fieldNames, eventSig.fieldTypes)
1096
1103
  }
1097
1104
 
1105
+ function fromApiSystemEventFields(vals: node.Val[], systemEventSig: SystemEventSig): Fields {
1106
+ return fromApiVals(
1107
+ vals,
1108
+ systemEventSig.fieldNames,
1109
+ systemEventSig.fieldTypes,
1110
+ systemEventSig.optionalFieldNames ?? [],
1111
+ systemEventSig.optionalFieldTypes ?? []
1112
+ )
1113
+ }
1114
+
1098
1115
  export interface Asset {
1099
1116
  alphAmount: Number256
1100
1117
  tokens?: Token[]
@@ -1339,37 +1356,45 @@ export interface CallContractResult<R> {
1339
1356
  events: ContractEvent[]
1340
1357
  }
1341
1358
 
1342
- export type ContractCreatedEvent = ContractEvent<{ address: HexString }>
1343
- export type ContractDestroyedEvent = ContractEvent<{ address: HexString }>
1359
+ export interface SystemEventSig extends EventSig {
1360
+ optionalFieldNames?: string[]
1361
+ optionalFieldTypes?: string[]
1362
+ }
1363
+
1364
+ export type ContractCreatedEvent = ContractEvent<{ address: Address; parentAddress?: Address }>
1365
+ export type ContractDestroyedEvent = ContractEvent<{ address: Address }>
1344
1366
 
1345
- function decodeFields(event: node.ContractEvent, eventSig: EventSig, eventIndex: number): Fields {
1367
+ function decodeSystemEvent(event: node.ContractEvent, systemEventSig: SystemEventSig, eventIndex: number): Fields {
1346
1368
  if (event.eventIndex !== eventIndex) {
1347
1369
  throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`)
1348
1370
  }
1349
- return fromApiVals(event.fields, eventSig.fieldNames, eventSig.fieldTypes)
1371
+ return fromApiSystemEventFields(event.fields, systemEventSig)
1350
1372
  }
1351
1373
 
1352
1374
  export function decodeContractCreatedEvent(event: node.ContractEvent): Omit<ContractCreatedEvent, 'contractAddress'> {
1353
- const fields = decodeFields(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex)
1375
+ const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex)
1354
1376
  return {
1355
1377
  blockHash: event.blockHash,
1356
1378
  txId: event.txId,
1357
1379
  eventIndex: event.eventIndex,
1358
1380
  name: Contract.ContractCreatedEvent.name,
1359
- fields: { address: fields['address'] as HexString }
1381
+ fields: {
1382
+ address: fields['address'] as Address,
1383
+ parentAddress: fields['parentAddress'] === undefined ? undefined : (fields['parentAddress'] as Address)
1384
+ }
1360
1385
  }
1361
1386
  }
1362
1387
 
1363
1388
  export function decodeContractDestroyedEvent(
1364
1389
  event: node.ContractEvent
1365
1390
  ): Omit<ContractDestroyedEvent, 'contractAddress'> {
1366
- const fields = decodeFields(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex)
1391
+ const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex)
1367
1392
  return {
1368
1393
  blockHash: event.blockHash,
1369
1394
  txId: event.txId,
1370
1395
  eventIndex: event.eventIndex,
1371
1396
  name: Contract.ContractDestroyedEvent.name,
1372
- fields: { address: fields['address'] as HexString }
1397
+ fields: { address: fields['address'] as Address }
1373
1398
  }
1374
1399
  }
1375
1400