Skip to content

Commit

Permalink
Update badges on vote
Browse files Browse the repository at this point in the history
  • Loading branch information
reyraa committed Oct 28, 2023
1 parent c10b844 commit 8122120
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const MYSQL_ENDPOINT = config.endpoints.mysql;
const accountsTableSchema = require('../../../database/schema/accounts');
const anchorsTableSchema = require('../../../database/schema/anchors');
const imagesTableSchema = require('../../../database/schema/images');
const badgesTableSchema = require('../../../database/schema/badges');
const {
MODULE_NAME_ANCHOR,
EVENT_NAME_ANCHOR_CREATED,
Expand All @@ -38,6 +39,12 @@ const getImagesTable = () => getTableInstance(
MYSQL_ENDPOINT,
);

const getBadgesTable = () => getTableInstance(
badgesTableSchema.tableName,
badgesTableSchema,
MYSQL_ENDPOINT,
);

// Command specific constants
const COMMAND_NAME = 'create';

Expand All @@ -55,6 +62,7 @@ const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
const accountsTable = await getAccountsTable();
const anchorsTable = await getAnchorsTable();
const imagesTable = await getImagesTable();
const badgesTable = await getBadgesTable();

// Use event data to get anchorID
const eventData = events.find(
Expand Down Expand Up @@ -105,6 +113,30 @@ const applyTransaction = async (blockHeader, tx, events, dbTrx) => {

await anchorsTable.upsert(anchorsNFT, dbTrx);
logger.debug(`Indexed anchor with ID ${anchorCreatedData.anchorID}.`);

const badge = await badgesTable.find({ badgeID: anchorCreatedData.badgeIDs[0] });
if (!badge.length) {
await BluebirdPromise.map(
anchorCreatedData.badgeIDs,
async (badgeID, index) => {
const badgeInfo = {
badgeID,
anchorID: '',
awardedTo: '',
type: 'anchor_of_the_day',
awardDate: anchorCreatedData.createdAt,
rank: BigInt(index + 1),
prize: BigInt(0),
claimed: false,
};
logger.trace(`Updating badge index for the badgeID ${badgeID}.`);
await badgesTable.upsert(badgeInfo, dbTrx);
logger.debug(`Updated badge index for the badgeID ${badgeID}.`);
return true;
},
{ concurrency: anchorCreatedData.badgeIDs.length },
);
}
return true;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
Logger,
MySQL: { getTableInstance },
} = require('lisk-service-framework');
const BluebirdPromise = require('bluebird');

const config = require('../../../../config');
const { getLisk32AddressFromPublicKey } = require('../../../utils/account');
Expand All @@ -10,9 +11,12 @@ const logger = Logger();

const MYSQL_ENDPOINT = config.endpoints.mysql;
const votesTableSchema = require('../../../database/schema/votes');
const anchorsTableSchema = require('../../../database/schema/anchors');
const badgesTableSchema = require('../../../database/schema/badges');

const {
MODULE_NAME_ANCHOR,
EVENT_NAME_ANCHOR_VOTED,
EVENT_NAME_COMMAND_EXECUTION_RESULT,
} = require('../../../../../blockchain-connector/shared/sdk/constants/names');

Expand All @@ -22,11 +26,24 @@ const getVotesTable = () => getTableInstance(
MYSQL_ENDPOINT,
);

const getAnchorsTable = () => getTableInstance(
anchorsTableSchema.tableName,
anchorsTableSchema,
MYSQL_ENDPOINT,
);

const getBadgesTable = () => getTableInstance(
badgesTableSchema.tableName,
badgesTableSchema,
MYSQL_ENDPOINT,
);

// Command specific constants
const COMMAND_NAME = 'vote';

// eslint-disable-next-line no-unused-vars
const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
// Do not process failed transactions
const { data: commandExecutedData = {} } = events.find(
({ module, name }) => module === MODULE_NAME_ANCHOR
&& name === EVENT_NAME_COMMAND_EXECUTION_RESULT,
Expand All @@ -35,7 +52,15 @@ const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
return false;
}

const eventData = events.find(
({ module, name }) => module === MODULE_NAME_ANCHOR
&& name === EVENT_NAME_ANCHOR_VOTED,
);
const { data: anchorVotedData } = eventData || { data: {} };

const votesTable = await getVotesTable();
const anchorsTable = await getAnchorsTable();
const badgesTable = await getBadgesTable();

const { anchorID } = tx.params;

Expand All @@ -49,6 +74,26 @@ const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
await votesTable.upsert(vote, dbTrx);
logger.debug(`Indexed vote with transaction ID ${dbTrx.id}.`);

const [anchor] = await anchorsTable.find(
{ anchorID },
['createdAt'],
dbTrx);
await BluebirdPromise.map(
anchorVotedData.updatedWinners,
async (winner, index) => {
const { anchorID: winningAnchorID, awardedTo } = winner;
const [badge] = await badgesTable.find(
{ awardDate: anchor.createdAt, rank: index + 1 },
['badgeID', 'anchorID', 'awardedTo', 'type', 'awardDate', 'rank', 'prize', 'claimed'],
dbTrx,
);

badge.anchorID = winningAnchorID;
badge.awardedTo = awardedTo;

await badgesTable.upsert(badge, dbTrx);
},
{ concurrency: anchorVotedData.updatedWinners.length });
return true;
};

Expand Down

0 comments on commit 8122120

Please sign in to comment.