Skip to content

Commit

Permalink
Merge pull request #13 from peaqnetwork/fix/1207669233005460_error-ev…
Browse files Browse the repository at this point in the history
…ent-handler

fix: block events and error handling
  • Loading branch information
jpgundrum authored Jul 1, 2024
2 parents 09bac62 + 3066a19 commit 2f15056
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
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

0 comments on commit 2f15056

Please sign in to comment.