Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschakki committed Oct 25, 2023
1 parent 551a5fe commit ceb1038
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ export class ReactCCCommand extends BaseCCCommand {
public async execute(ctx: CrossChainMessageContext): Promise<void> {
const { ccm, logger } = ctx;
logger.info('Executing React CCM');
// const methodContext = ctx.getMethodContext();
// const { sendingChainID, status, receivingChainID } = ccm;
// Decode the provided CCM parameters
const params = codec.decode<CCReactMessageParams>(CCReactMessageParamsSchema, ccm.params);
logger.info(params, 'parameters');
// Get helloMessageID and reactionType from the parameters
const { helloMessageID, reactionType } = params;
const reactionSubstore = this.stores.get(ReactionStore);
const messageCreatorAddress = cryptography.address.getAddressFromLisk32Address(helloMessageID);
let msgReactions: ReactionStoreData;

// Get existing reactions for a Hello message, or initialize an empty reaction object, if none exists,yet.
try {
msgReactions = await reactionSubstore.get(ctx, messageCreatorAddress);
} catch (error) {
Expand All @@ -56,16 +58,23 @@ export class ReactCCCommand extends BaseCCCommand {
);
msgReactions = { reactions: { like: [] } };
}

// Check if the reactions is a like
if (reactionType === 0) {
const hasLiked = msgReactions.reactions.like.indexOf(ctx.transaction.senderAddress);
// If the sender has already liked the message
if (hasLiked > -1) {
// Remove the sender address from the likes for the message
msgReactions.reactions.like = msgReactions.reactions.like.splice(hasLiked, 1);
// If the sender has not liked the message yet
} else {
// Add the sender address to the likes of the message
msgReactions.reactions.like.push(ctx.transaction.senderAddress);
}
} else {
logger.error({ reactionType }, 'invalid reaction type');
}
// Update the reaction store with the reactions for the specified Hello message
await reactionSubstore.set(ctx, messageCreatorAddress, msgReactions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,23 @@ export const getHelloRequestSchema = {
};

/**
* Parameters of the cross-chain token transfer command
* Parameters of the reactCrossChain CCM
*/
export interface CCReactMessageParams {
/**
* A number indicating the type of the reaction.
*/
reactionType: number;
/**
* ID of the message.
*/
helloMessageID: string;
/** Optional field for data / messages. */
data: string;
}

/**
* Schema for the parameters of the reactCrossChain CCM
*/
export const CCReactMessageParamsSchema = {
/** The unique identifier of the schema. */
Expand All @@ -92,14 +108,10 @@ export const CCReactMessageParamsSchema = {
dataType: 'uint32',
fieldNumber: 1,
},
/**
* ID of the message.
*/
helloMessageID: {
dataType: 'string',
fieldNumber: 2,
},
/** Optional field for data / messages. */
data: {
dataType: 'string',
fieldNumber: 3,
Expand All @@ -108,9 +120,3 @@ export const CCReactMessageParamsSchema = {
},
},
};

export interface CCReactMessageParams {
reactionType: number;
helloMessageID: string;
data: string;
}

0 comments on commit ceb1038

Please sign in to comment.