Skip to content

Commit

Permalink
feat: 🎸 add tracking for asset required mediators
Browse files Browse the repository at this point in the history
also add `AssetMediatorsAdded` event into schema

✅ Closes: DA-1025

Signed-off-by: Eric Richardson <eric@polymesh.network>
  • Loading branch information
polymath-eric committed Feb 21, 2024
1 parent 2bd3fe2 commit 5c4a27f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions db/migrations/6_add_asset_mediators.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter type "8f5a39c8ee" add value if not exists 'AssetMediatorsAdded' after 'LocalMetadataKeyDeleted';
13 changes: 12 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ enum EventIdEnum {
RegisterAssetMetadataGlobalType
MetadataValueDeleted
LocalMetadataKeyDeleted
SetAssetMediators
AssetMediatorsAdded
SetAssetMediators # This can be removed, `AssetMediatorsAdded` is emitted instead
AssetMediatorsRemoved

## capitaldistribution ##
Expand Down Expand Up @@ -2119,6 +2120,7 @@ type Asset @entity {
compliance: [Compliance]! @derivedFrom(field: "asset")
transferManagers: [TransferManager]! @derivedFrom(field: "asset")
trustedClaimIssuers: [TrustedClaimIssuer]! @derivedFrom(field: "asset")
mandatoryMediators: [AssetMandatoryMediator]! @derivedFrom(field: "asset")
eventIdx: Int!
createdBlock: Block!
updatedBlock: Block!
Expand Down Expand Up @@ -2167,6 +2169,15 @@ type Funding @entity {
updatedBlock: Block!
}

type AssetMandatoryMediator @entity {
id: ID! # /ticker/did
asset: Asset!
identityId: String! # The chain doesn't validate existence of the DID
addedBy: Identity!
createdBlock: Block!
updatedBlock: Block!
}

"""
Represents the deployed version of the node indexing the data
"""
Expand Down
37 changes: 37 additions & 0 deletions src/mappings/entities/mapAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Asset,
AssetDocument,
AssetHolder,
AssetMandatoryMediator,
AssetTransaction,
CallIdEnum,
EventIdEnum,
Expand All @@ -23,6 +24,7 @@ import {
getDocValue,
getFirstKeyFromJson,
getFirstValueFromJson,
getStringArrayValue,
getNumberValue,
getPortfolioValue,
getSecurityIdentifiers,
Expand Down Expand Up @@ -575,6 +577,34 @@ const handleAssetBalanceUpdated = async ({
await Promise.all(promises);
};

const handleAssetMediatorsAdded = async ({ blockId, params }: HandlerArgs) => {
const addedById = getTextValue(params[0]);
const ticker = serializeTicker(params[1]);
const mediators = getStringArrayValue(params[2]);

const createPromises = mediators.map(mediator =>
AssetMandatoryMediator.create({
id: `${ticker}/${mediator}`,
identityId: mediator,
assetId: ticker,
addedById,
createdBlockId: blockId,
updatedBlockId: blockId,
}).save()
);

await Promise.all(createPromises);
};

const handleAssetMediatorsRemoved = async ({ params }: HandlerArgs) => {
const ticker = serializeTicker(params[1]);
const mediators = getStringArrayValue(params[2]);

await Promise.all(
mediators.map(mediator => AssetMandatoryMediator.remove(`${ticker}/${mediator}`))
);
};

const handleAssetUpdateEvents = async ({
eventId,
blockId,
Expand Down Expand Up @@ -631,6 +661,13 @@ export async function mapAsset(args: HandlerArgs): Promise<void> {
if (eventId === EventIdEnum.AssetBalanceUpdated) {
await handleAssetBalanceUpdated(args);
}
if (eventId === EventIdEnum.AssetMediatorsAdded) {
await handleAssetMediatorsAdded(args);
}
if (eventId === EventIdEnum.AssetMediatorsRemoved) {
await handleAssetMediatorsRemoved(args);
}

await handleAssetUpdateEvents(args);
// Unhandled asset events - CustomAssetTypeRegistered, CustomAssetTypeRegistered, ExtensionRemoved, IsIssueable, TickerRegistered, TickerTransferred, TransferWithData
}
6 changes: 6 additions & 0 deletions src/mappings/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,9 @@ export const getMultiSigSigner = (
signerValue,
};
};

export const getStringArrayValue = (item: Codec): string[] => {
const set = JSON.parse(item.toString());

return set;
};

0 comments on commit 5c4a27f

Please sign in to comment.