Skip to content

Commit

Permalink
feat: 🎸 implement new ca events
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Pastecki <marcin.pastecki@inndei.com>
  • Loading branch information
sansan authored and mpastecki committed Apr 18, 2024
1 parent 7ec1aa8 commit 0fc2b71
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
7 changes: 6 additions & 1 deletion project.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ const filters = {
'AccountDeposit',
'AccountDepositIncoming',
'AccountWithdraw',
'ConfidentialAssetCreated',
'AccountAssetFrozen',
'AccountAssetUnfrozen',
'AssetCreated',
'AssetFrozen',
'AssetUnfrozen',
'AssetBurned',
'Issued',
'TransactionAffirmed',
'TransactionCreated',
Expand Down
20 changes: 16 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,19 @@ enum EventIdEnum {

## confidential assets ##
AccountCreated
ConfidentialAssetCreated
# ConfidentialAssetCreated -> uses existing AssetCreated
AccountDeposit
TransactionCreated
TransactionAffirmed
AccountWithdraw
TransactionRejected
TransactionExecuted
AccountDepositIncoming
AccountAssetFrozen
AccountAssetUnfrozen
# AssetFrozen -> defined before
# AssetUnfrozen -> defined before
Burned

## deprecated ##
Approval @deprecated
Expand Down Expand Up @@ -1130,8 +1135,10 @@ enum CallIdEnum {

## confidential assets ##
add_mediator_account
create_confidential_asset
mint_confidential_asset
# create_confidential_asset
# create_asset -> removing as defined before
# mint_confidential_asset -> mint
mint
add_transaction
sender_affirm_transaction
receiver_affirm_transaction
Expand All @@ -1143,7 +1150,11 @@ enum CallIdEnum {
affirm_transactions
execute_transaction
apply_incoming_balance
apply_incoming_balances
burn
reject_transaction
set_account_asset_frozen
set_asset_frozen

## deprecated ##
accept_authorization @deprecated
Expand Down Expand Up @@ -2459,6 +2470,7 @@ type ConfidentialAsset @entity {
totalSupply: BigInt!
allowedVenues: [Int!]
venueFiltering: Boolean!
isFrozen: Boolean
eventIdx: Int!
createdBlock: Block!
updatedBlock: Block!
Expand Down Expand Up @@ -2489,7 +2501,7 @@ type ConfidentialTransactionAffirmation @entity {
legId: Int!
account: ConfidentialAccount
identity: Identity!
type: AffirmingPartyEnum!
party: AffirmingPartyEnum!
status: AffirmStatusEnum!
proofs: [SenderProof!]
eventIdx: Int!
Expand Down
36 changes: 31 additions & 5 deletions src/mappings/entities/mapConfidentialAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const handleConfidentialAssetCreated = async (
eventIdx: number,
params: Codec[]
): Promise<void> => {
const [rawCreator, rawAssetId, rawAuditorsMediators] = params;
const [rawCreator, rawAssetId, , rawAuditorsMediators] = params;

const creatorId = getTextValue(rawCreator);
const assetId = getTextValue(rawAssetId);
Expand All @@ -39,7 +39,10 @@ const handleConfidentialAssetCreated = async (
}).save();
};

const handleConfidentialAssetIssued = async (blockId: string, params: Codec[]): Promise<void> => {
const handleConfidentialAssetIssuedOrBurned = async (
blockId: string,
params: Codec[]
): Promise<void> => {
const [, rawAssetId, , rawTotalSupply] = params;

const assetId = getTextValue(rawAssetId);
Expand Down Expand Up @@ -123,19 +126,38 @@ const handleVenueFiltering = async (blockId: string, params: Codec[]): Promise<v
}
};

const handleAssetFrozenUnfrozen = async (
blockId: string,
eventId: EventIdEnum,
params: Codec[]
): Promise<void> => {
const [, rawAssetId] = params;

const assetId = getTextValue(rawAssetId);

const asset = await ConfidentialAsset.get(assetId);

if (asset) {
asset.isFrozen = eventId === EventIdEnum.AssetFrozen;
asset.updatedBlockId = blockId;

await asset.save();
}
};

export const mapConfidentialAsset = async (args: HandlerArgs): Promise<void> => {
const { blockId, moduleId, eventId, eventIdx, params } = args;

if (moduleId !== ModuleIdEnum.confidentialasset) {
return;
}

if (eventId === EventIdEnum.ConfidentialAssetCreated) {
if (eventId === EventIdEnum.AssetCreated) {
await handleConfidentialAssetCreated(blockId, eventIdx, params);
}

if (eventId === EventIdEnum.Issued) {
await handleConfidentialAssetIssued(blockId, params);
if (eventId === EventIdEnum.Issued || eventId === EventIdEnum.Burned) {
await handleConfidentialAssetIssuedOrBurned(blockId, params);
}

if (eventId === EventIdEnum.VenueFiltering) {
Expand All @@ -153,4 +175,8 @@ export const mapConfidentialAsset = async (args: HandlerArgs): Promise<void> =>
if (eventId === EventIdEnum.VenueCreated) {
await handleVenueCreated(blockId, eventIdx, params);
}

if (eventId === EventIdEnum.AssetFrozen || eventId === EventIdEnum.Unfrozen) {
handleAssetFrozenUnfrozen(blockId, eventId, params);
}
};
30 changes: 15 additions & 15 deletions src/mappings/entities/mapConfidentialTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,34 @@ const handleConfidentialTransactionExecutedOrRejected = async (
};

type AffirmationsAndAffirmationType = {
type: AffirmingPartyEnum;
party: AffirmingPartyEnum;
proofs?: SenderProof[];
};

const getAffirmationTypeAndProofs = (party: Codec): AffirmationsAndAffirmationType => {
const partyObject = party.toJSON();
const getAffirmationTypeAndProofs = (rawParty: Codec): AffirmationsAndAffirmationType => {
const partyObject = rawParty.toJSON();

const [typeString] = Object.keys(partyObject);

let type: AffirmingPartyEnum;
let party: AffirmingPartyEnum;

if (typeString.toLocaleLowerCase() === 'mediator') {
type = AffirmingPartyEnum.Mediator;
party = AffirmingPartyEnum.Mediator;
}
if (typeString.toLocaleLowerCase() === 'receiver') {
type = AffirmingPartyEnum.Receiver;
party = AffirmingPartyEnum.Receiver;
}
if (typeString.toLocaleLowerCase() === 'sender') {
type = AffirmingPartyEnum.Sender;
party = AffirmingPartyEnum.Sender;
}

if (!type) {
throw new Error('Could not extract type from affirmation');
if (!party) {
throw new Error('Could not extract party from affirmation');
}

let proofs: SenderProof[];

if (type === AffirmingPartyEnum.Sender) {
if (party === AffirmingPartyEnum.Sender) {
const proofsObject = partyObject[typeString].proofs;

proofs = Object.keys(proofsObject).map(assetId => {
Expand All @@ -149,28 +149,28 @@ const getAffirmationTypeAndProofs = (party: Codec): AffirmationsAndAffirmationTy
});
}

return { type, proofs };
return { party, proofs };
};

const handleConfidentialTransactionAffirmed = async (
blockId: string,
eventIdx: number,
params: Codec[]
): Promise<void> => {
const [rawDid, rawTransactionId, rawLegId, rawType, rawPendingAffirmations] = params;
const [rawDid, rawTransactionId, rawLegId, rawParty, rawPendingAffirmations] = params;

const did = getTextValue(rawDid);
const transactionId = getTextValue(rawTransactionId);
const legId = getNumberValue(rawLegId);
const { type, proofs } = getAffirmationTypeAndProofs(rawType);
const { party, proofs } = getAffirmationTypeAndProofs(rawParty);
const pendingAffirmations = getNumberValue(rawPendingAffirmations);

const affirmation = ConfidentialTransactionAffirmation.create({
id: `${transactionId}/${type}/${did}`,
id: `${transactionId}/${party}/${did}`,
transactionId,
legId,
identityId: did,
type,
party,
status: AffirmStatusEnum.Affirmed,
proofs,
eventIdx,
Expand Down

0 comments on commit 0fc2b71

Please sign in to comment.