Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for indexing audio reclaim command #44

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const {

MODULE_NAME_AUDIO,
EVENT_NAME_AUDIO_CREATED,
EVENT_NAME_AUDIO_STREAMED,
EVENT_NAME_AUDIO_INCOME_RECLAIMED,

MODULE_NAME_PROFILE,
EVENT_NAME_PROFILE_CREATED,
Expand Down Expand Up @@ -166,6 +168,8 @@ const EVENT_TOPIC_MAPPINGS_BY_MODULE = {
},
[MODULE_NAME_AUDIO]: {
[EVENT_NAME_AUDIO_CREATED]: ['transactionID', 'senderAddress'],
[EVENT_NAME_AUDIO_STREAMED]: ['transactionID', 'senderAddress'],
[EVENT_NAME_AUDIO_INCOME_RECLAIMED]: ['transactionID', 'senderAddress'],
},
[MODULE_NAME_PROFILE]: {
[EVENT_NAME_PROFILE_CREATED]: ['transactionID', 'senderAddress'],
Expand Down
2 changes: 2 additions & 0 deletions services/blockchain-connector/shared/sdk/constants/names.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const EVENT_NAME_COLLECTION_TRANSFERED = 'collectionTransfered';
const MODULE_NAME_AUDIO = 'audio';
const EVENT_NAME_AUDIO_CREATED = 'audioCreated';
const EVENT_NAME_AUDIO_STREAMED = 'audioStreamed';
const EVENT_NAME_AUDIO_INCOME_RECLAIMED = 'audioIncomeReclaimed';

// Profiles
const MODULE_NAME_PROFILE = 'profile';
Expand Down Expand Up @@ -179,6 +180,7 @@ module.exports = {
MODULE_NAME_AUDIO,
EVENT_NAME_AUDIO_CREATED,
EVENT_NAME_AUDIO_STREAMED,
EVENT_NAME_AUDIO_INCOME_RECLAIMED,

MODULE_NAME_PROFILE,
EVENT_NAME_PROFILE_CREATED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
const featsTable = await getFeatsTable();

// Use event data to get audioID
const { data: audioCreatedData = {} } = events.find(
const eventData = events.find(
({ module, name }) => module === MODULE_NAME_AUDIO
&& name === EVENT_NAME_AUDIO_CREATED,
);
const { data: audioCreatedData } = eventData || { data: {} };

const senderAddress = getLisk32AddressFromPublicKey(tx.senderPublicKey);

Expand Down Expand Up @@ -127,7 +128,7 @@ const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
};

await audiosTable.upsert(audiosNFT, dbTrx);
logger.debug(`Indexed audio with ID ${audioCreatedData.audiosID}.`);
logger.debug(`Indexed audio with ID ${audioCreatedData.audioID}.`);
return true;
};

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

const config = require('../../../../config');

const logger = Logger();

const MYSQL_ENDPOINT = config.endpoints.mysql;
const ownersTableSchema = require('../../../database/schema/owners');

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

const getOwnersTable = () => getTableInstance(
ownersTableSchema.tableName,
ownersTableSchema,
MYSQL_ENDPOINT,
);

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

// eslint-disable-next-line no-unused-vars
const applyTransaction = async (blockHeader, tx, events, dbTrx) => {
const { data: commandExecutedData = {} } = events.find(
({ module, name }) => module === MODULE_NAME_AUDIO
&& name === EVENT_NAME_COMMAND_EXECUTION_RESULT,
);
if (!commandExecutedData.success) {
return false;
}

const ownersTable = await getOwnersTable();

// Use event data to get audioID
const eventData = events.find(
({ module, name }) => module === MODULE_NAME_AUDIO
&& name === EVENT_NAME_AUDIO_INCOME_RECLAIMED,
);
const { data: audioIncomeReclaimedData } = eventData || { data: { claimData: { audioIDs: [] } } };

await BluebirdPromise.map(
audioIncomeReclaimedData.claimData.audioIDs,
async audioID => {
const owners = await ownersTable.find({ audioID }, ['address', 'shares', 'income'], dbTrx);
const sender = owners.find(owner => owner.address === tx.senderAddress);
const info = {
...sender,
audioID,
income: 0,
};
logger.trace(`Updating owner index for the account with address ${sender.address} and audioID: ${audioID}.`);
await ownersTable.upsert(info, dbTrx);
logger.debug(`Updated owner index for the account with address ${sender.address} and audioID: ${audioID}.`);
return true;
},
{ concurrency: audioIncomeReclaimedData.claimData.audioIDs.length },
);

return true;
};

// eslint-disable-next-line no-unused-vars
const revertTransaction = async (blockHeader, tx, events, dbTrx) => {};

module.exports = {
COMMAND_NAME,
applyTransaction,
revertTransaction,
};