Skip to content

Commit

Permalink
feat: 🎸 track pending instruction mediators
Browse files Browse the repository at this point in the history
✅ Closes: DA-1100

Signed-off-by: Marcin Pastecki <marcin.pastecki@inndei.com>
  • Loading branch information
polymath-eric authored and mpastecki committed Apr 29, 2024
1 parent e334a82 commit b0ef404
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
10 changes: 10 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2393,13 +2393,23 @@ type CustomClaimType @entity {
updatedBlock: Block!
}

"""
Represents the state of an instruction mediator's affirmation
"""
enum MediatorAffirmationStatus {
Pending
Affirmed
Rejected
}

"""
Represents a mediator's approval for an instruction
"""
type MeditatorAffirmation @entity {
id: ID! # did/instruction ID
identity: Identity!
instruction: Instruction!
status: MediatorAffirmationStatus!
"""
If expiry is present the time should be check top ensure the affirmation is still valid
"""
Expand Down
60 changes: 46 additions & 14 deletions src/mappings/entities/mapSettlement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Instruction,
InstructionStatusEnum,
Leg,
MediatorAffirmationStatus,
MeditatorAffirmation,
ModuleIdEnum,
Settlement,
Expand All @@ -21,6 +22,7 @@ import {
getLegsValue,
getSettlementLeg,
getSignerAddress,
getStringArrayValue,
getTextValue,
} from '../util';
import { createPortfolioIfNotExists } from './mapPortfolio';
Expand Down Expand Up @@ -262,9 +264,11 @@ const handleInstructionFinalizedEvent = async (
const clearMediatorAffirmation = async () => {
const identityId = getTextValue(rawIdentityId);
const id = `${identityId}/${instructionId}`;
const affirmedMediator = await MeditatorAffirmation.get(id);
if (affirmedMediator) {
await MeditatorAffirmation.remove(id);
const mediatorAffirmation = await MeditatorAffirmation.get(id);
if (mediatorAffirmation) {
mediatorAffirmation.status = MediatorAffirmationStatus.Rejected;
mediatorAffirmation.updatedBlockId = blockId;
await mediatorAffirmation.save();
}
};

Expand Down Expand Up @@ -297,22 +301,46 @@ const handleMediatorAffirmation = async (blockId: string, params: Codec[]): Prom
const instructionId = getTextValue(rawInstructionId);
const expiry = getDateValue(expiryOpt);

await MeditatorAffirmation.create({
id: `${identityId}/${instructionId}`,
identityId,
instructionId,
expiry,
createdBlockId: blockId,
updatedBlockId: blockId,
}).save();
const affirmation = await MeditatorAffirmation.get(`${identityId}/${instructionId}`);

affirmation.status = MediatorAffirmationStatus.Affirmed;
affirmation.expiry = expiry;
affirmation.updatedBlockId = blockId;

await affirmation.save();
};

const handleMediatorWithdrawn = async (params: Codec[]): Promise<void> => {
const handleMediatorWithdrawn = async (blockId: string, params: Codec[]): Promise<void> => {
const [rawIdentityId, rawInstructionId] = params;
const identityId = getTextValue(rawIdentityId);
const instructionId = getTextValue(rawInstructionId);

await MeditatorAffirmation.remove(`${identityId}/${instructionId}`);
const affirmation = await MeditatorAffirmation.get(`${identityId}/${instructionId}`);

affirmation.status = MediatorAffirmationStatus.Pending;
affirmation.expiry = undefined;
affirmation.updatedBlockId = blockId;

await affirmation.save();
};

const handleInstructionMediators = async (blockId: string, params: Codec[]): Promise<void> => {
const [rawInstructionId, rawIdentityIds] = params;
const instructionId = getTextValue(rawInstructionId);
const identityIds = getStringArrayValue(rawIdentityIds);

const promises = identityIds.map(identityId => {
return MeditatorAffirmation.create({
id: `${identityId}/${instructionId}`,
identityId,
instructionId,
status: MediatorAffirmationStatus.Pending,
createdBlockId: blockId,
updatedBlockId: blockId,
}).save();
});

await Promise.all(promises);
};

export async function mapSettlement(args: HandlerArgs): Promise<void> {
Expand All @@ -339,7 +367,11 @@ export async function mapSettlement(args: HandlerArgs): Promise<void> {
}

if (eventId === EventIdEnum.MediatorAffirmationWithdrawn) {
await handleMediatorWithdrawn(params);
await handleMediatorWithdrawn(blockId, params);
}

if (eventId === EventIdEnum.InstructionMediators) {
await handleInstructionMediators(blockId, params);
}

if (updateEvents.includes(eventId)) {
Expand Down

0 comments on commit b0ef404

Please sign in to comment.