Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update events data #753

Merged
merged 4 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
800 changes: 396 additions & 404 deletions api/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"license": "GPL-3.0",
"peerDependencies": {
"@polkadot/api": "^8.6.1",
"@polkadot/api": "^8.9.1",
"@polkadot/wasm-crypto": "^6.1.1",
"rxjs": "^7.5.5"
},
Expand Down
2 changes: 1 addition & 1 deletion api/src/DebugMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class DebugMode extends GearTransaction {
return this.api.query.system.events((events) => {
events
.filter(({ event }) => this.api.events.gearDebug.DebugDataSnapshot.is(event))
.forEach(({ event }) => callback(new DebugDataSnapshot(event)));
.forEach(({ event }) => callback(event as DebugDataSnapshot));
});
}
}
9 changes: 5 additions & 4 deletions api/src/GearApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Event } from '@polkadot/types/interfaces';
import { SpRuntimeDispatchError } from '@polkadot/types/lookup';
import { RegistryError } from '@polkadot/types-codec/types';
import { GearProgram } from './Program';
import { GearMessage } from './Message';
import { GearBalance } from './Balance';
Expand All @@ -7,14 +11,11 @@ import { GearMessageReply } from './MessageReply';
import { GearWaitlist } from './Waitlist';
import { gearRpc, gearTypes } from './default';
import { GearApiOptions } from './types/interfaces';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { ContractExecResultErr, Event } from '@polkadot/types/interfaces';
import { GearBlock } from './Blocks';
import { GearStorage } from './Storage';
import { GearMailbox } from './Mailbox';
import { GearClaimValue } from './Claim';
import { GearCode } from './Code';
import { RegistryError } from '@polkadot/types-codec/types';

export class GearApi extends ApiPromise {
public program: GearProgram;
Expand Down Expand Up @@ -93,7 +94,7 @@ export class GearApi extends ApiPromise {
* @returns
*/
getExtrinsicFailedError(event: Event): RegistryError {
const error = event.data[0] as ContractExecResultErr;
const error = event.data[0] as SpRuntimeDispatchError;

const { isModule, asModule } = error;
return isModule ? this.registry.findMetaError(asModule) : null;
Expand Down
5 changes: 2 additions & 3 deletions api/src/events/Events.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { UnsubscribePromise } from '@polkadot/api/types';
import { GearApi } from '../GearApi';
import { ISystemAccountInfo, IBalanceCallback, IBlocksCallback } from '../types/interfaces';
import { createEventClass } from './GearEvents';
import { Transfer } from './GearEvents';
import { IGearEvent } from './types';
import { Transfer } from './GearEvents';

export class GearEvents {
private api: GearApi;
Expand All @@ -20,7 +19,7 @@ export class GearEvents {
events
.filter(({ event }) => event.method === method)
.forEach(({ event }) => {
callback(createEventClass(method, event));
callback(event as IGearEvent[M]);
});
});
}
Expand Down
149 changes: 41 additions & 108 deletions api/src/events/GearEventData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,132 +21,65 @@ export class GearEventData extends GenericEventData {
}
}

export class MessageEnqueuedData extends GearEventData {
public get id(): MessageId {
return this[0] as MessageId;
}

public get source(): UserId {
return this[1] as UserId;
}

public get destination(): ProgramId {
return this[2] as ProgramId;
}

public get entry(): Entry {
return this[3] as Entry;
}
export interface MessageEnqueuedData extends GenericEventData {
id: MessageId;
source: UserId;
destination: ProgramId;
entry: Entry;
}

export class UserMessageSentData extends GearEventData {
public get id(): MessageId {
return this[0]['id'];
}
public get source(): ProgramId {
return this[0]['source'];
}
public get destination(): UserId {
return this[0]['destination'];
}
public get payload(): Vec<u8> {
return this[0]['payload'];
}
public get value(): u128 {
return this[0]['value'];
}
public get reply(): Option<Reply> {
return this[0]['reply'];
}
public get expiration(): BlockNumber {
return this[1] as BlockNumber;
}
export interface UserMessageSentData extends GenericEventData {
message: {
id: MessageId;
source: ProgramId;
destination: UserId;
payload: Vec<u8>;
value: u128;
reply: Option<Reply>;
};
expiration: BlockNumber;
}

export class UserMessageReadData extends GearEventData {
public get id(): MessageId {
return this[0] as MessageId;
}

public get reason(): UserMessageReadReason {
return this[1] as UserMessageReadReason;
}
export interface UserMessageReadData extends GenericEventData {
id: MessageId;
reason: UserMessageReadReason;
}

export class MessagesDispatchedData extends GearEventData {
public get total(): u32 {
return this[0] as u32;
}

public get statuses(): BTreeMap<MessageId, DispatchStatus> {
return this[1] as BTreeMap<MessageId, DispatchStatus>;
}

public get stateChanged(): BTreeSet<ProgramId> {
return this[2] as BTreeSet<ProgramId>;
}
export interface MessagesDispatchedData extends GenericEventData {
total: u32;
statuses: BTreeMap<MessageId, DispatchStatus>;
stateChanged: BTreeSet<ProgramId>;
}

export class MessageWaitedData extends GearEventData {
public get id(): MessageId {
return this[0] as MessageId;
}

public get origin(): Option<MessageId> {
return this[1] as Option<MessageId>;
}

public get reason(): MessageWaitedReason {
return this[2] as MessageWaitedReason;
}

public get expiration(): BlockNumber {
return this[3] as BlockNumber;
}
export interface MessageWaitedData extends GenericEventData {
id: MessageId;
origin: Option<MessageId>;
reason: MessageWaitedReason;
expiration: BlockNumber;
}

export class MessageWokenData extends GearEventData {
public get id(): MessageId {
return this[0] as MessageId;
}

public get reason(): MessageWokenReason {
return this[1] as MessageWokenReason;
}
export interface MessageWakenData extends GenericEventData {
id: MessageId;
reason: MessageWokenReason;
}

export class CodeChangedData extends GearEventData {
public get id(): CodeId {
return this[0] as CodeId;
}
public get change(): CodeChangeKind {
return this[1] as CodeChangeKind;
}
export interface CodeChangedData extends GenericEventData {
id: CodeId;
change: CodeChangeKind;
}

export class ProgramChangedData extends GearEventData {
public get id(): ProgramId {
return this[0] as ProgramId;
}
public get change(): ProgramChangedKind {
return this[1] as ProgramChangedKind;
}
export interface ProgramChangedData extends GenericEventData {
id: ProgramId;
change: ProgramChangedKind;
}

export class DebugData extends GearEventData {
public get dispatchQueue(): Vec<QueuedDispatch> {
return this[0]['dispatchQueue'];
}

public get programs(): Vec<ProgramDetails> {
return this[0]['programs'];
}
export interface DebugData extends GenericEventData {
dispatchQueue: Vec<QueuedDispatch>;
programs: Vec<ProgramDetails>;
}

export class DebugModeData extends GearEventData {
public get enabled(): Bool {
return this[0] as Bool;
}
export interface DebugModeData extends GenericEventData {
enabled: Bool;
}

export class TransferData extends GearEventData {
Expand Down
90 changes: 13 additions & 77 deletions api/src/events/GearEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,37 @@ import {
MessageEnqueuedData,
MessagesDispatchedData,
MessageWaitedData,
MessageWokenData,
ProgramChangedData,
TransferData,
UserMessageReadData,
UserMessageSentData,
DebugModeData,
MessageWakenData,
} from './GearEventData';
import { IGearEvent } from './types';

export class GearGenericEvent extends GenericEvent {
constructor(event: Event) {
super(event.registry, event.toU8a());
}
export interface GearEvent<D extends GenericEventData> extends GenericEvent {
data: D;
}

export class MessageEnqueued extends GearGenericEvent {
public get data(): MessageEnqueuedData {
return new MessageEnqueuedData(this.getT('data'));
}
}
export interface MessageEnqueued extends GearEvent<MessageEnqueuedData> {}

export class UserMessageSent extends GearGenericEvent {
public get data(): UserMessageSentData {
return new UserMessageSentData(this.getT('data'));
}
}
export interface UserMessageSent extends GearEvent<UserMessageSentData> {}

export class UserMessageRead extends GearGenericEvent {
public get data(): UserMessageReadData {
return new UserMessageReadData(this.getT('data'));
}
}
export interface UserMessageRead extends GearEvent<UserMessageReadData> {}

export class MessagesDispatched extends GearGenericEvent {
public get data(): MessagesDispatchedData {
return new MessagesDispatchedData(this.getT('data'));
}
}
export interface MessagesDispatched extends GearEvent<MessagesDispatchedData> {}

export class MessageWaited extends GearGenericEvent {
public get data(): MessageWaitedData {
return new MessageWaitedData(this.getT('data'));
}
}
export interface MessageWaited extends GearEvent<MessageWaitedData> {}

export class MessageWaken extends GearGenericEvent {
public get data(): MessageWokenData {
return new MessageWokenData(this.getT('data'));
}
}
export interface MessageWaken extends GearEvent<MessageWakenData> {}

export class CodeChanged extends GearGenericEvent {
public get data(): CodeChangedData {
return new CodeChangedData(this.getT('data'));
}
}
export interface CodeChanged extends GearEvent<CodeChangedData> {}

export class ProgramChanged extends GearGenericEvent {
public get data(): ProgramChangedData {
return new ProgramChangedData(this.getT('data'));
}
}
export interface ProgramChanged extends GearEvent<ProgramChangedData> {}

export class DebugDataSnapshot extends GearGenericEvent {
public get data(): DebugData {
return new DebugData(this.getT('data'));
}
}
export interface DebugDataSnapshot extends GearEvent<DebugData> {}

export class DebugMode extends GearGenericEvent {
public get data(): DebugModeData {
return new DebugModeData(this.getT('data'));
}
}
export interface DebugMode extends GearEvent<DebugModeData> {}

export class Transfer extends GenericEvent {
constructor(event: Event) {
Expand All @@ -90,24 +47,3 @@ export class Transfer extends GenericEvent {
return new TransferData(this.get('data') as GenericEventData);
}
}

const events = {
MessageEnqueued: MessageEnqueued,
UserMessageSent: UserMessageSent,
UserMessageRead: UserMessageRead,
MessagesDispatched: MessagesDispatched,
MessageWaited: MessageWaited,
MessageWaken: MessageWaken,
CodeChanged: CodeChanged,
ProgramChanged: ProgramChanged,
DebugDataSnapshot: DebugDataSnapshot,
DebugMode: DebugMode,
};

export function createEventClass<M extends keyof IGearEvent = keyof IGearEvent>(
method: M,
event: Event,
): IGearEvent[M] {
const class_ = new events[method](event) as IGearEvent[M];
return class_;
}
10 changes: 5 additions & 5 deletions api/test/Gas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ describe('Calculate gas', () => {
});
const waitForReply = listenToUserMessageSent(api, programId);
await sendTransaction(api.message, alice, 'MessageEnqueued');
const umsData = await waitForReply(null); //transactionData[0]);
expect(umsData.id).toBeDefined();
messageId = umsData.id.toHex();
expect(umsData.reply).toBeDefined();
expect(umsData.reply.isNone).toBeTruthy();
const { message } = await waitForReply(null); //transactionData[0]);
expect(message.id).toBeDefined();
messageId = message.id.toHex();
expect(message.reply).toBeDefined();
expect(message.reply.isNone).toBeTruthy();
exitCode = 0; //umsData.reply.unwrap()[1].toNumber();
expect(exitCode).toBeDefined();
});
Expand Down
Loading