Skip to content

Commit

Permalink
feat: add webhook support for retireMsa, revokeDelegation (#664)
Browse files Browse the repository at this point in the history
# Problem

`retireMsa` and `revokeDelegation` extrinsic submissions were not
properly linked to the webhook processing, hence no webhook
notifications were sent. This PR adds the hooks in to the webhook
processing so that notifications may be sent when these operations
complete.

Closes #617
  • Loading branch information
JoeCap08055 authored Oct 25, 2024
1 parent e3d64e7 commit 2c20e7a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export function createWebhookRsp<O extends PublishGraphKeysOpts, T extends Publi
msaId: string,
options: O,
): T;
export function createWebhookRsp<T extends RetireMsaWebhookRsp>(txStatus: ITxStatus, msaId: string): T;
export function createWebhookRsp<T extends RevokeDelegationWebhookRsp>(txStatus: ITxStatus, msaId: string): T;
export function createWebhookRsp(
{ type: transactionType, providerId, referenceId }: ITxStatus,
msaId: string,
options: TxWebhookOpts,
options?: TxWebhookOpts,
): TxWebhookRsp {
const response = {
transactionType,
Expand Down
23 changes: 23 additions & 0 deletions apps/account-worker/src/transaction_notifier/notifier.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export class TxnNotifierService
);
}
break;

case TransactionType.SIWF_SIGNUP:
{
const siwfTxnValues = await this.blockchainService.handleSIWFTxnResult(extrinsicEvents);
Expand All @@ -175,6 +176,7 @@ export class TxnNotifierService
);
}
break;

case TransactionType.ADD_KEY:
{
const publicKeyValues = this.blockchainService.handlePublishKeyTxResult(successEvent);
Expand All @@ -187,6 +189,7 @@ export class TxnNotifierService
this.logger.log(`Keys: Added the key ${response.newPublicKey} for msaId ${response.msaId}.`);
}
break;

case TransactionType.ADD_PUBLIC_KEY_AGREEMENT:
{
const itemizedPageUpdated =
Expand All @@ -200,6 +203,26 @@ export class TxnNotifierService
this.logger.log(`Keys: Added the graph key msaId ${response.msaId} and schemaId ${response.schemaId}.`);
}
break;

case TransactionType.RETIRE_MSA:
{
const msaRetired = this.blockchainService.handleRetireMsaTxResult(successEvent);
const response = createWebhookRsp(txStatus, msaRetired.msaId);
webhookResponse = response;
}
break;

case TransactionType.REVOKE_DELEGATION:
{
const delegationRevoked = this.blockchainService.handleRevokeDelegationTxResult(successEvent);
const response = createWebhookRsp(
{ ...txStatus, providerId: delegationRevoked.providerId },
delegationRevoked.msaId,
);
webhookResponse = response;
}
break;

default:
this.logger.error(`Unknown transaction type on job.data: ${txStatus.type}`);
break;
Expand Down

This file was deleted.

53 changes: 53 additions & 0 deletions libs/blockchain/src/blockchain-rpc-query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ interface ItemizedPageUpdated {
debugMsg: string;
}

interface MsaRetired {
msaId: string;
debugMsg: string;
}

interface DelegationRevoked {
msaId: string;
providerId: string;
debugMsg: string;
}

export interface ICapacityInfo {
providerId: string;
currentBlockNumber: number;
Expand Down Expand Up @@ -398,6 +409,28 @@ export class BlockchainRpcQueryService extends PolkadotApiService {
return this.api.tx.msa.revokeDelegationByDelegator(providerId);
}

/**
* Handles the DelegationRevoked transaction result events and extracts the MSA ID and Provider ID from the event data.
* @param {Event} event - The DelegationRevoked event
* @returns {DelegationRevoked} The revoked delegation information
*/
public handleRevokeDelegationTxResult(event: Event): DelegationRevoked {
// Grab the event data
if (event && this.api.events.msa.DelegationRevoked.is(event)) {
const providerId = event.data.providerId.toString();
const msaId = event.data.delegatorId.toString();

return {
msaId,
providerId,
debugMsg: `Delegation revoked for msaId: ${msaId}, providerId: ${providerId}`,
};
}

this.logger.error(`Expected DelegationRevoked event but found ${event}`);
return {} as DelegationRevoked;
}

public createAddPublicKeyToMsaPayload(payload: KeysRequestPayloadDto): any {
const msaIdU64 = this.api.createType('u64', payload.msaId);

Expand Down Expand Up @@ -572,6 +605,26 @@ export class BlockchainRpcQueryService extends PolkadotApiService {
return this.api.tx.msa.retireMsa();
}

/**
* Handles the MsaRetired transaction result events and extracts the retired MSA ID from the event data.
* @param {Event} event - The MsaRetired event
* @returns {MsaRetired} The MSA ID that was retired
*/
public handleRetireMsaTxResult(event: Event): MsaRetired {
// Grab the event data
if (event && this.api.events.msa.MsaRetired.is(event)) {
const msaId = event.data.msaId.toString();

return {
msaId,
debugMsg: `MSA retired for msaId: ${msaId}`,
};
}

this.logger.error(`Expected MsaRetired event but found ${event}`);
return {} as MsaRetired;
}

public upsertPage(
msaId: AnyNumber,
schemaId: AnyNumber,
Expand Down

0 comments on commit 2c20e7a

Please sign in to comment.