@alephium/web3 0.5.0-rc.3 → 0.5.0-rc.5

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'>;
@@ -55,7 +55,6 @@ const global_1 = require("../global");
55
55
  const path = __importStar(require("path"));
56
56
  const events_1 = require("./events");
57
57
  const constants_1 = require("../constants");
58
- const codegen_1 = require("./codegen");
59
58
  var SourceKind;
60
59
  (function (SourceKind) {
61
60
  SourceKind[SourceKind["Contract"] = 0] = "Contract";
@@ -293,7 +292,6 @@ class Project {
293
292
  const projectArtifact = Project.buildProjectArtifact(sourceInfos, contracts, scripts, compilerOptions);
294
293
  const project = new Project(contractsRootDir, artifactsRootDir, sourceInfos, contracts, scripts, errorOnWarnings, projectArtifact);
295
294
  await project.saveArtifactsToFile(projectRootDir);
296
- (0, codegen_1.codegen)(project);
297
295
  return project;
298
296
  }
299
297
  static async loadArtifacts(provider, sourceInfos, projectArtifact, projectRootDir, contractsRootDir, artifactsRootDir, errorOnWarnings, compilerOptions) {
@@ -581,24 +579,29 @@ class Contract extends Artifact {
581
579
  return contract.fromApiContractState(state);
582
580
  }
583
581
  static fromApiEvent(event, codeHash, txId) {
584
- let eventSig;
582
+ let fields;
583
+ let name;
585
584
  if (event.eventIndex == Contract.ContractCreatedEventIndex) {
586
- eventSig = this.ContractCreatedEvent;
585
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent);
586
+ name = Contract.ContractCreatedEvent.name;
587
587
  }
588
588
  else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
589
- eventSig = this.ContractDestroyedEvent;
589
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent);
590
+ name = Contract.ContractDestroyedEvent.name;
590
591
  }
591
592
  else {
592
593
  const contract = Project.currentProject.contractByCodeHash(codeHash);
593
- eventSig = contract.eventsSig[event.eventIndex];
594
+ const eventSig = contract.eventsSig[event.eventIndex];
595
+ fields = fromApiEventFields(event.fields, eventSig);
596
+ name = eventSig.name;
594
597
  }
595
598
  return {
596
599
  txId: txId,
597
600
  blockHash: event.blockHash,
598
601
  contractAddress: event.contractAddress,
599
- name: eventSig.name,
602
+ name: name,
600
603
  eventIndex: event.eventIndex,
601
- fields: fromApiEventFields(event.fields, eventSig)
604
+ fields: fields
602
605
  };
603
606
  }
604
607
  fromApiTestContractResult(methodName, result, txId) {
@@ -680,7 +683,9 @@ Contract.ContractCreatedEventIndex = -1;
680
683
  Contract.ContractCreatedEvent = {
681
684
  name: 'ContractCreated',
682
685
  fieldNames: ['address'],
683
- fieldTypes: ['Address']
686
+ fieldTypes: ['Address'],
687
+ optionalFieldNames: ['parentAddress'],
688
+ optionalFieldTypes: ['Address']
684
689
  };
685
690
  Contract.ContractDestroyedEventIndex = -2;
686
691
  Contract.ContractDestroyedEvent = {
@@ -760,6 +765,9 @@ function fromApiFields(immFields, mutFields, fieldsSig) {
760
765
  function fromApiEventFields(vals, eventSig) {
761
766
  return (0, api_1.fromApiVals)(vals, eventSig.fieldNames, eventSig.fieldTypes);
762
767
  }
768
+ function fromApiSystemEventFields(vals, systemEventSig) {
769
+ return (0, api_1.fromApiVals)(vals, systemEventSig.fieldNames, systemEventSig.fieldTypes, systemEventSig.optionalFieldNames ?? [], systemEventSig.optionalFieldTypes ?? []);
770
+ }
763
771
  function toApiAsset(asset) {
764
772
  return {
765
773
  attoAlphAmount: (0, api_1.toApiNumber256)(asset.alphAmount),
@@ -874,25 +882,28 @@ class ContractFactory {
874
882
  }
875
883
  }
876
884
  exports.ContractFactory = ContractFactory;
877
- function decodeFields(event, eventSig, eventIndex) {
885
+ function decodeSystemEvent(event, systemEventSig, eventIndex) {
878
886
  if (event.eventIndex !== eventIndex) {
879
887
  throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`);
880
888
  }
881
- return (0, api_1.fromApiVals)(event.fields, eventSig.fieldNames, eventSig.fieldTypes);
889
+ return fromApiSystemEventFields(event.fields, systemEventSig);
882
890
  }
883
891
  function decodeContractCreatedEvent(event) {
884
- const fields = decodeFields(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
892
+ const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex);
885
893
  return {
886
894
  blockHash: event.blockHash,
887
895
  txId: event.txId,
888
896
  eventIndex: event.eventIndex,
889
897
  name: Contract.ContractCreatedEvent.name,
890
- fields: { address: fields['address'] }
898
+ fields: {
899
+ address: fields['address'],
900
+ parentAddress: fields['parentAddress'] === undefined ? undefined : fields['parentAddress']
901
+ }
891
902
  };
892
903
  }
893
904
  exports.decodeContractCreatedEvent = decodeContractCreatedEvent;
894
905
  function decodeContractDestroyedEvent(event) {
895
- const fields = decodeFields(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
906
+ const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex);
896
907
  return {
897
908
  blockHash: event.blockHash,
898
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.3",
3
+ "version": "0.5.0-rc.5",
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[] {
@@ -59,7 +59,6 @@ import { getCurrentNodeProvider } from '../global'
59
59
  import * as path from 'path'
60
60
  import { EventSubscription, subscribeToEvents } from './events'
61
61
  import { ONE_ALPH } from '../constants'
62
- import { codegen } from './codegen'
63
62
 
64
63
  export type FieldsSig = node.FieldsSig
65
64
  export type EventSig = node.EventSig
@@ -430,7 +429,6 @@ export class Project {
430
429
  projectArtifact
431
430
  )
432
431
  await project.saveArtifactsToFile(projectRootDir)
433
- codegen(project)
434
432
  return project
435
433
  }
436
434
 
@@ -850,38 +848,45 @@ export class Contract extends Artifact {
850
848
  }
851
849
 
852
850
  static ContractCreatedEventIndex = -1
853
- static ContractCreatedEvent: EventSig = {
851
+ static ContractCreatedEvent: SystemEventSig = {
854
852
  name: 'ContractCreated',
855
853
  fieldNames: ['address'],
856
- fieldTypes: ['Address']
854
+ fieldTypes: ['Address'],
855
+ optionalFieldNames: ['parentAddress'],
856
+ optionalFieldTypes: ['Address']
857
857
  }
858
858
 
859
859
  static ContractDestroyedEventIndex = -2
860
- static ContractDestroyedEvent: EventSig = {
860
+ static ContractDestroyedEvent: SystemEventSig = {
861
861
  name: 'ContractDestroyed',
862
862
  fieldNames: ['address'],
863
863
  fieldTypes: ['Address']
864
864
  }
865
865
 
866
866
  static fromApiEvent(event: node.ContractEventByTxId, codeHash: string | undefined, txId: string): ContractEvent {
867
- let eventSig: EventSig
867
+ let fields: Fields
868
+ let name: string
868
869
 
869
870
  if (event.eventIndex == Contract.ContractCreatedEventIndex) {
870
- eventSig = this.ContractCreatedEvent
871
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractCreatedEvent)
872
+ name = Contract.ContractCreatedEvent.name
871
873
  } else if (event.eventIndex == Contract.ContractDestroyedEventIndex) {
872
- eventSig = this.ContractDestroyedEvent
874
+ fields = fromApiSystemEventFields(event.fields, Contract.ContractDestroyedEvent)
875
+ name = Contract.ContractDestroyedEvent.name
873
876
  } else {
874
877
  const contract = Project.currentProject.contractByCodeHash(codeHash!)
875
- eventSig = contract.eventsSig[event.eventIndex]
878
+ const eventSig = contract.eventsSig[event.eventIndex]
879
+ fields = fromApiEventFields(event.fields, eventSig)
880
+ name = eventSig.name
876
881
  }
877
882
 
878
883
  return {
879
884
  txId: txId,
880
885
  blockHash: event.blockHash,
881
886
  contractAddress: event.contractAddress,
882
- name: eventSig.name,
887
+ name: name,
883
888
  eventIndex: event.eventIndex,
884
- fields: fromApiEventFields(event.fields, eventSig)
889
+ fields: fields
885
890
  }
886
891
  }
887
892
 
@@ -1097,6 +1102,16 @@ function fromApiEventFields(vals: node.Val[], eventSig: node.EventSig): Fields {
1097
1102
  return fromApiVals(vals, eventSig.fieldNames, eventSig.fieldTypes)
1098
1103
  }
1099
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
+
1100
1115
  export interface Asset {
1101
1116
  alphAmount: Number256
1102
1117
  tokens?: Token[]
@@ -1341,37 +1356,45 @@ export interface CallContractResult<R> {
1341
1356
  events: ContractEvent[]
1342
1357
  }
1343
1358
 
1344
- export type ContractCreatedEvent = ContractEvent<{ address: HexString }>
1345
- 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 }>
1346
1366
 
1347
- function decodeFields(event: node.ContractEvent, eventSig: EventSig, eventIndex: number): Fields {
1367
+ function decodeSystemEvent(event: node.ContractEvent, systemEventSig: SystemEventSig, eventIndex: number): Fields {
1348
1368
  if (event.eventIndex !== eventIndex) {
1349
1369
  throw new Error(`Invalid event index: ${event.eventIndex}, expected: ${eventIndex}`)
1350
1370
  }
1351
- return fromApiVals(event.fields, eventSig.fieldNames, eventSig.fieldTypes)
1371
+ return fromApiSystemEventFields(event.fields, systemEventSig)
1352
1372
  }
1353
1373
 
1354
1374
  export function decodeContractCreatedEvent(event: node.ContractEvent): Omit<ContractCreatedEvent, 'contractAddress'> {
1355
- const fields = decodeFields(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex)
1375
+ const fields = decodeSystemEvent(event, Contract.ContractCreatedEvent, Contract.ContractCreatedEventIndex)
1356
1376
  return {
1357
1377
  blockHash: event.blockHash,
1358
1378
  txId: event.txId,
1359
1379
  eventIndex: event.eventIndex,
1360
1380
  name: Contract.ContractCreatedEvent.name,
1361
- 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
+ }
1362
1385
  }
1363
1386
  }
1364
1387
 
1365
1388
  export function decodeContractDestroyedEvent(
1366
1389
  event: node.ContractEvent
1367
1390
  ): Omit<ContractDestroyedEvent, 'contractAddress'> {
1368
- const fields = decodeFields(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex)
1391
+ const fields = decodeSystemEvent(event, Contract.ContractDestroyedEvent, Contract.ContractDestroyedEventIndex)
1369
1392
  return {
1370
1393
  blockHash: event.blockHash,
1371
1394
  txId: event.txId,
1372
1395
  eventIndex: event.eventIndex,
1373
1396
  name: Contract.ContractDestroyedEvent.name,
1374
- fields: { address: fields['address'] as HexString }
1397
+ fields: { address: fields['address'] as Address }
1375
1398
  }
1376
1399
  }
1377
1400
 
@@ -1,2 +0,0 @@
1
- import { Project } from './contract';
2
- export declare function codegen(project: Project): void;