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

fix: add events to current working branch #18

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# peaq-js

Follow [documnetion](https://docs.peaq.network/sdk) for detailed overveiw of feature and functionalites of peaq SDK.
Follow [documentation](https://docs.peaq.network/sdk) for a detailed overview of the features and functionalities of peaq SDK.
85 changes: 48 additions & 37 deletions packages/sdk/src/modules/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ import { AnyTuple } from '@polkadot/types-codec/types';
import { truncatedString } from '../../utils';

type TErrorData = {
Module?: {
index?: string;
error?: string;
};
dispatchError: {
Module?: {
index?: string;
error?: string;
};
}
dispatchInfo: {
weight?: {
refTime?: string,
proofSize?: string
},
class?: string,
paysFee?: string
}
};

export class Base {
Expand Down Expand Up @@ -90,17 +100,15 @@ export class Base {

protected async _transactionError(
method: string,
eventData: PeaqEventData[]
): Promise<{ documentation: string[]; name: string } | null> {
eventData: PeaqEventData
): Promise<{ documentation: string[]; name: string, section: string } | null> {
const failedEvent = method === 'ExtrinsicFailed';
const api = this._getApi();
if (failedEvent) {
const error = eventData.find((item) =>
item.lookupName.includes('DispatchError')
);
const error = eventData;
const errorData = error?.data?.toHuman?.() as TErrorData | undefined;
const errorIdx = errorData?.Module?.error;
const moduleIdx = errorData?.Module?.index;
const errorIdx = errorData?.dispatchError.Module?.error;
const moduleIdx = errorData?.dispatchError.Module?.index;
if (errorIdx && moduleIdx) {
try {
const decode = api.registry.findMetaError({
Expand All @@ -110,18 +118,21 @@ export class Base {
return {
documentation: decode.docs,
name: decode.name,
section: decode.section
};
} catch (error) {
return {
documentation: ['Unknown error'],
name: 'UnknownError',
section: 'UnknownSection'
};
}
}
} else {
return {
documentation: ['Unknown error'],
name: 'UnknownError',
section: 'UnknownSection'
};
}
return null;
Expand All @@ -133,7 +144,7 @@ export class Base {
const api = this._getApi();
let subscribed = false;

try {
try {
// Sign the transaction
await extrinsics.signAsync(address, { nonce });
} catch (error: any) {
Expand Down Expand Up @@ -169,6 +180,9 @@ export class Base {
const executionBlockStopNr = inclusionBlockNr.addn(10);
let executionBlockNr = executionBlockStartNr;

// save instance of current block hash for peaqEvent
const _inclusionBlockHash = inclusionBlockHash;

// Subscribe to new blocks
const unsubscribeNewHeads = await api.rpc.chain.subscribeNewHeads(
async (lastHeader) => {
Expand All @@ -194,10 +208,6 @@ export class Base {
const extinsics: GenericExtrinsic<AnyTuple>[] = (
await api.rpc.chain.getBlock(blockHeader.hash)
).block.extrinsics;
const apiAt = await api.at(blockHeader.hash);
const events: any = await apiAt.query?.['system']?.[
'events'
];

executionBlockNr.iaddn(1);

Expand All @@ -213,30 +223,31 @@ export class Base {
unsubscribeNewHeads();
}

const eventsTriggeredByTx: any = []
.map((eventRecord : any) => {
const { event, phase } = eventRecord;
const types = event.typeDef;
const eventData: PeaqEventData[] = event.data.map((d: any, i: any) => {
return {
lookupName: types[i].lookupName!,
data: d
};
});
const events = await result.events;
const peaqEvents = events.map(({ event, phase }) => {
const { data, method, section } = event;
const eventData: PeaqEventData = { lookupName: method, data: data };
return {
event,
phase,
section: event.section,
method: event.method,
metaDocumentation: event.meta.docs.toString(),
eventData,
error: {
documentation: [],
name: ""
}
event: event,
phase: phase,
section: section,
method: method,
eventData: [eventData],
blockHash: _inclusionBlockHash
} as PeaqEvent;
});
resolve(eventsTriggeredByTx);
// check for any failed extrinsics
const extrinsicFailedEvents = peaqEvents.filter(peaqEvent => peaqEvent.method === "ExtrinsicFailed");
if (extrinsicFailedEvents.length > 0) {
const eventData = extrinsicFailedEvents[0].eventData[0];
const errorResp = await this._transactionError(extrinsicFailedEvents[0].method, eventData);
reject(
new Error(
`${errorResp?.name} for ${errorResp?.section}.`
)
);
}
resolve(peaqEvents);
unsub();
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/modules/did/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ export class Did extends Base {
);

const nonce = await this._getNonce(keyPair.address);
await this._newSignTx({nonce, address: keyPair, extrinsics: attributeExtrinsic});
const eventData = await this._newSignTx({nonce, address: keyPair, extrinsics: attributeExtrinsic});
// await attributeExtrinsic.signAsync(keyPair, { nonce });
const unsubscribe = await attributeExtrinsic.send((result) => {
statusCallback &&
statusCallback(result as unknown as ISubmittableResult);
});

return {
hash: attributeExtrinsic.hash as unknown as CodecHash,
hash: eventData[0]?.blockHash as unknown as CodecHash,
unsubscribe,
};
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export interface PeaqEvent{
section: string;
method: string;
eventData: PeaqEventData[];
error: {
blockHash?: string,
error?: {
documentation: string[];
name: string
} | null
Expand Down
Loading