Skip to content

Commit

Permalink
use forloop
Browse files Browse the repository at this point in the history
  • Loading branch information
jiqiang90 committed Jun 17, 2024
1 parent 2d90ef6 commit 183fced
Showing 1 changed file with 37 additions and 47 deletions.
84 changes: 37 additions & 47 deletions src/mappings/Rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,62 +350,52 @@ async function buildRewardEvents<A>(
let blockNumber = block.block.header.number.toString();
let blockTimestamp = timestamp(block);

const [, savingPromises] = block.events.reduce<[A, Promise<void>[]]>(
(accumulator, eventRecord, eventIndex) => {
let [innerAccumulator, currentPromises] = accumulator;

if (
!(
eventRecord.event.method == eventMethod &&
eventRecord.event.section == eventSection
)
)
return accumulator;
let innerAccumulator = initialInnerAccumulator;
for (let eventIndex = 0; eventIndex < block.events.length; eventIndex++) {
const eventRecord = block.events[eventIndex];

let [account, amount] = decodeDataFromReward(
eventRecordToSubstrateEvent(eventRecord),
);
if (
!(
eventRecord.event.method === eventMethod &&
eventRecord.event.section === eventSection
)
)
continue;

const newAccumulator = produceNewAccumulator(
innerAccumulator,
account.toString(),
);
let [account, amount] = decodeDataFromReward(
eventRecordToSubstrateEvent(eventRecord),
);

const eventId = eventIdFromBlockAndIdx(
blockNumber,
eventIndex.toString(),
);
innerAccumulator = produceNewAccumulator(
innerAccumulator,
account.toString(),
);

const accountAddress = account.toString();
const destinationAddress = accountsMapping[accountAddress];
const eventId = eventIdFromBlockAndIdx(blockNumber, eventIndex.toString());

const element = new HistoryElement(
eventId,
block.block.header.number.toNumber(),
blockTimestamp,
destinationAddress != undefined ? destinationAddress : accountAddress,
);
const accountAddress = account.toString();
const destinationAddress = accountsMapping[accountAddress];

if (extrinsic !== undefined) {
element.extrinsicHash = extrinsic.extrinsic.hash.toString();
element.extrinsicIdx = extrinsic.idx;
}
element.reward = produceReward(
newAccumulator,
eventIndex,
accountAddress,
amount.toString(),
);
const element = new HistoryElement(
eventId,
block.block.header.number.toNumber(),
blockTimestamp,
destinationAddress !== undefined ? destinationAddress : accountAddress,
);

currentPromises.push(element.save());
if (extrinsic !== undefined) {
element.extrinsicHash = extrinsic.extrinsic.hash.toString();
element.extrinsicIdx = extrinsic.idx;
}

return [newAccumulator, currentPromises];
},
[initialInnerAccumulator, []],
);
element.reward = produceReward(
innerAccumulator,
eventIndex,
accountAddress,
amount.toString(),
);

for (const promise of savingPromises) {
await promise;
await element.save();
}
}

Expand Down

0 comments on commit 183fced

Please sign in to comment.