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

Commit

Permalink
🔨 Add handling for legacy:reclaimLSK transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersubudhi committed Feb 6, 2024
1 parent 31e3ab8 commit cf01cdd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 26 deletions.
3 changes: 2 additions & 1 deletion services/export/shared/helpers/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const { requestConnector, requestIndexer } = require('./request');

const logger = Logger();

const publicKeyCache = CacheLRU('publicKey', { max: 100000 });
const NUM_MAX_CACHE_PUBLICKEY = 100000; // Approx. 30% of mainnet addresses at migration
const publicKeyCache = CacheLRU('publicKey', { max: NUM_MAX_CACHE_PUBLICKEY });

let tokenModuleData;
let loadingAssets = false;
Expand Down
1 change: 1 addition & 0 deletions services/export/shared/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const EVENT = Object.freeze({
RELAYER_FEE_PROCESSED: 'relayerFeeProcessed',
BEFORE_CCC_EXECUTION: 'beforeCCCExecution',
TRANSFER_CROSS_CHAIN: 'transferCrossChain',
ACCOUNT_RECLAIMED: 'accountReclaimed',
});

const MODULE_SUB_STORE = Object.freeze({
Expand Down
10 changes: 9 additions & 1 deletion services/export/shared/helpers/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
const { MODULE, COMMAND, TRANSACTION_STATUS } = require('./constants');

const normalizeTransactionAmount = (address, tx, currentChainID) => {
if (!('amount' in tx.params) || tx.executionStatus !== TRANSACTION_STATUS.SUCCESSFUL) {
// Amount normalization is only done for token:transfer & token:transferCrossChain transaction types only
if (
![
`${MODULE.TOKEN}:${COMMAND.TRANSFER}`,
`${MODULE.TOKEN}:${COMMAND.TRANSFER_CROSS_CHAIN}`,
].includes(tx.moduleCommand) ||
tx.executionStatus !== TRANSACTION_STATUS.SUCCESSFUL
) {
return null;
}

const amount = BigInt(tx.params.amount);

// Always a deduction for a successful token:transferCrossChain transaction
Expand Down
115 changes: 91 additions & 24 deletions services/export/shared/transactionsExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const {
} = require('./helpers/constants');
const {
resolveChainIDs,
getEvents,
getAllBlocksInAsc,
getAllTransactionsInAsc,
getAllEventsInAsc,
Expand Down Expand Up @@ -352,6 +353,36 @@ const getMessageFeeEntries = async (
return entries;
};

const getLegacyAccountReclaimEntries = async (
addressFromParams,
accountReclaimedEvent,
tx,
block,
) => {
const entries = [];

entries.push({
date: dateFromTimestamp(block.timestamp),
time: timeFromTimestamp(block.timestamp),
blockHeight: block.height,
transactionID: tx.id,
moduleCommand: null,
fee: null,
txFeeTokenID: null,
amount: accountReclaimedEvent.data.amount,
amountTokenID: await getFeeTokenID(),
senderAddress: null,
senderPublicKey: null,
recipientAddress: addressFromParams,
recipientPublicKey: await getPublicKeyByAddress(addressFromParams),
note: 'Legacy account balance reclaimed',
sendingChainID: await getCurrentChainID(),
receivingChainID: await getCurrentChainID(),
});

return entries;
};

const getSharedRewardsAssignedEntries = async (
addressFromParams,
rewardsAssignedEvent,
Expand Down Expand Up @@ -453,30 +484,47 @@ const getEntriesByChronology = async (params, sortedBlocks, sortedTransactions,
) {
const ccmID = getCcmIDFromTopic0(topic0);
const tx = await (async () => {
const transactionID = ccmID
? (() => {
// If current event's topic0 is a ccmID, determine the CCU transaction ID from the corresponding beforeCCCExecution event
let j = i - 1;
while (j--) {
const prevEvent = sortedEvents[j];
if (
prevEvent.module === MODULE.TOKEN &&
prevEvent.name === EVENT.BEFORE_CCC_EXECUTION &&
prevEvent.data.ccmID === ccmID
) {
return getTransactionIDFromTopic0(prevEvent.topics[0]);
const transactionID =
ccmID === null
? getTransactionIDFromTopic0(topic0)
: await (async () => {
// If current event's topic0 is a ccmID, determine the CCU transaction ID from the corresponding beforeCCCExecution event
let j = i - 1;
while (j--) {
const prevEvent = sortedEvents[j];
if (
prevEvent.module === MODULE.TOKEN &&
prevEvent.name === EVENT.BEFORE_CCC_EXECUTION &&
prevEvent.data.ccmID === ccmID
) {
return getTransactionIDFromTopic0(prevEvent.topics[0]);
}
}
}
logger.warn(
`Cannot determine CCU transactionID for ccmID ${ccmID} from event:\n${JSON.stringify(
e,
null,
'\t',
)}`,
);
throw Error(`CCU transactionID cannot be determined for ccmID ${ccmID}.`);
})()
: getTransactionIDFromTopic0(topic0);

const sortedEventsForHeight = await getEvents({
height: e.block.height,
sort: 'height:asc',
order: 'index:asc',
});
const correspondingBeforeCCCExecutionEvent = sortedEventsForHeight.find(
eventForHeight =>
eventForHeight.module === MODULE.TOKEN &&
eventForHeight.name === EVENT.BEFORE_CCC_EXECUTION &&
eventForHeight.data.ccmID === ccmID,
);
if (correspondingBeforeCCCExecutionEvent) {
return getTransactionIDFromTopic0(correspondingBeforeCCCExecutionEvent.topics[0]);
}

logger.warn(
`Cannot determine CCU transactionID for ccmID ${ccmID} from event:\n${JSON.stringify(
e,
null,
'\t',
)}`,
);
throw Error(`CCU transactionID cannot be determined for ccmID ${ccmID}.`);
})();

const txInList = sortedTransactions.find(t => t.id === transactionID);
if (txInList) return txInList;
Expand Down Expand Up @@ -561,7 +609,8 @@ const getEntriesByChronology = async (params, sortedBlocks, sortedTransactions,
if (
e.module === MODULE.TOKEN &&
e.name === EVENT.CCM_TRANSFER &&
e.data.recipientAddress === addressFromParams
e.data.recipientAddress === addressFromParams &&
e.data.receivingChainID === (await getCurrentChainID())
) {
const incomingCCTransferEntries = await getIncomingTransferCCEntries(
addressFromParams,
Expand Down Expand Up @@ -635,6 +684,24 @@ const getEntriesByChronology = async (params, sortedBlocks, sortedTransactions,
);
entries.push(...messageFeeEntries);
}

// Legacy account reclaims
if (
e.module === MODULE.LEGACY &&
e.name === EVENT.ACCOUNT_RECLAIMED &&
e.topics[2] === addressFromParams
) {
const accountReclaimedEvent = e;
const legacyAccountReclaimEntries = await getLegacyAccountReclaimEntries(
addressFromParams,
accountReclaimedEvent,
tx,
block,
);
entries.push(...legacyAccountReclaimEntries);
}

// TODO: Implement for PoM transaction
}

// Shared custodial reward received/sent
Expand Down

0 comments on commit cf01cdd

Please sign in to comment.